-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproxy.py
executable file
·296 lines (236 loc) · 8.58 KB
/
proxy.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
294
295
296
#!/usr/bin/env python3
import bitarray
import email
import email.policy
import os
import socket
import struct
import sys
import threading
maxRecvSize = 4096
browserEncScheme = 'utf-8'
proxyEncScheme = 'utf-8'
policy = email.policy.compat32.clone(linesep='\r\n')
otherIP = None
otherPort = None
serverMode = False
'''
Logic for the browser-side proxy
'''
def processBrowser(conn, client):
# get message and convert it to bit representation
message = input("Enter message to send: ")
bits = bitarray.bitarray()
bits.frombytes(message.encode(proxyEncScheme))
try:
# open connection to other proxy
sOther = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sOther.connect((otherIP, otherPort))
# loop until entire covert message is sent
messageSent = False
while not messageSent:
# receive request from browser
request = conn.recv(maxRecvSize).decode(browserEncScheme)
# insert covert message, determine if message incomplete
modifiedReq, messageSent = modifyCase(request, bits)
if not messageSent:
print(str(bits.length()) + " bits remaining;" +
" please send additional request.")
# send modified request to other proxy
sOther.send(modifiedReq.encode(proxyEncScheme))
responseBits = bitarray.bitarray()
eofFound = False
# loop until entire covert message received
while not eofFound:
# receive response from other proxy
response = sOther.recv(maxRecvSize)
if (len(response) == 0):
break
top, crlf, body = response.partition(b'\x0D\x0A\x0D\x0A')
top = top.decode(proxyEncScheme)
# determine if message incomplete
eofFound = interpretCase(top, responseBits)
# extract covert message
extractMessage(responseBits)
# forward the message to the browser
response = top.encode(browserEncScheme) + crlf + body
conn.send(response)
except socket.error as err:
print("Error connecting to other proxy: " + str(err))
finally:
if sOther:
sOther.close()
if conn:
conn.close()
'''
Logic for the server-side proxy
'''
def processServer(conn, client):
# set TCP_NODELAY to avoid combining sent packets
conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
eofFound = False
bits = bitarray.bitarray()
while not eofFound:
# receive request with covert message
modifiedReq = conn.recv(maxRecvSize).decode(proxyEncScheme)
if (len(modifiedReq) == 0):
break
# extract the covert message
eofFound = interpretCase(modifiedReq, bits)
try:
# determine intended web server
webSrv, webPort = determineWebSrv(modifiedReq)
sWeb = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sWeb.connect((webSrv, webPort))
# forward request to web server
sWeb.send(modifiedReq.encode(browserEncScheme))
# receive the response
response = sWeb.recv(maxRecvSize)
headers, crlf, body = response.partition(b'\x0D\x0A\x0D\x0A')
headers = headers.decode(browserEncScheme)
if not eofFound:
# send automated blank message to other proxy
responseLine, headers = extractHeaders(headers)
responseLine += ' '
newResponse = responseLine + '\r\n' + headers.as_string()
modResp = newResponse.encode(proxyEncScheme) + body
conn.send(modResp)
except (KeyError, socket.error) as err:
print(str(err))
sWeb.close()
conn.close()
extractMessage(bits)
# get message and convert it to bit representation
message = input("Enter message to send: ")
sendBits = bitarray.bitarray()
sendBits.frombytes(message.encode(proxyEncScheme))
# use last response to send entire covert message
messageSent = False
while not messageSent:
# insert covert message, determine if more requests needed
modHeaders, messageSent = modifyCase(headers, sendBits)
# send modified response to other proxy
modResp = modHeaders.encode(proxyEncScheme) + body
conn.send(modResp)
sWeb.close()
conn.close()
def interpretCase(modifiedReq, bits):
requestLine, headers = extractHeaders(modifiedReq)
tuples = headers.items()
# check for user-entered blank message
if requestLine.endswith(' '):
print("Received message:")
return True
# check for automated blank message
elif requestLine.endswith(' '):
return True
eofFound = False
for header, value in tuples:
chars = list(header)
for char in chars:
if char.islower():
bits.append(False)
elif char.isupper():
bits.append(True)
if value.endswith(' '):
eofFound = True
break
return eofFound
def modifyCase(request, bits):
# get header-value tuples
requestLine, headers = extractHeaders(request)
tuples = headers.items()
# check for user-entered blank message
if (bits.length() == 0):
requestLine += ' '
newRequest = requestLine + '\r\n' + headers.as_string()
return newRequest, True
messageSent = False
# modify the case of each header
for i in range(len(tuples)):
header, value = tuples[i]
chars = list(header)
if (bits.length() != 0):
for j in range(len(chars)):
if chars[j].isalpha():
try:
bit = bits.pop()
except IndexError:
chars[j] = chars[j].lower()
continue
# bit == 1
if bit:
chars[j] = chars[j].upper()
# bit == 0
else:
chars[j] = chars[j].lower()
# append EOF indicator
if (bits.length() == 0):
messageSent = True
value += ' '
del headers[header]
newHeader = ''.join(chars)
headers[newHeader] = value
# rebuild the request
newHeaders = headers.as_string()
newRequest = requestLine + '\r\n' + newHeaders
return newRequest, messageSent
def determineWebSrv(request):
requestLine, headers = extractHeaders(request)
for header, value in headers.items():
if (header.lower() == 'host'):
hostAndPort = value.split(':')
if (len(hostAndPort) == 2):
return socket.gethostbyname(hostAndPort[0]), hostAndPort[1]
else:
return socket.gethostbyname(value), 80
raise KeyError('Cannot determine intended host')
def extractHeaders(request):
requestLine, headers = request.split('\r\n', 1)
headers = email.message_from_string(headers, policy=policy)
return requestLine, headers
def extractMessage(bits):
for i in range(bits.length() % 8):
bits.pop()
if (bits.length() > 0):
bits.bytereverse()
recvMsg = bits.tobytes().decode(proxyEncScheme)[::-1]
print("Received message: " + recvMsg)
def main():
global serverMode, otherIP, otherPort
# check argument length
if (len(sys.argv) < 5):
print("Usage: " + sys.argv[0] + " <client/server mode> <port>" +
" <other proxy IP> <other proxy port>")
sys.exit(1)
# process arguments
role = sys.argv[1]
if (role.lower() == "server"):
serverMode = True
listPort = int(sys.argv[2])
otherIP = sys.argv[3]
otherPort = int(sys.argv[4])
# open listening socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', listPort))
s.listen(1)
except socket.err as err:
print("Error opening socket: " + err)
sys.exit(1)
# handle incoming connections
while True:
try:
conn, client = s.accept()
if serverMode:
t = threading.Thread(target=processServer, args=(conn, client))
else:
t = threading.Thread(target=processBrowser, args=(conn, client))
t.start()
except KeyboardInterrupt:
if s:
s.close()
os._exit(1)
if __name__ == '__main__':
main()