-
Notifications
You must be signed in to change notification settings - Fork 1
/
procpcapymon.py
executable file
·253 lines (221 loc) · 7.27 KB
/
procpcapymon.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/python -u
#
# Copyright 2015 Diana A. Vasile
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging
import binascii
import pcappy
import sys
import dpkt
import ctypes
import os
import webbrowser
from collections import OrderedDict,defaultdict
from multiprocessing import Process
from collections import deque
# ===================================
# TODO display top entries received rather than the last ones for the purpose of this demo
d= defaultdict(list)
associate = defaultdict(list)
# hardcoded value for mac address of router used
dac53mac = "0026f2010eb2"
macvendors = {}
ls = []
# by reading from nmap ma
def buildMacVendorsMap():
macs=open('nmap-mac-prefixes','r')
macs_lines=macs.readlines()
for line in macs_lines:
macvendors[line[0:6]] = line[7:].strip()
def sanitizeMac(mac):
temp = mac.replace(":", "").replace("-", "").replace(".", "").upper()
return temp[:2] + ":" + ":".join([temp[i] + temp[i+1] for i in range(2,12,2)])
def buildmesg():
# number of cells per html page
cells = 8
f= open("display.html","w")
# uses only those devices who have consented to their data being displayed by connecting to dac53
# array = list(set(associate[dac53mac][:]))
# uses all of the sending devices
array = ls[:]
# prepend the array with placeholders so that we do not get out of bounds exceptions
l = len(array)
if l < cells:
dif = cells-l
for x in range(0,dif):
array = [""] + array
# the main html setup for the page before building the rest with the data
mesg = """<html>
<head>
<meta http-equiv="refresh" content="10" >
<style>
table.style1 {
color: #333;
font-family: Helvetica, Arial, sans-serif;
margin-top:500px
width: 440px;
border-collapse: collapse; border-spacing: 0;}
table.style1 td, th {
border: 1px solid transparent;
height: 70px;
font-size: 40px;
transition: all 0.3s;
}
table.style1 th {
background: #DFDFDF;
font-weight: bold;
}
table.style1 td {
background: #FAFAFA;
text-align: center;
}
table.style1 tr:nth-child(even) td { background:#FEFEFE; }
table.style1 tr:nth-child(odd) td { background: #F1F1F1; }
table.style1 tr td:hover { background: #666; color: #FFF; } /* Hover cell effect! */
</style>
</head>
<body>
<div align="center" >
<table class="pure-table">
<tr>
<td><table class="style1"><tr><th>D8:BB:2C:66:E6:AF<br>Apple</th></tr><tr><td>LothianWIFI</td></tr><tr><td>Otopeni Airport</td></tr><tr><td>#WIFI@Changi</td></tr><tr><td>Furama BK 120</td></tr><tr><td>...</td></tr></table></td>
<td>"""+prettify(array.pop())+"""</td>
<td>"""+prettify(array.pop())+"""</td>
<td>"""+prettify(array.pop())+"""</td>
</tr>
<tr>
<td>"""+prettify(array.pop())+"""</td>
<td>"""+prettify(array.pop())+"""</td>
<td>"""+prettify(array.pop())+"""</td>
<td>"""+prettify(array.pop())+"""</td>
</tr>
</table>
</div>
</body>
</html>"""
f.write(mesg)
f.flush()
f.close()
def prettify(key):
if key == "":
return ""
# the number of lines displayed in each cell excluding the cell header (containing mac and vendor)
maxDisplayLines = 5
# get the device's manufacturer by the mac prefix
prefix = key[0:6].upper()
vendor = ""
try:
vendor = macvendors[prefix]
except KeyError:
print "Vendor info not found for mac prefix " + prefix
pass
# build the html text
thead = "<table class=\"style1\"><tr><th>"+sanitizeMac(key)+"<br>" + vendor +"</th></tr>"
tbody = ""
smac = set(d[key])
count = 0
for k in smac:
if count == maxDisplayLines - 1:
tbody = tbody + "<tr><td>...</td></tr>"
break
count = count + 1
tbody = tbody + "<tr><td>" + k + "</td></tr>"
# include placeholder lines to match the max number of display lines
if len(smac) < maxDisplayLines:
dif = maxDisplayLines - len(smac)
for x in range(0,dif):
tbody =tbody + "<tr><td/></tr>"
tbody = tbody + "</table>"
return thead + tbody
# the main function
def proc():
# prebuild a hashmap of the mac prefixes to vendors data.
buildMacVendorsMap()
global f
# TODO use debug flag rather than comment out.
# use trace to find bugs
# sys.settrace(utils.trace)
logging.basicConfig(filename='example.log',level=logging.INFO)
errbuf = None
# start the pcap setup. including setting the errbuf, enabling rfmon and setting the buffer's size
p = None
p = pcappy.pcap_create("en0", errbuf)
if(errbuf !=None):
print "error: pcap_create failed"
print errbuf
f.close()
if(pcappy.pcap_can_set_rfmon(p) != 0):
print "can set rfmon"
if (pcappy.pcap_set_rfmon(p,1) == 0):
print "activated rfmon"
else:
print "failed to activate rfmon"
f.close()
return
pcappy.pcap_set_buffer_size(p,512)
status = pcappy.pcap_activate(p)
if status != 0:
print "failed to activate pcap handler"
f.close()
return
# start the pcap loop, 0 = infinitely (can specify a certain nb of values for a limited capture -
# this does not necessarily mean you'll get those results - i.e. 2000 captures might only get 20
# suitable messages), with the pcap handler processing function specified
pcappy.pcap_loop(p,0,pcappy.pcap_handler(process),None)
# when finished, close the pcap handler
pcappy.pcap_close(p)
def process(usr,hdr,data):
try:
buildmesg()
raw_data = ctypes.string_at(data, 512)
tap = dpkt.radiotap.Radiotap(raw_data)
t_len=binascii.hexlify(raw_data[2:3])
t_len=int(t_len,16) #Convert to decimal
wlan = dpkt.ieee80211.IEEE80211(raw_data[t_len:])
msg_type = wlan.type
msg_subtype = wlan.subtype
if msg_type == 0:
src_mac = binascii.hexlify(wlan.mgmt.src)
dst_mac = binascii.hexlify(wlan.mgmt.dst)
if msg_subtype == 0x0 or msg_subtype == 0x02 or msg_subtype == 0x0b:
print "Associate " + sanitizeMac(src_mac) + " to router " + sanitizeMac(dst_mac)
associate[dst_mac].append(src_mac)
elif msg_subtype == 0x0a or msg_subtype == 0x0c:
print "Disassociate " + sanitizeMac(src_mac) + " from router " + sanitizeMac(dst_mac)
associate[dst_mac].append(src_mac)
elif msg_subtype == 4: #probe req
ssid = wlan.ies[0].info
if ssid == None or ssid == "":
return
if ssid == "dac53":
associate[dac53mac].append(src_mac)
logging.info("mac is " + sanitizeMac(src_mac) +" and ssid is " + unicode(ssid))
d[src_mac].append(ssid)
if src_mac not in ls:
ls.append(src_mac)
except dpkt.dpkt.NeedData:
print "empty raw data"
pass
except KeyError, e:
print "Key error occurred"
print e
pass
except UnicodeDecodeError:
print "Decoding error"
pass
if __name__ == '__main__':
c = Process (target= proc)
c.start()
c.join()