-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_syn_sender.py
121 lines (106 loc) · 4.51 KB
/
tcp_syn_sender.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
from socket import inet_aton
from checksum import cs
from pkt_sender import sender
lines: list
with open('info.txt', 'r') as fd:
lines = fd.readlines()
interface = lines[4][:-1]
dest_mac = lines[6][:17] # destination MAC address
src_mac = lines[5][:17] # source MAC address
proto3 = '08 00' # layer 3 protocol number
ver_ihl = '45' # version, header length
tos = '00' # type-of-service
total_len = '00 28' # total length -> 40 bytes
id = '07 c3' # ID
flags = '40 00' # flags and fragment offset
ttl = '40' # time-to-live
proto4 = '06' # layaer 4 protocol num
src_ip = inet_aton(lines[2]).hex() # source IP address
dest_ip = inet_aton(lines[0]).hex() # destination IP address
src_port = '%04x' % int(lines[3]) # source port
dest_port = '%04x' % int(lines[1]) # destination port
seq_num = '17 49 30 d1' # sequence number
ack = '00 00 00 00' # acknowledgement number
tcp_hl = '50 02' # TCP header length and flags
win_size = '72 10' # window size
up = '00 00' # urgent pointer
def frame_calc(destination_MAC=dest_mac,
source_MAC=src_mac,
protocol=proto3):
# Ethernet Frame
return (destination_MAC + source_MAC +
protocol).replace(' ', '')
def datagram_calc(version_headerlength=ver_ihl,
TOS=tos,
total_length=total_len,
ID=id,
flags_fragmentation_offset=flags,
TTL=ttl,
protocol=proto4,
source_IP=src_ip,
destination_IP=dest_ip):
# IP Datagram
datagram_cs = (version_headerlength + TOS + total_length +
ID + flags_fragmentation_offset + TTL +
protocol + '00 00' + source_IP +
destination_IP).replace(' ', '')
datagram_cs = ' '.join(datagram_cs[i:i + 2]
for i in range(0, len(datagram_cs), 2))
checksum_calculated = cs(datagram_cs)
return(version_headerlength + TOS + total_length + ID +
flags_fragmentation_offset + TTL + protocol +
checksum_calculated + source_IP + destination_IP
).replace(' ', '')
def segment_calc(source_IP=src_ip,
destination_IP=dest_ip,
protocol=proto4,
source_port=src_port,
destination_port=dest_port,
sequence_number=seq_num,
acknowledgement_number=ack,
TCP_headerlength=tcp_hl,
window_size=win_size,
urgent_pointer=up):
# TCP Segments
pseudo_header = source_IP + destination_IP + '00' + protocol + '00 14'
segment_cs = (pseudo_header + source_port + destination_port +
sequence_number + acknowledgement_number +
TCP_headerlength + window_size + '00 00' +
urgent_pointer).replace(' ', '')
segment_cs = ' '.join(segment_cs[i:i + 2]
for i in range(0, len(segment_cs), 2))
checksum_calculated = cs(segment_cs)
return(source_port + destination_port + sequence_number +
acknowledgement_number + TCP_headerlength + window_size +
checksum_calculated + urgent_pointer).replace(' ', '')
def syn_sender(destination_MAC=dest_mac,
source_MAC=src_mac,
protocol3=proto3,
version_headerlength=ver_ihl,
TOS=tos,
total_length=total_len,
ID=id,
flags_fragmentation_offset=flags,
TTL=ttl,
protocol4=proto4,
source_IP=src_ip,
destination_IP=dest_ip,
source_port=src_port,
destination_port=dest_port,
sequence_number=seq_num,
acknowledgement_number=ack,
TCP_headerlength=tcp_hl,
window_size=win_size,
urgent_pointer=up):
return sender(frame_calc(destination_MAC, source_MAC, protocol3) +
datagram_calc(version_headerlength, TOS, total_length,
ID, flags_fragmentation_offset, TTL, protocol4,
source_IP, destination_IP) +
segment_calc(source_IP, destination_IP, protocol4,
source_port, destination_port, sequence_number,
acknowledgement_number, TCP_headerlength,
window_size, urgent_pointer),
interface) == 54
if __name__ == '__main__':
if syn_sender():
print(f'Sent TCP SYN packet to {lines[0][:-1]} on {interface}')