-
Notifications
You must be signed in to change notification settings - Fork 0
/
songify.py
51 lines (43 loc) · 1.84 KB
/
songify.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
#!/usr/bin/env python
# import python libraries that we use
from optparse import OptionParser
from scapy.all import *
from netmidi import NetMidi
from tokenbucket import TokenBucket
# get params from command line
parser = OptionParser()
parser.add_option("-i", "--interface", dest="interface", help="interface to listen on", default="eth0")
parser.add_option("-p", "--pcap", dest="pcap", help="Use PCAP file instead of network interface. Pass path to PCAP file.", default=False)
parser.add_option("-m", "--midiport",dest="mport", help="The midi port number to output to", default=1)
# if you use -h this will print out a help description
options, remainder = parser.parse_args() # store options in variable
# create the NetMidi object for output
myNetMidi = NetMidi(int(options.mport))
#initialze leaky bucket algo
bucket = TokenBucket(20, 10)
# everytime scapy sees a packet this func will be called
def callback(pkt):
if bucket.consume(1):
if IP in pkt:
if pkt.haslayer(TCP) or pkt.haslayer(UDP): # tcp and udp are only ones that will have port numbers
size = pkt[IP].len
if TCP in pkt:
proto = "TCP"
port = min([pkt[TCP].sport,pkt[TCP].dport])
eph_port = max([pkt[TCP].sport,pkt[TCP].dport])
elif UDP in pkt:
proto = "UDP"
port = min([pkt[UDP].sport,pkt[UDP].dport])
eph_port = max([pkt[UDP].sport,pkt[UDP].dport])
myNetMidi.playNote(proto, port, eph_port, size)
elif pkt.haslayer(ICMP):
# ICMP packets are special
size = 1480
port = pkt[ICMP].type
eph_port = (pkt[ICMP].type + 1) * 15
myNetMidi.playNote("ICMP", port, eph_port, size)
# start sniffing
if options.pcap == False:
sniff(prn=callback, filter="tcp or udp or icmp", store=0, iface=options.interface) # listen to interface
else:
sniff(prn=callback, filter="tcp or udp or icmp", store=0, offline=options.pcap) # read pcap file