DVWA之命令注入
DVWA之命令注入
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>";
}
?>
ip引數沒有做任何的過濾就直接放在shell_exec函式中執行了,執行語句為shell_exec( 'ping 127.0.0.1 && whami ')。(linux下使用whoami檢視使用者命令,windows使用net user)
這裡對使用者輸入的ip並沒有進行任何的過濾,所以我們可以進行命令執行漏洞
由於是在windows系統下的cmd中進行,所以我們一開始便用dir列出所有檔案
利用| ipconfig獲得本機的IP地址
利用dir列出所有檔案
tips:
win10不支援通過ping用net user來檢視使用者
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>";
}
?>
檢視原始碼,發現把”&&” 、”;”轉為空,即刪除
”&&”與” &”的區別:
Command 1&&Command 2
先執行Command 1,執行成功後執行Command 2,否則不執行Command 2
Command 1&Command 2
先執行Command 1,不管是否成功,都會執行Command 2
輸入” 192.168.2.3& dir”時,同樣可以攻擊,表明沒有對”&”過濾,”&&”和”&”是有區別的,”&&”是短路運算子,只有前一步執行成功才會執行後一步,而”&”則兩個表示式都會執行。
由以上waf可知,此waf只是過濾了 “&&”和”;”這兩個特殊字元,所以,可以通過使用”&“,”|”,”||”繞過
可以用命令&dir檢視
可以用命令&;&dir檢視
原理:在經過對;的過濾後,自動拼接成&&
High
先看原始碼
1 <?php
2
3 if( isset( $_POST[ 'Submit' ] ) ) {
4 // Get input
5 $target = trim($_REQUEST[ 'ip' ]);
6
7 // Set blacklist
8 $substitutions = array(
9 '&' => '',
10 ';' => '',
11 '| ' => '', //仔細看|後有空字元,過濾不完全
12 '-' => '',
13 '$' => '',
14 '(' => '',
15 ')' => '',
16 '`' => '',
17 '||' => '',
18 );
19
20 // Remove any of the characters in the array (blacklist).
21 $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
22
23 // Determine OS and execute the ping command.
24 if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
25 // Windows
26 $cmd = shell_exec( 'ping ' . $target );
27 }
28 else {
29 // *nix
30 $cmd = shell_exec( 'ping -c 4 ' . $target );
31 }
32
33 // Feedback for the end user
34 echo "<pre>{$cmd}</pre>";
35 }
36
37 ?>
此waf將”&”,”;”,”| ”,”-”,”$”,”(”,”)”,”`”,”||”這些字元直接全部轉換成空格
仔細觀察,可以發現”| ”中,| 後面有一個空字元,因此,可以使用”|”進行繞過
用命令“|dir"查詢檔案
想要對管道符有更多瞭解,請看這篇部落格:管道符_夜飛雪的部落格-CSDN部落格_管道符
Impossible
先看原始碼
1 <?php
2
3 if( isset( $_POST[ 'Submit' ] ) ) {
4 // Check Anti-CSRF token
5 checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
6
7 // Get input
8 $target = $_REQUEST[ 'ip' ];
9 $target = stripslashes( $target );
10
11 // Split the IP into 4 octects
12 $octet = explode( ".", $target );
13
14 // Check IF each octet is an integer
15 if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
16 // If all 4 octets are int's put the IP back together.
17 $target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
18
19 // Determine OS and execute the ping command.
20 if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
21 // Windows
22 $cmd = shell_exec( 'ping ' . $target );
23 }
24 else {
25 // *nix
26 $cmd = shell_exec( 'ping -c 4 ' . $target );
27 }
28
29 // Feedback for the end user
30 echo "<pre>{$cmd}</pre>";
31 }
32 else {
33 // Ops. Let the user name theres a mistake
34 echo '<pre>ERROR: You have entered an invalid IP.</pre>';
35 }
36 }
37
38 // Generate Anti-CSRF token
39 generateSessionToken();
40
41 ?>
stripslashes(string) : 該函式會刪除字串string中的反斜槓,返回已剝離反斜槓的字串。
explode(separator,string,limit): 該函式把字串打散為陣列,返回字串的陣列。引數separator規定在哪裡分割字串,引數string是要分割的字串,可選引數limit規定所返回的陣列元素的數目。
is_numeric(string): 該檢測string是否為數字或數字字串,如果是返回TRUE,否則返回FALSE。
可以看到,Impossible級別的程式碼加入了Anti-CSRF token,同時對引數ip進行了嚴格的限制,只有諸如“數字.數字.數字.數字”的輸入才會被接收執行,因此不存在命令注入漏洞。