HackRequests 是基于Python3.x的一个给黑客们使用的http底层网络库。如果你需要一个不那么臃肿而且像requests一样优雅的设计,并且提供底层请求包/返回包原文来方便你进行下一步分析,如果你使用Burp Suite,可以将原始报文直接复制重放,对于大量的HTTP请求,hack-requests线程池也能帮你实现最快速的响应。
- 像requests一样好用的设计
- 提供接口获得底层请求包、返回包原文,方便下一步分析
- 支持发送HTTP原始报文,支持从Burp Suite等抓包软件中重放
- hack-requests内部使用连接池、线程池等技术,hack-requests会用最快的方式获取响应数据。使大量I/O密集型操作无需关注这些细节
hack-requests是单文件模块,可方便移植到其他项目中。
安装
仅支持python3
- pip install HackRequests
特征
更人性化的调用方式
在0.3.2版本中更新了更简单的调用方式。 之前http访问调用方式为
import HackRequests hack = HackRequests.hackRequests() url = “http://www.hacking8.com“ hh = hack.http(url) print(hh.text())
现在调用方式为
import HackRequests as hack url = “http://www.hacking8.com“ hh = hack.http(url) print(hh.text())
当然也是兼容之前的调用方式不需要关注参数类型
在requests模块中,为了方便使用,header、cookie、post等信息都是以字典形式传参,但对于黑客来说,常常截获到的是一个文本,手动转换成字典费时费力。但在HackRequests中,这些参数你既可以传入一个字典,也可以传入一个文本,程序会自动识别并转换。 ```python import HackRequests
hack = HackRequests.hackRequests() url = “http://www.hacking8.com“
header = ‘’’ Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: zh-CN,zh;q=0.9,en;q=0.8 ‘’’ hh = hack.http(url, headers=header) print(hh.text())
headers = { “Connection”: “keep-alive”, “User-Agent”: “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36” } uu = hack.http(url, headers=headers) print(uu.text())
<a name="NMcdJ"></a>### 提供底层包分析hackRequests返回结果中带有log参数,记录了request和response,在写扫描器的时候这两个参数参考非常重要。```pythonimport HackRequestshack = HackRequests.hackRequests()url = "http://www.hacking8.com"header = '''Connection: keep-aliveCache-Control: max-age=0Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8Accept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.9,en;q=0.8'''hh = hack.http(url, headers=header)print(hh.log.get("request"))print()print(hh.log.get("response"))
返回
GET / HTTP/1.1Host: www.hacking8.comConnection: Keep-AliveCache-Control: max-age=0Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8Accept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.9,en;q=0.8HTTP/1.1 200 OKServer: nginxDate: Sat, 01 Sep 2018 12:52:35 GMTContent-Type: text/htmlContent-Length: 580Last-Modified: Thu, 16 Aug 2018 09:50:56 GMTConnection: keep-aliveETag: "5b754900-244"Accept-Ranges: bytes
BurpSuite 重放
支持直接将代理抓包软件中的请求
import HackRequestshack = HackRequests.hackRequests()raw = '''GET / HTTP/1.1Host: www.hacking8.comConnection: Keep-AliveCache-Control: max-age=0Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8Accept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.9,en;q=0.8'''hh = hack.httpraw(raw)print(hh.text())
内置线程池
在并发网络访问方面,HackRequests的线程池可以帮助您把网络并发优化到极致。
import HackRequestsdef _callback(r:HackRequests.response):# 从回调函数取出结果,参数r是response结果print(r.text())threadpool = HackRequests.threadpool(threadnum=10,callback=_callback)url = "http://www.baidu.com"for i in range(50):threadpool.http(url)threadpool.run()
学习方面
代码开源,不到500行代码且中文注释可以帮助你更好的理解该项目思路。
说明文档
快速使用
import HackRequestsdef _callback(r:HackRequests.response):# 从回调函数取出结果,参数r是response结果print(r.text())threadpool = HackRequests.threadpool(threadnum=10,callback=_callback)url = "http://www.baidu.com"for i in range(50):threadpool.http(url)threadpool.run()
)
说明:HEAD模式可以帮助你更快的检测网页是否存在
使用hack.http()可以填写下列参数,当然,除了url参数外都不是必须的。
| 参数名 | 参数功能 | 参数类型 |
|---|---|---|
| url(必须) | 用于传递一个地址 | Str |
| post | post参数用于传递post提交,此参数被选择时,method自动变为POST,post参数的类型可以为Str或者Dict | Str/Dict |
| method | 访问模式,目前支持三种 HEAD、GET、POST,默认为GET | Str |
| location | 当状态码为301、302时会自动跳转,默认为True | Bool |
| proxy | 代理,需要传入一个tuple,类似 (‘127.0.0.1’,’8080’) | Tuple |
| headers | 自定义HTTP头,可传入字典或原始的请求头 | Str/Dict |
| cookie | 自定义Cookie,可传入字典或原始cookie字符串 | Str/Dict |
| referer | 模拟用户Referer | Str |
| user_agent | 用户请求头,若为空则会模拟一个正常的请求头 | Str |
发送原始响应头
使用hackRequests中的 httpraw方法
import HackRequestshack = HackRequests.hackRequests()raw = '''GET / HTTP/1.1Host: www.hacking8.comConnection: Keep-AliveCache-Control: max-age=0Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8Accept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.9,en;q=0.8'''hh = hack.httpraw(raw)print(hh.text())
| 参数名 | 参数类型 | 参数功能 |
|---|---|---|
| raw(必须) | Str | 原始报文 |
| ssl | Bool | 网站是否是https,默认为False |
| proxy | Tuple | 代理地址 |
| location | Bool | 自动跳转,默认为Ture |
response
可使用如下接口获取hack.http()的返回值
| 接口参数 | 功能 | 返回值类型 |
|---|---|---|
| status_code | 获取返回状态码 | Int |
| content() | 获取返回字节 | Bytes |
| text() | 获取返回文本(会自动转码) | Str |
| header | 返回原始响应头 | Str |
| headers | 返回原始响应头的字典形式 | Dict |
| charset | 获取编码类型 | Str |
| log | 获取底层发送的请求包/返回包 | Dict |
| url | 返回url,若发生跳转则为跳转后的 | Str |
| cookie | 返回请求后的Cookie | Str |
| cookies | 返回请求后的Cookie字典形式 | Dict |
线程池
import HackRequestsdef _callback(r:HackRequests.response):# 从回调函数取出结果,参数r是response结果print(r.text())threadpool = HackRequests.threadpool(threadnum=10,callback=_callback)url = "http://www.baidu.com"for i in range(50):threadpool.http(url)threadpool.run()
回调函数参数r是response类,见[说明文档]-[response]
在声明一个线程池为threadpool后,有以下三种方法可以调用
| 方法名 | 传入参数 | 功能 |
|---|---|---|
| http() | 见[说明文档]-[快速使用] | 将HTTP请求后加入现成队列,准备执行 |
| httpraw() | 见[说明文档]-[快速使用] | 将HTTP请求后加入现成队列,准备执行 |
| stop() | 停止线程池 | |
| run() | 启动线程池 |
