i'm programming noob following python video tutorial create packet sniffer , makes use of socket.af_packet
, system doesn't have this. i'm guessing it's because of different operating system. is there simple workaround this? here's code af_packet on first line of main
:
import socket import struct import textwrap def main(): conn = socket.socket(socket.af_packet, socket.sock_raw, socket.ntohs(3)) while true: raw_data, addr = conn.recvfrom(65536) # biggest buffer size dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data) print('\nethernet frame: ') print('destination: {}, source: {}, protocol: {}'.format(dest_mac, src_mac, eth_proto)) # unpack ethernet frame def ethernet_frame(data): # pass packets function 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:] #htons endian bit compatibility # return formatted mac address (ie aa:bb:cc:dd:ee:ff) def get_mac_addr(bytes_addr): bytes_str = map('{:02x}'.format, bytes_addr) # 2 decimal places return ':'.join(bytes_str).upper() # mac addr main()
i assume using windows
instead of af_packet
use af_inet
also instead of socket.ntohs(3)
use `socket.ipproto_ip