| 1 |
#!/usr/bin/env python |
| 2 |
__author__ = 'Ben "TheX1le" Smith, Marfi' |
| 3 |
__email__ = '[email protected]' |
| 4 |
__website__= '' |
| 5 |
__date__ = '09/19/09' |
| 6 |
__version__ = '2009.11.23' |
| 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 os |
| 27 |
|
| 28 |
from airdrop import install_dir |
| 29 |
|
| 30 |
class macOUI_lookup: |
| 31 |
""" |
| 32 |
A class for deaing with OUIs and deterimining device type |
| 33 |
""" |
| 34 |
def __init__(self,oui=None,GetFile=False): |
| 35 |
""" |
| 36 |
generate the two dictionaries and return them |
| 37 |
""" |
| 38 |
|
| 39 |
aircrackOUI = None |
| 40 |
self.OUI_PATH = ["/etc/aircrack-ng/airodump-ng-oui.txt", |
| 41 |
"/usr/local/etc/aircrack-ng/airodump-ng-oui.txt", |
| 42 |
"/usr/share/aircrack-ng/airodump-ng-oui.txt", |
| 43 |
"/var/lib/misc/oui.txt", |
| 44 |
"/usr/share/misc/oui.txt", |
| 45 |
"/var/lib/ieee-data/oui.txt", |
| 46 |
"/usr/share/ieee-data/oui.txt", |
| 47 |
"/etc/manuf/oui.txt", |
| 48 |
"/usr/share/wireshark/wireshark/manuf/oui.txt", |
| 49 |
"/usr/share/wireshark/manuf/oui.txt"] |
| 50 |
# append any oui paths provided by program using lib to list |
| 51 |
if oui is not None: |
| 52 |
self.OUI_PATH.append(oui) |
| 53 |
for PATH in self.OUI_PATH: |
| 54 |
if os.path.isfile(PATH): |
| 55 |
aircrackOUI=PATH |
| 56 |
if aircrackOUI is None: |
| 57 |
# default |
| 58 |
aircrackOUI=self.OUI_PATH[1] |
| 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 |
self.ouiUnPath = install_dir#path to oui.txt if module is installed |
| 62 |
self.ouiInPath = install_dir + '/support/' #path to oui.txt if module is not installed |
| 63 |
self.ouiTxt = aircrackOUI |
| 64 |
|
| 65 |
self.ouiRaw = self.ouiOpen() |
| 66 |
self.oui_company = self.ouiParse() #dict where oui's are the keys to company names |
| 67 |
self.company_oui = self.companyParse() #dict where company name is the key to oui's |
| 68 |
|
| 69 |
def compKeyChk(self,name): |
| 70 |
""" |
| 71 |
check for valid company name key |
| 72 |
""" |
| 73 |
compMatch = re.compile(name,re.I) |
| 74 |
if name in self.company_oui: |
| 75 |
return True |
| 76 |
for key in self.company_oui.keys(): |
| 77 |
if compMatch.search(key) is not None: |
| 78 |
return True |
| 79 |
return False |
| 80 |
|
| 81 |
def ouiKeyChk(self,name): |
| 82 |
""" |
| 83 |
check for a valid oui prefix |
| 84 |
""" |
| 85 |
|
| 86 |
if name in self.oui_company: |
| 87 |
return True |
| 88 |
else: |
| 89 |
return False |
| 90 |
|
| 91 |
|
| 92 |
def lookup_OUI(self,mac): |
| 93 |
""" |
| 94 |
Lookup a oui and return the company name |
| 95 |
""" |
| 96 |
if self.ouiKeyChk(mac) is not False: |
| 97 |
return self.oui_company[mac][0] |
| 98 |
else: |
| 99 |
return False |
| 100 |
|
| 101 |
def lookup_company(self,companyLst): |
| 102 |
""" |
| 103 |
look up a company name and return their OUI's |
| 104 |
""" |
| 105 |
oui = [] |
| 106 |
if type(companyLst) is list: |
| 107 |
for name in companyLst: |
| 108 |
compMatch = re.compile(name,re.I) |
| 109 |
if name in self.company_oui: |
| 110 |
oui.extend(self.company_oui[name]) |
| 111 |
else: |
| 112 |
for key in self.company_oui: |
| 113 |
if compMatch.search(key) is not None: |
| 114 |
oui.extend(self.company_oui[key]) |
| 115 |
|
| 116 |
elif type(companyLst) is str: |
| 117 |
if companyLst in self.company_oui: |
| 118 |
oui = self.company_oui[companyLst] |
| 119 |
else: |
| 120 |
|
| 121 |
compMatch = re.compile(companyLst,re.I) |
| 122 |
for key in self.company_oui: |
| 123 |
if compMatch.search(key) is not None: |
| 124 |
oui.extend(self.company_oui[key]) #return the oui for that key |
| 125 |
return oui |
| 126 |
|
| 127 |
def ouiOpen(self): |
| 128 |
""" |
| 129 |
open the file and read it in |
| 130 |
""" |
| 131 |
with open(self.ouiTxt, "r") as fid: |
| 132 |
text = fid.readlines() |
| 133 |
#text = ouiFile.read() |
| 134 |
return text |
| 135 |
|
| 136 |
def ouiParse(self): |
| 137 |
""" |
| 138 |
generate a oui to company lookup dict |
| 139 |
""" |
| 140 |
HexOui= {} |
| 141 |
Hex = re.compile('.*(hex).*') |
| 142 |
#matches the following example "00-00-00 (hex)\t\tXEROX CORPORATION" |
| 143 |
ouiLines = self.ouiRaw |
| 144 |
for line in ouiLines: |
| 145 |
if Hex.search(line) is not None: |
| 146 |
#return the matched text and build a list out of it |
| 147 |
lineList = Hex.search(line).group().replace("\t"," ").split(" ") |
| 148 |
#build a dict in the format of mac:company name |
| 149 |
HexOui[lineList[0].replace("-",":")] = [lineList[2]] |
| 150 |
return HexOui |
| 151 |
|
| 152 |
def companyParse(self): |
| 153 |
""" |
| 154 |
generate a company to oui lookup dict |
| 155 |
""" |
| 156 |
company_oui = {} |
| 157 |
for oui in self.oui_company: |
| 158 |
if self.oui_company[oui][0] in company_oui: |
| 159 |
company_oui[self.oui_company[oui][0]].append(oui) |
| 160 |
else: |
| 161 |
company_oui[self.oui_company[oui][0]] = [oui] |
| 162 |
return company_oui |
| 163 |
|
| 164 |
|
| 165 |
if __name__ == "__main__": |
| 166 |
import pdb |
| 167 |
# for testing |
| 168 |
x = macOUI_lookup() |
| 169 |
pdb.set_trace() |