- Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPyProxy.py
More file actions
Latest commit
154 lines (141 loc) · 4.12 KB
/
Copy pathPyProxy.py
File metadata and controls
154 lines (141 loc) · 4.12 KB
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 #
#=========================================================#
importsocket
importselect
importsys
importre
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"
)
defrecvall(sock):
data=b""
whileTrue:
r, w, e=select.select([sock], [], [], 0.5)
ifr:
chunk=sock.recv(MAXSIZE)
ifchunk:
data+=chunk
else:
break
else:
break
returndata
defaccept_connection(sock):
connection, address=sock.accept()
internal.append(connection)
defget_request_info(request):
method=re.match(b"([A-Z]+?) ", request)
ifmethod:
req=re.match(b"(.*?)\r\n", request).group(1).decode()
method=method.group(1).decode()
ifmethod=="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)
exceptsocket.gaierror:
host="UNRESOLVED"
else:
method=None
host=None
port=None
returnmethod, host, port, req
defconnecto(host, port):
sock=socket.socket()
try:
sock.connect((host, port))
exceptsocket.error:
sock.close()
sock=None
returnsock
defforward_requests(socklist):
r, w, e=select.select(socklist, [], socklist)
forsrcinr:
ifsrc==server:
accept_connection(src)
else:
request=recvall(src)
ifrequest:
method, host, port, req=get_request_info(request)
ifhost=="UNRESOLVED"ormethod=="CONNECT":
src.send(error_header)
internal.remove(src)
src.close()
elifhost:
print("[+]", req)
dst=connecto(host, port)
dst.send(request)
pipe[dst] =src
else:
internal.remove(src)
fordstinpipe:
ifpipe[dst]==src:
delpipe[dst]
dst.close()
break
src.close()
defforward_responses(socklist):
ifsocklist:
r, w, e=select.select(socklist, [], [], 1)
fordstinr:
src=pipe[dst]
response=recvall(dst)
ifresponse:
src.send(response)
internal.remove(src)
delpipe[dst]
src.close()
dst.close()
defcleanup():
forcininternal:
c.close()
forpinpipe:
p.close()
defmain():
args=sys.argv
iflen(args)==2:
try:
port=int(args[1])
ifport>MAXSIZE:
raiseValueError("Port should be in range(65535)")
exceptValueErroraserr:
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:
whileTrue:
forward_requests(internal)
forward_responses(list(pipe.keys()))
exceptKeyboardInterrupt:
print("[+] Exiting")
cleanup()
sys.exit()
if__name__=="__main__":
main()