DVWA of XSS (cross site scripting)

XSS

XSS , stands for Cross Site Scripting , namely cross-site scripting attacks, in a sense is a kind of injection attack is an attacker to inject malicious script code in a page, when the victim visited the page, the malicious code in its browser the execution, you need to emphasize that, XSS is not limited to JavaScript , also includes flash and other scripting languages. Depending on whether the malicious code stored in the server, XSS can be divided into memory type XSS reflective type XSS .

DOM -type XSS because of its specificity, are often divided into a third, which is based DOM tree XSS . For example server is often used document.boby.innerHtml other functions dynamically generated html page, if they do not function to filter or check at that certain variables will produce DOM type XSS .

DOM type XSS may be a storage type, there may be a reflective type.

 

 

Reflection type XSS

 

 

 

 

 

 

Below the four-level code analysis.

 

Low

 

Server-side core code

 

<?php 

// Is there any input? 

if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) { 

    // Feedback for end user 

    echo '<pre>Hello ' . $_GET[ 'name' ] . '</pre>'; 

} 

?>

 

 

 

 

The code may directly reference the name parameter, and there is no filtering and inspection, there are obvious XSS vulnerabilities.

 

Exploit

 

Input <Script> Alert (/ XSS /) </ Script> , successful playing box:

 

 

 

Corresponding XSS link:

http://192.168.50.100/dvwa/vulnerabilities/xss_r/?name=%3Cscript%3Ealert(/xss/)%3C%2Fscript%3E#

 

 

 

 

 

 

 

Medium Code:

 

<?php 
// Is there any input? 
if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) { 
    // Get input 
    $name = str_replace( '<script>', '', $_GET[ 'name' ] ); 
    // Feedback for end user 
    echo "<pre>Hello ${name}</pre>"; 
} 
?>

 

 

 

 

 

可以看到,这里对输入进行了过滤,基于黑名单的思想,使用str_replace函数将输入中的<script>删除,这种防护机制是可以被轻松绕过的。

 

 

 

漏洞利用:使用双写绕过:<sc<script>ript>alert(/xss/)</script>  成功弹框

 

 

 

 

 

 

 

 

或者大小写混合绕过:<ScRipt>alert(/xss/)</script>

 

HIGH代码:

 

 

<?php 
// Is there any input? 
if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) { 
    // Get input 
    $name = preg_replace( '/<(.*)s(.*)c(.*)r(.*)i(.*)p(.*)t/i', '', $_GET[ 'name' ] ); 
    // Feedback for end user 
    echo "<pre>Hello ${name}</pre>"; 
} 
?>

 

可以看到,High级别的代码同样使用黑名单过滤输入,preg_replace()函数用于正则表达式的搜索和替换,这使得双写绕过、大小写混淆绕过(正则表达式中i表示不区分大小写)不再有效。

 

漏洞利用:

 

虽然无法使用<script>标签注入XSS代码,但是可以通过img、body等标签的事件或者iframe等标签的src注入恶意的js代码。

 

输入<img src=1 onerror=alert(/xss/)>,成功弹框:

 

 

 

 

 

 

 

 

 

存储型XSS 

 

low代码:

 

<?php 
if( isset( $_POST[ 'btnSign' ] ) ) { 
    // Get input 
    $message = trim( $_POST[ 'mtxMessage' ] ); 
    $name    = trim( $_POST[ 'txtName' ] ); 
    // Sanitize message input 
    $message = stripslashes( $message ); 
    $message = mysql_real_escape_string( $message ); 
    // Sanitize name input 
    $name = mysql_real_escape_string( $name ); 
    // Update database 
    $query  = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );"; 
    $result = mysql_query( $query ) or die( '<pre>' . mysql_error() . '</pre>' ); 
    //mysql_close(); 
} 
?>

 

 

 

相关函数介绍

 

trim(string,charlist)

 

函数移除字符串两侧的空白字符或其他预定义字符,预定义字符包括、\t、\n、\x0B、\r以及空格,可选参数charlist支持添加额外需要删除的字符。 

 

mysql_real_escape_string(string,connection)

 

函数会对字符串中的特殊符号(\x00,\n,\r,\,‘,“,\x1a)进行转义。

 

stripslashes(string)

 

函数删除字符串中的反斜杠。

 

可以看到,对输入并没有做XSS方面的过滤与检查,且存储在数据库中,因此这里存在明显的存储型XSS漏洞。

 

 

 

漏洞利用:

 

message一栏输入<script>alert(/xss/)</script>,成功弹框:

 

 

 

 

 

 

并且因为是存储型,每次刷新或者进入之后都会弹框。

 

name一栏中有长度限制,可以抓包之后再修改数据

 

 

name一栏前端有字数限制,抓包改为<script>alert(/name/)</script>

 

 

 

 

 

 

 

 

 

 

Medium代码:

<?php 
if( isset( $_POST[ 'btnSign' ] ) ) { 
    // Get input 
    $message = trim( $_POST[ 'mtxMessage' ] ); 
    $name    = trim( $_POST[ 'txtName' ] ); 
    // Sanitize message input 
    $message = strip_tags( addslashes( $message ) ); 
    $message = mysql_real_escape_string( $message ); 
    $message = htmlspecialchars( $message ); 
    // Sanitize name input 
    $name = str_replace( '<script>', '', $name ); 
    $name = mysql_real_escape_string( $name ); 
    // Update database 
    $query  = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );"; 
    $result = mysql_query( $query ) or die( '<pre>' . mysql_error() . '</pre>' ); 
    //mysql_close(); 
} 
?>

 

 

 

 

相关函数说明

 

 

 

strip_tags() 函数剥去字符串中的HTML、XML以及PHP的标签,但允许使用<b>标签。

 

 

 

addslashes() 函数返回在预定义字符(单引号、双引号、反斜杠、NULL)之前添加反斜杠的字符串。

 

 

 

可以看到,由于对message参数使用了htmlspecialchars函数进行编码,因此无法再通过message参数注入XSS代码,但是对于name参数,只是简单过滤了<script>字符串,仍然存在存储型的XSS。

 

 

 

漏洞利用:
抓包双写绕过:<sc<script>ript>alert(/xss/)</script>

 

 

 

 

 

 

 

 

 成功弹窗:

 

 

 或者大小写混合绕过:

<Script>alert(/xss/)</script>

 

 

HIgh代码:

 

 

<?php 
if( isset( $_POST[ 'btnSign' ] ) ) { 
    // Get input 
    $message = trim( $_POST[ 'mtxMessage' ] ); 
    $name    = trim( $_POST[ 'txtName' ] ); 
    // Sanitize message input 
    $message = strip_tags( addslashes( $message ) ); 
    $message = mysql_real_escape_string( $message ); 
    $message = htmlspecialchars( $message ); 
    // Sanitize name input 
    $name = preg_replace( '/<(.*)s(.*)c(.*)r(.*)i(.*)p(.*)t/i', '', $name ); 
    $name = mysql_real_escape_string( $name ); 
    // Update database 
    $query  = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );"; 
    $result = mysql_query( $query ) or die( '<pre>' . mysql_error() . '</pre>' ); 
    //mysql_close(); 
} 
?>

 

 

 

可以看到,这里使用正则表达式过滤了<script>标签,但是却忽略了img、iframe等其它危险的标签,因此name参数依旧存在存储型XSS。

漏洞利用:

抓包改name参数为<img src=1 onerror=alert(1)>:

 

 

 

 

 

 

 

成功弹框

 

 

 

Guess you like

Origin www.cnblogs.com/xingyuner/p/12288902.html