-
Notifications
You must be signed in to change notification settings - Fork 0
/
HDMI_sniff_net_vlc.py
124 lines (95 loc) · 3.44 KB
/
HDMI_sniff_net_vlc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python2
#Packet sniffer in python
#For Linux - Sniffs all incoming and outgoing packets :)
#Silver Moon ([email protected])
#modified by danman
#modified again by [email protected]
import socket, sys
from struct import *
import struct
import binascii
import socket
import re
#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))
# This allows us to connect via VLC and the TCP handler
# /Applications/VLC.app/Contents/MacOS/VLC --playlist-autostart tcp://192.168.168.229:1234
lport = "1234"
lsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
lsocket.bind(("", int(lport)))
lsocket.listen(1)
print("[+] Listening on port " + lport)
connection, address = lsocket.accept()
print("[*] Connection from " + str(address[0]))
except socket.error , msg:
print 'Sockets could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
notopen=1
# Mpeg Demux Mime Boundary
connection.send("--myboundary")
connection.send("\n")
connection.send("Content-Type: image/jpeg")
connection.send("\n")
connection.send("\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])
sender="444444444444".decode("hex")
#print binascii.b2a_hex(sender)
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:]
# M-JPEG streamed as multipart - http://en.wikipedia.org/wiki/Motion_JPEG
if (dest_port==2068):
frame_n=ord(data[0])*256+ord(data[1])
part=ord(data[2])*256+ord(data[3])
if (part==0) : # & (notopen==1) :
# Mpeg Demux Mime Boundary
connection.send("\n")
connection.send("--myboundary")
connection.send("\n")
connection.send("Content-Type: image/jpeg")
connection.send("\n")
connection.send("\n")
notopen=0
if notopen==0:
# JFIF - http://www.w3.org/Graphics/JPEG/
connection.send(data[4:])