[ create a new paste ] login | about

Link: http://codepad.org/Bb6K4Kxe    [ raw code | fork ]

Python, pasted on May 22:
#!/usr/bin/python

#Packet sniffer in python
#For Linux - Sniffs all incoming and outgoing packets :)
#Silver Moon (m00n.silv3r@gmail.com)
#modified by danman
#modified by TheRealTentacle

import socket, sys
from struct import *
import struct
import	 time

#Convert a string of 6 characters of ethernet address into a dash separated hex string
def eth_addr (a) :
  b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]) , ord(a[1]) , ord(a[2]), ord(a[3]), ord(a[4]) , ord(a[5]))
  return b

#create a AF_PACKET type raw socket (thats basically packet level)
#define ETH_P_ALL    0x0003          /* Every packet (be careful!!!) */
try:
    s = socket.socket( socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003))
except socket.error , msg:
    print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

sender="000b78006001".decode("hex")
notopen=1
# = open('/tmp/fifo', 'w')
i = 0
sec = time.time()
now = 0
time.clock() 
piclen = 0
newline = "\r\n"

# receive a packet
while True:
    packet = s.recvfrom(65565)

    #packet string from tuple
    packet = packet[0]

    #parse ethernet header
    eth_length = 14

    eth_header = packet[:eth_length]
    eth = unpack('!6s6sH' , eth_header)
    eth_protocol = socket.ntohs(eth[2])

    if (packet[6:12]==sender) & (eth_protocol == 8) :

        #Parse IP header
        #take first 20 characters for the ip header
        ip_header = packet[eth_length:20+eth_length]

        #now unpack them :)
        iph = unpack('!BBHHHBBH4s4s' , ip_header)

        version_ihl = iph[0]
        version = version_ihl >> 4
        ihl = version_ihl & 0xF

        iph_length = ihl * 4

        ttl = iph[5]
        protocol = iph[6]
        s_addr = socket.inet_ntoa(iph[8]);
        d_addr = socket.inet_ntoa(iph[9]);

        #UDP packets
        if protocol == 17 :
	    u = iph_length + eth_length
	    udph_length = 8
	    udp_header = packet[u:u+8]

	    #now unpack them :)
	    udph = unpack('!HHHH' , udp_header)

	    source_port = udph[0]
	    dest_port = udph[1]
	    length = udph[2]
	    checksum = udph[3]

	    #get data from the packet
	    h_size = eth_length + iph_length + udph_length
	    data = packet[h_size:]

	    if (dest_port==2068):
	        frame_n=ord(data[0])*256+ord(data[1])
		part=ord(data[3])
	        piclen += len(data)

		if (part==0):
	            if (i > 0):
#		        sys.stdout.write(newline + "--videoboundary--" + newline)
#			f.close()
		        now = time.time() - sec
		    i = i + 1

#		    f = open('files/'+str(frame_n)+"_"+str(now)+'.jpg', 'w')
#		    print "frame",frame_n,"piclen",piclen,"time",str(now)

		    notopen=0
		    piclen = 0	
				
		if notopen==0:
		    sys.stdout.write(data[4:])

#		sys.stdout.write(newline + "--" + "videoboundary" + newline + newline)
#		f.write(data[4:])

#	        if (dest_port==2066):
#		    sys.stdout.write(data[16:])


Create a new paste based on this one


Comments: