Alex | 0989ecf | 2022-03-29 13:43:21 -0500 | [diff] [blame] | 1 | # Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com) |
| 2 | # Copyright 2019-2022 Mirantis, Inc. |
Alex | e864364 | 2021-08-23 14:08:46 -0500 | [diff] [blame] | 3 | """ |
| 4 | This is a WIP script. |
| 5 | Work is halted, |
| 6 | due to incompatibility between Mac OS X and Linux on how to handle network |
| 7 | """ |
Alex | 9e4bfaf | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 8 | import binascii |
| 9 | import socket |
| 10 | import struct |
Alex Savatieiev | 73aa99a | 2019-03-06 10:02:43 -0600 | [diff] [blame] | 11 | |
Alex | 9e4bfaf | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 12 | |
| 13 | class 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 | |
| 106 | def 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 | |
| 112 | def get_host(q): |
| 113 | try: |
| 114 | k = socket.gethostbyaddr(q) |
| 115 | except Exception: |
| 116 | k = 'Unknown' |
| 117 | return k |
| 118 | |
| 119 | |
| 120 | s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800)) |
| 121 | u = unpack() |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 122 | count = 32 |
Alex | 9e4bfaf | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 123 | while count > 0: |
| 124 | count -= 1 |
| 125 | # Capture packets from network |
| 126 | pkt = s.recvfrom(65565) |
| 127 | |
Alex | e864364 | 2021-08-23 14:08:46 -0500 | [diff] [blame] | 128 | print("\n\n=== [+] ------------ Ethernet Header----- [+]") |
Alex | 9e4bfaf | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 129 | |
| 130 | # print data on terminal |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 131 | for i in u.eth_header(pkt[0][0:14]).items(): |
Alex | 9e4bfaf | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 132 | a, b = i |
Alex | e864364 | 2021-08-23 14:08:46 -0500 | [diff] [blame] | 133 | print("{} : {} | ".format(a, b)) |
| 134 | print("\n=== [+] ------------ IP Header ------------[+]") |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 135 | for i in u.ip_header(pkt[0][14:34]).items(): |
Alex | 9e4bfaf | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 136 | a, b = i |
Alex | e864364 | 2021-08-23 14:08:46 -0500 | [diff] [blame] | 137 | print("{} : {} | ".format(a, b)) |
| 138 | print("\n== [+] ------------ Tcp Header ----------- [+]") |
| 139 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 140 | for i in u.tcp_header(pkt[0][34:54]).items(): |
Alex | 9e4bfaf | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 141 | a, b = i |
Alex | e864364 | 2021-08-23 14:08:46 -0500 | [diff] [blame] | 142 | print("{} : {} | ".format(a, b)) |
| 143 | print("\n===== Data ====") |
| 144 | print(pkt[0][54:]) |
| 145 | print("\n=======") |
| 146 | print(pkt[1:]) |