Python network sniffer

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Python network sniffer

Post by bourneassassin on Mon Mar 29, 2010 2:12 am
([msg=37424]see Python network sniffer[/msg])

Hey,

I'm doing research this semester, and was given a Python network sniffer to modify to suit our new needs. After getting the sniffer, I tried to run it to see how it works as-is, but it seems it doesn't want to work at all...

The program sets up listening on port 80 by default, on the local machine, to log all tcp traffic.

if __name__=='__main__':
if len(sys.argv)>1:
name=sys.argv[1]
if len(sys.argv)>2:
PORT=int(sys.argv[2])
global f
f = open(name, "a")

devs = pcap.findalldevs()
i=0
for eth in devs:
print " %d - %s" %(i,devs[i][0])
i+=1
sel=input(" Select interface: ")
dev=devs[sel][0]
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('',PORT))
s.listen(1)
print "Waiting for connection..."
conn,addr=s.accept()
print "Client succesfully connected!\n"

p = pcap.pcapObject()
net, mask = pcap.lookupnet(dev)
p.open_live(dev, 1600, 0, 100)
p.setfilter('tcp dst port 80',0,0)
p.setnonblock(1)
print "Listening on %s: \n" % (dev)
try:
while 1:
p.dispatch(1, print_packet)

# the loop method is another way of doing things
#p.loop(1, print_packet)

# as is the next() method
# p.next() returns a (pktlen, data, timestamp) tuple
# apply(print_packet,p.next())
except KeyboardInterrupt:
#print '%s' % sys.exc_type
print '%d packets received, %d packets dropped, %d packets dropped by interface' % p.stats()
print 'quit'
conn.send('quit\n')
conn.close()
f.close()

The issue I have is at conn,addr=s.accept(). The program just hangs no matter how many firefox browsers I open or how many links I click on. I am sudo'd in and selected my device to listen on. I've tried both wired (eth0) and wireless (wlan0) and both do not work. The only time I can get it to do anything is by commenting that line out (at which point it complains that conn does not exist, of course). The program is just suppose to listen on the local machine for all traffic coming in through port 80 (and I'm suppose to modify it once I get it working...)

Any ideas why? I'm using an Ubuntu machine.
bourneassassin
New User
New User
 
Posts: 3
Joined: Mon Mar 08, 2010 4:49 pm
Blog: View Blog (0)


Re: Python network sniffer

Post by Arameus on Mon Mar 29, 2010 10:09 am
([msg=37434]see Re: Python network sniffer[/msg])

indent that. I am not re-indenting it for you in IDLE, I will run it and help but only after you indent it properly so it will work + be readable.
Arameus
New User
New User
 
Posts: 36
Joined: Mon Feb 01, 2010 6:53 pm
Location: Ballston Spa, NY
Blog: View Blog (0)


Re: Python network sniffer

Post by the0nlyb0ss on Sun Sep 19, 2010 10:16 pm
([msg=46152]see Re: Python network sniffer[/msg])

Arameus wrote:indent that. I am not re-indenting it for you in IDLE, I will run it and help but only after you indent it properly so it will work + be readable.

Amen to that. Just press space four times / indent dude, it's not that hard.
"Knowledge is knowing that a tomato is a fruit, but Wisdom is knowing not to put it in a fruit salad."
User avatar
the0nlyb0ss
Experienced User
Experienced User
 
Posts: 54
Joined: Thu Sep 02, 2010 11:24 pm
Location: California
Blog: View Blog (0)


Re: Python network sniffer

Post by fashizzlepop on Sun Sep 19, 2010 10:20 pm
([msg=46153]see Re: Python network sniffer[/msg])

Why did you bother posting this in a dead thread? Check the timestamps before posting please.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2304
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests