forked from OffensivePython/PyProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyProxy.py
154 lines (141 loc) · 4.12 KB
/
PyProxy.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
#!/usr/bin/env python
#=========================================================#
# [+] Title: Simple Asynchronous HTTP Proxy #
# [+] Script: pyproxy.py #
# [+] Website: http://www.pythonforpentesting.com #
# [+] Twitter: @OffensivePython #
#=========================================================#
import socket
import select
import sys
import re
server = socket.socket()
internal = [] # Internal connections (browser)
pipe = {} # dst_socket:src_socket
# CONSTANTS
MAXSIZE = 65535
# Unresolved/HTTPS domain
error_header = (
b"HTTP/1.1 404 Not Found\r\n"
b"Connection: Close\r\n\r\n"
)
# Usage
usage = (
"usage: pyproxy.py port\r\n"
"Port should be in range 0-65535\r\n"
"e.g: pyproxy.py 8080"
)
def recvall(sock):
data = b""
while True:
r, w, e = select.select([sock], [], [], 0.5)
if r:
chunk = sock.recv(MAXSIZE)
if chunk:
data+= chunk
else:
break
else:
break
return data
def accept_connection(sock):
connection, address = sock.accept()
internal.append(connection)
def get_request_info(request):
method = re.match(b"([A-Z]+?) ", request)
if method:
req = re.match(b"(.*?)\r\n", request).group(1).decode()
method = method.group(1).decode()
if method=="CONNECT":
address = re.search(b"CONNECT (.*?):(.*?) HTTP", request)
host, port = address.group(1).decode(), int(address.group(2).decode())
else:
host = re.search(b"Host: (.*?)\r\n", request).group(1).decode()
port = 80
try:
host = socket.gethostbyname(host)
except socket.gaierror:
host = "UNRESOLVED"
else:
method = None
host = None
port = None
return method, host, port, req
def connecto(host, port):
sock = socket.socket()
try:
sock.connect((host, port))
except socket.error:
sock.close()
sock = None
return sock
def forward_requests(socklist):
r, w, e = select.select(socklist, [], socklist)
for src in r:
if src==server:
accept_connection(src)
else:
request = recvall(src)
if request:
method, host, port, req = get_request_info(request)
if host=="UNRESOLVED" or method=="CONNECT":
src.send(error_header)
internal.remove(src)
src.close()
elif host:
print("[+]", req)
dst = connecto(host, port)
dst.send(request)
pipe[dst] = src
else:
internal.remove(src)
for dst in pipe:
if pipe[dst]==src:
del pipe[dst]
dst.close()
break
src.close()
def forward_responses(socklist):
if socklist:
r, w, e = select.select(socklist, [], [], 1)
for dst in r:
src = pipe[dst]
response = recvall(dst)
if response:
src.send(response)
internal.remove(src)
del pipe[dst]
src.close()
dst.close()
def cleanup():
for c in internal:
c.close()
for p in pipe:
p.close()
def main():
args = sys.argv
if len(args)==2:
try:
port = int(args[1])
if port>MAXSIZE:
raise ValueError("Port should be in range(65535)")
except ValueError as err:
print(err)
sys.exit()
else:
print(usage)
sys.exit()
print("[+] PyProxy Listening on port %d"%port)
server.bind(("", port))
server.listen(5)
internal.append(server)
try:
while True:
forward_requests(internal)
forward_responses(list(pipe.keys()))
except KeyboardInterrupt:
print("[+] Exiting")
cleanup()
sys.exit()
if __name__=="__main__":
main()