| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Copyright (C) 2005-2006 Insecure.Com LLC. |
|---|
| 5 | # Copyright (C) 2007-2008 Adriano Monteiro Marques |
|---|
| 6 | # |
|---|
| 7 | # Author: Adriano Monteiro Marques <adriano@umitproject.org> |
|---|
| 8 | # João Paulo de Souza Medeiros <ignotus21@gmail.com> |
|---|
| 9 | # Luis A. Bastiao Silva <luis.kop@gmail.com> |
|---|
| 10 | # |
|---|
| 11 | # This program is free software; you can redistribute it and/or modify |
|---|
| 12 | # it under the terms of the GNU General Public License as published by |
|---|
| 13 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 14 | # (at your option) any later version. |
|---|
| 15 | # |
|---|
| 16 | # This program is distributed in the hope that it will be useful, |
|---|
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 19 | # GNU General Public License for more details. |
|---|
| 20 | # |
|---|
| 21 | # You should have received a copy of the GNU General Public License |
|---|
| 22 | # along with this program; if not, write to the Free Software |
|---|
| 23 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 24 | |
|---|
| 25 | import re |
|---|
| 26 | import os |
|---|
| 27 | import time |
|---|
| 28 | |
|---|
| 29 | from xml.sax import make_parser |
|---|
| 30 | from xml.sax.handler import ContentHandler |
|---|
| 31 | from xml.sax.saxutils import XMLGenerator |
|---|
| 32 | from xml.sax.xmlreader import AttributesImpl as Attributes |
|---|
| 33 | |
|---|
| 34 | from umitCore.I18N import _ |
|---|
| 35 | from umitCore.UmitLogging import log |
|---|
| 36 | |
|---|
| 37 | months = ('',_('January'), |
|---|
| 38 | _('February'), |
|---|
| 39 | _('March'), |
|---|
| 40 | _('April'), |
|---|
| 41 | _('May'), |
|---|
| 42 | _('June'), |
|---|
| 43 | _('July'), |
|---|
| 44 | _('August'), |
|---|
| 45 | _('September'), |
|---|
| 46 | _('October'), |
|---|
| 47 | _('November'), |
|---|
| 48 | _('December'),) |
|---|
| 49 | |
|---|
| 50 | class HostInfo(object): |
|---|
| 51 | def __init__(self, id): |
|---|
| 52 | self.id = id |
|---|
| 53 | |
|---|
| 54 | # Host ID |
|---|
| 55 | def get_id(self): |
|---|
| 56 | if self._id != 0: |
|---|
| 57 | return self._id |
|---|
| 58 | raise Exception("Id was not set yet.") |
|---|
| 59 | |
|---|
| 60 | def set_id(self, id): |
|---|
| 61 | try: |
|---|
| 62 | self._id = int(id) |
|---|
| 63 | except (TypeError, ValueError): |
|---|
| 64 | raise Exception("Invalid id! It must represent an integer, " |
|---|
| 65 | "received %r" % id) |
|---|
| 66 | |
|---|
| 67 | # TCP SEQUENCE |
|---|
| 68 | def set_tcpsequence(self, sequence): |
|---|
| 69 | if isinstance(sequence, list): |
|---|
| 70 | self._tcpsequence = sequence[0] |
|---|
| 71 | else: |
|---|
| 72 | self._tcpsequence = sequence |
|---|
| 73 | |
|---|
| 74 | def get_tcpsequence(self): |
|---|
| 75 | if self._tcpsequence: |
|---|
| 76 | return self._tcpsequence |
|---|
| 77 | return {} |
|---|
| 78 | |
|---|
| 79 | # TCPTS SEQUENCE |
|---|
| 80 | def set_tcptssequence(self, sequence): |
|---|
| 81 | if isinstance(sequence, list): |
|---|
| 82 | self._tcptssequence = sequence[0] |
|---|
| 83 | else: |
|---|
| 84 | self._tcptssequence = sequence |
|---|
| 85 | |
|---|
| 86 | def get_tcptssequence(self): |
|---|
| 87 | if self._tcptssequence: |
|---|
| 88 | return self._tcptssequence |
|---|
| 89 | return {} |
|---|
| 90 | |
|---|
| 91 | # VENDOR |
|---|
| 92 | def get_vendor(self): |
|---|
| 93 | try:return self._mac['vendor'] |
|---|
| 94 | except:return _('Unknown') |
|---|
| 95 | |
|---|
| 96 | # IP ID SEQUENCE |
|---|
| 97 | def set_ipidsequence(self, sequence): |
|---|
| 98 | if isinstance(sequence, list): |
|---|
| 99 | self._ipidsequence = sequence[0] |
|---|
| 100 | else: |
|---|
| 101 | self._ipidsequence = sequence |
|---|
| 102 | |
|---|
| 103 | def get_ipidsequence(self): |
|---|
| 104 | if self._ipidsequence: |
|---|
| 105 | return self._ipidsequence |
|---|
| 106 | return {} |
|---|
| 107 | |
|---|
| 108 | # OS CLASSES |
|---|
| 109 | def set_osclasses(self, classes): |
|---|
| 110 | self._osclasses = classes |
|---|
| 111 | |
|---|
| 112 | def get_osclasses(self): |
|---|
| 113 | return self._osclasses |
|---|
| 114 | |
|---|
| 115 | # OS MATCHES |
|---|
| 116 | def set_osmatches(self, matches): |
|---|
| 117 | if isinstance(matches, list): |
|---|
| 118 | self._osmatches = matches |
|---|
| 119 | |
|---|
| 120 | def get_osmatches(self): |
|---|
| 121 | if self._osmatches: |
|---|
| 122 | return self._osmatches |
|---|
| 123 | return [] |
|---|
| 124 | |
|---|
| 125 | # OS MATCH |
|---|
| 126 | def set_osmatch(self, match): |
|---|
| 127 | if isinstance(match, list): |
|---|
| 128 | self._osmatch = match[0] |
|---|
| 129 | else: |
|---|
| 130 | self._osmatch = match |
|---|
| 131 | |
|---|
| 132 | def get_osmatch(self): |
|---|
| 133 | if self._osmatch: |
|---|
| 134 | return self._osmatch |
|---|
| 135 | return {} |
|---|
| 136 | |
|---|
| 137 | # OS FINGERPRINT |
|---|
| 138 | def set_osfingerprint(self, fingerprint): |
|---|
| 139 | if isinstance(fingerprint, list): |
|---|
| 140 | self._osfingerprint = fingerprint[0] |
|---|
| 141 | else: |
|---|
| 142 | self._osfingerprint = fingerprint |
|---|
| 143 | |
|---|
| 144 | def get_osfingerprint(self): |
|---|
| 145 | if self._osfingerprint: |
|---|
| 146 | return self._osfingerprint |
|---|
| 147 | return {} |
|---|
| 148 | |
|---|
| 149 | # PORTS USED |
|---|
| 150 | def set_ports_used(self, ports): |
|---|
| 151 | self._ports_used = ports |
|---|
| 152 | |
|---|
| 153 | def get_ports_used(self): |
|---|
| 154 | return self._ports_used |
|---|
| 155 | |
|---|
| 156 | # TRACEROUTE |
|---|
| 157 | def set_trace(self, trace): |
|---|
| 158 | self._trace = trace |
|---|
| 159 | |
|---|
| 160 | def get_trace(self): |
|---|
| 161 | return self._trace |
|---|
| 162 | |
|---|
| 163 | def set_hops(self, hops): |
|---|
| 164 | self._hops = hops |
|---|
| 165 | |
|---|
| 166 | def get_hops(self): |
|---|
| 167 | return self._hops |
|---|
| 168 | |
|---|
| 169 | def get_hop_by_ttl(self, ttl): |
|---|
| 170 | for hop in self._hops: |
|---|
| 171 | if ttl == int(hop['ttl']): |
|---|
| 172 | return hop |
|---|
| 173 | return None |
|---|
| 174 | def get_number_of_hops(self): |
|---|
| 175 | count = 0 |
|---|
| 176 | for hop in self._hops: |
|---|
| 177 | if int(hop['ttl']) > count: |
|---|
| 178 | count = int(hop['ttl']) |
|---|
| 179 | return count |
|---|
| 180 | |
|---|
| 181 | # UPTIME |
|---|
| 182 | # FORMAT: {"seconds":"", "lastboot":""} |
|---|
| 183 | def set_uptime(self, uptime): |
|---|
| 184 | self._uptime = uptime |
|---|
| 185 | |
|---|
| 186 | def get_uptime(self): |
|---|
| 187 | if self._uptime: |
|---|
| 188 | return self._uptime |
|---|
| 189 | |
|---|
| 190 | # Avoid empty dict return |
|---|
| 191 | return {"seconds":"", "lastboot":""} |
|---|
| 192 | |
|---|
| 193 | # PORTS |
|---|
| 194 | def set_ports(self, port_list): |
|---|
| 195 | self._ports = port_list |
|---|
| 196 | |
|---|
| 197 | def get_ports(self): |
|---|
| 198 | return self._ports |
|---|
| 199 | |
|---|
| 200 | def set_extraports(self, port_list): |
|---|
| 201 | self._extraports = port_list |
|---|
| 202 | |
|---|
| 203 | def get_extraports(self): |
|---|
| 204 | return self._extraports |
|---|
| 205 | |
|---|
| 206 | # HOSTNAMES |
|---|
| 207 | def set_hostnames(self, hostname_list): |
|---|
| 208 | self._hostnames = hostname_list |
|---|
| 209 | |
|---|
| 210 | def get_hostnames(self): |
|---|
| 211 | return self._hostnames |
|---|
| 212 | |
|---|
| 213 | # IP |
|---|
| 214 | def set_ip_address(self, addr): |
|---|
| 215 | log.warning(_("umitCore.NmapParser.set_ip_address deprecated! Use \ |
|---|
| 216 | umitCore.NmapParser.set_ip instead.")) |
|---|
| 217 | self.set_ip(addr) |
|---|
| 218 | |
|---|
| 219 | def get_ip_address(self): |
|---|
| 220 | log.warning(_("umitCore.NmapParser.get_ip_address deprecated! Use \ |
|---|
| 221 | umitCore.NmapParser.get_ip instead.")) |
|---|
| 222 | return self.get_ip() |
|---|
| 223 | |
|---|
| 224 | def set_ip(self, addr): |
|---|
| 225 | self._ip = addr |
|---|
| 226 | |
|---|
| 227 | def get_ip(self): |
|---|
| 228 | return self._ip |
|---|
| 229 | |
|---|
| 230 | # COMMENT |
|---|
| 231 | def get_comment(self): |
|---|
| 232 | return self._comment |
|---|
| 233 | |
|---|
| 234 | def set_comment(self, comment): |
|---|
| 235 | self._comment = comment |
|---|
| 236 | |
|---|
| 237 | # MAC |
|---|
| 238 | def set_mac_address(self, addr): |
|---|
| 239 | log.warning(_("umitCore.NmapParser.set_mac_address deprecated! Use \ |
|---|
| 240 | umitCore.NmapParser.set_mac instead.")) |
|---|
| 241 | self.set_mac(addr) |
|---|
| 242 | |
|---|
| 243 | def get_mac_address(self): |
|---|
| 244 | log.warning(_("umitCore.NmapParser.get_mac_address deprecated! Use \ |
|---|
| 245 | umitCore.NmapParser.get_mac instead.")) |
|---|
| 246 | return self.get_mac() |
|---|
| 247 | |
|---|
| 248 | def set_mac(self, addr): |
|---|
| 249 | self._mac = addr |
|---|
| 250 | |
|---|
| 251 | def get_mac(self): |
|---|
| 252 | return self._mac |
|---|
| 253 | |
|---|
| 254 | # IPv6 |
|---|
| 255 | def set_ipv6_address(self, addr): |
|---|
| 256 | log.warning(_("umitCore.NmapParser.set_ipv6_address deprecated! Use \ |
|---|
| 257 | umitCore.NmapParser.set_ipv6 instead.")) |
|---|
| 258 | self.set_ipv6(addr) |
|---|
| 259 | |
|---|
| 260 | def get_ipv6_address(self): |
|---|
| 261 | log.warning(_("umitCore.NmapParser.get_ipv6_address deprecated! Use \ |
|---|
| 262 | umitCore.NmapParser.get_ipv6 instead.")) |
|---|
| 263 | return self.get_ipv6() |
|---|
| 264 | |
|---|
| 265 | def set_ipv6(self, addr): |
|---|
| 266 | self._ipv6 = addr |
|---|
| 267 | |
|---|
| 268 | def get_ipv6(self): |
|---|
| 269 | return self._ipv6 |
|---|
| 270 | |
|---|
| 271 | # STATE |
|---|
| 272 | def set_state(self, status): |
|---|
| 273 | self._state = status |
|---|
| 274 | |
|---|
| 275 | def get_state(self): |
|---|
| 276 | return self._state |
|---|
| 277 | |
|---|
| 278 | # HOSTNAME |
|---|
| 279 | def get_hostname(self): |
|---|
| 280 | hostname = '' |
|---|
| 281 | try: |
|---|
| 282 | hostname = self._hostnames[0]['hostname'] + ' ' |
|---|
| 283 | except: |
|---|
| 284 | pass |
|---|
| 285 | |
|---|
| 286 | # FIXME: Check if i can return the 'addr' key directly from get_ip, |
|---|
| 287 | # get_ipv6 and get_mac |
|---|
| 288 | if self.ip: |
|---|
| 289 | hostname += self._ip['addr'] |
|---|
| 290 | elif self.ipv6: |
|---|
| 291 | hostname += self._ipv6['addr'] |
|---|
| 292 | elif self.mac: |
|---|
| 293 | hostname += self._mac['addr'] |
|---|
| 294 | else: |
|---|
| 295 | hostname = _('Unknown Host') |
|---|
| 296 | |
|---|
| 297 | return hostname |
|---|
| 298 | |
|---|
| 299 | def get_open_ports(self): |
|---|
| 300 | ports = self.get_ports() |
|---|
| 301 | open = 0 |
|---|
| 302 | |
|---|
| 303 | for i in ports: |
|---|
| 304 | port = i['port'] |
|---|
| 305 | for p in port: |
|---|
| 306 | if re.findall('open', p['port_state']): |
|---|
| 307 | open+=1 |
|---|
| 308 | |
|---|
| 309 | return open |
|---|
| 310 | |
|---|
| 311 | def get_filtered_ports(self): |
|---|
| 312 | ports = self.get_ports() |
|---|
| 313 | extraports = self.get_extraports() |
|---|
| 314 | filtered = 0 |
|---|
| 315 | |
|---|
| 316 | for i in ports: |
|---|
| 317 | port = i['port'] |
|---|
| 318 | for p in port: |
|---|
| 319 | if re.findall('filtered', p['port_state']): |
|---|
| 320 | filtered+=1 |
|---|
| 321 | for extra in extraports: |
|---|
| 322 | if extra["state"] == "filtered": |
|---|
| 323 | filtered += int(extra["count"]) |
|---|
| 324 | return filtered |
|---|
| 325 | |
|---|
| 326 | def get_closed_ports(self): |
|---|
| 327 | ports = self.get_ports() |
|---|
| 328 | extraports = self.get_extraports() |
|---|
| 329 | closed = 0 |
|---|
| 330 | |
|---|
| 331 | for i in ports: |
|---|
| 332 | port = i['port'] |
|---|
| 333 | for p in port: |
|---|
| 334 | if re.findall('closed', p['port_state']): |
|---|
| 335 | closed+=1 |
|---|
| 336 | for extra in extraports: |
|---|
| 337 | if extra["state"] == "closed": |
|---|
| 338 | closed += int(extra["count"]) |
|---|
| 339 | return closed |
|---|
| 340 | |
|---|
| 341 | def get_scanned_ports(self): |
|---|
| 342 | ports = self.get_ports() |
|---|
| 343 | extraports = self.get_extraports() |
|---|
| 344 | scanned = 0 |
|---|
| 345 | |
|---|
| 346 | for i in ports: |
|---|
| 347 | port = i['port'] |
|---|
| 348 | for p in port: |
|---|
| 349 | scanned+=1 |
|---|
| 350 | for extra in extraports: |
|---|
| 351 | scanned += int(extra["count"]) |
|---|
| 352 | return scanned |
|---|
| 353 | |
|---|
| 354 | def get_services(self): |
|---|
| 355 | services = [] |
|---|
| 356 | for port in self.ports: |
|---|
| 357 | for p in port.get("port", []): |
|---|
| 358 | services.append({"service_name":p.get("service_name", |
|---|
| 359 | _("unknown")), |
|---|
| 360 | "portid":p.get("portid", ""), |
|---|
| 361 | "service_version":p.get("service_version", |
|---|
| 362 | _("Unknown version")), |
|---|
| 363 | "service_product":p.get("service_product", ""), |
|---|
| 364 | "port_state":p.get("port_state", _("Unknown")), |
|---|
| 365 | "protocol":p.get("protocol", "")}) |
|---|
| 366 | return services |
|---|
| 367 | |
|---|
| 368 | # Properties |
|---|
| 369 | id = property(get_id, set_id) |
|---|
| 370 | tcpsequence = property(get_tcpsequence, set_tcpsequence) |
|---|
| 371 | osclasses = property(get_osclasses, set_osclasses) |
|---|
| 372 | osmatch = property(get_osmatch, set_osmatch) |
|---|
| 373 | osmatches = property(get_osmatches, set_osclasses) |
|---|
| 374 | osfingerprint = property(get_osfingerprint, set_osfingerprint) |
|---|
| 375 | ports = property(get_ports, set_ports) |
|---|
| 376 | ports_used = property(get_ports_used, set_ports_used) |
|---|
| 377 | extraports = property(get_extraports, set_extraports) |
|---|
| 378 | uptime = property(get_uptime, set_uptime) |
|---|
| 379 | hostnames = property(get_hostnames, set_hostnames) |
|---|
| 380 | tcptssequence = property(get_tcptssequence, set_tcptssequence) |
|---|
| 381 | ipidsequence = property(get_ipidsequence, set_ipidsequence) |
|---|
| 382 | ip = property(get_ip, set_ip) |
|---|
| 383 | ipv6 = property(get_ipv6, set_ipv6) |
|---|
| 384 | mac = property(get_mac, set_mac) |
|---|
| 385 | state = property(get_state, set_state) |
|---|
| 386 | comment = property(get_comment, set_comment) |
|---|
| 387 | services = property(get_services) |
|---|
| 388 | trace = property(get_trace, set_trace) |
|---|
| 389 | hops = property(get_hops, set_hops) |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | _id = 0 |
|---|
| 393 | _tcpsequence = {} |
|---|
| 394 | _osclasses = [] |
|---|
| 395 | _osmatch = [] |
|---|
| 396 | _osmatches = [] |
|---|
| 397 | _osfingerprint = {} |
|---|
| 398 | _ports = [] |
|---|
| 399 | _ports_used = [] |
|---|
| 400 | _extraports = [] |
|---|
| 401 | _uptime = {} |
|---|
| 402 | _hostnames = [] |
|---|
| 403 | _tcptssequence = {} |
|---|
| 404 | _ipidsequence = {} |
|---|
| 405 | _ip = {} |
|---|
| 406 | _ipv6 = {} |
|---|
| 407 | _mac = {} |
|---|
| 408 | _state = '' |
|---|
| 409 | _comment = '' |
|---|
| 410 | _trace = [] |
|---|
| 411 | _hops = [] |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | class ParserBasics(object): |
|---|
| 415 | def __init__ (self): |
|---|
| 416 | self.nmap = {'nmaprun':{},\ |
|---|
| 417 | 'scaninfo':[],\ |
|---|
| 418 | 'verbose':'',\ |
|---|
| 419 | 'debugging':'',\ |
|---|
| 420 | 'hosts':[],\ |
|---|
| 421 | 'runstats':{}} |
|---|
| 422 | |
|---|
| 423 | def set_host_comment(self, host_id, comment): |
|---|
| 424 | for host in self.nmap['hosts']: |
|---|
| 425 | if host.id == host_id: |
|---|
| 426 | host.comment = comment |
|---|
| 427 | break |
|---|
| 428 | else: |
|---|
| 429 | raise Exception("Comment could not be saved! Host not \ |
|---|
| 430 | found at NmapParser!") |
|---|
| 431 | |
|---|
| 432 | def get_host_comment(self, host_id): |
|---|
| 433 | for host in self.nmap.get('hosts', []): |
|---|
| 434 | if host.id == host_id: |
|---|
| 435 | return host.comment |
|---|
| 436 | else: |
|---|
| 437 | raise Exception("Comment could not be saved! Host not \ |
|---|
| 438 | found at NmapParser!") |
|---|
| 439 | |
|---|
| 440 | def get_profile(self): |
|---|
| 441 | return self.nmap['nmaprun'].get('profile', '') |
|---|
| 442 | |
|---|
| 443 | def set_profile(self, profile): |
|---|
| 444 | self.nmap['nmaprun']['profile'] = profile |
|---|
| 445 | |
|---|
| 446 | def get_profile_name(self): |
|---|
| 447 | return self.nmap['nmaprun'].get('profile_name', '') |
|---|
| 448 | |
|---|
| 449 | def set_profile_name(self, name): |
|---|
| 450 | self.nmap['nmaprun']['profile_name'] = name |
|---|
| 451 | |
|---|
| 452 | def get_profile_description(self): |
|---|
| 453 | return self.nmap['nmaprun'].get('description', '') |
|---|
| 454 | |
|---|
| 455 | def set_profile_description(self, description): |
|---|
| 456 | self.nmap['nmaprun']['description'] = description |
|---|
| 457 | |
|---|
| 458 | def get_profile_hint(self): |
|---|
| 459 | return self.nmap['nmaprun'].get('hint', '') |
|---|
| 460 | |
|---|
| 461 | def set_profile_hint(self, hint): |
|---|
| 462 | self.nmap['nmaprun']['hint'] = hint |
|---|
| 463 | |
|---|
| 464 | def get_profile_annotation(self): |
|---|
| 465 | return self.nmap['nmaprun'].get('annotation', '') |
|---|
| 466 | |
|---|
| 467 | def set_profile_annotation(self, annotation): |
|---|
| 468 | self.nmap['nmaprun']['annotation'] = annotation |
|---|
| 469 | |
|---|
| 470 | def get_profile_options(self): |
|---|
| 471 | options = self.nmap['nmaprun'].get('options', '') |
|---|
| 472 | if isinstance(options, list): |
|---|
| 473 | return ','.join(options) |
|---|
| 474 | elif isinstance(options, basestring): |
|---|
| 475 | return options |
|---|
| 476 | |
|---|
| 477 | def set_profile_options(self, options): |
|---|
| 478 | if isinstance(options, (list, basestring)): |
|---|
| 479 | self.nmap['nmaprun']['options'] = options |
|---|
| 480 | elif isinstance(options, dict): |
|---|
| 481 | self.nmap['nmaprun']['options'] = options.keys() |
|---|
| 482 | else: |
|---|
| 483 | raise Exception("Profile option error: wrong argument format! " |
|---|
| 484 | "Need a string, list or dict.") |
|---|
| 485 | |
|---|
| 486 | def get_target(self): |
|---|
| 487 | return self.nmap['nmaprun'].get('target', '') |
|---|
| 488 | |
|---|
| 489 | def set_target(self, target): |
|---|
| 490 | self.nmap['nmaprun']['target'] = target |
|---|
| 491 | |
|---|
| 492 | def get_nmap_output(self): |
|---|
| 493 | return self.nmap['nmaprun'].get('nmap_output', '') |
|---|
| 494 | |
|---|
| 495 | def set_nmap_output(self, nmap_output): |
|---|
| 496 | self.nmap['nmaprun']['nmap_output'] = nmap_output |
|---|
| 497 | |
|---|
| 498 | def get_debugging_level (self): |
|---|
| 499 | return self.nmap.get('debugging', '') |
|---|
| 500 | |
|---|
| 501 | def set_debugging_level(self, level): |
|---|
| 502 | self.nmap['debugging'] = level |
|---|
| 503 | |
|---|
| 504 | def get_verbose_level (self): |
|---|
| 505 | return self.nmap.get('verbose', '') |
|---|
| 506 | |
|---|
| 507 | def set_verbose_level(self, level): |
|---|
| 508 | self.nmap['verbose'] = level |
|---|
| 509 | |
|---|
| 510 | def get_scaninfo(self): |
|---|
| 511 | return self.nmap.get('scaninfo', '') |
|---|
| 512 | |
|---|
| 513 | def set_scaninfo(self, info): |
|---|
| 514 | self.nmap['scaninfo'] = info |
|---|
| 515 | |
|---|
| 516 | def get_services_scanned (self): |
|---|
| 517 | if self._services_scanned is None: |
|---|
| 518 | return self._services_scanned |
|---|
| 519 | |
|---|
| 520 | services = [] |
|---|
| 521 | for scan in self.nmap.get('scaninfo', []): |
|---|
| 522 | services.append(scan['services']) |
|---|
| 523 | |
|---|
| 524 | self._services_scanned = ','.join(services) |
|---|
| 525 | return self._services_scanned |
|---|
| 526 | |
|---|
| 527 | def set_services_scanned (self, services_scanned): |
|---|
| 528 | self._services_scanned = services_scanned |
|---|
| 529 | |
|---|
| 530 | def get_nmap_command (self): |
|---|
| 531 | return self._verify_output_options(self.nmap['nmaprun'].get('args', '')) |
|---|
| 532 | |
|---|
| 533 | def set_nmap_command(self, command): |
|---|
| 534 | self.nmap['nmaprun']['args'] = self._verify_output_options(command) |
|---|
| 535 | |
|---|
| 536 | def get_scan_type (self): |
|---|
| 537 | types = [] |
|---|
| 538 | for t in self.nmap.get('scaninfo', []): |
|---|
| 539 | types.append(t['type']) |
|---|
| 540 | return types |
|---|
| 541 | |
|---|
| 542 | def get_protocol (self): |
|---|
| 543 | protocols = [] |
|---|
| 544 | for proto in self.nmap.get('scaninfo', []): |
|---|
| 545 | protocols.append(proto['protocol']) |
|---|
| 546 | return protocols |
|---|
| 547 | |
|---|
| 548 | def get_num_services (self): |
|---|
| 549 | if self._num_services is None: |
|---|
| 550 | return self._num_services |
|---|
| 551 | |
|---|
| 552 | num = 0 |
|---|
| 553 | for n in self.nmap.get('scaninfo', []): |
|---|
| 554 | num += int(n['numservices']) |
|---|
| 555 | |
|---|
| 556 | self._num_services = num |
|---|
| 557 | return self._num_services |
|---|
| 558 | |
|---|
| 559 | def set_num_services (self, num_services): |
|---|
| 560 | self._num_services = num_services |
|---|
| 561 | |
|---|
| 562 | def get_date (self): |
|---|
| 563 | epoch = int(self.nmap['nmaprun'].get('start', '0')) |
|---|
| 564 | return time.localtime (epoch) |
|---|
| 565 | |
|---|
| 566 | def get_start(self): |
|---|
| 567 | return self.nmap['nmaprun'].get('start', '0') |
|---|
| 568 | |
|---|
| 569 | def set_start(self, start): |
|---|
| 570 | self.nmap['nmaprun']['start'] = start |
|---|
| 571 | |
|---|
| 572 | def set_date(self, date): |
|---|
| 573 | if isinstance(date, int): |
|---|
| 574 | self.nmap['nmaprun']['start'] = date |
|---|
| 575 | else: |
|---|
| 576 | raise Exception("Wrong date format. Date should be saved \ |
|---|
| 577 | in epoch format!") |
|---|
| 578 | |
|---|
| 579 | def get_open_ports(self): |
|---|
| 580 | ports = 0 |
|---|
| 581 | |
|---|
| 582 | for h in self.nmap.get('hosts', []): |
|---|
| 583 | ports += h.get_open_ports() |
|---|
| 584 | |
|---|
| 585 | return ports |
|---|
| 586 | |
|---|
| 587 | def get_filtered_ports(self): |
|---|
| 588 | ports = 0 |
|---|
| 589 | |
|---|
| 590 | for h in self.nmap.get('hosts', []): |
|---|
| 591 | ports += h.get_filtered_ports() |
|---|
| 592 | |
|---|
| 593 | |
|---|
| 594 | log.debug(">>> EXTRAPORTS: %s" % str(self.list_extraports)) |
|---|
| 595 | |
|---|
| 596 | return ports |
|---|
| 597 | |
|---|
| 598 | def get_closed_ports(self): |
|---|
| 599 | ports = 0 |
|---|
| 600 | |
|---|
| 601 | for h in self.nmap['hosts']: |
|---|
| 602 | ports += h.get_closed_ports() |
|---|
| 603 | |
|---|
| 604 | return ports |
|---|
| 605 | |
|---|
| 606 | def get_formated_date(self): |
|---|
| 607 | date = self.get_date() |
|---|
| 608 | return "%s %s, %s - %s:%s" % (months[date[1]], |
|---|
| 609 | str(date[2]), |
|---|
| 610 | str(date[0]), |
|---|
| 611 | str(date[3]).zfill(2), |
|---|
| 612 | str(date[4]).zfill(2)) |
|---|
| 613 | |
|---|
| 614 | def get_scanner (self): |
|---|
| 615 | return self.nmap['nmaprun'].get('scanner', '') |
|---|
| 616 | |
|---|
| 617 | def set_scanner(self, scanner): |
|---|
| 618 | self.nmap['nmaprun']['scanner'] = scanner |
|---|
| 619 | |
|---|
| 620 | def get_scanner_version (self): |
|---|
| 621 | return self.nmap['nmaprun'].get('version', '') |
|---|
| 622 | |
|---|
| 623 | def set_scanner_version(self, version): |
|---|
| 624 | self.nmap['nmaprun']['version'] = version |
|---|
| 625 | |
|---|
| 626 | # IPv4 |
|---|
| 627 | def get_ipv4_addresses (self): |
|---|
| 628 | log.warning(_("umitCore.NmapParser.get_ipv4_address deprecated! Use \ |
|---|
| 629 | umitCore.NmapParser.get_ipv4 instead.")) |
|---|
| 630 | return self.get_ipv4() |
|---|
| 631 | |
|---|
| 632 | def get_ipv4(self): |
|---|
| 633 | addresses = [] |
|---|
| 634 | for host in self.nmap.get('hosts', []): |
|---|
| 635 | try: |
|---|
| 636 | addresses.append(host.get_ip().get('addr', '')) |
|---|
| 637 | except: |
|---|
| 638 | pass |
|---|
| 639 | |
|---|
| 640 | return addresses |
|---|
| 641 | |
|---|
| 642 | # MAC |
|---|
| 643 | def get_mac_addresses (self): |
|---|
| 644 | log.warning(_("umitCore.NmapParser.get_mac_address deprecated! Use \ |
|---|
| 645 | umitCore.NmapParser.get_mac instead.")) |
|---|
| 646 | return self.get_mac() |
|---|
| 647 | |
|---|
| 648 | def get_mac(self): |
|---|
| 649 | addresses = [] |
|---|
| 650 | for host in self.nmap.get('hosts', []): |
|---|
| 651 | try: |
|---|
| 652 | addresses.append(host.get_mac().get('addr', '')) |
|---|
| 653 | except: |
|---|
| 654 | pass |
|---|
| 655 | |
|---|
| 656 | return addresses |
|---|
| 657 | |
|---|
| 658 | # IPv6 |
|---|
| 659 | def get_ipv6_addresses (self): |
|---|
| 660 | log.warning(_("umitCore.NmapParser.get_ipv6_address deprecated! Use \ |
|---|
| 661 | umitCore.NmapParser.get_ipv6 instead.")) |
|---|
| 662 | return self.get_ipv6() |
|---|
| 663 | |
|---|
| 664 | def get_ipv6(self): |
|---|
| 665 | addresses = [] |
|---|
| 666 | for host in self.nmap.get('hosts', []): |
|---|
| 667 | try: |
|---|
| 668 | addresses.append(host.get_ipv6().get('addr', '')) |
|---|
| 669 | except: |
|---|
| 670 | pass |
|---|
| 671 | |
|---|
| 672 | return addresses |
|---|
| 673 | |
|---|
| 674 | def get_hostnames (self): |
|---|
| 675 | hostnames = [] |
|---|
| 676 | for host in self.nmap.get('hosts', []): |
|---|
| 677 | hostnames += host.get_hostnames() |
|---|
| 678 | return hostnames |
|---|
| 679 | |
|---|
| 680 | def get_ports(self): |
|---|
| 681 | ports = [] |
|---|
| 682 | for port in self.nmap.get('hosts', []): |
|---|
| 683 | ports.append(port.get_ports()) |
|---|
| 684 | |
|---|
| 685 | return ports |
|---|
| 686 | |
|---|
| 687 | def get_hosts(self): |
|---|
| 688 | return self.nmap.get('hosts', None) |
|---|
| 689 | |
|---|
| 690 | # TRACEROUTE |
|---|
| 691 | def get_hops(self): |
|---|
| 692 | return self.nmap.get('hops', None) |
|---|
| 693 | |
|---|
| 694 | def get_trace(self): |
|---|
| 695 | return self.nmap.get('trace', None) |
|---|
| 696 | |
|---|
| 697 | |
|---|
| 698 | def get_runstats(self): |
|---|
| 699 | return self.nmap.get('runstats', None) |
|---|
| 700 | |
|---|
| 701 | def set_runstats(self, stats): |
|---|
| 702 | self.nmap['runstats'] = stats |
|---|
| 703 | |
|---|
| 704 | def get_hosts_down(self): |
|---|
| 705 | return int(self.nmap['runstats'].get('hosts_down', '0')) |
|---|
| 706 | |
|---|
| 707 | def set_hosts_down(self, down): |
|---|
| 708 | self.nmap['runstats']['hosts_down'] = int(down) |
|---|
| 709 | |
|---|
| 710 | def get_hosts_up(self): |
|---|
| 711 | return int(self.nmap['runstats'].get('hosts_up', '0')) |
|---|
| 712 | |
|---|
| 713 | def set_hosts_up(self, up): |
|---|
| 714 | self.nmap['runstats']['hosts_up'] = int(up) |
|---|
| 715 | |
|---|
| 716 | def get_hosts_scanned(self): |
|---|
| 717 | return int(self.nmap['runstats'].get('hosts_scanned', '0')) |
|---|
| 718 | |
|---|
| 719 | def set_hosts_scanned(self, scanned): |
|---|
| 720 | self.nmap['runstats']['hosts_scanned'] = int(scanned) |
|---|
| 721 | |
|---|
| 722 | def get_finish_time (self): |
|---|
| 723 | return time.localtime(int(self.nmap['runstats'].get('finished_time', |
|---|
| 724 | '0'))) |
|---|
| 725 | |
|---|
| 726 | def set_finish_time(self, finish): |
|---|
| 727 | self.nmap['runstats']['finished_time'] = int(finish) |
|---|
| 728 | |
|---|
| 729 | def get_finish_epoc_time(self): |
|---|
| 730 | return int(self.nmap['runstats'].get('finished_time', '0')) |
|---|
| 731 | |
|---|
| 732 | def set_finish_epoc_time(self, time): |
|---|
| 733 | self.nmap['runstats']['finished_time'] = time |
|---|
| 734 | |
|---|
| 735 | def get_scan_name(self): |
|---|
| 736 | return self.nmap.get("scan_name", "") |
|---|
| 737 | |
|---|
| 738 | def set_scan_name(self, scan_name): |
|---|
| 739 | self.nmap["scan_name"] = scan_name |
|---|
| 740 | |
|---|
| 741 | def get_formated_finish_date(self): |
|---|
| 742 | date = self.get_finish_time() |
|---|
| 743 | return "%s %s, %s - %s:%s" % (months[date[1]], |
|---|
| 744 | str(date[2]), |
|---|
| 745 | str(date[0]), |
|---|
| 746 | str(date[3]).zfill(2), |
|---|
| 747 | str(date[4]).zfill(2)) |
|---|
| 748 | |
|---|
| 749 | def _verify_output_options (self, command): |
|---|
| 750 | found = re.findall ('(-o[XGASN]{1}) {0,1}', command) |
|---|
| 751 | splited = command.split (' ') |
|---|
| 752 | |
|---|
| 753 | if found: |
|---|
| 754 | for option in found: |
|---|
| 755 | pos = splited.index(option) |
|---|
| 756 | del(splited[pos+1]) |
|---|
| 757 | del(splited[pos]) |
|---|
| 758 | |
|---|
| 759 | return ' '.join (splited) |
|---|
| 760 | |
|---|
| 761 | def get_comments(self): |
|---|
| 762 | return [host.comment for host in self.nmap['hosts']] |
|---|
| 763 | |
|---|
| 764 | profile = property(get_profile, set_profile) |
|---|
| 765 | profile_name = property(get_profile_name, set_profile_name) |
|---|
| 766 | profile_description = property(get_profile_description, |
|---|
| 767 | set_profile_description) |
|---|
| 768 | profile_hint = property(get_profile_hint, set_profile_hint) |
|---|
| 769 | profile_annotation = property(get_profile_annotation, |
|---|
| 770 | set_profile_annotation) |
|---|
| 771 | profile_options = property(get_profile_options, set_profile_options) |
|---|
| 772 | target = property(get_target, set_target) |
|---|
| 773 | nmap_output = property(get_nmap_output, set_nmap_output) |
|---|
| 774 | debugging_level = property(get_debugging_level, set_debugging_level) |
|---|
| 775 | verbose_level = property(get_verbose_level, set_verbose_level) |
|---|
| 776 | scaninfo = property(get_scaninfo, set_scaninfo) |
|---|
| 777 | services_scanned = property(get_services_scanned, set_services_scanned) |
|---|
| 778 | nmap_command = property(get_nmap_command, set_nmap_command) |
|---|
| 779 | scan_type = property(get_scan_type) |
|---|
| 780 | protocol = property(get_protocol) |
|---|
| 781 | num_services = property(get_num_services, set_num_services) |
|---|
| 782 | date = property(get_date, set_date) |
|---|
| 783 | open_ports = property(get_open_ports) |
|---|
| 784 | filtered_ports = property(get_filtered_ports) |
|---|
| 785 | closed_ports = property(get_closed_ports) |
|---|
| 786 | formated_date = property(get_formated_date) |
|---|
| 787 | scanner = property(get_scanner, set_scanner) |
|---|
| 788 | scanner_version = property(get_scanner_version, set_scanner_version) |
|---|
| 789 | ipv4 = property(get_ipv4) |
|---|
| 790 | mac = property(get_mac) |
|---|
| 791 | ipv6 = property(get_ipv6) |
|---|
| 792 | hostnames = property(get_hostnames) |
|---|
| 793 | ports = property(get_ports) |
|---|
| 794 | hosts = property(get_hosts) |
|---|
| 795 | runstats = property(get_runstats, set_runstats) |
|---|
| 796 | hosts_down = property(get_hosts_down, set_hosts_down) |
|---|
| 797 | hosts_up = property(get_hosts_up, set_hosts_up) |
|---|
| 798 | hosts_scanned = property(get_hosts_scanned, set_hosts_scanned) |
|---|
| 799 | finish_time = property(get_finish_time, set_finish_time) |
|---|
| 800 | finish_epoc_time = property(get_finish_epoc_time, set_finish_epoc_time) |
|---|
| 801 | formated_finish_date = property(get_formated_finish_date) |
|---|
| 802 | comments = property(get_comments) |
|---|
| 803 | start = property(get_start, set_start) |
|---|
| 804 | scan_name = property(get_scan_name, set_scan_name) |
|---|
| 805 | trace = property(get_trace) |
|---|
| 806 | hops = property(get_hops) |
|---|
| 807 | |
|---|
| 808 | _num_services = None |
|---|
| 809 | _services_scanned = None |
|---|
| 810 | |
|---|
| 811 | |
|---|
| 812 | class NmapParserSAX(ParserBasics, ContentHandler): |
|---|
| 813 | def __init__(self): |
|---|
| 814 | ParserBasics.__init__(self) |
|---|
| 815 | self.id_sequence = 0 |
|---|
| 816 | |
|---|
| 817 | self.in_run_stats = False |
|---|
| 818 | self.in_host = False |
|---|
| 819 | self.in_ports = False |
|---|
| 820 | self.in_port = False |
|---|
| 821 | self.in_os = False |
|---|
| 822 | self.list_extraports = [] |
|---|
| 823 | |
|---|
| 824 | # Creating a traceroute condition |
|---|
| 825 | self.in_trace = False |
|---|
| 826 | |
|---|
| 827 | self.nmap_xml_file = None |
|---|
| 828 | self.unsaved = False |
|---|
| 829 | |
|---|
| 830 | def set_parser(self, parser): |
|---|
| 831 | self.parser = parser |
|---|
| 832 | |
|---|
| 833 | def set_xml_file(self, nmap_xml_file): |
|---|
| 834 | self.nmap_xml_file = nmap_xml_file |
|---|
| 835 | |
|---|
| 836 | def parse(self): |
|---|
| 837 | if self.nmap_xml_file: |
|---|
| 838 | if isinstance(self.nmap_xml_file, basestring): |
|---|
| 839 | self.parser.parse(self.nmap_xml_file) |
|---|
| 840 | else: |
|---|
| 841 | log.debug(">>> XML content: %s" % self.nmap_xml_file.read()) |
|---|
| 842 | self.nmap_xml_file.seek(0) |
|---|
| 843 | self.parser.parse(self.nmap_xml_file) |
|---|
| 844 | |
|---|
| 845 | # Closing file to avoid problems with file descriptors |
|---|
| 846 | self.nmap_xml_file.close() |
|---|
| 847 | else: |
|---|
| 848 | raise Exception("There's no file to be parsed!") |
|---|
| 849 | |
|---|
| 850 | def _parse_nmaprun(self, attrs): |
|---|
| 851 | run_tag = "nmaprun" |
|---|
| 852 | |
|---|
| 853 | self.nmap[run_tag]["nmap_output"] = attrs.get("nmap_output", "") |
|---|
| 854 | self.nmap[run_tag]["profile"] = attrs.get("profile", "") |
|---|
| 855 | self.nmap[run_tag]["profile_name"] = attrs.get("profile_name", "") |
|---|
| 856 | self.nmap[run_tag]["hint"] = attrs.get("hint", "") |
|---|
| 857 | self.nmap[run_tag]["description"] = attrs.get("description", "") |
|---|
| 858 | self.nmap[run_tag]["annotation"] = attrs.get("annotation", "") |
|---|
| 859 | self.nmap[run_tag]["options"] = attrs.get("options", "") |
|---|
| 860 | self.nmap[run_tag]["target"] = attrs.get("target", "") |
|---|
| 861 | self.nmap[run_tag]["start"] = attrs.get("start", "") |
|---|
| 862 | self.nmap[run_tag]["args"] = attrs.get("args", "") |
|---|
| 863 | self.nmap[run_tag]["scanner"] = attrs.get("scanner", "") |
|---|
| 864 | self.nmap[run_tag]["version"] = attrs.get("version", "") |
|---|
| 865 | self.nmap[run_tag]["xmloutputversion"] = attrs.get("xmloutputversion", |
|---|
| 866 | "") |
|---|
| 867 | self.nmap["scan_name"] = attrs.get("scan_name", "") |
|---|
| 868 | |
|---|
| 869 | def _parse_scaninfo(self, attrs): |
|---|
| 870 | dic = {} |
|---|
| 871 | |
|---|
| 872 | dic["type"] = attrs.get("type", "") |
|---|
| 873 | dic["protocol"] = attrs.get("protocol", "") |
|---|
| 874 | dic["numservices"] = attrs.get("numservices", "") |
|---|
| 875 | dic["services"] = attrs.get("services", "") |
|---|
| 876 | |
|---|
| 877 | self.nmap["scaninfo"].append(dic) |
|---|
| 878 | |
|---|
| 879 | def _parse_verbose(self, attrs): |
|---|
| 880 | self.nmap["verbose"] = attrs.get("level", "") |
|---|
| 881 | |
|---|
| 882 | def _parse_debugging(self, attrs): |
|---|
| 883 | self.nmap["debugging"] = attrs.get("level", "") |
|---|
| 884 | |
|---|
| 885 | def _parse_runstats_finished(self, attrs): |
|---|
| 886 | self.nmap["runstats"]["finished_time"] = attrs.get("time", "") |
|---|
| 887 | |
|---|
| 888 | def _parse_runstats_hosts(self, attrs): |
|---|
| 889 | self.nmap["runstats"]["hosts_up"] = attrs.get("up", "") |
|---|
| 890 | self.nmap["runstats"]["hosts_down"] = attrs.get("down", "") |
|---|
| 891 | self.nmap["runstats"]["hosts_scanned"] = attrs.get("total", "") |
|---|
| 892 | |
|---|
| 893 | def generate_id(self): |
|---|
| 894 | self.id_sequence += 1 |
|---|
| 895 | return self.id_sequence |
|---|
| 896 | |
|---|
| 897 | def _parse_host(self, attrs): |
|---|
| 898 | self.host_info = HostInfo(self.generate_id()) |
|---|
| 899 | self.host_info.comment = attrs.get("comment", "") |
|---|
| 900 | |
|---|
| 901 | def _parse_host_status(self, attrs): |
|---|
| 902 | self.host_info.set_state(attrs.get("state", "")) |
|---|
| 903 | |
|---|
| 904 | def _parse_host_address(self, attrs): |
|---|
| 905 | address_attributes = {"type":attrs.get("addrtype", ""), |
|---|
| 906 | "vendor":attrs.get("vendor", ""), |
|---|
| 907 | "addr":attrs.get("addr", "")} |
|---|
| 908 | |
|---|
| 909 | if address_attributes["type"] == "ipv4": |
|---|
| 910 | self.host_info.set_ip(address_attributes) |
|---|
| 911 | elif address_attributes["type"] == "ipv6": |
|---|
| 912 | self.host_info.set_ipv6(address_attributes) |
|---|
| 913 | elif address_attributes["type"] == "mac": |
|---|
| 914 | self.host_info.set_mac(address_attributes) |
|---|
| 915 | |
|---|
| 916 | def _parse_host_hostname(self, attrs): |
|---|
| 917 | self.list_hostnames.append({"hostname":attrs.get("name", ""), |
|---|
| 918 | "hostname_type":attrs.get("type", "")}) |
|---|
| 919 | |
|---|
| 920 | def _parse_host_extraports(self, attrs): |
|---|
| 921 | self.list_extraports.append({"state":attrs.get("state", ""), |
|---|
| 922 | "count":attrs.get("count", "")}) |
|---|
| 923 | |
|---|
| 924 | def _parse_host_port(self, attrs): |
|---|
| 925 | self.dic_port = {"protocol":attrs.get("protocol", ""), |
|---|
| 926 | "portid":attrs.get("portid", "")} |
|---|
| 927 | |
|---|
| 928 | def _parse_host_port_state(self, attrs): |
|---|
| 929 | self.dic_port["port_state"] = attrs.get("state", "") |
|---|
| 930 | |
|---|
| 931 | def _parse_host_port_service(self, attrs): |
|---|
| 932 | self.dic_port["service_name"] = attrs.get("name", "") |
|---|
| 933 | self.dic_port["service_method"] = attrs.get("method", "") |
|---|
| 934 | self.dic_port["service_conf"] = attrs.get("conf", "") |
|---|
| 935 | self.dic_port["service_product"] = attrs.get("product", "") |
|---|
| 936 | self.dic_port["service_version"] = attrs.get("version", "") |
|---|
| 937 | self.dic_port["service_extrainfo"] = attrs.get("extrainfo", "") |
|---|
| 938 | |
|---|
| 939 | def _parse_host_osmatch(self, attrs): |
|---|
| 940 | tmp = self._parsing(attrs, ['name', 'accuracy']) |
|---|
| 941 | if tmp != {} and self.list_osmatches == []: |
|---|
| 942 | self.host_info.set_osmatch(tmp) |
|---|
| 943 | elif tmp != {} and tmp.has_key('accuracy'): |
|---|
| 944 | last_osmatch = self.host_info.get_osmatch() |
|---|
| 945 | if last_osmatch.has_key('accuracy') and \ |
|---|
| 946 | tmp['accuracy'] > last_osmatch['accuracy']: |
|---|
| 947 | self.host_info.set_osmatch(tmp) |
|---|
| 948 | |
|---|
| 949 | self.list_osmatches.append(tmp) |
|---|
| 950 | |
|---|
| 951 | def _parse_host_portused(self, attrs): |
|---|
| 952 | self.list_portused.append(self._parsing(attrs, |
|---|
| 953 | ['state','proto','portid'])) |
|---|
| 954 | |
|---|
| 955 | def _parse_host_osclass(self, attrs): |
|---|
| 956 | self.list_osclass.append(self._parsing(attrs, ['type', |
|---|
| 957 | 'vendor', |
|---|
| 958 | 'osfamily', |
|---|
| 959 | 'osgen', |
|---|
| 960 | 'accuracy'])) |
|---|
| 961 | |
|---|
| 962 | def _parse_host_osfingerprint(self, attrs): |
|---|
| 963 | self.host_info.set_osfingerprint(self._parsing(attrs, ['fingerprint'])) |
|---|
| 964 | |
|---|
| 965 | |
|---|
| 966 | def _parsing(self, attrs, attrs_list): |
|---|
| 967 | # Returns a dict with the attributes of a given tag with the |
|---|
| 968 | # atributes names as keys and their respective values |
|---|
| 969 | dic = {} |
|---|
| 970 | for at in attrs_list: |
|---|
| 971 | dic[at] = attrs.get(at, "") |
|---|
| 972 | return dic |
|---|
| 973 | |
|---|
| 974 | def _parse_host_uptime(self, attrs): |
|---|
| 975 | self.host_info.set_uptime(self._parsing(attrs, ["seconds", "lastboot"])) |
|---|
| 976 | |
|---|
| 977 | |
|---|
| 978 | def _parse_host_tcpsequence(self, attrs): |
|---|
| 979 | self.host_info.set_tcpsequence(self._parsing(attrs, ['index', |
|---|
| 980 | 'class', |
|---|
| 981 | 'difficulty', |
|---|
| 982 | 'values'])) |
|---|
| 983 | |
|---|
| 984 | def _parse_host_tcptssequence(self, attrs): |
|---|
| 985 | self.host_info.set_tcptssequence(self._parsing(attrs, ['class', |
|---|
| 986 | 'values'])) |
|---|
| 987 | |
|---|
| 988 | def _parse_host_ipidsequence(self, attrs): |
|---|
| 989 | self.host_info.set_ipidsequence(self._parsing(attrs, ['class', |
|---|
| 990 | 'values'])) |
|---|
| 991 | def _parse_host_trace(self, attrs): |
|---|
| 992 | self.host_info.set_trace(self._parsing(attrs, ['port', 'proto'])) |
|---|
| 993 | |
|---|
| 994 | def _parse_host_trace_hop(self, attrs): |
|---|
| 995 | tmp = self._parsing(attrs, ['ttl', 'rtt', 'ipaddr', 'host']) |
|---|
| 996 | self.list_hop.append(tmp) |
|---|
| 997 | |
|---|
| 998 | |
|---|
| 999 | def startElement(self, name, attrs): |
|---|
| 1000 | if name == "nmaprun": |
|---|
| 1001 | self._parse_nmaprun(attrs) |
|---|
| 1002 | elif name == "scaninfo": |
|---|
| 1003 | self._parse_scaninfo(attrs) |
|---|
| 1004 | elif name == "verbose": |
|---|
| 1005 | self._parse_verbose(attrs) |
|---|
| 1006 | elif name == "debugging": |
|---|
| 1007 | self._parse_debugging(attrs) |
|---|
| 1008 | elif name == "runstats": |
|---|
| 1009 | self.in_run_stats = True |
|---|
| 1010 | elif self.in_run_stats and name == "finished": |
|---|
| 1011 | self._parse_runstats_finished(attrs) |
|---|
| 1012 | elif self.in_run_stats and name == "hosts": |
|---|
| 1013 | self._parse_runstats_hosts(attrs) |
|---|
| 1014 | elif name == "host": |
|---|
| 1015 | self.in_host = True |
|---|
| 1016 | self._parse_host(attrs) |
|---|
| 1017 | self.list_ports = [] |
|---|
| 1018 | elif self.in_host and name == "status": |
|---|
| 1019 | self._parse_host_status(attrs) |
|---|
| 1020 | elif self.in_host and name == "address": |
|---|
| 1021 | self._parse_host_address(attrs) |
|---|
| 1022 | elif self.in_host and name == "hostnames": |
|---|
| 1023 | self.in_hostnames = True |
|---|
| 1024 | self.list_hostnames = [] |
|---|
| 1025 | elif self.in_host and self.in_hostnames and name == "hostname": |
|---|
| 1026 | self._parse_host_hostname(attrs) |
|---|
| 1027 | elif self.in_host and name == "ports": |
|---|
| 1028 | self.list_extraports = [] |
|---|
| 1029 | self.list_port = [] |
|---|
| 1030 | self.in_ports = True |
|---|
| 1031 | elif self.in_host and self.in_ports and name == "extraports": |
|---|
| 1032 | self._parse_host_extraports(attrs) |
|---|
| 1033 | elif self.in_host and self.in_ports and name == "port": |
|---|
| 1034 | self.in_port = True |
|---|
| 1035 | self._parse_host_port(attrs) |
|---|
| 1036 | elif self.in_host and self.in_ports and \ |
|---|
| 1037 | self.in_port and name == "state": |
|---|
| 1038 | self._parse_host_port_state(attrs) |
|---|
| 1039 | elif self.in_host and self.in_ports and \ |
|---|
| 1040 | self.in_port and name == "service": |
|---|
| 1041 | self._parse_host_port_service(attrs) |
|---|
| 1042 | elif self.in_host and name == "os": |
|---|
| 1043 | self.in_os = True |
|---|
| 1044 | self.list_portused = [] |
|---|
| 1045 | self.list_osclass = [] |
|---|
| 1046 | self.list_osmatches = [] |
|---|
| 1047 | elif self.in_host and self.in_os and name == "osmatch": |
|---|
| 1048 | self._parse_host_osmatch(attrs) |
|---|
| 1049 | elif self.in_host and self.in_os and name == "portused": |
|---|
| 1050 | self._parse_host_portused(attrs) |
|---|
| 1051 | elif self.in_host and self.in_os and name == "osclass": |
|---|
| 1052 | self._parse_host_osclass(attrs) |
|---|
| 1053 | elif self.in_host and self.in_os and name == "osfingerprint": |
|---|
| 1054 | self._parse_host_osfingerprint(attrs) |
|---|
| 1055 | elif self.in_host and name == "uptime": |
|---|
| 1056 | self._parse_host_uptime(attrs) |
|---|
| 1057 | elif self.in_host and name == "tcpsequence": |
|---|
| 1058 | self._parse_host_tcpsequence(attrs) |
|---|
| 1059 | elif self.in_host and name == "tcptssequence": |
|---|
| 1060 | self._parse_host_tcptssequence(attrs) |
|---|
| 1061 | elif self.in_host and name == "ipidsequence": |
|---|
| 1062 | self._parse_host_ipidsequence(attrs) |
|---|
| 1063 | # Creating a traceroute condition |
|---|
| 1064 | elif self.in_host and name == "trace": |
|---|
| 1065 | self.in_trace = True |
|---|
| 1066 | self.list_hop = [] |
|---|
| 1067 | self._parse_host_trace(attrs) |
|---|
| 1068 | elif self.in_trace and name == "hop": |
|---|
| 1069 | self._parse_host_trace_hop(attrs) |
|---|
| 1070 | |
|---|
| 1071 | |
|---|
| 1072 | def endElement(self, name): |
|---|
| 1073 | if name == "runstats": |
|---|
| 1074 | self.in_run_stats = False |
|---|
| 1075 | elif name == "host": |
|---|
| 1076 | self.in_host = False |
|---|
| 1077 | self.host_info.set_ports(self.list_ports) |
|---|
| 1078 | self.nmap["hosts"].append(self.host_info) |
|---|
| 1079 | del(self.list_ports) |
|---|
| 1080 | elif self.in_host and name == "hostnames": |
|---|
| 1081 | self.in_hostnames = False |
|---|
| 1082 | self.host_info.set_hostnames(self.list_hostnames) |
|---|
| 1083 | elif self.in_host and name == "ports": |
|---|
| 1084 | self.in_ports = False |
|---|
| 1085 | self.list_ports.append({"extraports":self.list_extraports, |
|---|
| 1086 | "port":self.list_port}) |
|---|
| 1087 | self.host_info.set_extraports(self.list_extraports) |
|---|
| 1088 | elif self.in_host and self.in_ports and name == "port": |
|---|
| 1089 | self.in_port = False |
|---|
| 1090 | self.list_port.append(self.dic_port) |
|---|
| 1091 | del(self.dic_port) |
|---|
| 1092 | elif self.in_host and self.in_os and name == "os": |
|---|
| 1093 | self.in_os = False |
|---|
| 1094 | self.host_info.set_ports_used(self.list_portused) |
|---|
| 1095 | self.host_info.set_osclasses(self.list_osclass) |
|---|
| 1096 | self.host_info.set_osmatches(self.list_osmatches) |
|---|
| 1097 | |
|---|
| 1098 | del(self.list_portused) |
|---|
| 1099 | del(self.list_osclass) |
|---|
| 1100 | del(self.list_osmatches) |
|---|
| 1101 | |
|---|
| 1102 | # Creating a traceroute condition |
|---|
| 1103 | elif self.in_host and name == "trace": |
|---|
| 1104 | self.in_trace = False |
|---|
| 1105 | self.host_info.set_hops(self.list_hop) |
|---|
| 1106 | |
|---|
| 1107 | del(self.list_hop) |
|---|
| 1108 | |
|---|
| 1109 | |
|---|
| 1110 | def write_xml(self, xml_file): |
|---|
| 1111 | xml_file = self._verify_file(xml_file) |
|---|
| 1112 | self.write_parser = XMLGenerator(xml_file) |
|---|
| 1113 | |
|---|
| 1114 | # First, start the document: |
|---|
| 1115 | self.write_parser.startDocument() |
|---|
| 1116 | |
|---|
| 1117 | # Nmaprun element: |
|---|
| 1118 | self._write_nmaprun() |
|---|
| 1119 | |
|---|
| 1120 | # Scaninfo element: |
|---|
| 1121 | self._write_scaninfo() |
|---|
| 1122 | |
|---|
| 1123 | # Verbose element: |
|---|
| 1124 | self._write_verbose() |
|---|
| 1125 | |
|---|
| 1126 | # Debugging element: |
|---|
| 1127 | self._write_debugging() |
|---|
| 1128 | |
|---|
| 1129 | # Hosts elements: |
|---|
| 1130 | self._write_hosts() |
|---|
| 1131 | |
|---|
| 1132 | # Runstats element: |
|---|
| 1133 | self._write_runstats() |
|---|
| 1134 | |
|---|
| 1135 | # End of the xml file: |
|---|
| 1136 | self.write_parser.endElement("nmaprun") |
|---|
| 1137 | self.write_parser.endDocument() |
|---|
| 1138 | |
|---|
| 1139 | def _write_runstats(self): |
|---|
| 1140 | ################## |
|---|
| 1141 | # Runstats element |
|---|
| 1142 | self.write_parser.startElement("runstats", Attributes(dict())) |
|---|
| 1143 | |
|---|
| 1144 | ## Finished element |
|---|
| 1145 | self.write_parser.startElement("finished", |
|---|
| 1146 | Attributes(dict(time = str(self.finish_epoc_time)))) |
|---|
| 1147 | self.write_parser.endElement("finished") |
|---|
| 1148 | |
|---|
| 1149 | ## Hosts element |
|---|
| 1150 | self.write_parser.startElement("hosts", |
|---|
| 1151 | Attributes(dict(up = str(self.hosts_up), |
|---|
| 1152 | down = str(self.hosts_down), |
|---|
| 1153 | total = str(self.hosts_scanned)))) |
|---|
| 1154 | self.write_parser.endElement("hosts") |
|---|
| 1155 | |
|---|
| 1156 | |
|---|
| 1157 | self.write_parser.endElement("runstats") |
|---|
| 1158 | # End of Runstats element |
|---|
| 1159 | ######################### |
|---|
| 1160 | |
|---|
| 1161 | def _write_hosts(self): |
|---|
| 1162 | for host in self.hosts: |
|---|
| 1163 | # Start host element |
|---|
| 1164 | self.write_parser.startElement("host", |
|---|
| 1165 | Attributes(dict(comment=host.comment))) |
|---|
| 1166 | |
|---|
| 1167 | # Status element |
|---|
| 1168 | self.write_parser.startElement("status", |
|---|
| 1169 | Attributes(dict(state=host.state))) |
|---|
| 1170 | self.write_parser.endElement("status") |
|---|
| 1171 | |
|---|
| 1172 | |
|---|
| 1173 | ################## |
|---|
| 1174 | # Address elements |
|---|
| 1175 | ## IPv4 |
|---|
| 1176 | if isinstance(host.ip, dict): |
|---|
| 1177 | ## Remove None value items |
|---|
| 1178 | self.__remove_none_keys(host.ip) |
|---|
| 1179 | self.write_parser.startElement("address", |
|---|
| 1180 | Attributes(dict(addr=host.ip.get("addr", ""), |
|---|
| 1181 | vendor=host.ip.get("vendor", ""), |
|---|
| 1182 | addrtype=host.ip.get("type", "")))) |
|---|
| 1183 | self.write_parser.endElement("address") |
|---|
| 1184 | |
|---|
| 1185 | ## IPv6 |
|---|
| 1186 | if isinstance(host.ipv6, dict): |
|---|
| 1187 | self.write_parser.startElement("address", |
|---|
| 1188 | Attributes(dict(addr=host.ipv6.get("addr", ""), |
|---|
| 1189 | vendor=host.ipv6.get("vendor", ""), |
|---|
| 1190 | addrtype=host.ipv6.get("type", "")))) |
|---|
| 1191 | self.write_parser.endElement("address") |
|---|
| 1192 | |
|---|
| 1193 | ## MAC |
|---|
| 1194 | if isinstance(host.mac, dict): |
|---|
| 1195 | self.write_parser.startElement("address", |
|---|
| 1196 | Attributes(dict(addr=host.mac.get("addr", ""), |
|---|
| 1197 | vendor=host.mac.get("vendor", ""), |
|---|
| 1198 | addrtype=host.mac.get("type", "")))) |
|---|
| 1199 | self.write_parser.endElement("address") |
|---|
| 1200 | # End of Address elements |
|---|
| 1201 | ######################### |
|---|
| 1202 | |
|---|
| 1203 | |
|---|
| 1204 | ################### |
|---|
| 1205 | # Hostnames element |
|---|
| 1206 | self.write_parser.startElement("hostnames", Attributes({})) |
|---|
| 1207 | |
|---|
| 1208 | for hname in host.hostnames: |
|---|
| 1209 | if isinstance(hname, dict): |
|---|
| 1210 | self.write_parser.startElement("hostname", |
|---|
| 1211 | Attributes(dict(name = hname.get("hostname", ""), |
|---|
| 1212 | type = hname.get("hostname_type", "")))) |
|---|
| 1213 | |
|---|
| 1214 | self.write_parser.endElement("hostname") |
|---|
| 1215 | |
|---|
| 1216 | self.write_parser.endElement("hostnames") |
|---|
| 1217 | # End of Hostnames element |
|---|
| 1218 | ########################## |
|---|
| 1219 | |
|---|
| 1220 | |
|---|
| 1221 | ############### |
|---|
| 1222 | # Ports element |
|---|
| 1223 | self.write_parser.startElement("ports", Attributes({})) |
|---|
| 1224 | |
|---|
| 1225 | for ps in host.ports: |
|---|
| 1226 | ## Extraports elements |
|---|
| 1227 | for ext in ps["extraports"]: |
|---|
| 1228 | if isinstance(ext, dict): |
|---|
| 1229 | self.__remove_none_keys(ext) |
|---|
| 1230 | self.write_parser.startElement("extraports", |
|---|
| 1231 | Attributes(dict(count = str(ext.get("count", "")), |
|---|
| 1232 | state = ext.get("state", "")))) |
|---|
| 1233 | self.write_parser.endElement("extraports") |
|---|
| 1234 | |
|---|
| 1235 | ## Port elements |
|---|
| 1236 | for p in ps["port"]: |
|---|
| 1237 | if isinstance(p, dict): |
|---|
| 1238 | self.__remove_none_keys(p) |
|---|
| 1239 | self.write_parser.startElement("port", |
|---|
| 1240 | Attributes(dict(portid = p.get("portid", ""), |
|---|
| 1241 | protocol = p.get("protocol", "")))) |
|---|
| 1242 | |
|---|
| 1243 | ### Port state |
|---|
| 1244 | self.write_parser.startElement("state", |
|---|
| 1245 | Attributes(dict(state=p.get("port_state", "")))) |
|---|
| 1246 | self.write_parser.endElement("state") |
|---|
| 1247 | |
|---|
| 1248 | ### Port service info |
|---|
| 1249 | self.write_parser.startElement("service", |
|---|
| 1250 | Attributes(dict(conf = p.get("service_conf", ""), |
|---|
| 1251 | method = p.get("service_method", ""), |
|---|
| 1252 | name = p.get("service_name", ""), |
|---|
| 1253 | product = p.get("service_product", ""), |
|---|
| 1254 | version = p.get("service_version", ""), |
|---|
| 1255 | extrainfo = p.get("service_extrainfo", "")\ |
|---|
| 1256 | ))) |
|---|
| 1257 | self.write_parser.endElement("service") |
|---|
| 1258 | |
|---|
| 1259 | self.write_parser.endElement("port") |
|---|
| 1260 | |
|---|
| 1261 | self.write_parser.endElement("ports") |
|---|
| 1262 | # End of Ports element |
|---|
| 1263 | ###################### |
|---|
| 1264 | |
|---|
| 1265 | |
|---|
| 1266 | ############ |
|---|
| 1267 | # OS element |
|---|
| 1268 | self.write_parser.startElement("os", Attributes({})) |
|---|
| 1269 | |
|---|
| 1270 | ## Ports used elements |
|---|
| 1271 | for pu in host.ports_used: |
|---|
| 1272 | if isinstance(pu, dict): |
|---|
| 1273 | self.__remove_none_keys(pu) |
|---|
| 1274 | self.write_parser.startElement("portused", |
|---|
| 1275 | Attributes(dict(state = pu.get("state", ""), |
|---|
| 1276 | proto = pu.get("proto", ""), |
|---|
| 1277 | portid = pu.get("portid", "")))) |
|---|
| 1278 | self.write_parser.endElement("portused") |
|---|
| 1279 | |
|---|
| 1280 | ## Osclass elements |
|---|
| 1281 | for oc in host.osclasses: |
|---|
| 1282 | if isinstance(oc, dict): |
|---|
| 1283 | self.__remove_none_keys(oc) |
|---|
| 1284 | self.write_parser.startElement("osclass", |
|---|
| 1285 | Attributes(dict(vendor = oc.get("vendor", ""), |
|---|
| 1286 | osfamily = oc.get("osfamily", ""), |
|---|
| 1287 | type = oc.get("type", ""), |
|---|
| 1288 | osgen = oc.get("osgen", ""), |
|---|
| 1289 | accuracy = oc.get("accuracy", "")))) |
|---|
| 1290 | self.write_parser.endElement("osclass") |
|---|
| 1291 | |
|---|
| 1292 | ## Osmatch elements |
|---|
| 1293 | for om in host.osmatches: |
|---|
| 1294 | if isinstance(om, dict): |
|---|
| 1295 | self.__remove_none_keys(om) |
|---|
| 1296 | self.write_parser.startElement("osmatch", |
|---|
| 1297 | Attributes(dict(name = om.get("name", ""), |
|---|
| 1298 | accuracy = om.get("accuracy", "")))) |
|---|
| 1299 | self.write_parser.endElement("osmatch") |
|---|
| 1300 | |
|---|
| 1301 | ## Osfingerprint element |
|---|
| 1302 | if isinstance(host.osfingerprint, dict): |
|---|
| 1303 | self.__remove_none_keys(host.osfingerprint) |
|---|
| 1304 | self.write_parser.startElement("osfingerprint", |
|---|
| 1305 | Attributes(dict(fingerprint = \ |
|---|
| 1306 | host.osfingerprint.get("fingerprint", "")))) |
|---|
| 1307 | self.write_parser.endElement("osfingerprint") |
|---|
| 1308 | |
|---|
| 1309 | |
|---|
| 1310 | self.write_parser.endElement("os") |
|---|
| 1311 | # End of OS element |
|---|
| 1312 | ################### |
|---|
| 1313 | |
|---|
| 1314 | # Uptime element |
|---|
| 1315 | if isinstance(host.uptime, dict): |
|---|
| 1316 | self.write_parser.startElement("uptime", |
|---|
| 1317 | Attributes(dict(seconds = host.uptime.get("seconds", ""), |
|---|
| 1318 | lastboot = host.uptime.get("lastboot", "")))) |
|---|
| 1319 | self.write_parser.endElement("uptime") |
|---|
| 1320 | |
|---|
| 1321 | ##################### |
|---|
| 1322 | # Sequences elementes |
|---|
| 1323 | ## TCP Sequence element |
|---|
| 1324 | # Cannot use dict() here, because of the 'class' attribute. |
|---|
| 1325 | if isinstance(host.tcpsequence, dict): |
|---|
| 1326 | self.write_parser.startElement("tcpsequence", |
|---|
| 1327 | Attributes({"index":host.tcpsequence.get("index", ""), |
|---|
| 1328 | "class":host.tcpsequence.get("class", ""), |
|---|
| 1329 | "difficulty":host.tcpsequence.get("difficulty", ""), |
|---|
| 1330 | "values":host.tcpsequence.get("values", "")})) |
|---|
| 1331 | self.write_parser.endElement("tcpsequence") |
|---|
| 1332 | |
|---|
| 1333 | ## IP ID Sequence element |
|---|
| 1334 | if isinstance(host.ipidsequence, dict): |
|---|
| 1335 | self.write_parser.startElement("ipidsequence", |
|---|
| 1336 | Attributes({"class":host.ipidsequence.get("class", ""), |
|---|
| 1337 | "values":host.ipidsequence.get("values", "")})) |
|---|
| 1338 | self.write_parser.endElement("ipidsequence") |
|---|
| 1339 | |
|---|
| 1340 | ## TCP TS Sequence element |
|---|
| 1341 | if isinstance(host.tcptssequence, dict): |
|---|
| 1342 | self.write_parser.startElement("tcptssequence", |
|---|
| 1343 | Attributes({"class":host.tcptssequence.get("class", ""), |
|---|
| 1344 | "values":host.tcptssequence.get("values", "")})) |
|---|
| 1345 | self.write_parser.endElement("tcptssequence") |
|---|
| 1346 | # End of sequences elements |
|---|
| 1347 | ########################### |
|---|
| 1348 | |
|---|
| 1349 | # End host element |
|---|
| 1350 | self.write_parser.endElement("host") |
|---|
| 1351 | |
|---|
| 1352 | def _write_debugging(self): |
|---|
| 1353 | self.write_parser.startElement("debugging", Attributes(dict( |
|---|
| 1354 | level=str(self.debugging_level)))) |
|---|
| 1355 | self.write_parser.endElement("debugging") |
|---|
| 1356 | |
|---|
| 1357 | def _write_verbose(self): |
|---|
| 1358 | self.write_parser.startElement("verbose", Attributes(dict( |
|---|
| 1359 | level=str(self.verbose_level)))) |
|---|
| 1360 | self.write_parser.endElement("verbose") |
|---|
| 1361 | |
|---|
| 1362 | def _write_scaninfo(self): |
|---|
| 1363 | for scan in self.scaninfo: |
|---|
| 1364 | if isinstance(scan, dict): |
|---|
| 1365 | self.write_parser.startElement("scaninfo", |
|---|
| 1366 | Attributes(dict(type = scan.get("type", ""), |
|---|
| 1367 | protocol = scan.get("protocol", ""), |
|---|
| 1368 | numservices = scan.get("numservices", ""), |
|---|
| 1369 | services = scan.get("services", "")))) |
|---|
| 1370 | self.write_parser.endElement("scaninfo") |
|---|
| 1371 | |
|---|
| 1372 | def _write_nmaprun(self): |
|---|
| 1373 | self.write_parser.startElement("nmaprun", |
|---|
| 1374 | Attributes(dict(annotation = str(self.profile_annotation), |
|---|
| 1375 | args = str(self.nmap_command), |
|---|
| 1376 | description = str(self.profile_description), |
|---|
| 1377 | hint = str(self.profile_hint), |
|---|
| 1378 | nmap_output = str(self.nmap_output), |
|---|
| 1379 | options = str(self.profile_options), |
|---|
| 1380 | profile = str(self.profile), |
|---|
| 1381 | profile_name = str(self.profile_name), |
|---|
| 1382 | scanner = str(self.scanner), |
|---|
| 1383 | start = str(self.start), |
|---|
| 1384 | startstr = str(self.formated_date), |
|---|
| 1385 | target = str(self.target), |
|---|
| 1386 | version = str(self.scanner_version), |
|---|
| 1387 | scan_name = str(self.scan_name)))) |
|---|
| 1388 | |
|---|
| 1389 | def _verify_file(self, xml_file): |
|---|
| 1390 | if isinstance(xml_file, basestring): |
|---|
| 1391 | if os.access(os.path.split(xml_file)[0], os.W_OK): |
|---|
| 1392 | xml_file = open(xml_file, "w") |
|---|
| 1393 | xml_file.seek(0) |
|---|
| 1394 | return xml_file |
|---|
| 1395 | else: |
|---|
| 1396 | raise Exception("Don't have write permissions to given path.") |
|---|
| 1397 | else: |
|---|
| 1398 | try: |
|---|
| 1399 | mode = xml_file.mode |
|---|
| 1400 | if mode == "r+" or mode == "w" or mode == "w+": |
|---|
| 1401 | xml_file.seek(0) |
|---|
| 1402 | except IOError: |
|---|
| 1403 | raise Exception("File descriptor is not able to write!") |
|---|
| 1404 | else: |
|---|
| 1405 | return xml_file |
|---|
| 1406 | |
|---|
| 1407 | def __remove_none_keys(self, dic): |
|---|
| 1408 | pop_list = [] |
|---|
| 1409 | for k in dic: |
|---|
| 1410 | if dic[k] is None: |
|---|
| 1411 | pop_list.append(k) |
|---|
| 1412 | for k in pop_list: |
|---|
| 1413 | dic.pop(k) |
|---|
| 1414 | |
|---|
| 1415 | def set_unsaved(self): |
|---|
| 1416 | self.unsaved = True |
|---|
| 1417 | |
|---|
| 1418 | def is_unsaved(self): |
|---|
| 1419 | return self.unsaved |
|---|
| 1420 | |
|---|
| 1421 | def nmap_parser_sax(nmap_xml_file=""): |
|---|
| 1422 | parser = make_parser() |
|---|
| 1423 | nmap_parser = NmapParserSAX() |
|---|
| 1424 | |
|---|
| 1425 | parser.setContentHandler(nmap_parser) |
|---|
| 1426 | nmap_parser.set_parser(parser) |
|---|
| 1427 | nmap_parser.set_xml_file(nmap_xml_file) |
|---|
| 1428 | |
|---|
| 1429 | return nmap_parser |
|---|
| 1430 | |
|---|
| 1431 | NmapParser = nmap_parser_sax |
|---|
| 1432 | |
|---|
| 1433 | |
|---|
| 1434 | def main(): |
|---|
| 1435 | import optparse |
|---|
| 1436 | from pprint import pprint |
|---|
| 1437 | |
|---|
| 1438 | parser = optparse.OptionParser(usage="%prog INPUT [OUTPUT]") |
|---|
| 1439 | _, args = parser.parse_args() |
|---|
| 1440 | |
|---|
| 1441 | save_output = False |
|---|
| 1442 | if not len(args): |
|---|
| 1443 | parser.error("Expected an INPUT file at least") |
|---|
| 1444 | elif len(args) == 2: |
|---|
| 1445 | save_output = True |
|---|
| 1446 | elif len(args) > 2: |
|---|
| 1447 | parser.error("Expected an INPUT file and an OUTPUT file only") |
|---|
| 1448 | |
|---|
| 1449 | input = open(args[0]) |
|---|
| 1450 | |
|---|
| 1451 | if save_output: |
|---|
| 1452 | try: |
|---|
| 1453 | output = open(args[1], 'w') |
|---|
| 1454 | except IOError: |
|---|
| 1455 | input.close() |
|---|
| 1456 | raise |
|---|
| 1457 | |
|---|
| 1458 | np = NmapParser(input) |
|---|
| 1459 | np.parse() |
|---|
| 1460 | input.close() |
|---|
| 1461 | if save_output: |
|---|
| 1462 | np.write_xml(output) |
|---|
| 1463 | output.close() |
|---|
| 1464 | |
|---|
| 1465 | |
|---|
| 1466 | # Display some parts of the parsed xml |
|---|
| 1467 | |
|---|
| 1468 | print "Trace:" |
|---|
| 1469 | for host in np.nmap["hosts"]: |
|---|
| 1470 | print host.get_osmatches() |
|---|
| 1471 | print host.get_osmatch() |
|---|
| 1472 | #print host.get_osfingerprint() |
|---|
| 1473 | #number_of_hops = host.get_number_of_hops() |
|---|
| 1474 | #for ttl in range(1, number_of_hops + 1): |
|---|
| 1475 | #hop = host.get_hop_by_ttl(ttl) |
|---|
| 1476 | #print hop |
|---|
| 1477 | |
|---|
| 1478 | print "Comment:", |
|---|
| 1479 | pprint(np.nmap["hosts"][-1].comment) |
|---|
| 1480 | #comment = property(get_comment, set_comment) |
|---|
| 1481 | |
|---|
| 1482 | print "TCP sequence:", |
|---|
| 1483 | pprint(np.nmap["hosts"][-1].tcpsequence) |
|---|
| 1484 | #tcpsequence = property(get_tcpsequence, set_tcpsequence) |
|---|
| 1485 | |
|---|
| 1486 | print "TCP TS sequence:", |
|---|
| 1487 | pprint(np.nmap["hosts"][-1].tcptssequence) |
|---|
| 1488 | #tcptssequence = property(get_tcptssequence, set_tcptssequence) |
|---|
| 1489 | |
|---|
| 1490 | print "IP ID sequence:", |
|---|
| 1491 | pprint(np.nmap["hosts"][-1].ipidsequence) |
|---|
| 1492 | #ipidsequence = property(get_ipidsequence, set_ipidsequence) |
|---|
| 1493 | |
|---|
| 1494 | print "Uptime:", |
|---|
| 1495 | pprint(np.nmap["hosts"][-1].uptime) |
|---|
| 1496 | #uptime = property(get_uptime, set_uptime) |
|---|
| 1497 | |
|---|
| 1498 | print "OS Match:", |
|---|
| 1499 | pprint(np.nmap["hosts"][-1].osmatch) |
|---|
| 1500 | #osmatch = property(get_osmatch, set_osmatch) |
|---|
| 1501 | |
|---|
| 1502 | print "Ports:", |
|---|
| 1503 | pprint(np.nmap["hosts"][-1].ports) |
|---|
| 1504 | #ports = property(get_ports, set_ports) |
|---|
| 1505 | |
|---|
| 1506 | print "Ports used:", |
|---|
| 1507 | pprint(np.nmap["hosts"][-1].ports_used) |
|---|
| 1508 | #ports_used = property(get_ports_used, set_ports_used) |
|---|
| 1509 | |
|---|
| 1510 | print "OS Class:", |
|---|
| 1511 | pprint(np.nmap["hosts"][-1].osclasses) |
|---|
| 1512 | #osclasses = property(get_osclasses, set_osclasses) |
|---|
| 1513 | |
|---|
| 1514 | print "Hostnames:", |
|---|
| 1515 | pprint(np.nmap["hosts"][-1].hostnames) |
|---|
| 1516 | #hostnames = property(get_hostnames, set_hostnames) |
|---|
| 1517 | |
|---|
| 1518 | print "IP:", |
|---|
| 1519 | pprint(np.nmap["hosts"][-1].ip) |
|---|
| 1520 | #ip = property(get_ip, set_ip) |
|---|
| 1521 | |
|---|
| 1522 | print "IPv6:", |
|---|
| 1523 | pprint(np.nmap["hosts"][-1].ipv6) |
|---|
| 1524 | #ipv6 = property(get_ipv6, set_ipv6) |
|---|
| 1525 | |
|---|
| 1526 | print "MAC:", |
|---|
| 1527 | pprint(np.nmap["hosts"][-1].mac) |
|---|
| 1528 | #mac = property(get_mac, set_mac) |
|---|
| 1529 | |
|---|
| 1530 | print "State:", |
|---|
| 1531 | pprint(np.nmap["hosts"][-1].state) |
|---|
| 1532 | #state = property(get_state, set_state) |
|---|
| 1533 | |
|---|
| 1534 | if __name__ == '__main__': |
|---|
| 1535 | main() |
|---|