Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue26 #30

Merged
merged 8 commits into from
May 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
v0.4.8, 05/05/2014 -- Changes in OS fingerprint data API
- NmapHost now holds its OS fingerprint data in NmapHost.os
(NmapOSFingerpring object)
- fingerprint is now a property which means you have to call
it without () NmapHost.os.fingerprint and should be called
directly from NmapHost.os
- NmapHost.os.fingerprints return an array of os
fingerprints (strings)
- NmapHost.os.fingerprint return a concatenated string of
all fingerprints
- Fingerprints data are now accessible via
NmapHost.os.osmatches which returns a list of NmapOSMatch
objects
- NmapOSMatch objects might contain a list of NmapOSClass
objects matching with it
- NmapOSClass objects might contain a list of CPE object
related to the os class (CPE class will be improved and
API enriched)
- TODO: finalize API doc and create all related unit tests
v0.4.7, 03/05/2014 -- minor fix for issue25
- fixed exception when optional service tag is not present
in <port> tag
Expand Down
105 changes: 13 additions & 92 deletions libnmap/objects/host.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,9 @@
#!/usr/bin/env python
from libnmap.diff import NmapDiff
from libnmap.objects.os import NmapOSFingerprint


class NmapHost(object):
"""
NmapHost is a class representing a host object of NmapReport
"""
class NmapOSFingerprint(object):
"""
NmapOSFingerprint is a easier API for using os fingerprinting
"""
def __init__(self, osfp_data):
self.__fingerprint = ''
self.__osmatch = ''
self.__osclass = ''
if 'osfingerprint' in osfp_data:
self.__fingerprint = osfp_data['osfingerprint']

__sortfct = lambda osent: int(osent['accuracy'])
if 'osmatch' in osfp_data:
try:
self.__osmatch = sorted(osfp_data['osmatch'],
key=__sortfct,
reverse=True)
except (KeyError, TypeError):
self.__osmatch = []

if 'osclass' in osfp_data:
try:
self.__osclass = sorted(osfp_data['osclass'],
key=__sortfct,
reverse=True)
except (KeyError, TypeError):
self.__osclass = []

def osmatch(self, min_accuracy=90):
os_array = []
for match_entry in self.__osmatch:
try:
if int(match_entry['accuracy']) >= min_accuracy:
os_array.append(match_entry['name'])
except (KeyError, TypeError):
pass
return os_array

def osclass(self, min_accuracy=90):
os_array = []
for osclass_entry in self.__osclass:
try:
if int(osclass_entry['accuracy']) >= min_accuracy:
_relevantkeys = ['type', 'vendor', 'osfamily', 'osgen']
_ftstr = "|".join([vkey + ": " + osclass_entry[vkey]
for vkey in osclass_entry
if vkey in _relevantkeys])
os_array.append(_ftstr)
except (KeyError, TypeError):
pass
return os_array

def fingerprint(self):
return self.__fingerprint

def __repr__(self):
_fmtstr = ''
if len(self.osmatch()):
_fmtstr += "OS:\r\n"
for _osmline in self.osmatch():
_fmtstr += " {0}\r\n".format(_osmline)
elif len(self.osclass()):
_fmtstr += "OS CLASS:\r\n"
for _oscline in self.osclass():
_fmtstr += " {0}\r\n".format(_oscline)
elif len(self.fingerprint()):
_fmtstr += "OS FINGERPRINT:\r\n"
_fmtstr += " {0}".format(self.fingerprint())
return _fmtstr

"""
NmapHost is a class representing a host object of NmapReport
"""
Expand All @@ -101,9 +29,12 @@ def __init__(self, starttime='', endtime='', address=None, status=None,
self._services = services if services is not None else []
self._extras = extras if extras is not None else {}
self._osfingerprinted = False
self.os = None
if 'os' in self._extras:
self.os = self.NmapOSFingerprint(self._extras['os'])
self.os = NmapOSFingerprint(self._extras['os'])
self._osfingerprinted = True
else:
self.os = NmapOSFingerprint({})

self._ipv4_addr = None
self._ipv6_addr = None
Expand Down Expand Up @@ -336,31 +267,23 @@ def os_class_probabilities(self):
Returns an array of possible OS class detected during
the OS fingerprinting.

Example [{'accuracy': '96', 'osfamily': 'embedded',
'type': 'WAP', 'vendor': 'Netgear'}, {...}]

:return: dict describing the OS class detected and the accuracy
:return: Array of NmapOSClass objects
"""
rval = []
try:
rval = self._extras['os']['osclass']
except (KeyError, TypeError):
pass
if self.os is not None:
rval = self.os.osclasses
return rval

def os_match_probabilities(self):
"""
Returns an array of possible OS match detected during
the OS fingerprinting

:return: dict describing the OS version detected and the accuracy
:return: array of NmapOSMatches objects
"""

rval = []
try:
rval = self._extras['os']['osmatch']
except (KeyError, TypeError):
pass
if self.os is not None:
rval = self.os.osmatches
return rval

@property
Expand All @@ -380,10 +303,8 @@ def os_fingerprint(self):
:return: string
"""
rval = ''
try:
rval = self._extras['os']['osfingerprint']
except (KeyError, TypeError):
pass
if self.os is not None:
rval = self.os.fingerprints.join("\n")
return rval

def os_ports_used(self):
Expand Down
Loading