| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
import re |
|---|
| 7 |
from dadict import DADict |
|---|
| 8 |
from error import log_loading |
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
ETHER_ANY = "\x00"*6 |
|---|
| 15 |
ETHER_BROADCAST = "\xff"*6 |
|---|
| 16 |
|
|---|
| 17 |
ETH_P_ALL = 3 |
|---|
| 18 |
ETH_P_IP = 0x800 |
|---|
| 19 |
ETH_P_ARP = 0x806 |
|---|
| 20 |
ETH_P_IPV6 = 0x86dd |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
ARPHDR_ETHER = 1 |
|---|
| 24 |
ARPHDR_METRICOM = 23 |
|---|
| 25 |
ARPHDR_PPP = 512 |
|---|
| 26 |
ARPHDR_LOOPBACK = 772 |
|---|
| 27 |
ARPHDR_TUN = 65534 |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
IPV6_ADDR_UNICAST = 0x01 |
|---|
| 32 |
IPV6_ADDR_MULTICAST = 0x02 |
|---|
| 33 |
IPV6_ADDR_CAST_MASK = 0x0F |
|---|
| 34 |
IPV6_ADDR_LOOPBACK = 0x10 |
|---|
| 35 |
IPV6_ADDR_GLOBAL = 0x00 |
|---|
| 36 |
IPV6_ADDR_LINKLOCAL = 0x20 |
|---|
| 37 |
IPV6_ADDR_SITELOCAL = 0x40 |
|---|
| 38 |
IPV6_ADDR_SCOPE_MASK = 0xF0 |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
IPV6_ADDR_6TO4 = 0x0100 |
|---|
| 42 |
IPV6_ADDR_UNSPECIFIED = 0x10000 |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
MTU = 1600 |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
def load_protocols(filename): |
|---|
| 53 |
spaces = re.compile("[ \t]+|\n") |
|---|
| 54 |
dct = DADict(_name=filename) |
|---|
| 55 |
try: |
|---|
| 56 |
for l in open(filename): |
|---|
| 57 |
try: |
|---|
| 58 |
shrp = l.find("#") |
|---|
| 59 |
if shrp >= 0: |
|---|
| 60 |
l = l[:shrp] |
|---|
| 61 |
l = l.strip() |
|---|
| 62 |
if not l: |
|---|
| 63 |
continue |
|---|
| 64 |
lt = tuple(re.split(spaces, l)) |
|---|
| 65 |
if len(lt) < 2 or not lt[0]: |
|---|
| 66 |
continue |
|---|
| 67 |
dct[lt[0]] = int(lt[1]) |
|---|
| 68 |
except Exception,e: |
|---|
| 69 |
log_loading.info("Couldn't parse file [%s]: line [%r] (%s)" % (filename,l,e)) |
|---|
| 70 |
except IOError: |
|---|
| 71 |
log_loading.info("Can't open /etc/protocols file") |
|---|
| 72 |
return dct |
|---|
| 73 |
|
|---|
| 74 |
IP_PROTOS=load_protocols("/etc/protocols") |
|---|
| 75 |
|
|---|
| 76 |
def load_ethertypes(filename): |
|---|
| 77 |
spaces = re.compile("[ \t]+|\n") |
|---|
| 78 |
dct = DADict(_name=filename) |
|---|
| 79 |
try: |
|---|
| 80 |
f=open(filename) |
|---|
| 81 |
for l in f: |
|---|
| 82 |
try: |
|---|
| 83 |
shrp = l.find("#") |
|---|
| 84 |
if shrp >= 0: |
|---|
| 85 |
l = l[:shrp] |
|---|
| 86 |
l = l.strip() |
|---|
| 87 |
if not l: |
|---|
| 88 |
continue |
|---|
| 89 |
lt = tuple(re.split(spaces, l)) |
|---|
| 90 |
if len(lt) < 2 or not lt[0]: |
|---|
| 91 |
continue |
|---|
| 92 |
dct[lt[0]] = int(lt[1], 16) |
|---|
| 93 |
except Exception,e: |
|---|
| 94 |
log_loading.info("Couldn't parse file [%s]: line [%r] (%s)" % (filename,l,e)) |
|---|
| 95 |
f.close() |
|---|
| 96 |
except IOError,msg: |
|---|
| 97 |
pass |
|---|
| 98 |
return dct |
|---|
| 99 |
|
|---|
| 100 |
ETHER_TYPES=load_ethertypes("/etc/ethertypes") |
|---|
| 101 |
|
|---|
| 102 |
def load_services(filename): |
|---|
| 103 |
spaces = re.compile("[ \t]+|\n") |
|---|
| 104 |
tdct=DADict(_name="%s-tcp"%filename) |
|---|
| 105 |
udct=DADict(_name="%s-udp"%filename) |
|---|
| 106 |
try: |
|---|
| 107 |
f=open(filename) |
|---|
| 108 |
for l in f: |
|---|
| 109 |
try: |
|---|
| 110 |
shrp = l.find("#") |
|---|
| 111 |
if shrp >= 0: |
|---|
| 112 |
l = l[:shrp] |
|---|
| 113 |
l = l.strip() |
|---|
| 114 |
if not l: |
|---|
| 115 |
continue |
|---|
| 116 |
lt = tuple(re.split(spaces, l)) |
|---|
| 117 |
if len(lt) < 2 or not lt[0]: |
|---|
| 118 |
continue |
|---|
| 119 |
if lt[1].endswith("/tcp"): |
|---|
| 120 |
tdct[lt[0]] = int(lt[1].split('/')[0]) |
|---|
| 121 |
elif lt[1].endswith("/udp"): |
|---|
| 122 |
udct[lt[0]] = int(lt[1].split('/')[0]) |
|---|
| 123 |
except Exception,e: |
|---|
| 124 |
log_loading.warning("Couldn't file [%s]: line [%r] (%s)" % (filename,l,e)) |
|---|
| 125 |
f.close() |
|---|
| 126 |
except IOError: |
|---|
| 127 |
log_loading.info("Can't open /etc/services file") |
|---|
| 128 |
return tdct,udct |
|---|
| 129 |
|
|---|
| 130 |
TCP_SERVICES,UDP_SERVICES=load_services("/etc/services") |
|---|
| 131 |
|
|---|
| 132 |
class ManufDA(DADict): |
|---|
| 133 |
def fixname(self, val): |
|---|
| 134 |
return val |
|---|
| 135 |
def _get_manuf_couple(self, mac): |
|---|
| 136 |
oui = ":".join(mac.split(":")[:3]).upper() |
|---|
| 137 |
return self.__dict__.get(oui,(mac,mac)) |
|---|
| 138 |
def _get_manuf(self, mac): |
|---|
| 139 |
return self._get_manuf_couple(mac)[1] |
|---|
| 140 |
def _get_short_manuf(self, mac): |
|---|
| 141 |
return self._get_manuf_couple(mac)[0] |
|---|
| 142 |
def _resolve_MAC(self, mac): |
|---|
| 143 |
oui = ":".join(mac.split(":")[:3]).upper() |
|---|
| 144 |
if oui in self: |
|---|
| 145 |
return ":".join([self[oui][0]]+ mac.split(":")[3:]) |
|---|
| 146 |
return mac |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
def load_manuf(filename): |
|---|
| 152 |
try: |
|---|
| 153 |
manufdb=ManufDA(_name=filename) |
|---|
| 154 |
for l in open(filename): |
|---|
| 155 |
try: |
|---|
| 156 |
l = l.strip() |
|---|
| 157 |
if not l or l.startswith("#"): |
|---|
| 158 |
continue |
|---|
| 159 |
oui,shrt=l.split()[:2] |
|---|
| 160 |
i = l.find("#") |
|---|
| 161 |
if i < 0: |
|---|
| 162 |
lng=shrt |
|---|
| 163 |
else: |
|---|
| 164 |
lng = l[i+2:] |
|---|
| 165 |
manufdb[oui] = shrt,lng |
|---|
| 166 |
except Exception,e: |
|---|
| 167 |
log_loading.warning("Couldn't parse one line from [%s] [%r] (%s)" % (filename, l, e)) |
|---|
| 168 |
except IOError: |
|---|
| 169 |
|
|---|
| 170 |
pass |
|---|
| 171 |
return manufdb |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
class KnowledgeBase: |
|---|
| 182 |
def __init__(self, filename): |
|---|
| 183 |
self.filename = filename |
|---|
| 184 |
self.base = None |
|---|
| 185 |
|
|---|
| 186 |
def lazy_init(self): |
|---|
| 187 |
self.base = "" |
|---|
| 188 |
|
|---|
| 189 |
def reload(self, filename = None): |
|---|
| 190 |
if filename is not None: |
|---|
| 191 |
self.filename = filename |
|---|
| 192 |
oldbase = self.base |
|---|
| 193 |
self.base = None |
|---|
| 194 |
self.lazy_init() |
|---|
| 195 |
if self.base is None: |
|---|
| 196 |
self.base = oldbase |
|---|
| 197 |
|
|---|
| 198 |
def get_base(self): |
|---|
| 199 |
if self.base is None: |
|---|
| 200 |
self.lazy_init() |
|---|
| 201 |
return self.base |
|---|
| 202 |
|
|---|
| 203 |
|
|---|