-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
111 lines (94 loc) · 4.35 KB
/
main.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
#importing libraries
import socket
from struct import *
import binascii
class Packet:
def __init__(self, src_ip, dest_ip, dest_port):
# all values need to be at least one byte long (-> we need to add up some values)
############
# IP segment
self.version = 0x4
self.ihl = 0x5
self.type_of_service = 0x0
self.total_length = 0x28
self.identification = 0xabcd
self.flags = 0x0
self.fragment_offset = 0x0
self.ttl = 0x40
self.protocol = 0x6
self.header_checksum = 0x0
self.src_ip = src_ip
self.dest_ip = dest_ip
self.src_addr = socket.inet_aton(src_ip)
self.dest_addr = socket.inet_aton(dest_ip)
self.v_ihl = (self.version << 4) + self.ihl
self.f_fo = (self.flags << 13) + self.fragment_offset
#############
# TCP segment
self.src_port = 0x3039
self.dest_port = dest_port
self.seq_no = 0x0
self.ack_no = 0x0
self.data_offset = 0x5
self.reserved = 0x0
self.ns, self.cwr, self.ece, self.urg, self.ack, self.psh, self.rst, self.syn, self.fin = 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0
self.window_size = 0x7110
self.checksum = 0x0
self.urg_pointer = 0x0
self.data_offset_res_flags = (self.data_offset << 12) + (self.reserved << 9) + (self.ns << 8) + (self.cwr << 7) + (self.ece << 6) + (self.urg << 5) + (self.ack << 4) + (self.psh << 3) + (self.rst << 2) + (self.syn << 1) + self.fin
########
# packet
self.tcp_header = b"01"
self.ip_header = b"01"
self.packet = b""
def calc_checksum(self, msg):
s = 0
for i in range(0, len(msg), 2):
w= ord(msg[i]) + (ord(msg[i+1]) << 8)
s = s + w
# s = 0x119cc
s = (s >> 16) + (s & 0xffff)
# s = 0x19cd
s = ~s & 0xffff
# s = 0xe632
return s
def generate_tmp_ip_header(self):
tmp_ip_header = pack("!BBHHHBBH4s4s", self.v_ihl, self.type_of_service, self.total_length,
self.identification, self.f_fo,
self.ttl, self.protocol, self.header_checksum,
self.src_addr,
self.dest_addr)
return tmp_ip_header
def generate_tmp_tcp_header(self):
tmp_tcp_header = pack("!HHLLHHHH", self.src_port, self.dest_port,
self.seq_no,
self.ack_no,
self.data_offset_res_flags, self.window_size,
self.checksum, self.urg_pointer)
return tmp_tcp_header
def generate_packet(self):
# IP header + checksum
final_ip_header = pack("!BBHHHBBH4s4s", self.v_ihl, self.type_of_service, self.total_length,
self.identification, self.f_fo,
self.ttl, self.protocol, self.calc_checksum(self.generate_tmp_ip_header()),
self.src_addr,
self.dest_addr)
# TCP header + checksum
tmp_tcp_header = self.generate_tmp_tcp_header()
pseudo_header = pack("!4s4sBBH", self.src_addr, self.dest_addr, self.checksum, self.protocol, len(tmp_tcp_header))
psh = pseudo_header + tmp_tcp_header
final_tcp_header = pack("!HHLLHHHH", self.src_port, self.dest_port,
self.seq_no,
self.ack_no,
self.data_offset_res_flags, self.window_size,
self.calc_checksum(psh), self.urg_pointer)
self.ip_header = final_ip_header
self.tcp_header = final_tcp_header
self.packet = final_ip_header + final_tcp_header
def send_packet(self):
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
s.sendto(self.packet, (self.dest_ip, 0))
data = s.recv(1024)
s.close()
return data