This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
packet_filter.py
176 lines (144 loc) · 5.2 KB
/
packet_filter.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from optparse import OptionParser
from threading import Thread
import time
import json
import pcapy
from scapy.layers.all import IP, Ether,ICMP, TCP
import pydb
import netifaces
import OSC # pyOSC
import copy
import traceback
parser = OptionParser()
parser.add_option('-c', '--connect', default='localhost:9999', help='osc host:port to connect to')
parser.add_option('-i', '--interface', default='eth0', help='network interface to listen on')
parser.add_option('-f', '--file', default=None, help='pcap file to load instead of interface')
parser.add_option('-a', '--interface_address', default='127.0.0.1', help='interface-address to help with incoming/outgoing sorting')
(options, args) = parser.parse_args()
print options
print args
hostport = options.connect.split(':')
hostport[1] = int(hostport[1])
hostport = tuple(hostport)
openfile = options.file
# global variables
nw_traffic_global = {}
nw_traffic_inout = {'in': 0, 'out': 0}
def main():
oc = OSC.OSCClient()
oc.connect(hostport)
kbints = 0
interface_address=options.interface_address
if openfile:
p = pcapy.open_offline(openfile)
else:
# begin listening to network traffic
interface = options.interface
interface_address = netifaces.ifaddresses(interface)[2][0]['addr']
p = pcapy.open_live(interface, 1024, False, 10240)
# create and start threads
network_traffic = communication_thread()
network_traffic.start()
try:
(header, payload) = p.next()
while header:
e = Ether(payload)
t = e.getlayer(TCP)
if t:
i = e.getlayer(IP)
nw_traffic_global.setdefault(t.dport,0)
try:
nw_traffic_global[t.dport]+=1
except KeyError:
print 'race'
nw_traffic_inout['in' if i.dst == interface_address else 'out']+=1
if e.haslayer(ICMP):
x = OSC.OSCMessage()
x.setAddress('/plinker/ping')
x.append(1)
oc.send(x)
print 'ICMP ping sent'
second = header.getts()[0] # [1] = miliseconds
try:
(header, payload) = p.next()
except Exception,e:
pass
#print type(e)
except Exception,e:
print traceback.print_exc(e)
except KeyboardInterrupt:
pass
# shut down
try:
p.close()
except Exception,e:
print e
time.sleep(1)
# setting status to false ends the threadss
print 'setting status'
network_traffic.status = False
print 'set status'
#bar.status = False
print "goodbye"
oc.close()
# thread class
class communication_thread(Thread):
def __init__(self):
Thread.__init__(self)
self.value = 1
self.interval = 0.42
self.status = True
self.services = {80: 'web', 443: 'web',
22: 'ssh',
25: 'ftp', 26: 'ftp', 115: 'ftp', 445: 'ftp', # 445 = samba
5222: 'chat', 5269: 'chat', 1863: 'chat', 5190: 'chat', 194: 'chat', 994: 'chat', 6667: 'chat',
}
self.defaultservice = 'other'
def run(self):
oc = OSC.OSCClient()
oc.connect(hostport)
values_last = {}
while self.status:
#print '----'
#print values_last
traffic = copy.copy(nw_traffic_global)
inout = copy.copy(nw_traffic_inout)
nw_traffic_global.clear()
nw_traffic_inout.clear()
nw_traffic_inout['in']=0
nw_traffic_inout['out']=0
values = {}
for (name,value) in values_last.iteritems():
if value:
values[name]=0
handledservices = []
# send data to chuck
# remove values in array
keys = traffic.keys()
for key in keys:
service = self.services.get(key, self.defaultservice)
current = float(traffic.get(key,0))
values.setdefault(service, 0.)
values[service]+=current
handledservices.append(service)
for (name, count) in values.iteritems():
x = OSC.OSCMessage()
x.setAddress('/plinker/%s' % name) # no ,f; gets added automagically
#print x.address
current = float(count)/(values_last.get(name, count) or 1)
x.append(current)
oc.send(x)
if(name == 'chat'):
print (name, current, count, values_last.get(name, None))
x = OSC.OSCMessage()
x.setAddress('/plinker/inout') # no ,f; gets added automagically
x.append(float(inout['in'])/float(inout['out'] or 1))
oc.send(x)
x = OSC.OSCMessage()
x.setAddress('/plinker/count') # no ,f; gets added automagically
x.append(float(inout['in'])+float(inout['out']))
oc.send(x)
values_last = values
# wait
time.sleep(self.interval)
main()