-
Notifications
You must be signed in to change notification settings - Fork 7
/
tcp-constructor.py
executable file
·91 lines (71 loc) · 2.16 KB
/
tcp-constructor.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
#!/usr/bin/env python2
import socket, struct, getopt, sys, os
opts, args = getopt.gnu_getopt(sys.argv, '', ['port=', 'seq=', 'addr=', 'next', 'reverse'])
class Tcp:
def __init__(self):
self.addr = "localhost"
self.port = "12345"
self.mss = 1460
self.wscale = 7
self.seq = 5000000
tcp = Tcp()
conn = []
reverse = False
for k, v in opts:
if k == "--addr":
tcp.addr = v
if k == "--port":
tcp.port = int(v)
if k == "--seq":
tcp.seq = int(v)
if k == "--next":
conn.append(tcp)
tcp = Tcp()
if k == "--reverse":
reverse = True
conn.append(tcp)
if reverse:
src = 1
dst = 0
else:
src = 0
dst = 1
TCP_REPAIR = 19
TCP_REPAIR_QUEUE = 20
TCP_QUEUE_SEQ = 21
TCP_REPAIR_OPTIONS = 22
TCPOPT_MSS = 2
TCPOPT_WINDOW = 3
TCPOPT_TIMESTAMP = 8
TCPOPT_SACK_PERM = 4
TCP_RECV_QUEUE = 1
TCP_SEND_QUEUE = 2
# create reply socket with TCP_REPAIR to put it into the correct state
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_TCP, TCP_REPAIR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_TCP, TCP_REPAIR_QUEUE, TCP_SEND_QUEUE)
seq = struct.pack('=L', int(conn[src].seq))
s.setsockopt(socket.SOL_TCP, TCP_QUEUE_SEQ, seq)
s.setsockopt(socket.SOL_TCP, TCP_REPAIR_QUEUE, TCP_RECV_QUEUE)
ack = struct.pack('=L', int(conn[dst].seq))
s.setsockopt(socket.SOL_TCP, TCP_QUEUE_SEQ, ack)
s.bind((conn[src].addr, conn[src].port))
s.connect((conn[dst].addr, conn[dst].port))
opt = ''
opt += struct.pack('=LHH', TCPOPT_WINDOW,
int(conn[src].wscale), int(conn[dst].wscale))
opt += struct.pack('=LL', TCPOPT_MSS, int(conn[src].mss))
#opt += struct.pack('=LL', TCPOPT_SACK_PERM, 0)
#opt += struct.pack('=LL', TCPOPT_TIMESTAMP, 0)
s.setsockopt(socket.SOL_TCP, TCP_REPAIR_OPTIONS, opt)
#if ts:
# ts = struct.pack('=L', int(ts))
# s.setsockopt(socket.SOL_TCP, TCP_TIMESTAMP, ts)
s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
sys.stdout.write("start")
sys.stdout.flush()
os.read(0, 5)
# we are now ready to send data
s.setsockopt(socket.SOL_TCP, TCP_REPAIR, 0)
os.execvp(args[1], args[1:])