模块简介

HackRequests 是基于Python3.x的一个给黑客们使用的 http 底层网络库。如果你需要一个不那么臃肿而且像 requests 一样优雅的设计,并且提供底层请求包/返回包原文来方便你进行下一步分析,如果你使用Burp Suite,可以将原始报文直接复制重放,对于大量的 HTTP 请求,hack-requests 线程池也能帮你实现最快速的响应。

  • 像 requests 一样好用的设计
  • 提供接口获得底层请求包、返回包原文,方便下一步分析
  • 支持发送 HTTP 原始报文,支持从 Burp Suite 等抓包软件中重放
  • hack-requests 是单文件模块,可方便移植到其他项目中。

模块安装

1
pip install HackRequests

仅支持 Python3.x

特征

不需要关注参数类型

requests模块中,为了方便使用,header、cookie、post等信息都是以字典形式传参,但对于黑客来说,常常截获到的是一个文本,手动转换成字典费时费力。但在HackRequests中,这些参数你既可以传入一个字典,也可以传入一个文本,程序会自动识别并转换。

request:

1
2
3
4
5
6
7
8
9
10
11
12
import requests

url = "https://www.qwesec.com"

# 需要使用字典类型
headers = {
"User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
}
# 发送请求
requests = requests.get(url=url, headers=headers)
# 获取响应
print(requests.text)

HackerRequests

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import HackRequests as HR

hacker = HR.hackRequests()

url = "https://www.qwesec.com"
headers = """
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.71 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Sec-Ch-Ua: "Not_A Brand";v="8", "Chromium";v="120"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "macOS"
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
If-None-Match: W/"65b777c2-cbec"
If-Modified-Since: Mon, 29 Jan 2024 10:02:42 GMT
Priority: u=0, i
Connection: close
"""
# 发送请求
req = hacker.http(url=url, ssl=True, headers=headers)
# 输出响应
print(req.text())

提供底层包分析

hackRequests返回结果中带有log参数,记录了requestresponse,在写扫描器的时候这两个参数参考非常重要。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import HackRequests as HR

hacker = HR.hackRequests()

url = "https://www.qwesec.com"
headers = """
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.71 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Sec-Ch-Ua: "Not_A Brand";v="8", "Chromium";v="120"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "macOS"
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
If-None-Match: W/"65b777c2-cbec"
If-Modified-Since: Mon, 29 Jan 2024 10:02:42 GMT
Priority: u=0, i
Connection: close
"""
req = hacker.http(url=url, ssl=True, headers=headers)
# 获取请求头
print(req.log.get('request'))
print("")
# 获取响应头
print(req.log.get('response'))
#req = hacker.httpraw(raw=raw, ssl=True)
#print(req.log.get('request'))
#print(req.log.get('response'))

Burpsuite 重放

支持直接将代理抓包软件中的请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import HackRequests as HR

hacker = HR.hackRequests()

raw = """
GET / HTTP/2
Host: www.qwesec.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.71 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Sec-Ch-Ua: "Not_A Brand";v="8", "Chromium";v="120"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "macOS"
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
If-None-Match: W/"65b777c2-cbec"
If-Modified-Since: Mon, 29 Jan 2024 10:02:42 GMT
Priority: u=0, i
Connection: close
"""
# 发送请求
req = hacker.httpraw(raw=raw, ssl=True)
# 输出响应
print(req.text())

内置线程池

在并发网络访问方面,HackRequests 的线程池可以帮助您把网络并发优化到极致。

1
2
3
4
5
6
7
8
9
10
11
12
import HackRequests as HR

def _callback(r:HR.response):
# 从回调函数取出结果,参数r是response结果
print(r.text())

threadpool = HR.threadpool(threadnum=10,callback=_callback)
url = "https://www.qwesec.com"
# 50个线程
for i in range(50):
threadpool.http(url)
threadpool.run()

学习方面

代码开源,不到500行代码且中文注释可以帮助你更好的理解该项目思路。

参数名 参数类型 参数功能
raw(必须) Str 原始报文
ssl Bool 网站是否是https,默认为False
proxy Tuple 代理地址
location Bool 自动跳转,默认为Ture
real_host str 用于host头注入中在header host字段填写注入语句,这里填写真实地址 如 “127.0.0.1:8000” 具体参考:https://github.com/boy-hack/hack-requests/blob/master/demo/CVE-2016-10033.py

注: httpraw 方法最后会解析格式到http方法,所以http方法使用的参数这里都可以使用。

说明文档

快速使用

1
2
3
4
import HackRequests
hack = HackRequests.hackRequests()
url = "http://www.baidu.com/index.php"
u = hack.http(url,method="HEAD")

说明: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
real_host 用于host头注入中在header host字段填写注入语句,这里填写真实地址 如 “127.0.0.1:8000” 具体参考:https://github.com/boy-hack/hack-requests/blob/master/demo/CVE-2016-10033.py str

发送原始数据

使用hackRequests中的 httpraw方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import HackRequests as HR

hacker = HR.hackRequests()

raw = """
GET / HTTP/2
Host: www.qwesec.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.71 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Priority: u=0, i
Connection: close
"""
req = hacker.httpraw(raw=raw, ssl=True)
print(req.text())
参数名 参数类型 参数功能
raw(必须) Str 原始报文
ssl Bool 网站是否是https,默认为False
proxy Tuple 代理地址
location Bool 自动跳转,默认为Ture
real_host str 用于host头注入中在header host字段填写注入语句,这里填写真实地址 如 “127.0.0.1:8000” 具体参考:https://github.com/boy-hack/hack-requests/blob/master/demo/CVE-2016-10033.py

注: httpraw 方法最后会解析格式到http方法,所以http方法使用的参数这里都可以使用

response

可使用如下接口获取hack.http()hack.httpraw()的返回值

接口参数 功能 返回值类型
status_code 获取返回状态码 Int
content() 获取返回字节 Bytes
text() 获取返回文本(会自动转码) Str
header 返回原始响应头 Str
headers 返回原始响应头的字典形式 Dict
charset 获取编码类型 Str
log 获取底层发送的请求包/返回包 Dict
url 返回url,若发生跳转则为跳转后的 Str
cookie 返回请求后的Cookie Str
cookies 返回请求后的Cookie字典形式 Dict

线程池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import HackRequests


def _callback(r:HackRequests.response):
# 从回调函数取出结果,参数r是response结果
print(r.text())


threadpool = HackRequests.threadpool(threadnum=10,callback=_callback,timeout=10)
# 可设置http访问的超时时间,不设置则默认为10s。线程数量[threadnum]设置根据自己电脑配置设置,默认为10,值越大线程越多同一秒访问的网站数量也越多。
url = "http://www.baidu.com"
for i in range(50):
threadpool.http(url)
threadpool.run()

回调函数参数 rresponse 类,见[说明文档]-[response]
在声明一个线程池为threadpool后,有以下三种方法可以调用

方法名 传入参数 功能
http() 见[说明文档]-[快速使用] 将HTTP请求后加入现成队列,准备执行
httpraw() 见[说明文档]-[快速使用] 将HTTP请求后加入现成队列,准备执行
stop() 停止线程池
run() 启动线程池

官方文档:https://github.com/boy-hack/hack-requests