blob: 96e49a0252a4d811f9eaa070e3cd79d6a10f652b [file] [log] [blame]
Alex0989ecf2022-03-29 13:43:21 -05001# Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com)
2# Copyright 2019-2022 Mirantis, Inc.
Alexe8643642021-08-23 14:08:46 -05003"""
4This is a WIP script.
5Work is halted,
6due to incompatibility between Mac OS X and Linux on how to handle network
7"""
Alex9e4bfaf2019-06-11 15:21:59 -05008import binascii
9import socket
10import struct
Alex Savatieiev73aa99a2019-03-06 10:02:43 -060011
Alex9e4bfaf2019-06-11 15:21:59 -050012
13class unpack:
14 def __init__(self):
15 self.data = None
16
17 # Ethernet Header
18 def eth_header(self, data):
19 storeobj = data
20 storeobj = struct.unpack("!6s6sH", storeobj)
21 destination_mac = binascii.hexlify(storeobj[0])
22 source_mac = binascii.hexlify(storeobj[1])
23 eth_protocol = storeobj[2]
24 data = {"Destination Mac": destination_mac,
25 "Source Mac": source_mac,
26 "Protocol": eth_protocol}
27 return data
28
29 # ICMP HEADER Extraction
30 def icmp_header(self, data):
31 icmph = struct.unpack('!BBH', data)
32 icmp_type = icmph[0]
33 code = icmph[1]
34 checksum = icmph[2]
35 data = {'ICMP Type': icmp_type,
36 "Code": code,
37 "CheckSum": checksum}
38 return data
39
40 # UDP Header Extraction
41 def udp_header(self, data):
42 storeobj = struct.unpack('!HHHH', data)
43 source_port = storeobj[0]
44 dest_port = storeobj[1]
45 length = storeobj[2]
46 checksum = storeobj[3]
47 data = {"Source Port": source_port,
48 "Destination Port": dest_port,
49 "Length": length,
50 "CheckSum": checksum}
51 return data
52
53 # IP Header Extraction
54 def ip_header(self, data):
55 storeobj = struct.unpack("!BBHHHBBH4s4s", data)
56 _version = storeobj[0]
57 _tos = storeobj[1]
58 _total_length = storeobj[2]
59 _identification = storeobj[3]
60 _fragment_Offset = storeobj[4]
61 _ttl = storeobj[5]
62 _protocol = storeobj[6]
63 _header_checksum = storeobj[7]
64 _source_address = socket.inet_ntoa(storeobj[8])
65 _destination_address = socket.inet_ntoa(storeobj[9])
66
67 data = {'Version': _version,
68 "Tos": _tos,
69 "Total Length": _total_length,
70 "Identification": _identification,
71 "Fragment": _fragment_Offset,
72 "TTL": _ttl,
73 "Protocol": _protocol,
74 "Header CheckSum": _header_checksum,
75 "Source Address": _source_address,
76 "Destination Address": _destination_address}
77 return data
78
79 # Tcp Header Extraction
80 def tcp_header(self, data):
81 storeobj = struct.unpack('!HHLLBBHHH', data)
82 _source_port = storeobj[0]
83 _destination_port = storeobj[1]
84 _sequence_number = storeobj[2]
85 _acknowledge_number = storeobj[3]
86 _offset_reserved = storeobj[4]
87 _tcp_flag = storeobj[5]
88 _window = storeobj[6]
89 _checksum = storeobj[7]
90 _urgent_pointer = storeobj[8]
91 data = {"Source Port": _source_port,
92 "Destination Port": _destination_port,
93 "Sequence Number": _sequence_number,
94 "Acknowledge Number": _acknowledge_number,
95 "Offset & Reserved": _offset_reserved,
96 "Tcp Flag": _tcp_flag,
97 "Window": _window,
98 "CheckSum": _checksum,
99 "Urgent Pointer": _urgent_pointer
100 }
101 return data
102
103# Mac Address Formating
104
105
106def mac_formater(a):
107 b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]), ord(
108 a[1]), ord(a[2]), ord(a[3]), ord(a[4]), ord(a[5]))
109 return b
110
111
112def get_host(q):
113 try:
114 k = socket.gethostbyaddr(q)
115 except Exception:
116 k = 'Unknown'
117 return k
118
119
120s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))
121u = unpack()
Alex836fac82019-08-22 13:36:16 -0500122count = 32
Alex9e4bfaf2019-06-11 15:21:59 -0500123while count > 0:
124 count -= 1
125 # Capture packets from network
126 pkt = s.recvfrom(65565)
127
Alexe8643642021-08-23 14:08:46 -0500128 print("\n\n=== [+] ------------ Ethernet Header----- [+]")
Alex9e4bfaf2019-06-11 15:21:59 -0500129
130 # print data on terminal
Alex3bc95f62020-03-05 17:00:04 -0600131 for i in u.eth_header(pkt[0][0:14]).items():
Alex9e4bfaf2019-06-11 15:21:59 -0500132 a, b = i
Alexe8643642021-08-23 14:08:46 -0500133 print("{} : {} | ".format(a, b))
134 print("\n=== [+] ------------ IP Header ------------[+]")
Alex3bc95f62020-03-05 17:00:04 -0600135 for i in u.ip_header(pkt[0][14:34]).items():
Alex9e4bfaf2019-06-11 15:21:59 -0500136 a, b = i
Alexe8643642021-08-23 14:08:46 -0500137 print("{} : {} | ".format(a, b))
138 print("\n== [+] ------------ Tcp Header ----------- [+]")
139
Alex3bc95f62020-03-05 17:00:04 -0600140 for i in u.tcp_header(pkt[0][34:54]).items():
Alex9e4bfaf2019-06-11 15:21:59 -0500141 a, b = i
Alexe8643642021-08-23 14:08:46 -0500142 print("{} : {} | ".format(a, b))
143 print("\n===== Data ====")
144 print(pkt[0][54:])
145 print("\n=======")
146 print(pkt[1:])