0

I am trying to read an ARP request with my python script which is on my computer that is running Ubuntu 14.04. Wireshark shows that I AM receiving the ARP requests but I am not able to read it with python. I am not sure if the problem is in my computer or not, but I think that somehow the problem is in my computer and not the script.

I am running this script with sudo python "..../name.py"

This is my python script so far:

import dpkt
import socket
import binascii
import time

def to_Readble(addr):
    s=list()
    addr=binascii.hexlify(addr)
    for i in xrange(12/2):
        s.append(addr[i*2:i*2+2])
    r=":".join(s)
    return r

def to_Sendable(r):
    s=r.split(":")
    for i in xrange(6):
        s[i]=binascii.unhexlify(s[i])
    addr=''.join(s)
    return addr

def buildARP(src_mac, src_ip, to_mac, to_ip):
    arp_p = dpkt.arp.ARP()
    arp_p.sha = to_Sendable(src_mac) #add configure
    arp_p.spa = socket.inet_aton(src_ip)
    arp_p.tha = to_Sendable(to_mac) #add configure
    arp_p.tpa = socket.inet_aton(to_ip)
    arp_p.op = dpkt.arp.ARP_OP_REPLY #reply

    packet = dpkt.ethernet.Ethernet()
    packet.src = to_Sendable(src_mac)
    packet.dst = to_Sendable(to_mac)
    packet.data = arp_p
    packet.type = dpkt.ethernet.ETH_TYPE_ARP
    return packet


raw_sock=socket.socket(socket.PF_PACKET, socket.SOCK_RAW,socket.htons(0x0003))#changed ver
raw_sock.bind(("eth0", dpkt.ethernet.ETH_TYPE_ARP))
#dns_sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#dns_sock.bind(('',53))
my_mac=to_Readble(raw_sock.getsockname()[-1])
print my_mac
#my_mac="e0:06:e6:d7:c6:c3"
dif="00:00:00:00:00:00"
dif1="ff:ff:ff:ff:ff:ff"
rout="192.168.122.1"
ip_macs={}
print "running"
while 1:
    for i in ip_macs.keys():#sending stuff
        raw_sock.send(str(buildARP(my_mac,rout,ip_macs[i],i)))
    try:
        # raw_sock.recv(1024) Blocks the script!!!!***!!!
        data = raw_sock.recv(1024)
        print "got arp"

    ... 

Someone has an Idea for why I am not getting the requests?

pah
  • 4,700
  • 6
  • 28
  • 37

1 Answers1

1

You need socket.listen() after the socket.bind().
Rather that repeat other answers, see this So Answer:
(Very) basic Python client socket example

Community
  • 1
  • 1
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60