本文檔討論如何以最簡潔的方式解析HTTP響應主體.
### 得到一個響應
要獲得響應,您可以使用 Rex::Proto::Http::Client, 或 the HttpClient mixin來生成一個http請求.如果你正在編寫一個模塊,你應該使用mixin
以下是使用HttpClient中的#send_request_cgi方法的示例:
~~~
res = send_request_cgi({'uri'=>'/index.php'})
~~~
返回值res是一個Rex::Proto::Http::Response 對象,但是也可能由于連接/響應超時而得到一個NilClass。
### 得到一個響應主體
一個Rex::Proto::Http::Response對象,下面是如何檢索HTTP正文
~~~
data = res.body
~~~
如果你想獲得原始的http響應(包括響應信息,代碼,頭,主體).你可以簡單的
~~~
raw_res = res.to_s
~~~
但是在這個文檔,我們只關注主體
### 選擇正確的解析器
| 格式 |解析器 |
| --- | --- |
| HTML | Nokogiri |
|XML|Nokogiri|
|JSON|JSON|
如果您需要解析的格式不在列表中,則返回res.body
#### 用Nokogiri解析HTML
當你有一個Rex::Proto::Http::Response 的時候,調用的方法是:
~~~
html = res.get_html_document
~~~
這會給你一個Nokogiri::HTML::Document,它將允許你使用Nokogiri api
Nokogiri有兩種常用的方法來查找元素:#at和#search。主要區別是#at方法將只返回第一個結果,而#search將返回所有找到的結果(在一個數組中)。
考慮下面的例子作為你的HTML響應:
~~~
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<div class="greetings">
<div id="english">Hello</div>
<div id="spanish">Hola</div>
<div id="french">Bonjour</div>
</div>
</body>
<html>
~~~
#### #at的基本例子
如果使用#at方法來查找DIV元素
~~~
html = res.get_html_document
greeting = html.at(' div ')
~~~
然后,greeting變量應該是一個Nokogiri::XML::Element對象,它給了我們這個HTML塊(因為#at方法只返回第一個結果)
~~~
<div class="greetings">
<div id="english">Hello</div>
<div id="spanish">Hola</div>
<div id="french">Bonjour</div>
</div>
~~~
從特定元素樹中抓取元素
~~~
html = res.get_html_document
greeting = html.at('div//div')
~~~
然后,該greeting變量應該給我們這個HTML塊:
~~~
<div id="english">Hello</div>
~~~
#### 抓取具有特定屬性的元素
例如我們不想要英文的,我們想要西班牙語的.那我們可以這樣做
~~~
html = res.get_html_document
greeting = html.at('div[@id="spanish"]')
~~~
#### 抓取具有特定文本的元素
假設我只知道有一個DIV元素說“Bonjour”,我想抓住它,然后我可以這樣做:
~~~
html = res.get_html_document
greeting = html.at('//div[contains(text(), "Bonjour")]')
~~~
或者讓我們說,我不知道“Bonjour”這個詞是什么元素,那么我可以對此有點模糊
~~~
html = res.get_html_document
greeting = html.at('[text()*="Bonjour"]')
~~~
### #search的基本用法
#search方法返回一個元素數組。假設我們想要找到所有的DIV元素,那么這里是怎么做
~~~ruby
html = res.get_html_document
divs = html.search('div')
~~~
#### 獲取文本
當你有一個元素時,你總是可以調用#text方法來獲取文本。例如
~~~ruby
html = res.get_html_document
greeting = html.at('[text()*="Bonjour"]')
print_status(greeting.text)
~~~
#text方法也可以被用作去除所有HTML標簽
~~~ruby
html = res.get_html_document
print_line(html.text)
~~~
以上將輸出:
~~~
"\n\nHello, World!\n\n\n\nHello\nHola\nBonjour\n\n\n"
~~~
如果你是想保留HTML標簽,那么不要調用#text,而是調用#inner_html。
#### 訪問屬性
使用一個元素,只需調用#attributes即可。
#### dom樹
使用#next方法轉到下一個元素。
使用#previous方法回到前一個元素。
使用#parent方法來查找父元素。
使用#children方法獲取所有的子元素。
使用#traverse方法進行復雜的解析。
### 解析xml
要從Rex::Proto::Http::Response獲取XML正文,請執行
~~~
xml = res.get_xml_document
~~~
其余的應該和解析HTML非常相似。
### 解析json
要從Rex::Proto::Http::Response獲取json正文,請執行
~~~
json = res.get_json_document
~~~
- Home
- 開始使用
- 安裝metasploit開發環境
- 使用metasploit
- 使用git
- 報告一個bug
- 貢獻代碼
- 貢獻給metasploit
- 創建一個loginscans Metasploit模塊
- 接受模塊和增強功能的指導
- 常見的Metasploit模塊代碼錯誤
- 樣式提示
- metasploit提交者
- metasploit開發
- 為什么是ruby
- 樣式提示
- 如何開始寫一個exploit
- 如何開始寫一個輔助模塊
- 如何開始寫一個post模塊
- 如何開始寫一個Meterpreter腳本
- 載入外部模塊
- exploit rank
- Metasploit模塊引用標識符
- 怎么在你的exploit中確認window補丁程序級別
- 如何使用filedropper清理文件
- 如何棄用metasploit模塊
- 如何在模塊開發中報告或儲存數據
- 在metasploit如何使用日志
- 如何在metasploit對JavaScript進行混淆
- 如何解析一個http響應
- 如何使用HTTPClient發送HTTP請求
- 如何使用命令階段
- 如何使用數據儲存選項
- 如何在window后期開發中使用railgun
- 如何在exploit中使用powershell
- 如何使用PhpEXE來利用任意文件上傳漏洞
- 如何使用FILEFORMAT mixin創建一個文件格式exploit
- 如何使用BrowserExploitServer編寫一個瀏覽器exploit
- 如何使用HttpServer編寫瀏覽器exploit
- 如何編寫一個check()方法
- 如何使用Seh mixin來利用異常處理程序
- 如何在Windows上使用WbemExec進行寫入權限攻擊
- 如何使用httpserver和httpclient編寫一個模塊
- payloads如何工作
- 如何免殺
- 如何正確使用metasploit模塊