[ create a new paste ] login | about

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

Python, pasted on May 26:
#!/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 (therealrichy@gmail.com)

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

UDP_SOUND_IP = "127.0.0.1"
UDP_SOUND_PORT = 5007		

UDP_VIDEO_IP = "127.0.0.1"
UDP_VIDEO_PORT = 5006

#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

ssound = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
svideo = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

#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

newline = "\r\n"
lastpart = 0
lastpacket = 0
audiobuffer = ""

#parse ethernet header
eth_length = 14
udph_length = 8

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

    #packet string from tuple
    packet = packet[0]

    eth_protocol = ord(packet[12])

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

		version_ihl = ord(packet[eth_length])
		protocol = ord(packet[eth_length + 9])

		version = version_ihl >> 4
		ihl = version_ihl & 0xF
		iph_length = ihl << 2

		tiphethl = iph_length + eth_length

		#UDP packets
		if protocol == 17 :
			dest_port = (ord(packet[2 + tiphethl]) << 8) + ord(packet[3 + tiphethl])

			#get data from the packet
			h_size = tiphethl + udph_length

			if (dest_port==2068):
				part	= (ord(packet[2 + h_size]) << 8) + ord(packet[3+ h_size]) 

				# packet missing
  				if (lastpart != (part - 1)):
					notopen = 1

				if part == 0 :
					notopen = 0

				# last packet
				if (lastpart == (part - 0x8001)):
					notopen = 0
					lastpacket = 1

				lastpart = part
				
				if notopen==0:

					svideo.sendto((packet[4 + h_size:]), (UDP_VIDEO_IP, UDP_VIDEO_PORT))

					if (lastpacket == 1):
						lastpacket = 0

			if (dest_port==2066):
				ssound.sendto(packet[16 + h_size:],(UDP_SOUND_IP, UDP_SOUND_PORT))




	   


Create a new paste based on this one


Comments: