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
| import socket import struct
def parse_ip_packet(data): ip_header = struct.unpack("!BBHHHBBH4s4s", data[:20]) version = ip_header[0] >> 4 ihl = ip_header[0] & 0xF ttl = ip_header[5] protocol = ip_header[6] src_ip = socket.inet_ntoa(ip_header[8]) dest_ip = socket.inet_ntoa(ip_header[9])
return { "Version": version, "IHL": ihl, "TTL": ttl, "Protocol": protocol, "Source IP": src_ip, "Destination IP": dest_ip }
def main(): ip_packet_data = b'\x45\x00\x00\x28\x67\x12\x40\x00\x40\x01\x17\x2c\xc0\xa8\x00\x01\xc0\xa8\x00\x02'
parsed_packet = parse_ip_packet(ip_packet_data) print(parsed_packet)
def format_bytes_2_bin(): binary_data = b'\x45\x00\x00\x28\x67\x12\x40\x00\x40\x01\x17\x2c\xc0\xa8\x00\x01\xc0\xa8\x00\x02'
binary_string = ''.join(format(byte, '08b') for byte in binary_data) print(binary_string)
for b in binary_data: print(b, format(b, '08b'))
|