前言
DVWA(Damn Vulnerable Web Application)是安全入门者们最为熟知的练习靶场,它能够帮助小白对常见的安全问题进行理解,并且随着理解问题的深入自主调整靶场的安全级别。
DVWA靶场共包含十四个练习模块,每个模块分为 Low,Medium,High,Impossible四种难度,而Impossible和High相对而言,对于后续在进行过基础安全建设的生产环境中的实战更加具有参考意义。
- Brute Force——暴力破解
- Command Injection——命令注入
- CSRF——跨站请求伪造
- File Inclusion——文件包含
- File Upload——文件上传漏洞
- Insecure CAPTCHA——不安全的验证
- SQL Injection——sql注入
- SQL Injection(Blind)——sql注入(盲注)
- Weak Session IDs——不安全的SessionID
- XSS(DOM)——DOM型XSS
- XSS(Reflected)——反射型XSS
- XSS(Stored)——存储型XSS
- CSP Bypass ——绕过内容安全策略
- JavaScript——JS攻击
一、模块介绍
Command Injection,命令注入是指由于程序中对提交的数据过滤不严格,导致通过构造特殊字符串(将自己的输入作为系统命令的参数拼接到命令行中),被程序执行后可获取到敏感的数据或资源情况。
原理分析
A & B A && B A | B A || B AB间无制约关系
A执行成功后B执行 A的输出是B的输入 A失败后执行B
二、各级通关
命令注入的形成需要如下三个条件:
- 使用了内部调用shell的函数:system(),exec()等
- 将外界传入的参数没有足够的过滤,直接传递给内部调用shell的函数
- 参数中shell的元字符没有被转义
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>";
}
?>
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>";
}
?>
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();
?>