DVWA-1.9系列操作之Command Injection
2017-01-22 09:21
615 查看
DVWA-1.9系列一共分为10个功能模块:
Brute Force(暴力破解)
Command Injection(命令行注入)
CSRF(跨站请求伪造)
File Inclusion(文件包含)
File Upload(文件上传)
Insecure CAPTCHA(不安全的验证码)
SQL Injection(SQL注入)
SQL Injection(Blind)(SQL盲注)
XSS(Reflected)(反射型跨站脚本)
XSS(Stored)(存储型跨站脚本)
之前两篇文章分别是介绍了DVWA以及第一个功能模块Brute Force(暴力破解)。
这一篇我们继续介绍第二个功能模块:Command Injection(命令行注入)
该模块主要实现的是一个ping 某个ip的功能,但由于对输入的内容验证不完善导致命令执行漏洞。
主要页面如下:
Low
我们可以看到,服务器通过判断操作系统执行不同ping命令,但是对ip参数并未做任何的过滤,导致了严重的命令注入漏洞。
stristr()函数定义和用法:
stristr() 函数搜索字符串在另一字符串中的第一次出现。
注释:该函数是二进制安全的。
注释:该函数是不区分大小写的。如需进行区分大小写的搜索,请使用 strstr() 函数。
语法:stristr(string,search,before_search)
php_uname(mode)函数定义和用法:
这个函数会返回运行php的操作系统的相关描述,参数mode可取值:
”a”(此为默认,包含序列”s n r v m”里的所有模式),
”s”(返回操作系统名称),
”n”(返回主机名),
“r”(返回版本名称),
”v”(返回版本信息),
”m”(返回机器类型)。
漏洞利用:
无论是linux系统还是windows系统,我们都可以用&&来执行多条命令语句。所以我们执行的payload可以是
Medium
通过源代码我们可以看到,代码中的主要过滤原则是采用黑名单的方式:
把”&&”和”;”直接删除,但是只是单纯了删除了这两个,还是存在命令执行漏洞。
漏洞利用:
通过源码我们可以知道,他主要就是把”&&”和”;”放进了黑名单里,不可以用“&&”了,但是“&”任然可以使用呀。这里我们需要说一下“&&”,“;”和“&”的区别:
command1&command2&command3 三个命令同时执行
command1;command2;command3 不管前面命令执行成功没有,后面的命令继续执行
command1&&command2 只有前面命令执行成功,后面命令才继续执行
所以我们使用payload
成功显示用户。
High
通过源代码可以看出,high级别把黑名单机制更加扩大了。我们可以看到代码中把
尝试使用payload
成功显示用户。
Command 1 | Command 2 “|”是管道符,表示将Command 1的输出作为Command 2的输入,并且只打印Command 2执行的结果。
通过源代码分析我们可以看到:
该模块中加入了Anti-CSRF token来防范CSRF攻击,同时每次随机生成了一个token,当用户提交的时候,在服务器端比对一下token值是否正确,不正确就丢弃掉,正确就验证通过。
通过加入stripslashes函数来对输入的ip信息进行删除字符串中的反斜杠操作。
对输入的信息通过“.”号进行分割,分割成多个数组。
然后采用白名单机制来验证输入的信息是不是都是数字组成,然后把所有的数字通过“.”进行拼接,这样就保证了输入的信息只能是以
Brute Force(暴力破解)
Command Injection(命令行注入)
CSRF(跨站请求伪造)
File Inclusion(文件包含)
File Upload(文件上传)
Insecure CAPTCHA(不安全的验证码)
SQL Injection(SQL注入)
SQL Injection(Blind)(SQL盲注)
XSS(Reflected)(反射型跨站脚本)
XSS(Stored)(存储型跨站脚本)
之前两篇文章分别是介绍了DVWA以及第一个功能模块Brute Force(暴力破解)。
这一篇我们继续介绍第二个功能模块:Command Injection(命令行注入)
1. Command Injection简介
Command Injection(命令注入),是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的。PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一。该模块主要实现的是一个ping 某个ip的功能,但由于对输入的内容验证不完善导致命令执行漏洞。
主要页面如下:
2. Low 安全级别分析
首先我们把安全级别设置为Low级别。查看源代码: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>"; } ?>
我们可以看到,服务器通过判断操作系统执行不同ping命令,但是对ip参数并未做任何的过滤,导致了严重的命令注入漏洞。
stristr()函数定义和用法:
stristr() 函数搜索字符串在另一字符串中的第一次出现。
注释:该函数是二进制安全的。
注释:该函数是不区分大小写的。如需进行区分大小写的搜索,请使用 strstr() 函数。
语法:stristr(string,search,before_search)
php_uname(mode)函数定义和用法:
这个函数会返回运行php的操作系统的相关描述,参数mode可取值:
”a”(此为默认,包含序列”s n r v m”里的所有模式),
”s”(返回操作系统名称),
”n”(返回主机名),
“r”(返回版本名称),
”v”(返回版本信息),
”m”(返回机器类型)。
漏洞利用:
无论是linux系统还是windows系统,我们都可以用&&来执行多条命令语句。所以我们执行的payload可以是
127.0.0.1&&whoami可以发现返回了执行结果,显示了linux的用户名:
3. Medium 安全级别分析
首先我们把安全级别设置为Medium级别。查看源代码:Medium
<?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>"; } ?>
通过源代码我们可以看到,代码中的主要过滤原则是采用黑名单的方式:
$substitutions = array( '&&' => '', ';' => '', );
把”&&”和”;”直接删除,但是只是单纯了删除了这两个,还是存在命令执行漏洞。
漏洞利用:
通过源码我们可以知道,他主要就是把”&&”和”;”放进了黑名单里,不可以用“&&”了,但是“&”任然可以使用呀。这里我们需要说一下“&&”,“;”和“&”的区别:
command1&command2&command3 三个命令同时执行
command1;command2;command3 不管前面命令执行成功没有,后面的命令继续执行
command1&&command2 只有前面命令执行成功,后面命令才继续执行
所以我们使用payload
127.0.0.1&whoami查看返回结果:
成功显示用户。
4. High 安全级别分析
把安全级别设置为High级别。查看源代码: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>"; } ?>
通过源代码可以看出,high级别把黑名单机制更加扩大了。我们可以看到代码中把
'| '替换成了空白符,我们可以延伸一下
'|'却没有被过滤掉。
尝试使用payload
127.0.0.1|whoami查看返回结果:
成功显示用户。
Command 1 | Command 2 “|”是管道符,表示将Command 1的输出作为Command 2的输入,并且只打印Command 2执行的结果。
5. Impossible 安全级别分析
把安全级别设置为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(); ?>
通过源代码分析我们可以看到:
该模块中加入了Anti-CSRF token来防范CSRF攻击,同时每次随机生成了一个token,当用户提交的时候,在服务器端比对一下token值是否正确,不正确就丢弃掉,正确就验证通过。
// Check Anti-CSRF token checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
通过加入stripslashes函数来对输入的ip信息进行删除字符串中的反斜杠操作。
$target = stripslashes( $target );
对输入的信息通过“.”号进行分割,分割成多个数组。
// Split the IP into 4 octects $octet = explode( ".", $target );
然后采用白名单机制来验证输入的信息是不是都是数字组成,然后把所有的数字通过“.”进行拼接,这样就保证了输入的信息只能是以
数字.数字.数字.数字的形式,避免了命令执行漏洞。
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];
相关文章推荐
- DVWA-1.9系列操作之Brute Force
- DVWA-1.9系列操作之CSRF
- DVWA-1.9全级别教程之Command Injection
- DVWA-1.9系列操作之FileInclusion
- DVWA-1.9系列(简介)
- Kali下利用XAMPP搭建DVWA及使用command injection
- 【原创】【2】rich editor系列教程。了解document.execommand操作,保存丢失的range,实时反馈样式给工具栏
- DVWA 之命令注入(Command Injection)
- DVWA笔记之二:Command Injection
- DVWA - Command Injection (low, medium, high)
- –DVWA-command injection
- DVWA(V1.10)中Command Injection的high等级绕过
- Excel Services OverView系列--3使用Excel Web Services操作Excel工作薄
- ASP.NET 2.0中的数据操作系列教程:定制数据修改界面
- ASP.NET 2.0中的数据操作系列教程:定制数据修改界面
- VC操作Word系列
- C#3.0入门系列(六)-之OrderBy操作
- 最强开源搜索引擎DotLucene1.9+写你自己的超强桌面搜索+索引Office系列文档(PPT/DOC/XLS/OneNote/Project/Visio)+RTF+PFD+XML+各种图片!!!
- Excel Services OverView系列--3使用Excel Web Services操作Excel工作薄
- Python学习系列之异常和文件操作