我試圖在python上發出ARP請求。我的代碼:在python上發出ARP請求
import socket
from struct import pack
from uuid import getnode as get_mac
def main():
dest_ip = [10, 7, 31, 99]
local_mac = [int(("%x" % get_mac())[i:i+2], 16) for i in range(0, 12, 2)]
local_ip = [int(x) for x in socket.gethostbyname(socket.gethostname()).split('.')]
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.SOCK_RAW)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.bind(('', 0))
ARP_FRAME = [
pack('!H', 0x0001), # HRD
pack('!H', 0x0800), # PRO
pack('!B', 0x06), # HLN
pack('!B', 0x04), # PLN
pack('!H', 0x0001), # OP
pack('!6B', *local_mac), # SHA
pack('!4B', *local_ip), # SPA
pack('!6B', *(0x00,)*6), # THA
pack('!4B', *dest_ip), # TPA
]
print(ARP_FRAME)
sock.sendto(b''.join(ARP_FRAME), ('255.255.255.255', 0))
sock.close()
if __name__ == "__main__":
main()
當我執行此代碼時,Wireshark不捕獲任何數據包。 我覺得在套接字問題。當我做socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.SOCK_RAW)
我得到AttributeError: module 'socket' has no attribute 'AF_PACKET'
。我做錯了什麼,以及如何解決它?
OS X 10.11.3
出於好奇,有什麼理由不使用scapy嗎? – Goodies
第二層幀是一個帶有廣播目的地MAC地址(FFFF.FFFF.FFFF)和ARP ethertype(0x0806)的標準以太網幀。三層報文是一個ARP請求。有關ARP數據包格式,請參閱http://www.tcpipguide.com/free/t_ARPMessageFormat.htm。 –
@Goodies這是「計算機網絡」課程的家庭作業。 – 0x1337