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