信安学习day2:DVWA的搭建和命令执行漏洞

DVWA的搭建:

 phpstudy:搜索官网即可

DVWA资料包:https://codeload.github.com/digininja/DVWA/zip/refs/heads/masters​​​​

phpstudy的配置:

一些的安装,打开即是这个样子,然后启用apache和mysql看能否启动成功

若出现了mysql打不开的情况,大概率是因为你已经有了mysql的端口,会出现冲突,因此我们需要把原来 的端口给关闭了,之后也可以通过phpstudy里使用mysql,所以大可关闭。

使用windows+R:

将有关mysql 的全部关闭,然后用管理员的身份打开终端,输入 sc delete mysql,见成功即可;

DVWA配置

这时就可以将mysql的服务打开,然后把phpstudy关闭,将DVWA的资料包放到phpstudy的文件夹WWW中,压缩,然后重命名为DVWA,打开进入config,将里面的文件名删去dist,然后打开,把db_user 和 db_password改成phpstudy的基本用户密码(通常是root,密码用户都是),找到有key的两行代码,输入6LdJJlUUAAAAAH1Q6cTpZRQ2Ah8VpyzhnffD0mBb和6LdJJlUUAAAAAM2a3HrgzLczqdYp4g05EqDs-W4K ,保存即可 

从phpstudy的目录打开进入Extensions的文件夹,然后一直打开php有关的文件,直到进入这个

 找到php.ini的文件,打开,找到有关这个allow_url_include的代码,然后改为On。这是DVWA的环境就配置好了。

启动PHP study,打开浏览器,输入127.0.0.1/DVWA,

 找到这个,点击就有一个please  log

输入DVWA默认的用户(admin)和密码(password)

就进入了此页面。

 


命令执行漏洞:

命令执行漏洞——RCE漏洞:

 在我们常见的路由器、防火墙、入侵检测等设备的web管理界面上,一般会给用户提供一个ping操作的web界面,用户从web界面输入目标IP,提交给后台并返回测试结果,如果没有安全控制,则可能会以此通过该接口提交恶意命令,让后台执行,从而达到攻击者的目的(获得后台服务器权限)

  命令连接符:|、;、&、||、&&;

 

Cmd1|cmd2:无论cmd1是否执行成功,cmd2都会执行

Cmd1;cmd2:无论cmd1是否执行成功,cmd2都会执行

Cmd1&cmd2:无论cmd1是否执行成功,cmd2都会执行

Cmd1||cmd2:仅在cmd1执行失败时才执行cmd2——通常是用于linux

Cmd1&&cmd2:仅在cmd1执行成功时才执行cmd2

常用的cmd命令:

Whoami 查看当前用户名;

Ipconfig  查看网卡信息;

Shutdown -s -t 0 关机

Net user [username] [password] /add  增加一个用户名为username密码为password的新用户

Dir   目标文件目录内容

Type [file_name]  查看filename文件内容

首先要将DVWA的防御难度调为low级别:

 输入127.0.0.1&whoami

 

发现自己的用户名被套出来了,我们可以看看此时源代码:

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Get input
    $target = $_REQUEST[ 'ip' ];——将输入的值赋值到target

    // Determine OS and execute the ping command.
    if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
        // Windows
        $cmd = shell_exec( 'ping  ' . $target );——shell_exec一个函数,通过shell环境进行执行,以字符串返回,问题也就在这儿
    }                                       这里就可以将这些符号替换成空,或判断用户输入这些符号就或终止执行
    else {
        // *nix
        $cmd = shell_exec( 'ping  -c 4 ' . $target );
    }

    // Feedback for the end user
    echo "<pre>{$cmd}</pre>";
}

?>

 那我们开始把难度提高为medium:我们可以看到中级它还没写全,对于连接符,只过滤了’;’和’&&’。

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Get input
    $target = $_REQUEST[ 'ip' ];

    // Set blacklist
    $substitutions = array(
        '&&' => '',
        ';'  => '',
    );

    // Remove any of the characters in the array (blacklist).
    $target = str_replace( array_keys( $substitutions ), $substitutions, $target );——将连接符替换成空

    // Determine OS and execute the ping command.
    if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
        // Windows
        $cmd = shell_exec( 'ping  ' . $target );
    }
    else {
        // *nix
        $cmd = shell_exec( 'ping  -c 4 ' . $target );
    }

    // Feedback for the end user
    echo "<pre>{$cmd}</pre>";
}

?>

 那高级的情况呢?看似是全的,但是会出现一些小问题——这种会在CTF中出现。

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Get input
    $target = trim($_REQUEST[ 'ip' ]);

    // Set blacklist
    $substitutions = array(
        '&'  => '',
        ';'  => '',
        '| ' => '',——漏洞:这里’| ’是有’|’和’ ’才进行替换
        '-'  => '',
        '$'  => '',
        '('  => '',
        ')'  => '',
        '`'  => '',
        '||' => '',
    );

    // Remove any of the characters in the array (blacklist).
    $target = str_replace( array_keys( $substitutions ), $substitutions, $target );

    // Determine OS and execute the ping command.
    if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
        // Windows
        $cmd = shell_exec( 'ping  ' . $target );
    }
    else {
        // *nix
        $cmd = shell_exec( 'ping  -c 4 ' . $target );
    }

    // Feedback for the end user
    echo "<pre>{$cmd}</pre>";
}

?>

那不可能的级别是一种防御典范,一个很经典的算法:

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Check Anti-CSRF token
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );

    // Get input
    $target = $_REQUEST[ 'ip' ];
    $target = stripslashes( $target );——stipslashes去除用户输入的\

    // Split the IP into 4 octects
    $octet = explode( ".", $target );——explode把用户输入的数据根据.分开,如127.0.0.1 被分隔成 127  0  0  1

    // Check IF each octet is an integer
    if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {                              ——分别判断分成的四个部分是不是数字,必须满足条件和四个部分
        // If all 4 octets are int's put the IP back together.
        $target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3]; ——将四个数字进行拼接

        // Determine OS and execute the ping command.
        if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
            // Windows
            $cmd = shell_exec( 'ping  ' . $target );
        }
        else {
            // *nix
            $cmd = shell_exec( 'ping  -c 4 ' . $target );
        }

        // Feedback for the end user
        echo "<pre>{$cmd}</pre>";
    }
    else {
        // Ops. Let the user name theres a mistake
        echo '<pre>ERROR: You have entered an invalid IP.</pre>';
    }
}

// Generate Anti-CSRF token
generateSessionToken();

?>

渗透测试解决方法:

白盒测试:可以看到源代码,查看源代码过滤机制;

黑盒测试:看不到源代码,依次尝试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值