#!/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
#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
newline = "\r\n"
lastpart = 0
lastpacket = 0
#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):
# sys.stdout.write(packet[4 + h_size:])
# frame_n = (ord(data[0]) << 8) + ord(data[1])
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 (part==0):
# sys.stdout.write(newline + "--videoboundary--" + newline)
if notopen==0:
if (part == 0):
buffer = (packet[4 + h_size:])
if (part > 0):
buffer = str(buffer) + str(packet[4 + h_size:])
if (lastpacket == 1):
lastpacket = 0
sys.stdout.write(buffer)
# sys.stdout.write(newline + "--" + "videoboundary" + newline + newline)
# if (dest_port==2066):
# sys.stdout.write(data[16:])