-
Notifications
You must be signed in to change notification settings - Fork 4
/
tcp.py
executable file
·436 lines (333 loc) · 11.7 KB
/
tcp.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import socket
import time
import sys
from Peach.Engine.engine import Engine
from Peach.Engine.common import PeachException
from Peach.publisher import Publisher
from Peach.publisher import Timeout
from Peach.publisher import PublisherSoftException
from Peach.Utilities.common import *
import Peach
def Debug(msg):
if Peach.Engine.engine.Engine.debug:
print(msg)
#class Timeout(SoftException):
# def __init__(self, msg):
# self.msg = msg
#
# def __str__(self):
# return self.msg
class Tcp(Publisher):
"""
A simple TCP client publisher.
"""
def __init__(self, host, port, timeout=0.25, throttle=0):
"""
@type host: string
@param host: Remote host
@type port: number
@param port: Remote port
@type timeout: number
@param timeout: How long to wait for reponse
@type throttle: number
@param throttle: How long to wait between connections
"""
Publisher.__init__(self)
self._host = host
try:
self._port = int(port)
except:
raise PeachException("The Tcp publisher parameter for port was not a valid number.")
try:
self._timeout = float(timeout)
except:
raise PeachException("The Tcp publisher parameter for timeout was not a valid number.")
try:
self._throttle = float(throttle)
except:
raise PeachException("The Tcp publisher parameter for throttle was not a valid number.")
self._socket = None
def start(self):
pass
def stop(self):
self.close()
def connect(self):
"""
Create connection.
"""
self.close()
if self._throttle > 0:
time.sleep(self._throttle)
# Try connecting many times
# before we crash.
for i in range(30):
try:
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket.connect((self._host, self._port))
exception = None
break
except:
self._socket = None
exception = sys.exc_info()
# Wait half sec and try again
time.sleep(1)
if self._socket is None:
value = ""
try:
value = str(exception[1])
except:
pass
raise PeachException("TCP onnection attempt failed: %s" % value)
self.buff = ""
self.pos = 0
def close(self):
"""
Close connection if open.
"""
try:
if self._socket is not None:
self._socket.close()
finally:
self._socket = None
self.buff = ""
self.pos = 0
def send(self, data):
"""
Send data via sendall.
@type data: string
@param data: Data to send
"""
if Peach.Engine.engine.Engine.debug:
print(">>>>>>>>>>>>>>>>")
print("tcp.Tcp.send():")
printHex(data)
try:
self._socket.sendall(data)
except:
if Peach.Engine.engine.Engine.debug:
print("Tcp: Sendall failed: " + str(sys.exc_info()[1]))
raise PublisherSoftException("sendall failed: " + str(sys.exc_info()[1]))
def _receiveBySize(self, size):
"""
This is now a buffered receiver.
@rtype: string
@return: received data.
"""
# Do we already a have it?
if size + self.pos < len(self.buff):
ret = self.buff[self.pos:self.pos + size]
self.pos += size
return ret
# Only ask for the diff of what we don't already have
diffSize = (self.pos + size) - len(self.buff)
try:
if Peach.Engine.engine.Engine.debug:
print("Asking for %d, need %d, have %d" % (size, diffSize, len(self.buff) - self.pos))
self._socket.settimeout(self._timeout)
ret = self._socket.recv(diffSize)
if not ret:
# Socket was closed
if Peach.Engine.engine.Engine.debug:
print("Socket is closed")
raise PublisherSoftException("Socket is closed")
if Peach.Engine.engine.Engine.debug:
print("<<<<<<<<<<<<<<<<<")
print("tcp.Tcp.receive():")
printHex(ret)
self.buff += ret
except socket.error as e:
if str(e).find('The socket operation could not complete without blocking') != -1:
if Peach.Engine.engine.Engine.debug:
print("timed out waiting for data")
raise Timeout(
"Timed out waiting for data [%d:%d:%d:%d]" % (len(self.buff), (size + self.pos), size, diffSize))
elif str(e).find('An existing connection was forcibly') != -1:
if Peach.Engine.engine.Engine.debug:
print("Socket was closed!")
raise PublisherSoftException("Socket is closed")
else:
if Peach.Engine.engine.Engine.debug:
print("recv failed: " + str(sys.exc_info()[1]))
raise PublisherSoftException("recv failed: " + str(sys.exc_info()[1]))
finally:
self._socket.settimeout(None)
ret = self.buff[self.pos:]
self.pos = len(self.buff)
return ret
def _receiveByAvailable(self):
"""
Receive as much as possible prior to timeout.
@rtype: string
@return: received data.
"""
self._socket.settimeout(self._timeout)
try:
ret = self._socket.recv(4096)
if not ret:
raise PublisherSoftException("Socket is closed")
if Peach.Engine.engine.Engine.debug:
print("<<<<<<<<<<<<<<<<<")
print("tcp.Tcp.receive():")
printHex(ret)
self.buff += ret
except socket.error as e:
if str(e).find('The socket operation could not complete without blocking') == -1:
pass
else:
raise PublisherSoftException("recv failed: " + str(sys.exc_info()[1]))
self._socket.settimeout(None)
ret = self.buff[self.pos:]
self.pos = len(self.buff)
return ret
def receive(self, size=None):
if size is None:
return self._receiveByAvailable()
else:
return self._receiveBySize(size)
class TcpListener(Tcp):
"""
A TCP Listener publisher. This publisher
supports the following state actions:
* start - Start listening
* stop - Stop listening
* accept - Accept a client connection
* close - Close a client connection
"""
def __init__(self, host, port, timeout=0.25):
Tcp.__init__(self, host, port, timeout)
self._listen = None
self._clientAddr = None
def start(self):
self.close()
if self._listen is None:
for i in range(3):
try:
self._listen = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._listen.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._listen.bind((self._host, self._port))
self._listen.listen(1)
exception = None
break
except:
self._listen = None
exception = sys.exc_info()
time.sleep(0.5)
if self._listen is None:
value = ""
try:
value = str(exception[1])
except:
pass
raise PeachException("TCP bind attempt failed: %s" % value)
self.buff = ""
self.pos = 0
def stop(self):
try:
if self._socket is not None:
self._socket.close()
except:
pass
finally:
self._socket = None
# Avoid TIME_WAIT state by not closing our listener
#try:
# if self._listen != None:
# self._listen.close()
#except:
# pass
#
#finally:
# self._listen = None
def accept(self):
self.buff = ""
self.pos = 0
conn, addr = self._listen.accept()
self._socket = conn
self._clientAddr = addr
def close(self):
try:
if self._socket is not None:
self._socket.close()
except:
pass
finally:
self._socket = None
def connect(self):
raise PeachException("Action 'connect' not supported")
try:
import win32gui, win32con, win32process
import sys, time, os, signal
class TcpListenerLaunchGui(TcpListener):
"""
Does TcpListener goodness and also can laun a program. After
some defined amount of time we will try and close the
GUI application by sending WM_CLOSE than kill it.
"""
def __init__(self, host, port, windowname, timeout=0.25):
TcpListener.__init__(self, host, port, timeout)
self._windowName = windowname
def stop(self):
self.closeApp(self._windowName)
TcpListener.stop(self)
def call(self, method, args):
"""
Launch program to consume file
@type method: string
@param method: Command to execute
@type args: array of objects
@param args: Arguments to pass
"""
realArgs = [method, "/c", method]
for a in args:
realArgs.append(a)
#print "Spawning %s" % method
os.spawnv(os.P_NOWAIT, os.path.join(os.getenv('SystemRoot'), 'system32', 'cmd.exe'), realArgs)
@staticmethod
def enumCallback(hwnd, windowName):
"""
Will get called by win32gui.EnumWindows, once for each
top level application window.
"""
try:
# Get window title
title = win32gui.GetWindowText(hwnd)
# Is this our guy?
if title.find(windowName) == -1:
return
(threadId, processId) = win32process.GetWindowThreadProcessId(hwnd)
# Send WM_CLOSE message
try:
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
win32gui.PostQuitMessage(hwnd)
except:
pass
# Give it upto 5 sec
for i in range(100):
if win32process.GetExitCodeProcess(processId) != win32con.STILL_ACTIVE:
# Process exited already
return
time.sleep(0.25)
try:
# Kill application
win32process.TerminateProcess(processId, 0)
except:
pass
except:
pass
def closeApp(self, title):
"""
Close Application by window title
"""
#print "CloseApp: %s" % title
win32gui.EnumWindows(TcpListenerLaunchGui.enumCallback, title)
except:
pass
class TcpProxyB(TcpListener):
def __init__(self, host, port):
TcpListener.__init__(self, host, port)
class TcpProxyA(Tcp):
def __init__(self, host, port):
Tcp.__init__(self, host, port)