文章会讨论 DVWA 中低、中、高、不可能级别的命令行注入

这里的需求是在服务器上可以 ping 一下其他的服务器

低级

Hacker 试下输入 192.168.31.130; cat /etc/apache2/apache2.conf;

瞬间爆炸, 竟然是直接执行 shell 的结果。再看看代码

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

竟然没有任何的参数校验!这主机安全可以说是形同虚设。 Hacker 竟然不废任何力气,就得到一个 weshell 了!接下来的攻击主要是就看攻击者的想象力了,什么改掉你的文件,什么创建个新页面做负载均衡,批量将 php 重名了。。。

中级

中级代码有做了一点参数校验

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

会将有;&&都移除了。

Hacker 试了一下 || cat /etc/apache2/apache2.conf 。。。注入成功。而 || 的意思是第一条命令执行失败就会执行下一条。。。

Hacker 又试了一下 & cat /etc/apache2/apache2.conf。。。注入也成功。 &是后台执行,然后再执行新的代码。

主要是过滤太少的参数了。

高级

高级的话就过滤的参数比较多。

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

|cat /etc/apache2/apache2.conf 就可以了,因为 substitutions 中的 | 后面有个空格, |+<space>才会被替换 。。。有点无语

不可能

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

不能级别,添加了 token 用来对付 CSRF 跨域请求攻击。还对参数进行真正的验证,只有 ip 参数才能够 ping。如果是我写的话可能会用估计会用正则表达式。这个方式验证也可以了。

DVWA 黑客攻防演练(三)命令行注入(Command Injection)的更多相关文章

  1. DVWA 黑客攻防演练(一) 介绍及安装

    原本是像写一篇 SELinux 的文章的.而我写总结文章的时候,总会去想原因是什么,为什么会有这种需求.而我发觉 SELinux 的需求是编程人员的神奇代码或者维护者的脑袋短路而造成系统容易被攻击.就 ...

  2. DVWA 黑客攻防演练(五)文件上传漏洞 File Upload

    说起文件上传漏洞 ,可谓是印象深刻.有次公司的网站突然访问不到了,同事去服务器看了一下.所有 webroot 文件夹下的所有文件都被重命名成其他文件,比如 jsp 文件变成 jsp.s ,以致于路径映 ...

  3. DVWA 黑客攻防演练(二)暴力破解 Brute Froce

    暴力破解,简称"爆破".不要以为没人会对一些小站爆破.实现上我以前用 wordpress 搭建一个博客开始就有人对我的站点进行爆破.这是装了 WordfenceWAF 插件后的统计 ...

  4. DVWA 黑客攻防演练(八)SQL 注入 SQL Injection

    web 程序中离不开数据库,但到今天 SQL注入是一种常见的攻击手段.如今现在一些 orm 框架(Hibernate)或者一些 mapper 框架( iBatis)会对 SQL 有一个更友好的封装,使 ...

  5. DVWA 黑客攻防演练(九) SQL 盲注 SQL Injection (Blind)

    上一篇文章谈及了 dvwa 中的SQL注入攻击,而这篇和上一篇内容很像,都是关于SQL注入攻击.和上一篇相比,上一篇的注入成功就马上得到所有用户的信息,这部分页面上不会返回一些很明显的信息供你调试,就 ...

  6. DVWA 黑客攻防演练(十四)CSRF 攻击 Cross Site Request Forgery

    这么多攻击中,CSRF 攻击,全称是 Cross Site Request Forgery,翻译过来是跨站请求伪造可谓是最防不胜防之一.比如删除一篇文章,添加一笔钱之类,如果开发者是没有考虑到会被 C ...

  7. DVWA 黑客攻防演练(十一) 存储型 XSS 攻击 Stored Cross Site Scripting

    上一篇文章会介绍了反射型 XSS 攻击.本文主要是通过 dvwa 介绍存储型 XSS 攻击.存储型 XSS 攻击影响范围极大.比如是微博.贴吧之类的,若有注入漏洞,再假如攻击者能用上一篇文章类似的代码 ...

  8. DVWA 黑客攻防演练(十)反射型 XSS 攻击 Reflected Cross Site Scripting

    XSS (Cross-site scripting) 攻击,为和 CSS 有所区分,所以叫 XSS.又是一种防不胜防的攻击,应该算是一种 "HTML注入攻击",原本开发者想的是显示 ...

  9. DVWA 黑客攻防演练(六)不安全的验证码 Insecure CAPTCHA

    之前在 CSRF 攻击 的那篇文章的最后,我觉得可以用验证码提高攻击的难度. 若有验证码的话,就比较难被攻击者利用 XSS 漏洞进行的 CSRF 攻击了,因为要识别验证码起码要调用api,跨域会被浏览 ...

随机推荐

  1. [Swift]LeetCode152. 乘积最大子序列 | Maximum Product Subarray

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  2. [Swift]LeetCode314. 二叉树的竖直遍历 $ Binary Tree Vertical Order Traversal

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  3. 【Scala篇】--Scala中Trait、模式匹配、样例类、Actor模型

    一.前述 Scala Trait(特征) 相当于 Java 的接口,实际上它比接口还功能强大. 模式匹配机制相当于java中的switch-case. 使用了case关键字的类定义就是样例类(case ...

  4. java代码之美(9)---guava之Lists、Maps

    guava之Lists.Maps 谷歌提供了guava包里面有很多的工具类,Lists和Maps集合工具,集合操作做了些优化提升. 1.概述 1.静态工厂方法 (1)Guava提供了能够推断范型的静态 ...

  5. C#版(击败100.00%的提交) - Leetcode 744. 寻找比目标字母大的最小字母 - 题解

    C#版 - Leetcode 744. 寻找比目标字母大的最小字母 - 题解 744.Find Smallest Letter Greater Than Target 在线提交: https://le ...

  6. 知其所以然~tcp和udp的区别

    TCP UDP TCP与UDP基本区别 基于连接与无连接 TCP要求系统资源较多,UDP较少: UDP程序结构较简单 流模式(TCP)与数据报模式(UDP); TCP保证数据正确性,UDP可能丢包 T ...

  7. Chapter 5 Blood Type——25

    "I usually am — but about what in particular this time?" “我通常都是 —— 但是这次有什么特殊的吗?” "Dit ...

  8. Chapter 4 Invitations——22

    "Are you going all by yourself?" he asked, and I couldn't tell if he was suspicious I had ...

  9. angr初使用(1)

    angr是早在几年前就出来了的一款很好用的工具,如今也出了docker,所以想直接安个docker来跑一跑. docker pull angr/angr .下载下来以后,进入docker ,这时并没有 ...

  10. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->新增模块管理界面导出功能(可按条件导出)

    导出功能在很多应用场景中都需要,RDIFramework.NET V3.2版本在模块管理界面新增了导出功能,方便管理员对所有配置的模块进行管理. 一.Web版模块管理导出功能 Web版本的模块导出功能 ...