1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| import binascii import socket import struct
def send_arp_request(source_ip, target_ip, interface): broadcast_bytes = b'\xff\xff\xff\xff\xff\xff' src_mac_bytes = binascii.unhexlify('00:50:56:c0:00:08'.replace(':', '')) ethernet_header = struct.pack("!6s6s2s", broadcast_bytes, src_mac_bytes, b'\x08\x06')
source_ip = socket.inet_aton(source_ip) target_mac_bytes = b'\x00\x00\x00\x00\x00\x00' target_ip = socket.inet_aton(target_ip)
arp_request = struct.pack('!HHBBH6s4s6s4s', 0x0001, 0x0800, 6, 4, 0x0001, src_mac_bytes, source_ip, target_mac_bytes, target_ip)
raw_socket = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.htons(0x0806))
raw_socket.bind((interface, 0))
raw_socket.send(ethernet_header + arp_request)
response = raw_socket.recv(2048)
_, _, _, _, _, _, _, _, sender_mac, sender_ip, target_mac, target_ip = struct.unpack('!6s6s2sHHBBH6s4s6s4s', response)
print(f"Reply -> IP: {socket.inet_ntoa(sender_ip)} MAC: {':'.join(format(x, '02x') for x in sender_mac)}")
raw_socket.close()
if __name__ == "__main__": src_ip = '172.16.66.1' dst_ip = '172.16.66.130' inter = 'vmnet8'
send_arp_request(src_ip, dst_ip, inter)
|