天天看點

Security ❀ XSS 反射型 Reflected Cross Site Scripting (XSS)

文章目錄

  • ​​1、low​​
  • ​​2、medium​​
  • ​​3、high​​
  • ​​4、impossible​​

1、low

源碼解析:

<?php

header ("X-XSS-Protection: 0");

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

?>      

源碼無防護,支援XSS攻擊即可;

Security ❀ XSS 反射型 Reflected Cross Site Scripting (XSS)

結果驗證:

Security ❀ XSS 反射型 Reflected Cross Site Scripting (XSS)

2、medium

源碼解析:

<?php

header ("X-XSS-Protection: 0");

// 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函數區分大小寫;

Security ❀ XSS 反射型 Reflected Cross Site Scripting (XSS)

結果驗證:

Security ❀ XSS 反射型 Reflected Cross Site Scripting (XSS)

3、high

源碼解析:

<?php

header ("X-XSS-Protection: 0");

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

?>      

源碼過濾script的正規表達式,可以使用其他标簽進行XSS;

Security ❀ XSS 反射型 Reflected Cross Site Scripting (XSS)

4、impossible

<?php

// Is there any input?
if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {
    // Check Anti-CSRF token
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );

    // Get input
    $name = htmlspecialchars( $_GET[ 'name' ] );

    // Feedback for end user
    echo "<pre>Hello ${name}</pre>";
}

// Generate Anti-CSRF token
generateSessionToken();

?>