#! /usr/local/bin/python3.5
import socket
import struct
import textwrap
TAB_1 = '\t - '
TAB_2 = '\t\t - '
TAB_3 = '\t\t\t - '
TAB_4 = '\t\t\t\t - '
DATA_TAB_1 = '\t '
DATA_TAB_2 = '\t\t '
DATA_TAB_3 = '\t\t\t '
DATA_TAB_4 = '\t\t\t\t '
def main():
conn = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
while True:
raw_data, addr = conn.recvfrom(65536)
dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)
#######################################################################################
# Print only ICMP ping packets
#######################################################################################
if eth_proto == 8:
print('\n Ethernet Frame: ')
print(TAB_1 + 'Destination: {}, Source: {}, Protocol: {}'.format(dest_mac, src_mac, eth_proto))
(version, header_length, ttl, proto, src, target, data) = ipv4_Packet(data)
print(TAB_1 + "IPV4 Packet:")
print(TAB_2 + 'Version: {}, Header Length: {}, TTL: {}'.format(version, header_length, ttl))
print(TAB_3 + 'protocol: {}, Source: {}, Target: {}'.format(proto, src, target))
# ICMP
if proto == 1:
icmp_type, code, checksum, data = icmp_packet(data)
print(TAB_1 + 'ICMP Packet:')
print(TAB_2 + 'Type: {}, Code: {}, Checksum: {},'.format(icmp_type, code, checksum))
print(TAB_2 + 'ICMP Data:')
print(format_output_line(DATA_TAB_3, data))
# Unpack Ethernet Frame
def ethernet_frame(data):
dest_mac, src_mac, proto = struct.unpack('! 6s 6s H', data[:14])
return get_mac_addr(dest_mac), get_mac_addr(src_mac), socket.htons(proto), data[14:]
# Format MAC Address
def get_mac_addr(bytes_addr):
bytes_str = map('{:02x}'.format, bytes_addr)
mac_addr = ':'.join(bytes_str).upper()
return mac_addr
# Unpack IPv4 Packets Recieved
def ipv4_Packet(data):
version_header_len = data[0]
version = version_header_len >> 4
header_len = (version_header_len & 15) * 4
ttl, proto, src, target = struct.unpack('! 8x B B 2x 4s 4s', data[:20])
return version, header_len, ttl, proto, ipv4(src), ipv4(target), data[header_len:]
# Returns Formatted IP Address
def ipv4(addr):
return '.'.join(map(str, addr))
# Unpacks for any ICMP Packet
def icmp_packet(data):
icmp_type, code, checksum = struct.unpack('! B B H', data[:4])
return icmp_type, code, checksum, data[4:]
# Formats the output line
def format_output_line(prefix, string, size=80):
size -= len(prefix)
if isinstance(string, bytes):
string = ''.join(r'\x{:02x}'.format(byte) for byte in string)
if size % 2:
size-= 1
return '\n'.join([prefix + line for line in textwrap.wrap(string, size)])
main()
import socket
import struct
import textwrap
TAB_1 = '\t - '
TAB_2 = '\t\t - '
TAB_3 = '\t\t\t - '
TAB_4 = '\t\t\t\t - '
DATA_TAB_1 = '\t '
DATA_TAB_2 = '\t\t '
DATA_TAB_3 = '\t\t\t '
DATA_TAB_4 = '\t\t\t\t '
def main():
conn = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
while True:
raw_data, addr = conn.recvfrom(65536)
dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)
#######################################################################################
# Print only ICMP ping packets
#######################################################################################
if eth_proto == 8:
print('\n Ethernet Frame: ')
print(TAB_1 + 'Destination: {}, Source: {}, Protocol: {}'.format(dest_mac, src_mac, eth_proto))
(version, header_length, ttl, proto, src, target, data) = ipv4_Packet(data)
print(TAB_1 + "IPV4 Packet:")
print(TAB_2 + 'Version: {}, Header Length: {}, TTL: {}'.format(version, header_length, ttl))
print(TAB_3 + 'protocol: {}, Source: {}, Target: {}'.format(proto, src, target))
# ICMP
if proto == 1:
icmp_type, code, checksum, data = icmp_packet(data)
print(TAB_1 + 'ICMP Packet:')
print(TAB_2 + 'Type: {}, Code: {}, Checksum: {},'.format(icmp_type, code, checksum))
print(TAB_2 + 'ICMP Data:')
print(format_output_line(DATA_TAB_3, data))
# Unpack Ethernet Frame
def ethernet_frame(data):
dest_mac, src_mac, proto = struct.unpack('! 6s 6s H', data[:14])
return get_mac_addr(dest_mac), get_mac_addr(src_mac), socket.htons(proto), data[14:]
# Format MAC Address
def get_mac_addr(bytes_addr):
bytes_str = map('{:02x}'.format, bytes_addr)
mac_addr = ':'.join(bytes_str).upper()
return mac_addr
# Unpack IPv4 Packets Recieved
def ipv4_Packet(data):
version_header_len = data[0]
version = version_header_len >> 4
header_len = (version_header_len & 15) * 4
ttl, proto, src, target = struct.unpack('! 8x B B 2x 4s 4s', data[:20])
return version, header_len, ttl, proto, ipv4(src), ipv4(target), data[header_len:]
# Returns Formatted IP Address
def ipv4(addr):
return '.'.join(map(str, addr))
# Unpacks for any ICMP Packet
def icmp_packet(data):
icmp_type, code, checksum = struct.unpack('! B B H', data[:4])
return icmp_type, code, checksum, data[4:]
# Formats the output line
def format_output_line(prefix, string, size=80):
size -= len(prefix)
if isinstance(string, bytes):
string = ''.join(r'\x{:02x}'.format(byte) for byte in string)
if size % 2:
size-= 1
return '\n'.join([prefix + line for line in textwrap.wrap(string, size)])
main()