Skip to content

Commit 9d5c463

Browse files
committed
Fix for issue 28 and added code samples
1 parent 1042db1 commit 9d5c463

7 files changed

Lines changed: 97 additions & 4 deletions

File tree

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
v0.4.9, 14/05/2014 -- Fix for issue 28 and added code samples
12
v0.4.8, 05/05/2014 -- Changes in OS fingerprint data API
23
- NmapHost now holds its OS fingerprint data in NmapHost.os
34
(NmapOSFingerpring object)

examples/check_cpe.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
from libnmap.parser import NmapParser
4+
5+
rep = NmapParser.parse_fromfile('libnmap/test/files/full_sudo6.xml')
6+
7+
print("Nmap scan discovered {0}/{1} hosts up".format(rep.hosts_up,
8+
rep.hosts_total))
9+
for _host in rep.hosts:
10+
if _host.is_up():
11+
print("+ Host: {0} {1}".format(_host.address,
12+
" ".join(_host.hostnames)))
13+
14+
# get CPE from service if available
15+
for s in _host.services:
16+
print " Service: {0}/{1} ({2})".format(s.port,
17+
s.protocol,
18+
s.state)
19+
# NmapService.cpelist returns an array of CPE objects
20+
for _serv_cpe in s.cpelist:
21+
print " CPE: {0}".format(_serv_cpe.cpestring)
22+
23+
if _host.os_fingerprinted:
24+
print " OS Fingerprints"
25+
for osm in _host.os.osmatches:
26+
print(" Found Match:{0} ({1}%)".format(osm.name,
27+
osm.accuracy))
28+
# NmapOSMatch.get_cpe() method return an array of string
29+
# unlike NmapOSClass.cpelist which returns an array of CPE obj
30+
for cpe in osm.get_cpe():
31+
print("\t CPE: {0}".format(cpe))

examples/os_fingerprint.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
3+
from libnmap.parser import NmapParser
4+
5+
rep = NmapParser.parse_fromfile('libnmap/test/files/os_scan6.xml')
6+
7+
print("{0}/{1} hosts up".format(rep.hosts_up, rep.hosts_total))
8+
for _host in rep.hosts:
9+
if _host.is_up():
10+
print("{0} {1}".format(_host.address, " ".join(_host.hostnames)))
11+
if _host.os_fingerprinted:
12+
print("OS Fingerprint:")
13+
msg = ''
14+
for osm in _host.os.osmatches:
15+
print("Found Match:{0} ({1}%)".format(osm.name, osm.accuracy))
16+
for osc in osm.osclasses:
17+
print("\tOS Class: {0}".format(osc.description))
18+
for cpe in osc.cpelist:
19+
print("\tCPE: {0}".format(cpe.cpestring))
20+
else:
21+
print "No fingerprint available"

libnmap/objects/host.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ def status(self, statusdict):
200200
"""
201201
self._status = statusdict
202202

203+
def is_up(self):
204+
"""
205+
method to determine if host is up or not
206+
207+
:return: bool
208+
"""
209+
rval = False
210+
if self.status == 'up':
211+
rval = True
212+
return rval
213+
203214
@property
204215
def hostnames(self):
205216
"""

libnmap/objects/os.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,21 @@ def accuracy(self):
144144
"""
145145
return int(self._accuracy)
146146

147+
def get_cpe(self):
148+
"""
149+
This method return a list of cpe stings and not CPE objects as
150+
the NmapOSClass.cpelist property. This method is a helper to
151+
simplify data management.
152+
153+
For more advanced handling of CPE data, use NmapOSClass.cpelist
154+
and use the methods from CPE class
155+
"""
156+
_cpelist = []
157+
for osc in self.osclasses:
158+
for cpe in osc.cpelist:
159+
_cpelist.append(cpe.cpestring)
160+
return _cpelist
161+
147162
def __repr__(self):
148163
rval = "{0}: {1}".format(self.name, self.accuracy)
149164
for _osclass in self._osclasses:
@@ -239,6 +254,19 @@ def type(self):
239254
"""
240255
return self._type
241256

257+
@property
258+
def description(self):
259+
"""
260+
Accessor helper which returns a concataned string of
261+
the valuable attributes from NmapOSClass object
262+
263+
:return: string
264+
"""
265+
rval = "{0}: {1}, {2}".format(self.type, self.vendor, self.osfamily)
266+
if len(self.osgen):
267+
rval += "({0})".format(self.osgen)
268+
return rval
269+
242270
def __repr__(self):
243271
rval = "{0}: {1}, {2}".format(self.type, self.vendor, self.osfamily)
244272
if len(self.osgen):

libnmap/objects/service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ def __init__(self, portid, protocol='tcp', state=None,
3939
self._service = service if service is not None else {}
4040

4141
self._cpelist = []
42-
for _cpe in service['cpelist']:
43-
_cpeobj = CPE(_cpe)
44-
self._cpelist.append(_cpeobj)
42+
if 'cpelist' in self._service:
43+
for _cpe in self._service['cpelist']:
44+
_cpeobj = CPE(_cpe)
45+
self._cpelist.append(_cpeobj)
4546

4647
self._owner = ''
4748
if owner is not None and 'name' in owner:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='python-libnmap',
8-
version='0.4.8',
8+
version='0.4.9',
99
author='Ronald Bister',
1010
author_email='mini.pelle@gmail.com',
1111
packages=['libnmap', 'libnmap.plugins', 'libnmap.objects'],

0 commit comments

Comments
 (0)