| 1 |
#!/usr/bin/env python |
| 2 |
__author__ = 'Ben "TheX1le" Smith, Marfi' |
| 3 |
__email__ = '[email protected]' |
| 4 |
__website__= '' |
| 5 |
__date__ = '04/26/2011' |
| 6 |
__version__ = '2011.4.26' |
| 7 |
__file__ = 'ouiParse.py' |
| 8 |
__data__ = 'a class for dealing with the oui txt file' |
| 9 |
|
| 10 |
""" |
| 11 |
######################################## |
| 12 |
# |
| 13 |
# This program and its support programs are free software; you can redistribute it and/or modify it |
| 14 |
# under the terms of the GNU General Public License version 2 as |
| 15 |
# published by the Free Software Foundation; version 2. |
| 16 |
# |
| 17 |
# This program is distributed in the hope that it will be useful, but |
| 18 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
# General Public License for more details. |
| 21 |
# |
| 22 |
######################################### |
| 23 |
""" |
| 24 |
|
| 25 |
import re |
| 26 |
import sys |
| 27 |
if sys.version_info[0] >= 3: |
| 28 |
import requests |
| 29 |
else: |
| 30 |
import urllib |
| 31 |
import os |
| 32 |
import pdb |
| 33 |
#this lib is crap and needs to be rewritten -Textile |
| 34 |
|
| 35 |
if os.getenv('AIRGRAPH_HOME') is not None and os.path.isdir(os.getenv('AIRGRAPH_HOME')): |
| 36 |
path=os.getenv('AIRGRAPH_HOME') + '/support/' |
| 37 |
if not os.path.isdir(path): |
| 38 |
try: |
| 39 |
os.mkdir(path) |
| 40 |
except: |
| 41 |
raise Exception("Can't create destination directory (%s)!" % path) |
| 42 |
elif os.path.isdir('./support/'): |
| 43 |
path='./support/' |
| 44 |
elif os.path.isdir('/usr/local/share/airgraph-ng/'): |
| 45 |
path='/usr/local/share/airgraph-ng/' |
| 46 |
elif os.path.isdir('/usr/share/airgraph-ng/'): |
| 47 |
path='/usr/share/airgraph-ng/' |
| 48 |
else: |
| 49 |
raise Exception("Could not determine path, please, check your installation or set AIRGRAPH_HOME environment variable") |
| 50 |
|
| 51 |
class macOUI_lookup: |
| 52 |
""" |
| 53 |
A class for deaing with OUIs and deterimining device type |
| 54 |
""" |
| 55 |
def __init__(self, oui=False): |
| 56 |
""" |
| 57 |
generate the two dictionaries and return them |
| 58 |
""" |
| 59 |
#a poor fix where if we have no file it tries to download it |
| 60 |
self.ouiTxtUrl = "http://standards-oui.ieee.org/oui.txt" |
| 61 |
|
| 62 |
self.ouiTxt = oui |
| 63 |
if not oui or not os.path.isfile(self.ouiTxt): |
| 64 |
self.ouiUpdate() |
| 65 |
self.ouiTxt = path + "oui.txt" |
| 66 |
self.last_error = None |
| 67 |
self.identDeviceDict(path + 'ouiDevice.txt') |
| 68 |
self.identDeviceDictWhacMac(path + 'whatcDB.csv') |
| 69 |
self.ouiRaw = self.ouiOpen(self.ouiTxt) |
| 70 |
self.oui_company = self.ouiParse() #dict where oui's are the keys to company names |
| 71 |
self.company_oui = self.companyParse() #dict where company name is the key to oui's |
| 72 |
|
| 73 |
def compKeyChk(self,name): |
| 74 |
""" |
| 75 |
check for valid company name key |
| 76 |
""" |
| 77 |
compMatch = re.compile(name,re.I) |
| 78 |
if name in self.company_oui: |
| 79 |
return True |
| 80 |
for key in list(self.company_oui.keys()): |
| 81 |
if compMatch.search(key) is not None: |
| 82 |
return True |
| 83 |
return False |
| 84 |
|
| 85 |
def ouiKeyChk(self,name): |
| 86 |
""" |
| 87 |
check for a valid oui prefix |
| 88 |
""" |
| 89 |
|
| 90 |
if name in self.oui_company: |
| 91 |
return True |
| 92 |
else: |
| 93 |
return False |
| 94 |
|
| 95 |
def lookup_OUI(self,mac): |
| 96 |
""" |
| 97 |
Lookup a oui and return the company name |
| 98 |
""" |
| 99 |
if self.ouiKeyChk(mac) is not False: |
| 100 |
return self.oui_company[mac] |
| 101 |
else: |
| 102 |
return False |
| 103 |
|
| 104 |
def lookup_company(self,companyLst): |
| 105 |
""" |
| 106 |
look up a company name and return their OUI's |
| 107 |
""" |
| 108 |
oui = [] |
| 109 |
if type(companyLst) is list: |
| 110 |
for name in companyLst: |
| 111 |
compMatch = re.compile(name,re.I) |
| 112 |
if name in self.company_oui: |
| 113 |
oui.extend(self.company_oui[name]) |
| 114 |
else: |
| 115 |
for key in self.company_oui: |
| 116 |
if compMatch.search(key) is not None: |
| 117 |
oui.extend(self.company_oui[key]) |
| 118 |
|
| 119 |
elif type(companyLst) is str: |
| 120 |
if companyLst in self.company_oui: |
| 121 |
oui = self.company_oui[companyLst] |
| 122 |
else: |
| 123 |
|
| 124 |
compMatch = re.compile(companyLst,re.I) |
| 125 |
for key in self.company_oui: |
| 126 |
if compMatch.search(key) is not None: |
| 127 |
oui.extend(self.company_oui[key]) #return the oui for that key |
| 128 |
return oui |
| 129 |
|
| 130 |
def ouiOpen(self,fname,flag='R'): |
| 131 |
""" |
| 132 |
open the file and read it in |
| 133 |
flag denotes use of read or readlines |
| 134 |
""" |
| 135 |
try: |
| 136 |
with open(fname, "r") as fid: |
| 137 |
if flag == 'RL': |
| 138 |
text = fid.readlines() |
| 139 |
elif flag == 'R': |
| 140 |
text = fid.read() |
| 141 |
return text |
| 142 |
except IOError: |
| 143 |
return False |
| 144 |
|
| 145 |
|
| 146 |
def ouiParse(self): |
| 147 |
""" |
| 148 |
generate a oui to company lookup dict |
| 149 |
""" |
| 150 |
HexOui= {} |
| 151 |
Hex = re.compile('.*(hex).*') |
| 152 |
#matches the following example "00-00-00 (hex)\t\tXEROX CORPORATION" |
| 153 |
ouiLines = self.ouiRaw.split("\n") |
| 154 |
#split each company into a list one company per position |
| 155 |
for line in ouiLines: |
| 156 |
if Hex.search(line) is not None: |
| 157 |
lineList = Hex.search(line).group().replace("\t"," ").split(" ") |
| 158 |
#return the matched text and build a list out of it |
| 159 |
HexOui[lineList[0].replace("-",":")] = lineList[2].strip() |
| 160 |
#build a dict in the format of mac:company name |
| 161 |
return HexOui |
| 162 |
|
| 163 |
def companyParse(self): |
| 164 |
""" |
| 165 |
generate a company to oui lookup dict |
| 166 |
""" |
| 167 |
company_oui = {} |
| 168 |
for oui in self.oui_company: |
| 169 |
if self.oui_company[oui] in company_oui: |
| 170 |
company_oui[self.oui_company[oui]].append(oui) |
| 171 |
else: |
| 172 |
company_oui[self.oui_company[oui]] = [oui] |
| 173 |
return company_oui |
| 174 |
|
| 175 |
def ouiUpdate(self): |
| 176 |
""" |
| 177 |
Grab the oui txt file off the ieee.org website |
| 178 |
""" |
| 179 |
try: |
| 180 |
print(("Getting OUI file from %s to %s" %(self.ouiTxtUrl, path))) |
| 181 |
if sys.version_info[0] == 2: |
| 182 |
urllib.request.urlretrieve(self.ouiTxtUrl, path + "oui.txt") |
| 183 |
else: |
| 184 |
response = requests.get(self.ouiTxtUrl) |
| 185 |
with open(path + "oui.txt", "wb") as file: |
| 186 |
bytes_written = file.write(response.content) |
| 187 |
print("Completed Successfully") |
| 188 |
except Exception as error: |
| 189 |
print(("Could not download file:\n %s\n Exiting airgraph-ng" %(error))) |
| 190 |
sys.exit(0) |
| 191 |
|
| 192 |
def identDeviceDict(self,fname): |
| 193 |
""" |
| 194 |
Create two dicts allowing device type lookup |
| 195 |
one for oui to device and one from device to OUI group |
| 196 |
""" |
| 197 |
self.ouitodevice = {} |
| 198 |
self.devicetooui = {} |
| 199 |
data = self.ouiOpen(fname,'RL') |
| 200 |
if data == False: |
| 201 |
self.last_error = "Unable to open lookup file for parsing" |
| 202 |
return False |
| 203 |
for line in data: |
| 204 |
dat = line.strip().split(',') |
| 205 |
self.ouitodevice[dat[1]] = dat[0] |
| 206 |
if dat[0] in list(self.devicetooui.keys()): |
| 207 |
self.devicetooui[dat[0]].append(dat[1]) |
| 208 |
else: |
| 209 |
self.devicetooui[dat[0]] = [dat[1]] |
| 210 |
|
| 211 |
def identDeviceDictWhacMac(self,fname): |
| 212 |
""" |
| 213 |
Create two dicts allowing device type lookup from whatmac DB |
| 214 |
one for oui to device and one from the device to OUI group |
| 215 |
""" |
| 216 |
self.ouitodeviceWhatmac3 = {} |
| 217 |
self.ouitodeviceWhatmac = {} |
| 218 |
self.devicetoouiWhacmac = {} |
| 219 |
data = self.ouiOpen(fname,'RL') |
| 220 |
if data == False: |
| 221 |
self.last_error = "Unble to open lookup file for parsing" |
| 222 |
return False |
| 223 |
for line in data: |
| 224 |
dat = line.strip().split(',') |
| 225 |
dat[0] = dat[0].upper() |
| 226 |
self.ouitodeviceWhatmac[dat[0]] = dat[1] |
| 227 |
self.ouitodeviceWhatmac3[dat[0][0:8]] = dat[1] # a db to support the 3byte lookup from whatmac |
| 228 |
if dat[1] in list(self.devicetoouiWhacmac.keys()): |
| 229 |
self.devicetoouiWhacmac[dat[1]].append(dat[0]) |
| 230 |
else: |
| 231 |
self.devicetoouiWhacmac[dat[1]] = [dat[0]] |
| 232 |
|