Skip to content

Commit 69f115f

Browse files
committed
draft of change in NmapParser
1 parent cc5586b commit 69f115f

File tree

11 files changed

+113
-109
lines changed

11 files changed

+113
-109
lines changed

libnmap/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
from libnmap import NmapDiff
2+
from libnmap.diff import NmapDiff
33

44

55
class NmapHost(object):

libnmap/parser.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
import os
33
import xml.etree.ElementTree as ET
44
from StringIO import StringIO
5-
from libnmap import NmapHost, NmapService, NmapReport
5+
from libnmap.common import NmapHost, NmapService
6+
from libnmap.report import NmapReport
67

78

89
class NmapParser(object):
910
@classmethod
1011
def parse(cls, nmap_data=None, data_type='XML'):
12+
nmapobj = None
1113
if data_type == "XML":
12-
cls.parse_xml(nmap_data)
14+
nmapobj = cls.parse_xml(nmap_data)
15+
return nmapobj
1316

1417
@classmethod
1518
def parse_xml(cls, nmap_data=None, data_type='XML'):
@@ -24,7 +27,7 @@ def parse_xml(cls, nmap_data=None, data_type='XML'):
2427
elif isinstance(nmap_data, file):
2528
tree = ET.parse(nmap_data)
2629
except:
27-
raise
30+
raise NmapParserException("Wrong XML structure: cannot parse data")
2831

2932
root = tree.getroot()
3033
if root.tag == 'nmaprun':
@@ -58,10 +61,10 @@ def _parse_xml_report(cls, root=None):
5861
nmap_scan['_hosts'].append(cls._parse_xml_host(el))
5962
elif el.tag == 'runstats':
6063
nmap_scan['_runstats'] = cls._parse_runstats(el)
61-
else:
62-
print "struct pparse unknown attr: {0} value: {1}".format(
63-
el.tag,
64-
el.get(el.tag))
64+
#else:
65+
# print "struct pparse unknown attr: {0} value: {1}".format(
66+
# el.tag,
67+
# el.get(el.tag))
6568
return NmapReport('dummy', nmap_scan)
6669

6770
@classmethod

libnmap/plugins/mongodb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pymongo import MongoClient
44
from bson.objectid import ObjectId
55

6-
from libnmap import ReportDecoder, ReportEncoder
6+
from libnmap.reportjson import ReportDecoder, ReportEncoder
77
from libnmap.plugins.backendplugin import NmapBackendPlugin
88

99

libnmap/plugins/sqlite.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
import sqlite3
33

4-
from libnmap import NmapReport
4+
from libnmap.report import NmapReport
55
from libnmap.plugins.backendplugin import NmapBackendPlugin
66

77

libnmap/report.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
import sys
33
import inspect
4-
from libnmap import NmapDiff
4+
from libnmap.diff import NmapDiff
55
from libnmap.plugins.backendplugin import NmapBackendPlugin
66

77

libnmap/reportjson.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python
22
import json
3-
from libnmap import NmapHost, NmapService, NmapReport, NmapParser
3+
from libnmap.common import NmapHost, NmapService
4+
from libnmap.report import NmapReport
5+
from libnmap.parser import NmapParser
46

57

68
class ReportEncoder(json.JSONEncoder):

libnmap/test/host_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22

33
import unittest
4-
from libnmap import NmapParser
4+
from libnmap.parser import NmapParser
55

66
host1 = """
77
<host starttime="1361738377" endtime="1361738377">
@@ -148,10 +148,10 @@
148148

149149
class TestNmapHost(unittest.TestCase):
150150
def test_eq_host(self):
151-
h1 = NmapParser.parse_host(host1)
152-
h2 = NmapParser.parse_host(host2)
153-
h3 = NmapParser.parse_host(host3)
154-
h4 = NmapParser.parse_host(host4)
151+
h1 = NmapParser.parse(host1)
152+
h2 = NmapParser.parse(host2)
153+
h3 = NmapParser.parse(host3)
154+
h4 = NmapParser.parse(host4)
155155

156156
self.assertNotEqual(h1, h2)
157157
self.assertEqual(h1, h1)
@@ -160,9 +160,9 @@ def test_eq_host(self):
160160
self.assertNotEqual(h2, h3)
161161

162162
def test_diff_host(self):
163-
h1 = NmapParser.parse_host(host1)
164-
h2 = NmapParser.parse_host(host2)
165-
h3 = NmapParser.parse_host(host3)
163+
h1 = NmapParser.parse(host1)
164+
h2 = NmapParser.parse(host2)
165+
h3 = NmapParser.parse(host3)
166166

167167
c1 = h1.diff(h2)
168168
c2 = h1.diff(h3)

libnmap/test/parser_test.py

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import unittest
44
import os
5-
from libnmap import NmapParser, NmapParserException
5+
from libnmap.parser import NmapParser, NmapParserException
66

77

88
class TestNmapParser(unittest.TestCase):
@@ -35,47 +35,47 @@ def setUp(self):
3535
self.flist = self.flist_full
3636

3737
self.ports_string = """<ports><extraports state="closed" count="996">
38-
<extrareasons reason="resets" count="996"/>
39-
</extraports>
40-
<port protocol="tcp" portid="22">
41-
<state state="open" reason="syn-ack" reason_ttl="53"/>
42-
<service name="ssh" method="table" conf="3"/>
43-
</port>
44-
<port protocol="tcp" portid="25">
45-
<state state="filtered" reason="admin-prohibited" \
46-
reason_ttl="253" reason_ip="109.133.192.1"/>
47-
<service name="smtp" method="table" conf="3"/>
48-
</port>
49-
<port protocol="tcp" portid="80">
50-
<state state="open" reason="syn-ack" reason_ttl="51"/>
51-
<service name="http" method="table" conf="3"/>
52-
</port>
53-
<port protocol="tcp" portid="9929">
54-
<state state="open" reason="syn-ack" reason_ttl="53"/>
55-
<service name="nping-echo" method="table" conf="3"/>
56-
</port>
57-
</ports>"""
38+
<extrareasons reason="resets" count="996"/>
39+
</extraports>
40+
<port protocol="tcp" portid="22">
41+
<state state="open" reason="syn-ack" reason_ttl="53"/>
42+
<service name="ssh" method="table" conf="3"/>
43+
</port>
44+
<port protocol="tcp" portid="25">
45+
<state state="filtered" reason="admin-prohibited" \
46+
reason_ttl="253" reason_ip="109.133.192.1"/>
47+
<service name="smtp" method="table" conf="3"/>
48+
</port>
49+
<port protocol="tcp" portid="80">
50+
<state state="open" reason="syn-ack" reason_ttl="51"/>
51+
<service name="http" method="table" conf="3"/>
52+
</port>
53+
<port protocol="tcp" portid="9929">
54+
<state state="open" reason="syn-ack" reason_ttl="53"/>
55+
<service name="nping-echo" method="table" conf="3"/>
56+
</port>
57+
</ports>"""
5858

5959
self.ports_string2 = """<ports><extraports state="closed" count="996">
60-
<extrareasons reason="resets" count="996"/>
61-
</extraports>
62-
<port protocol="tcp" portid="A2">
63-
<state state="open" reason="syn-ack" reason_ttl="53"/>
64-
<service name="ssh" method="table" conf="3"/>
65-
</port>
66-
<port protocol="tcp" portid="25">
67-
<state state="filtered" reason="admin-prohibited" \
68-
reason_ttl="253" reason_ip="109.133.192.1"/>
69-
<service name="smtp" method="table" conf="3"/>
70-
</port>
71-
<port protocol="tcp" portid="80">
72-
<state state="open" reason="syn-ack" reason_ttl="51"/>i
73-
<service name="http" method="table" conf="3"/></port>
74-
<port protocol="tcp" portid="9929">
75-
<state state="open" reason="syn-ack" reason_ttl="53"/>
76-
<service name="nping-echo" method="table" conf="3"/>
77-
</port>
78-
</ports>"""
60+
<extrareasons reason="resets" count="996"/>
61+
</extraports>
62+
<port protocol="tcp" portid="A2">
63+
<state state="open" reason="syn-ack" reason_ttl="53"/>
64+
<service name="ssh" method="table" conf="3"/>
65+
</port>
66+
<port protocol="tcp" portid="25">
67+
<state state="filtered" reason="admin-prohibited" \
68+
reason_ttl="253" reason_ip="109.133.192.1"/>
69+
<service name="smtp" method="table" conf="3"/>
70+
</port>
71+
<port protocol="tcp" portid="80">
72+
<state state="open" reason="syn-ack" reason_ttl="51"/>i
73+
<service name="http" method="table" conf="3"/></port>
74+
<port protocol="tcp" portid="9929">
75+
<state state="open" reason="syn-ack" reason_ttl="53"/>
76+
<service name="nping-echo" method="table" conf="3"/>
77+
</port>
78+
</ports>"""
7979

8080
self.port_string = """
8181
<port protocol="tcp" portid="25">
@@ -125,51 +125,51 @@ def test_class_parser(self):
125125
NmapParser.parse(s)
126126

127127
def test_class_ports_parser(self):
128-
plist = NmapParser.parse_ports(self.ports_string)
129-
self.assertEqual(len(plist), 4)
130-
self.assertEqual(sorted([p.port for p in plist]),
131-
sorted([22, 25, 9929, 80]))
132-
self.assertRaises(ValueError,
133-
NmapParser.parse_ports,
128+
plist = NmapParser.parse(self.ports_string)
129+
self.assertEqual(len(plist), 4)
130+
self.assertEqual(sorted([p.port for p in plist]),
131+
sorted([22, 25, 9929, 80]))
132+
self.assertRaises(ValueError,
133+
NmapParser.parse,
134134
self.ports_string2)
135135

136136
def test_class_port_parser(self):
137-
p = NmapParser.parse_port(self.port_string)
137+
p = NmapParser.parse(self.port_string)
138138
self.assertEqual(p.port, 25)
139139
self.assertNotEqual(p.state, "open")
140140
self.assertEqual(p.state, "filtered")
141141
self.assertEqual(p.service, "smtp")
142142

143143
def test_port_except(self):
144144
self.assertRaises(ValueError,
145-
NmapParser.parse_port,
145+
NmapParser.parse,
146146
self.port_string2)
147147
self.assertRaises(ValueError,
148-
NmapParser.parse_port,
148+
NmapParser.parse,
149149
self.port_string3)
150150
self.assertRaises(NmapParserException,
151-
NmapParser.parse_port,
151+
NmapParser.parse,
152152
self.port_string4)
153153
self.assertRaises(NmapParserException,
154-
NmapParser.parse_port,
154+
NmapParser.parse,
155155
self.port_string5)
156156
self.assertRaises(ValueError,
157-
NmapParser.parse_port,
157+
NmapParser.parse,
158158
self.port_string6)
159159
self.assertRaises(NmapParserException,
160-
NmapParser.parse_port,
160+
NmapParser.parse,
161161
self.port_string7)
162162
self.assertRaises(NmapParserException,
163-
NmapParser.parse_port,
163+
NmapParser.parse,
164164
self.port_string8)
165165
self.assertRaises(NmapParserException,
166-
NmapParser.parse_port,
166+
NmapParser.parse,
167167
self.port_string9)
168168

169169
def test_parser_generic(self):
170170
plist = NmapParser.parse_fromstring(self.ports_string)
171-
for ks in plist.keys():
172-
print plist[ks]
171+
for p in plist:
172+
print p
173173

174174
if __name__ == '__main__':
175175
test_suite = ['test_class_parser', 'test_class_ports_parser',

libnmap/test/report_diff_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import unittest
44
import os
5-
from libnmap import NmapParser, NmapReport
5+
from libnmap.parser import NmapParser
6+
from libnmap.report import NmapReport
67

78

89
class TestNmapReportDiff(unittest.TestCase):
@@ -16,16 +17,12 @@ def setUp(self):
1617

1718
def test_diff_host_list(self):
1819
fdir = os.path.dirname(os.path.realpath(__file__))
19-
d1 = NmapParser.parse_fromfile("%s/%s" % (fdir, 'files/1_hosts.xml'))
20-
d2 = NmapParser.parse_fromfile("%s/%s" % (fdir, 'files/2_hosts.xml'))
21-
d3 = NmapParser.parse_fromfile("%s/%s" % (fdir,
20+
r1 = NmapParser.parse_fromfile("%s/%s" % (fdir, 'files/1_hosts.xml'))
21+
r2 = NmapParser.parse_fromfile("%s/%s" % (fdir, 'files/2_hosts.xml'))
22+
r3 = NmapParser.parse_fromfile("%s/%s" % (fdir, 'files/1_hosts.xml'))
23+
r4 = NmapParser.parse_fromfile("%s/%s" % (fdir,
2224
'files/2_hosts_achange.xml'))
2325

24-
r1 = NmapReport("r1", d1)
25-
r2 = NmapReport("r2", d2)
26-
r3 = NmapReport("r3", d1)
27-
r4 = NmapReport("r4", d3)
28-
2926
d1 = r1.diff(r2)
3027
self.assertEqual(d1.changed(), set(['hosts_total', 'hosts_up']))
3128
self.assertEqual(d1.unchanged(), set(['hosts_down',

libnmap/test/service_test.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python
22

33
import unittest
4-
from libnmap import NmapParser, NmapDiffException
4+
from libnmap.parser import NmapParser
5+
from libnmap.diff import NmapDiffException
56

67
service1 = """
78
<port protocol="tcp" portid="22">
@@ -120,19 +121,19 @@
120121

121122
class TestNmapService(unittest.TestCase):
122123
def setUp(self):
123-
self.s1 = NmapParser.parse_port(service1)
124-
self.s2 = NmapParser.parse_port(service2)
125-
self.s3 = NmapParser.parse_port(service3)
126-
self.s4 = NmapParser.parse_port(service4)
127-
self.s5 = NmapParser.parse_port(service5)
128-
self.s6 = NmapParser.parse_port(service6)
129-
self.s7 = NmapParser.parse_port(service7)
124+
self.s1 = NmapParser.parse(service1)
125+
self.s2 = NmapParser.parse(service2)
126+
self.s3 = NmapParser.parse(service3)
127+
self.s4 = NmapParser.parse(service4)
128+
self.s5 = NmapParser.parse(service5)
129+
self.s6 = NmapParser.parse(service6)
130+
self.s7 = NmapParser.parse(service7)
130131

131132
def test_port_state_changed(self):
132-
nservice1 = NmapParser.parse_port(port_string)
133-
nservice2 = NmapParser.parse_port(port_string_other2)
134-
nservice3 = NmapParser.parse_port(port_string_other3)
135-
nservice4 = NmapParser.parse_port(port_string_other4)
133+
nservice1 = NmapParser.parse(port_string)
134+
nservice2 = NmapParser.parse(port_string_other2)
135+
nservice3 = NmapParser.parse(port_string_other3)
136+
nservice4 = NmapParser.parse(port_string_other4)
136137

137138
self.assertEqual(nservice1.diff(nservice2).changed(), set(['state']))
138139
self.assertRaises(NmapDiffException, nservice1.diff, nservice3)
@@ -143,21 +144,21 @@ def test_port_state_changed(self):
143144
set(['state', 'service']))
144145

145146
def test_port_state_unchanged(self):
146-
nservice1 = NmapParser.parse_port(port_string)
147-
nservice2 = NmapParser.parse_port(port_string_other2)
148-
#nservice3 = NmapParser.parse_port(port_string_other3)
149-
#nservice4 = NmapParser.parse_port(port_string_other4)
147+
nservice1 = NmapParser.parse(port_string)
148+
nservice2 = NmapParser.parse(port_string_other2)
149+
#nservice3 = NmapParser.parse(port_string_other3)
150+
#nservice4 = NmapParser.parse(port_string_other4)
150151

151152
self.assertEqual(nservice1.diff(nservice2).unchanged(),
152153
set(['banner', 'protocol', 'port', 'service', 'id']))
153154

154155
def test_port_service_changed(self):
155-
nservice1 = NmapParser.parse_port(port_string)
156-
nservice2 = NmapParser.parse_port(port_string_other2)
157-
nservice4 = NmapParser.parse_port(port_string_other4)
158-
nservice5 = NmapParser.parse_port(port_string_other5)
159-
nservice8 = NmapParser.parse_port(port_string_other8)
160-
nservice9 = NmapParser.parse_port(port_string_other9)
156+
nservice1 = NmapParser.parse(port_string)
157+
nservice2 = NmapParser.parse(port_string_other2)
158+
nservice4 = NmapParser.parse(port_string_other4)
159+
nservice5 = NmapParser.parse(port_string_other5)
160+
nservice8 = NmapParser.parse(port_string_other8)
161+
nservice9 = NmapParser.parse(port_string_other9)
161162

162163
self.assertEqual(nservice1.diff(nservice2).changed(),
163164
set(['state']))

0 commit comments

Comments
 (0)