Skip to content

Commit 59eec29

Browse files
committed
Merge branch 'cpe'
2 parents 66a859c + 6c139c1 commit 59eec29

5 files changed

Lines changed: 155 additions & 23 deletions

File tree

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
v0.5.1, 26/05/2014 -- Added basic API for class CPE
2+
- interface similar to python-cpe for more
3+
advanced usage of CPE, I strongly recommend you
4+
to use python-cpe. Nice code, good doc.
15
v0.5.0, 17/05/2014 -- Rewrite of NmapProcess
26
- removed Threads to read stdout/stderr
37
- replaced by Process

libnmap/objects/cpe.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
class CPE(object):
3+
"""
4+
CPE class offers an API for basic CPE objects.
5+
These objects could be found in NmapService or in <os> tag
6+
within NmapHost.
7+
8+
:todo: interpret CPE string and provide appropriate API
9+
"""
10+
def __init__(self, cpestring):
11+
self._cpestring = cpestring
12+
self.cpedict = {}
13+
14+
zk = ['cpe', 'part', 'vendor', 'product', 'version',
15+
'update', 'edition', 'language']
16+
self._cpedict = dict((k, '') for k in zk)
17+
splitup = cpestring.split(':')
18+
self._cpedict.update(dict(zip(zk, splitup)))
19+
20+
@property
21+
def cpestring(self):
22+
"""
23+
Accessor for the full CPE string.
24+
"""
25+
return self._cpestring
26+
27+
def __repr__(self):
28+
return self._cpestring
29+
30+
def get_part(self):
31+
"""
32+
Returns the cpe part (/o, /h, /a)
33+
"""
34+
return self._cpedict['part']
35+
36+
def get_vendor(self):
37+
"""
38+
Returns the vendor name
39+
"""
40+
return self._cpedict['vendor']
41+
42+
def get_product(self):
43+
"""
44+
Returns the product name
45+
"""
46+
return self._cpedict['product']
47+
48+
def get_version(self):
49+
"""
50+
Returns the version of the cpe
51+
"""
52+
return self._cpedict['version']
53+
54+
def get_update(self):
55+
"""
56+
Returns the update version
57+
"""
58+
return self._cpedict['update']
59+
60+
def get_edition(self):
61+
"""
62+
Returns the cpe edition
63+
"""
64+
return self._cpedict['edition']
65+
66+
def get_language(self):
67+
"""
68+
Returns the cpe language
69+
"""
70+
return self._cpedict['language']
71+
72+
def is_application(self):
73+
"""
74+
Returns True if cpe describes an application
75+
"""
76+
return (self.get_part() == '/a')
77+
78+
def is_hardware(self):
79+
"""
80+
Returns True if cpe describes a hardware
81+
"""
82+
return (self.get_part() == '/h')
83+
84+
def is_operating_system(self):
85+
"""
86+
Returns True if cpe describes an operating system
87+
"""
88+
return (self.get_part() == '/o')

libnmap/objects/os.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
11
import warnings
2-
3-
4-
class CPE(object):
5-
"""
6-
CPE class offers an API for basic CPE objects.
7-
These objects could be found in NmapService or in <os> tag
8-
within NmapHost.
9-
10-
:todo: interpret CPE string and provide appropriate API
11-
"""
12-
def __init__(self, cpestring):
13-
self._cpestring = cpestring
14-
15-
@property
16-
def cpestring(self):
17-
"""
18-
Accessor for the full CPE string.
19-
"""
20-
return self._cpestring
21-
22-
def __repr__(self):
23-
return self._cpestring
2+
from libnmap.objects.cpe import CPE
243

254

265
class OSFPPortUsed(object):

libnmap/test/test_cpe.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python
2+
3+
from libnmap.objects.os import CPE
4+
import unittest
5+
6+
7+
class TestNmapFP(unittest.TestCase):
8+
def setUp(self):
9+
self.cpelist = ['cpe:/a:apache:http_server:2.2.22',
10+
'cpe:/a:heimdal:kerberos',
11+
'cpe:/a:openbsd:openssh:5.9p1',
12+
'cpe:/o:apple:iphone_os:5',
13+
'cpe:/o:apple:mac_os_x:10.8',
14+
'cpe:/o:apple:mac_os_x',
15+
'cpe:/o:linux:linux_kernel:2.6.13',
16+
'cpe:/o:linux:linux_kernel',
17+
'cpe:/o:microsoft:windows_7',
18+
'cpe:/o:microsoft:windows_7::-:professional',
19+
'cpe:/o:microsoft:windows_7::sp1',
20+
'cpe:/o:microsoft:windows',
21+
'cpe:/o:microsoft:windows_server_2008::beta3',
22+
'cpe:/o:microsoft:windows_server_2008',
23+
'cpe:/o:microsoft:windows_server_2008::sp1',
24+
'cpe:/o:microsoft:windows_vista::-',
25+
'cpe:/o:microsoft:windows_vista::sp1',
26+
'cpe:/o:microsoft:windows_vista::sp2']
27+
28+
def test_cpe(self):
29+
apa = CPE(self.cpelist[0])
30+
31+
self.assertTrue(apa.is_application())
32+
self.assertFalse(apa.is_hardware())
33+
self.assertFalse(apa.is_operating_system())
34+
35+
36+
win = CPE(self.cpelist[12])
37+
self.assertEqual(win.get_vendor(), 'microsoft')
38+
self.assertEqual(win.get_product(), 'windows_server_2008')
39+
self.assertEqual(win.get_version(), '')
40+
self.assertEqual(win.get_update(), 'beta3')
41+
self.assertEqual(win.get_edition(), '')
42+
self.assertEqual(win.get_language(), '')
43+
44+
def test_full_cpe(self):
45+
cpestr = 'cpe:/a:mozilla:firefox:2.0::osx:es-es'
46+
resdict = { 'part':'/a', 'vendor':"mozilla", 'product':"firefox", 'version':"2.0", 'update':'', 'edition':"osx", 'language':'es-es' }
47+
ocpe = CPE(cpestr)
48+
objdict = {'part': ocpe.get_part(),
49+
'vendor': ocpe.get_vendor(),
50+
'product': ocpe.get_product(),
51+
'version': ocpe.get_version(),
52+
'update': ocpe.get_update(),
53+
'language': ocpe.get_language(),
54+
'edition': ocpe.get_edition()
55+
}
56+
self.assertEqual(objdict, resdict)
57+
58+
if __name__ == '__main__':
59+
test_suite = ['test_cpe', 'test_full_cpe']
60+
suite = unittest.TestSuite(map(TestNmapFP, test_suite))
61+
test_result = unittest.TextTestRunner(verbosity=2).run(suite)

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.5.0',
8+
version='0.5.1',
99
author='Ronald Bister',
1010
author_email='mini.pelle@gmail.com',
1111
packages=['libnmap', 'libnmap.plugins', 'libnmap.objects'],

0 commit comments

Comments
 (0)