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