-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpacumen.py
74 lines (61 loc) · 2.11 KB
/
pacumen.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
import scapy.all as sc
import scipy.sparse as sparse
import numpy
import cPickle as pickle
import os
import oneclasstree
def make_feature_vectors_from_pcap(fname, bucketsize=10000, remove_empty_packets=True):
cachefile = fname + ('.%d.fvec' % (bucketsize,))
if os.path.exists(cachefile):
try:
with open(cachefile, 'rb') as f:
rv = pickle.load(f)
assert hasattr(rv, 'shape')
return rv
except:
pass
pkts = list(sc.rdpcap(fname))
times = numpy.array([numpy.round(pkt.time, 3) * 1000 for pkt in pkts], dtype=numpy.long)
times = numpy.sort(times)
times = times - times[0]
print 'TIMES: %s' % (times,)
sizes = numpy.array([len(pkt) for pkt in pkts], dtype=numpy.long)
buckets = times / bucketsize
print 'BUCKETS: %s' % (buckets,)
def _mk_sparse(bucket):
szs = sizes[buckets == bucket]
rv = sparse.lil_matrix((1,65536), dtype=numpy.long)
for sz in szs:
rv[0,sz] = rv[0,sz] + 1
return rv
svectors = [_mk_sparse(b) for b in range(buckets[-1] + 1)]
print "SHAPES: %s" % ([s.shape for s in svectors],)
rv = sparse.vstack(svectors).tocsr()
# pick the smallest packet size and kill it!!!!! this gets rid of empty packets
if remove_empty_packets:
cols = rv.nonzero()[1]
ep = cols.min()
print 'assuming packet size %d is empty' % (ep,)
rv[:,ep] = 0
try:
with open(cachefile, 'wb') as f:
pickle.dump(rv, f, protocol=2)
finally:
return rv
def get_class_fvecs(classname, directory='tdata/train', remove_zero=True):
fvs = []
def _walk_fn(arg, dirname, fnames):
fnames = ["%s/%s" % (dirname, f) for f in fnames if f[-5:] == '.pcap' and f[:len(classname)] == classname]
fvs.extend([make_feature_vectors_from_pcap(f) for f in fnames])
os.path.walk(directory, _walk_fn, None)
fvs = sparse.vstack(fvs).tocsr()
if remove_zero:
nzrows = numpy.unique(fvs.nonzero()[0])
fvs = fvs[nzrows,:]
return fvs
def classify_pcap(classifier, pcap):
fv = make_feature_vectors_from_pcap(pcap)
result = classifier.classify(fv)
result = oneclasstree.bayesian(result)[1]
#print pcap, result, result > 0.5
return result