2016-06-07 175 views
1

我是一個編程noob下面的蟒蛇視頻教程來創建一個數據包嗅探器,它使用socket.AF_PACKET,但我的系統沒有這個。我猜這是因爲不同的操作系統。 有沒有簡單的解決方法呢?這是我對main第一行代碼AF_PACKET:是否有替代適用於Mac OS X的socket.AF_PACKET?

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 into this 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 is endian bit compatibility 

# Return properly 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() 

回答

0

我假設你使用的是Windows

代替AF_PACKET使用AF_INET

而且不是socket.ntohs(3)使用`socket.IPPROTO_IP

+1

FWIW,他沒有使用Windows。標題和標籤都說OS X. – jweyrich

相關問題