I'm trying to make ARP request on python. My code:
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()
When I execute this code, Wireshark does not catch any packets.
I think problem in socket. When I do socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.SOCK_RAW)
I get AttributeError: module 'socket' has no attribute 'AF_PACKET'
. What am I doing wrong and how fix it?
OS X 10.11.3
255.255.255.255
? That makes no sense since this is not an IP packet. The frame is sent to the broadcast MAC address, and the ARP (not IP) packet contains the destination IP address to be resolved. – Endstoppedsendto
method. Is there another ways for sending? – Degrading