作者:方园不帅_718 | 来源:互联网 | 2023-05-30 12:01
0x00 写在前面
从零学习php,最终目的实现代码审计入门,软件采用sublime text,环境使用phpstudy搭建,数据库是navicat,需要有基本的前端基础、简单的php+mysql后端基础、渗透知识和漏洞原理,文章跟随流沙前辈学习记录,看看曾经遥不可及的代码审计能不能慢慢啃下来。
本章为代码审计入门第六篇-DVWA靶场篇,对DVWA靶场漏洞进行代码审计。
0x01 常用的命令连接符
Windows下常用的命令连接符
&,以test、whoami、dir命令测试
1.前面为假,执行后面命令
2.前面为真,前后命令都执行
通俗解释: 前面命令执行后接着去执行后面命令
&&,以test、whoami、dir命令测试
1.前面为假,前后都不执行命令
2.前面为真,前后都执行命令
通俗解释:前面命令执行成功了才会去执行后面命令。
| 和 || ,以ping和whoami为例
| ,浅浅的讲:直接执行后面命令
|| ,前面为假才执行后面命令
Linux下常用的命令连接符和windows一致,唯一区别的是 ; 的使用,;的作用连接多条命令,每条命令都执行这里以vps为例。
常用的命令连接符就差不多了,还有一个常用的但不是命令连接符的东东,基本就这么多。
> 写入并覆盖
>> 追加
0x02 Command Injection
命令注入模块,注释写在代码里,尽量用详细的语言描述代码含义。
level-low
命令执行,但没有作任何限制
low.php{$cmd}";
}?>
level-medium
相比于low,增加一个黑名单机制
medium.php
'',';' => '',);//可以理解为把 && 和 ; 设为一个黑名单// Remove any of the charactars in the array (blacklist).$target = str_replace( array_keys( $substitutions ), $substitutions, $target );// 用str_replace对变量 $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$html .= "{$cmd}";
}?>
level-high
相比于medium,黑名单内容增加
high.php '',';' => '','| ' => '', //注意此处过滤有空格,过滤的是|空格'-' => '','$' => '','(' => '',')' => '','`' => '','||' => '',);//设置黑名单 & ; | - $ ( ) ` ||// 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$html .= "{$cmd}";
}?>
level-impossible
既然是impossibale,那以咱水平肯定绕不过去,直接看代码。这里先把 $target 输出,看看代码是如何防止命令注入的。
impossiable.php{$cmd}";}else {// Ops. Let the user name theres a mistake$html .= 'ERROR: You have entered an invalid IP.';}
}// Generate Anti-CSRF token
generateSessionToken();?>
调试也可以看出上面代码的数组分组
这里虽然没用禁用所谓的危险函数shell_exec,但对于用户的输入内容过滤严格从而避免命令注入漏洞。
0x03 总结
沙雕考试暂且结束,暂获一周躺平权。