-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsock.py
483 lines (366 loc) · 12.9 KB
/
sock.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import re
import ssl
import sys
import socket
import websocket
import telnetlib
from time import time
from socket import timeout as Timeout, error as SocketError
def Bytes(s):
if isinstance(s, str): return s.encode("ascii")
assert isinstance(s, bytes)
return s
def Str(s):
if isinstance(s, bytes): return s.decode("ascii")
assert isinstance(s, str)
return s
__all__ = "Sock Sock6 toSock SockU SockU6 toSockU Timeout SocketError".split()
DEFAULT_TIMEOUT = 30
PORT_REGEXP = re.compile(r"(:| |;|/|\|)+(?P<port>\d+)$")
'''
TODO:
- tests
- check Sock6/toSock6
- read_until(_re) accept list also
- think about losing socket data on EOFError
- quick fail mode? if exploit fails, read_until("$ ") will wait the whole timeout;
'''
def parse_addr(addr, port=None):
"""
If port is embedded in addr string, it should be delimited with one or more of:
" :;|/"
Examples:
parse_addr("localhost", 3123)
parse_addr(("localhost", 3123))
parse_addr("127.0.0.1:3123")
parse_addr("127.0.0.1: 3123")
parse_addr("127.0.0.1|3123")
parse_addr("127.0.0.1/3123")
parse_addr("127.0.0.1 3123")
parse_addr("example.com:3123")
parse_addr("example.com:| /3123")
"""
_host = None
_port = None
if port is not None:
_host = addr
_port = int(port)
elif isinstance(addr, tuple):
_host = addr[0]
_port = int(addr[1])
elif isinstance(addr, str):
if addr.startswith("ws://") or addr.startswith("wss://"):
return addr
match = PORT_REGEXP.search(addr.strip())
if match:
_host = addr[:match.start()]
_port = int(match.group("port"))
if _host is None or _port is None:
raise TypeError("Can't understand address: addr=%s, port=%s" % (addr, port))
if not isinstance(_host, str):
raise TypeError("Host should be string")
# strip IPv6 brackets
_host = _host.strip("[]")
return _host, _port
class AbstractSock(object):
"""
SomeSock("127.0.0.1", 3123, timeout=15)
- timeout should be given using explicit keyword
"""
SOCKET_FAMILY = NotImplemented
SOCKET_TYPE = NotImplemented
RECV_SIZE = 4096
def __init__(self, *addr, **timeout_dict):
self.addr = parse_addr(*addr)
# python2 does not allow (*args, timeout=None) :(
self.timeout = float(timeout_dict.pop("timeout", DEFAULT_TIMEOUT))
if timeout_dict:
raise TypeError("Only timeout should be given through keyword args")
self.buf = b""
self.eof = False
self._init_sock()
self._connect()
return
@classmethod
def from_socket(cls, sock, timeout=None):
self = object.__new__(cls)
self.addr = sock.getpeername()
self.sock = sock
assert self.SOCKET_TYPE == sock.type
if timeout is not None:
self.timeout = float(timeout)
self.sock.settimeout(self.timeout)
else:
self.timeout = self.sock.gettimeout()
self.buf = b""
self.eof = False
return self
def _init_sock(self):
self.sock = socket.socket(self.SOCKET_FAMILY, self.SOCKET_TYPE)
self.sock.settimeout(self.timeout)
def _connect(self):
return NotImplemented
def recv(self):
return NotImplemented
def send(self):
return NotImplemented
def send_line(self, line):
return self.send(Bytes(line) + b"\n")
def read_line(self, timeout=None):
return self.read_until(b"\n", timeout=timeout)
def read_one(self, timeout=None):
"""
Read something from socket
timeout = -1 - wait until something new in socket
timeout = 0 - return cached buffer + socket buffer immediately
timeout = N - wait N seconds until something new in socket, else raise TimeoutError
"""
self._fill_one(timeout)
if not self.buf and timeout != 0:
raise EOFError("Connection closed")
res = self.buf
self.buf = b""
return res
def read_all(self, timeout=None):
"""
Read everything from socket (the other side should close socket before timeout)
"""
self.read_cond(lambda x: x.eof, timeout)
res = self.buf
self.buf = b""
return res
def skip_until(self, s, timeout=None):
"""
Skip everything until first occurence of string @s, stop before occurence.
Return nothing.
"""
s = Bytes(s)
self.read_cond(lambda x: s in x.buf, timeout)
start = self.buf.find(s)
self.buf = self.buf[start:]
return
def skip_until_re(self, r, flags=0, timeout=None):
"""
Skip everything until first match of regexp @r, stop before match.
Return match.
"""
r = Bytes(r)
match = self.read_cond(
lambda x: re.search(r, x.buf, flags=flags), timeout)
self.buf = self.buf[match.start():]
return match if len(match.groups()) > 1 else match.group(len(match.groups()))
def read_until(self, s, timeout=None):
"""
Read everything until first occurence of string @s, stop after occurence.
Return everything before @s, and @s.
"""
s = Bytes(s)
self.read_cond(lambda x: s in x.buf, timeout)
end = self.buf.find(s) + len(s)
res = self.buf[:end]
self.buf = self.buf[end:]
return res
def read_until_re(self, r, flags=0, timeout=None):
"""
Read everything until first match of regexp @r, stop after match.
Return match.
Note: if you need the data before match, you can make group (.*?) for that:
r1 = r"(\d) coins"
r2 = r"(.*?)(\d coins)"
"""
r = Bytes(r)
match = self.read_cond(lambda x: re.search(r, x.buf, flags=flags), timeout)
self.buf = self.buf[match.end():]
return match if len(match.groups()) > 1 else match.group(len(match.groups()))
def read_nbytes(self, n, timeout=None):
self.read_cond(lambda x: len(x.buf) >= n, timeout)
self.buf, res = self.buf[n:], self.buf[:n]
return res
def read_cond(self, cond, timeout=None):
"""
Read bytes while @cond(self) is False. Return @cond return.
self.buf should be analyzed/cropped by caller, if needed
(this is rather low-level function, helper)
"""
time_start = time()
remaining = timeout
if timeout is None:
timeout = self.timeout
if self.eof and not self.buf:
raise EOFError("Connection closed")
res = cond(self)
while not res:
self._fill_one(remaining)
res = cond(self)
if res:
break
if self.eof:
raise EOFError("Connection closed")
if timeout == -1:
remaining = -1
elif timeout == 0:
raise Timeout("read_cond timeout")
else:
remaining = time_start + timeout - time()
if remaining <= 0:
raise Timeout("read_cond timeout")
return res
def _fill_one(self, timeout=None):
"""Read something from socket.
timeout = -1 - blocking until read
timeout = 0 - non-blocking
timeout = N - blocking until read or timeout
"""
if timeout is None:
timeout = self.timeout
if timeout == 0:
self.sock.setblocking(False)
try:
self.buf += self.recv(self.RECV_SIZE)
except SocketError:
# WHAT?
pass
return
if timeout == -1:
self.sock.settimeout(None) # blocking, infinity timeout
else:
self.sock.setblocking(True) # it's overriden by settimeout, but for clarity
self.sock.settimeout(timeout)
buf = self.recv(self.RECV_SIZE)
self.eof = (not buf)
self.buf += buf
return
@property
def socket(self):
return self.sock
@property
def fileno(self):
return self.sock.fileno()
def write(self, s):
return self.send(s)
def shut_wr(self):
self.sock.shutdown(socket.SHUT_WR)
def shut_rd(self):
self.sock.shutdown(socket.SHUT_RD)
def close(self):
return self.sock.close()
def __del__(self):
self.sock.close()
def interact_telnet(self):
sys.stdout.buffer.write(self.buf)
self.buf = b""
t = telnetlib.Telnet()
t.sock = self.sock
return t.interact()
def interact(self):
# copied from Telnetlib with minor fixes
import selectors
_TelnetSelector = selectors.SelectSelector
sys.stdout.buffer.write(self.buf)
self.buf = b""
with _TelnetSelector() as selector:
selector.register(self.sock, selectors.EVENT_READ)
selector.register(sys.stdin, selectors.EVENT_READ)
while True:
for key, events in selector.select():
if key.fileobj is self.sock:
try:
text = self.read_one()
except EOFError:
print('*** Connection closed by remote host ***')
return
if text:
sys.stdout.buffer.write(text)
sys.stdout.flush()
elif key.fileobj is sys.stdin:
line = sys.stdin.readline()
if not line:
return
self.send(line)
# pwnlib.tubes method name style
class AbstractPwnlibTubes(object):
def recvall(self, *args, **kwargs):
return self.read_all(*args, **kwargs)
def recvline(self, *args, **kwargs):
return self.read_line(*args, **kwargs)
def recvuntil(self, *args, **kwargs):
return self.read_until(*args, **kwargs)
def recvregex(self, *args, **kwargs):
return self.read_until_re(*args, **kwargs)
def sendline(self, *args, **kwargs):
return self.send_line(*args, **kwargs)
def readall(self, *args, **kwargs):
return self.recvall(*args, **kwargs)
def readline(self, *args, **kwargs):
return self.recvline(*args, **kwargs)
def readuntil(self, *args, **kwargs):
return self.recvuntil(*args, **kwargs)
def readregex(self, *args, **kwargs):
return self.recvregex(*args, **kwargs)
def interactive(self, *args, **kwargs):
return self.interact(*args, **kwargs)
class IPv4Mixin(object):
SOCKET_FAMILY = socket.AF_INET
class IPv6Mixin(object):
SOCKET_FAMILY = socket.AF_INET6
class TCPMixIn(object):
SOCKET_TYPE = socket.SOCK_STREAM
def _connect(self):
self.sock.connect(self.addr)
def recv(self, bufsize):
return self.sock.recv(bufsize)
def send(self, s):
return self.sock.sendall(Bytes(s))
class SSLMixIn(TCPMixIn):
def _connect(self):
TCPMixIn._connect(self)
self.sock = ssl.wrap_socket(self.sock)
class UDPMixIn(object):
SOCKET_TYPE = socket.SOCK_DGRAM
def _connect(self):
pass # udp doesn't need connect
def recv(self, bufsize):
while True:
data, addr = self.sock.recvfrom(bufsize)
if addr == self.addr:
return data
# TODO: warning about non-matching addr
def send(self, s):
return self.sock.sendto(s, self.addr)
class MyWebSocket(websocket.WebSocket):
def __init__(self, *args, **kwargs):
self.buf = b''
super(MyWebSocket, self).__init__(*args, **kwargs)
def setblocking(self, val):
pass
def recvbytes(self, bufsize):
self.buf += self.recv()
r = self.buf[:bufsize]
self.buf = self.buf[bufsize:]
return r
class WebSocketMixIn(object):
SOCKET_TYPE = socket.SOCK_STREAM
def _init_sock(self):
self.sock = websocket.create_connection(self.addr, timeout=self.timeout, class_=MyWebSocket)
def _connect(self):
pass
def recv(self, bufsize):
return self.sock.recvbytes(bufsize)
def send(self, s):
return self.sock.send_binary(Bytes(s))
class WebSock(WebSocketMixIn, AbstractSock, AbstractPwnlibTubes):
pass
class Sock(TCPMixIn, IPv4Mixin, AbstractSock, AbstractPwnlibTubes):
pass
class Sock6(TCPMixIn, IPv6Mixin, AbstractSock, AbstractPwnlibTubes):
pass
class SockU(UDPMixIn, IPv4Mixin, AbstractSock, AbstractPwnlibTubes):
pass
class SockU6(UDPMixIn, IPv6Mixin, AbstractSock, AbstractPwnlibTubes):
pass
class SSLSock(SSLMixIn, IPv4Mixin, AbstractSock, AbstractPwnlibTubes):
pass
class SSLSock6(SSLMixIn, IPv6Mixin, AbstractSock, AbstractPwnlibTubes):
pass
toSock = Sock.from_socket
toSockU = SockU.from_socket