-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzzer.py
144 lines (99 loc) · 4.65 KB
/
fuzzer.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import time
import requests
import threading
import json
import logging
import sys
from transformer import HeaderGenerator
log = logging.getLogger(__name__)
logging.getLogger("urllib3").setLevel(logging.WARNING)
class ServerFuzzer(threading.Thread):
"""Fuzzes HTTP server by generating headers using ML engine"""
def __init__(self, url, model, maxRequests=100):
super().__init__()
self.baseUrl = url
self.model = model
self.requestInterval = 1 # In seconds
self.maxRequests = maxRequests
def request(self, method, endpoint, headers):
"""Make HTTP request and return response"""
payload = {'some': 'data'}
url = self.baseUrl + endpoint
log.info(f"Performing {method} request to: {url}")
if method.upper() == "GET":
return requests.get(url, headers=headers)
elif method.upper() == "POST":
return requests.post(url, data=json.dumps(payload), headers=headers)
else:
raise Exception(f"Request type '{method}' not implemented")
def headersFromResponse(self, resp):
"""Obtain headers from response and format them"""
prefix = f"HTTP/1.1 {resp.status_code} {resp.reason}\n"
headers = "\n".join(f"{k}: {v}" for k, v in resp.headers.items())
return prefix + headers
def parseModelHeaders(self, headers:list) -> dict:
"""Transform model output to dict headers"""
parsed = {}
headers = "".join(headers).split("\n")
for h in headers:
(hName, hArg) = tuple(h.split(":", 1))
parsed[hName] = hArg
return parsed
def genRequestHeaders(self, headersText) -> (str, str, dict):
"""Pass response headers to ML engine and return generated request headers"""
log.debug(f"Generating request headers using '{self.model.__name__}' model")
predictedHeaders = self.model.translate(headersText)[1:-1]
log.debug(f"Predicted request headers: {predictedHeaders}")
method = predictedHeaders[0].upper()
endpoint = "" if predictedHeaders[1] == "/" else predictedHeaders[1]
headers = self.parseModelHeaders(predictedHeaders[4:])
return method, endpoint, headers
def similar(self, response1:requests.Response, response2:requests.Response):
"""Checks similarity of headers to previous one"""
if not (response1 != None and response2 != None):
return False
headers1 = self.headersFromResponse(response1)
headers2 = self.headersFromResponse(response2)
log.debug(f"headers1 len {len(headers1)}, headers2 len {len(headers2)}")
# TODO: More sophisticated. Calculate some score
return True if len(headers2) == len(headers1) else False
def run(self):
headers, prevResponse = None, None
method, endpoint = "GET", ""
while self.maxRequests:
self.maxRequests -= 1
# Initial request launches with GET method to base URL
try:
newResponse = self.request(method, endpoint, headers)
except Exception as e:
log.error(f"Cannot perform request: {e}")
return
log.info(f"Response. Status: {newResponse.status_code} {newResponse.reason}")
if self.similar(newResponse, prevResponse):
log.info("Stop fuzzing. Current response is similar to previous")
break
else:
prevResponse = newResponse
headers = self.headersFromResponse(newResponse)
# Obtain parameters for next request
method, endpoint, headers = self.genRequestHeaders(headers)
log.debug(f"Waiting {self.requestInterval} seconds...")
time.sleep(self.requestInterval)
if __name__ == "__main__":
logging.basicConfig(stream=sys.stdout, level=logging.NOTSET)
tm = HeaderGenerator(
modelPath="./save/request_model_2551_10.pth.tar",
srcVocPath="./save/srcVocab.pcl",
trgVocPath="./save/trgVocab.pcl"
)
serverFuzzer = ServerFuzzer(url="http://google.com", model=tm)
# resp = serverFuzzer.request("POST", "/", None)
# #print("Response. Headers len:", len(resp.headers))
# #print("Similarity:", serverFuzzer.similarity(resp, None))
# respHeaders = serverFuzzer.headersFromResponse(resp)
# #print("Formatted headers:", respHeaders)
# method, endpoint, headers = serverFuzzer.genRequestHeaders(respHeaders)
# print("Generated request method:", method)
# print("Generated request endpoint:", endpoint)
# print("Generated request headers:", headers)
serverFuzzer.run()