-
Notifications
You must be signed in to change notification settings - Fork 1
/
BurpDroid.py
293 lines (262 loc) · 9.81 KB
/
BurpDroid.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import socket
import ssl
from _thread import start_new_thread
from certauth.certauth import CertificateAuthority
from OpenSSL import crypto
from OpenSSL.SSL import FILETYPE_PEM
# from HttpProxy import HTTPPROXY
from os import lseek, path, makedirs, getcwd
from select import select
from cprint import *
from json import loads
from sys import exit
import time
class HTTPPROXY:
def __init__(self, websocket):
self.buffer_size = 4096
self.websocket = websocket
self.ModifedResponse = ""
self.Capture = True
def handle_request(self, client, firstrequest, host):
head = self.parse_head(firstrequest)
headers = head["headers"]
request = "{}\r\n".format(head["meta"])
for key, value in headers.items():
request += "{}: {}\r\n".format(key, value)
request += "\r\n"
if "content-length" in headers:
while len(head["chunk"]) < int(headers["content-length"]):
head["chunk"] += client.recv(self.buffer_size)
request = request.encode() + head["chunk"]
port = 80
response = self.send_to_server(host, port, request)
if response == "drop":
client.close()
return False
client.sendall(response)
client.close()
def ReciveModifedRequestHTTP(self, data):
self.websocket.recive_browserData(data)
while True:
if self.ModifedResponse != "" and self.ModifedResponse != "drop":
return self.ModifedResponse.encode()
elif self.ModifedResponse == "drop":
return "drop"
time.sleep(0.5)
def setData(self, data):
self.ModifedResponse = data
def resetReponse(self):
self.ModifedResponse = ""
def send_to_server(self, host, port, data):
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.connect((socket.gethostbyname(host), port))
if self.Capture:
formated_request = ""
str_req = str(data).split("\\r\\n")
for i in str_req:
cprint.ok(i)
formated_request = formated_request + i + "\n"
new_request = self.ReciveModifedRequestHTTP(formated_request)
if new_request == "drop":
return "drop"
server.sendall(new_request)
self.resetReponse()
else:
server.sendall(data)
head = self.parse_head(server.recv(4096))
headers = head["headers"]
response = "{}\r\n".format(head["meta"])
for key, value in headers.items():
response += "{}: {}\r\n".format(key, value)
response += "\r\n"
if "content-length" in headers:
while len(head["chunk"]) < int(headers["content-length"]):
head["chunk"] += server.recv(self.buffer_size)
response = response.encode() + head["chunk"]
server.close()
return response
def parse_head(self, head_request):
nodes = head_request.split(b"\r\n\r\n")
heads = nodes[0].split(b"\r\n")
meta = heads.pop(0).decode("utf-8")
data = {
"meta": meta,
"headers": {},
"chunk": b""
}
if len(nodes) >= 2:
data["chunk"] = nodes[1]
for head in heads:
pieces = head.split(b": ")
key = pieces.pop(0).decode("utf-8")
if key.startswith("Connection: "):
data["headers"][key.lower()] = "close"
else:
data["headers"][key.lower()] = b": ".join(
pieces).decode("utf-8")
return data
class HTTPSPROXY:
def __init__(self, websocket):
INSTALLED_DIR = "/data/data/com.termux/files/usr/var/burpdroid-proxy/templates"
self.CERT_DIR = INSTALLED_DIR + "/cert/"
self.CA_CERT_NAME = "Ndroid.pem"
self.config = ""
self.isDev = True
self.websocket = websocket
self.ModifedResponse = ""
self.HTTPPROXY = HTTPPROXY(websocket)
self.Capture = True
def set_config(self, config_path="config.json"):
self.config = loads(config_path)
def check_ca_auth(self):
ca_file = self.CERT_DIR + "ca/"
if path.isdir(ca_file):
pass
else:
makedirs(ca_file)
cprint.info("Your can find your ca cert here: " +
ca_file + self.CA_CERT_NAME)
return CertificateAuthority(
'Nsecurity', ca_file + self.CA_CERT_NAME)
def check_cert(self, ca, host):
certpath = self.CERT_DIR + host + ".cert"
keypath = self.CERT_DIR + host + ".key"
if path.isfile(certpath) and path.isfile(keypath):
return certpath, keypath
else:
cert, key = ca.load_cert(host, wildcard=True)
with open(certpath, "w") as file:
file.write(crypto.dump_certificate(
crypto.FILETYPE_PEM, cert).decode("utf-8"))
with open(keypath, "w") as file:
file.write(crypto.dump_privatekey(
crypto.FILETYPE_PEM, key).decode("utf-8"))
return certpath, keypath
def recv_timeout(self, the_socket, timeout=1):
the_socket.setblocking(0)
data = b''
begin = time.time()
while True:
if data and time.time()-begin > timeout:
break
elif time.time()-begin > timeout*2:
break
try:
datax = the_socket.recv(8192)
if datax:
data += datax
begin = time.time()
else:
break
except:
pass
return data
def setData(self, data):
self.ModifedResponse = data
def setCapture(self):
if self.Capture:
self.HTTPPROXY.Capture = False
self.Capture = False
else:
self.HTTPPROXY.Capture = True
self.Capture = True
def resetReponse(self):
self.ModifedResponse = ""
def ReciveModifedRequest(self, formated_request):
self.websocket.recive_browserData(formated_request)
while True:
if self.ModifedResponse != "" and self.ModifedResponse != "drop":
return self.ModifedResponse.encode()
elif self.ModifedResponse == "drop":
return "drop"
time.sleep(0.5)
def request_handler(self, conn, tunn, request):
conn.setblocking(False)
datax = b""
while True:
data = self.recv_timeout(conn)
if len(data) != 0:
try:
if request and self.Capture:
formated_request = ""
str_req = str(data).split("\\r\\n")
for i in str_req:
# cprint.ok(i)
formated_request = formated_request + i + "\n"
moddeddata = self.ReciveModifedRequest(
formated_request)
if moddeddata == "drop":
conn.close()
return False
tunn.sendall(moddeddata)
self.resetReponse()
break
else:
tunn.sendall(data)
break
except Exception as e:
cprint.fatal(e)
else:
break
def request_to_host(self, request):
host, port = "", 443
str_req = str(request).split("\\r\\n")
for i in str_req:
if "Host:" in i:
host = i.split(" ")[1]
host_info = host.split(":")
if len(host_info) == 2 and host_info[1] == 443:
port = int(443)
elif len(host_info) == 1 and host_info[0] != "":
port = int(80)
host = host_info[0]
return host, port
def create_encrypted_socket(self, host, port):
tunn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
wrappedSocket = ssl.create_default_context().wrap_socket(tunn, server_hostname=host)
wrappedSocket.connect((host, port))
return wrappedSocket
def encrypt_socket(self, socket, ca, host):
certfile, keyfile = self.check_cert(ca, host)
try:
conn_s = ssl.wrap_socket(
socket, keyfile=keyfile, certfile=certfile, server_side=True)
conn_s.do_handshake()
return conn_s
except:
return None
def handle(self, conn, ca):
conn_s = ""
request = conn.recv(40960)
print(request)
host, port = self.request_to_host(request)
print(host, port)
if host == "":
return 0
if port == 443:
conn.sendall(b'HTTP/1.1 200 Connection Established\r\n\r\n')
wrappedSocket = self.create_encrypted_socket(host, port)
conn_s = self.encrypt_socket(conn, ca, host)
if conn_s == None:
return False
self.request_handler(conn_s, wrappedSocket, True)
self.request_handler(wrappedSocket, conn_s, False)
else:
self.HTTPPROXY.handle_request(conn, request, host)
def start_server(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('127.0.0.1', 8080))
sock.listen()
ca = self.check_ca_auth()
while True:
conn, addr = sock.accept()
# conn.settimeout(2)
cprint.info("connected ===> " + str(addr))
try:
self.handle(conn, ca)
except Exception as e:
conn.close()
print(e)
if __name__ == "__main__":
print("You can't run this module dirctly")