DVWA靶场实战(二)

二、Command Injection:

1.漏洞介绍:

  Command Injection,中文叫做命令注入,是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的。PHP命令注入攻击漏洞是PHP应用程序常见的脚本漏洞之一。

2.漏洞成因:

  (1)外部参数可控:

    应用程序调用了执行系统命令的函数,比如服务器程序通过system、eval、exec等函数直接或者间接的调用cmd.exe。

  (2)内部拼接命令:

    服务器将输入的恶意参数拼接到正常命令中,从而执行命令造成攻击。

3.漏洞危害:

  黑客如果能够利用命令执行漏洞,那么将像控制自己电脑一样控制,自由的进行操作,比如关闭防火墙、查询密码、开启远程服务等操作。

4.防御措施:

  (1)设计者尽可能少设计使用一些命令执行函数。

  (2)若有必要使用,那么必须对特殊函数做过滤,对用户输入的命令做检查,对客户端提交的变量在进入执行命令前做好过滤和检查等。

5.攻击方法:

  可以用以下命令来拼接输入的命令:

    ·A;BA

      →不论正确与否都会执行B

    ·A&B

      →A后台运行,A和B同时执行

    ·A&&B

      →A执行成功后才会执行B

    ·A|B

      →A执行的输出结果作为B命令的参数,A不论正确与否,都会执行B

    ·A||B

      →A执行失败后才会执行B的命令

6.实战:

  在开始实战前有个注意事项,在Low级别下,尝试用“127.0.0.1&&ipconfig”命令进行注入后,发现会有乱码情况。那么解决的方法如下:

    ①在DVWA的安装目录下(……/WWW/DVWA-master/dvwa/includes)找到文件“dvwaPage.inc.php”

    ②打开这个文件,然后全文查找charest=utf-8,有好几处charest=utf-8,然后全部修改成为charest=gb2312

    ③接下来就不会有乱码了

  接下来正式进入实战环节:

    (1)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
$html .= "<pre>{$cmd}</pre>";
} ?>

        可以看到这里直接将target变量放入shell_exec()执行ping命令,没有任何过滤,用户端可以直接拼接特定的命令,来执行并获取想要的信息。

        首先测试“127.0.0.1”这里加上后台命令后就是“ping 127.0.0.1”

        接下去,测试“127.0.0.1&&ipconfig”,执行命令为“ping 127.0.0.1 && ipconfig”,这里结果如下:

        最后尝试“127.0.0.1 && ipconfig && systeminfo”,没问题,可以通过构造恶意传参系统命令或者直接传木马。

  (2)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
$html .= "<pre>{$cmd}</pre>";
} ?>

    这里设置了“黑名单过滤规则”,但是只起到了过滤两种字符分别是“&&”和“;”两种字符,但可以想办法绕过。比如“127.0.0.1 & ipconfig”、“127.0.0.1 | ipconfig”、“111 || ipconfig”等

  (3)High:

    代码分析:

<?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
$html .= "<pre>{$cmd}</pre>";
} ?>

    这里虽然看上去有有些敏感字符被过滤了,但是“|”的后面明显有个空格,意思就是只要不使用空格还是可以绕过的,比如“127.0.0.1 |ipconfig”就是仍然生效的。当然,ipconfig指令也是可以替换的。

 (4)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
$html .= "<pre>{$cmd}</pre>";
}
else {
// Ops. Let the user name theres a mistake
$html .= '<pre>ERROR: You have entered an invalid IP.</pre>';
}
} // Generate Anti-CSRF token
generateSessionToken(); ?>

    这里已经严格限制了输入接收的参数只能是“数字.数字.数字.数字”的模式,所以不存在命令注入的漏洞了。

DVWA靶场实战(二)——Command Injection的更多相关文章

  1. DVWA靶场实战(七)——SQL Injection

    DVWA靶场实战(七) 七.SQL Injection: 1.漏洞原理: SQL Inject中文叫做SQL注入,是发生在web端的安全漏洞,主要是实现非法操作,例如欺骗服务器执行非法查询,他的危害在 ...

  2. DVWA笔记之二:Command Injection

    命令注入 1.Low级别 <?php  if( isset( $_POST[ 'Submit' ]  ) ) {      // Get input      $target = $_REQUE ...

  3. DVWA靶场实战(十二)——XSS(Stored)

    DVWA靶场实战(十二) 五.XSS(Stored): 1.漏洞原理: XSS的Stored被称作存储型XSS漏洞,漏洞的原理为语句被保存到服务器上,显示到HTML页面中,经常出现在用户评论的页面,攻 ...

  4. DVWA靶场实战(五)——File Upload

    DVWA靶场实战(五) 五.File Upload: 1.漏洞原理: File Upload中文名叫做文件上传,文件上传漏洞是指用户上传了一个可执行脚本文件(php.jsp.xml.cer等文件),而 ...

  5. DVWA靶场实战(三)——CSRF

    DVWA靶场实战(三) 三.CSRF: 1.漏洞原理: CSRF(Cross-site request forgery),中文名叫做"跨站请求伪造",也被称作"one c ...

  6. DVWA靶场实战(四)——File Inclusion

    DVWA靶场实战(四) 四.File Inclusion: 1.漏洞原理: 随着网站的业务的需求,程序开发人员一般希望代码更加灵活,所以将被包含的文件设置为变量,用来进行动态调用,但是正是这种灵活性通 ...

  7. DVWA靶场实战(一)——Brute Force

    DVWA靶场实战(一) 一.Brute Force: 1.漏洞原理: Brute Force是暴力破解的意思,大致原理就是利用穷举法,穷举出所有可能的密码. 2.攻击方法: Burpsuite中的In ...

  8. DVWA靶场实战(六)——Insecure CAPTCHA

    DVWA靶场实战(六) 六.Insecure CAPTCHA: 1.漏洞原理: Insecure CAPTCHA(不安全的验证码),CAPTCHA全程为Completely Automated Pub ...

  9. DVWA靶场实战(九)——Weak Session IDS

    DVWA靶场实战(九) 九.Weak Session IDS: 1.漏洞原理: Weak Session IDS也叫做弱会话,当用户登录后,在服务器就会创造一个会话(session),叫做会话控制,接 ...

随机推荐

  1. day49-JDBC和连接池05

    JDBC和连接池05 11.BasicDAO 先来分析一个问题 前面我们使用了Apache-DBUtils和Druid简化了JDBC开发,但仍存在以下不足: SQL语句是固定的,不能通过参数传入,通用 ...

  2. 论文解读(GGD)《Rethinking and Scaling Up Graph Contrastive Learning: An Extremely Efficient Approach with Group Discrimination》

    论文信息 论文标题:Rethinking and Scaling Up Graph Contrastive Learning: An Extremely Efficient Approach with ...

  3. 虚拟化_Vmware——敬请期待!

    Esxi 7.0 安装部署完成! vSphere vCenter 安装部署完成!

  4. 齐博x1商业模块仅限一个国际域名使用

    应用市场的所有商业模块 仅授权一个国际域名,大家不要试图复制到其它国际域名下使用. 仅支持一个国际域名使用,二级域名不限,但前提需要先用 www.开头的国际域名先安装,然后再到二级域名安装,并且二级域 ...

  5. python查找相似图片或重复图片

    1.查找重复图片 利用文件的MD5值可查找完全一样的重复图片 import os,time,hashlib def getmd5(file): if not os.path.isfile(file): ...

  6. Istio(十三):Istio项目实际案例——Online Boutique

    目录 一.模块概览 二.系统环境 三.创建Kubernetes(k8s)集群 3.1 创建Kubernetes(k8s)集群 3.2 Kubernetes集群环境 四.安装istio 4.1 安装Is ...

  7. linux读写一个NTFS分区

    为了读写一个NTFS分区的数据,挂载的时候出现错误提示如下: root@tv:/home/xx# mount -t ntfs-3g /dev/sdb1 /media/sxx/硬盘B-临时文件 The ...

  8. 中小型企业综合项目(Nginx+LVS+Tomcat+MGR+Nexus+NFS)

    Nginx+Tomcat+Mysql综合实验 1.环境准备 服务器 IP地址 作用 系统版本 数据库服务器1 192.168.100.111 MGR集群数据库master节点 Rocky8.6 数据库 ...

  9. python tcp select 多路复用

    1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 # 文件名:tcpserver.py 4 5 import socket 6 import time 7 ...

  10. scrapy 解析xml格式的数据

    XMLFeedSpider 主要用于 解析 xml格式的数据 创建一个scrapy 项目文件 scrapy startproject xxx 创建一个spider scrapy genspider - ...