Skip to content

Commit 204c8e3

Browse files
committed
feat: added support for mac address in NmapHost and cascaded attribute support in NmapDiff
1 parent 58b0d0a commit 204c8e3

7 files changed

Lines changed: 65 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.py[cod]
22
*.swp
3+
.pylintrc
34
*~
45
*.lock
56
*.DS_Store

TODO

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
0.7.1: - clean-up blacked code and pylint it
22
0.7.1: - add unittest for defusedxml to fix billionlaugh and external entities security issues
3-
0.7.1: - add CSV backend support
43
0.7.1: - Change License from CC-BY to Apache 2.0
5-
0.7.1: - Update readme file to reflect the xml fixed issue + how to install it
4+
0.7.1: - Enabled defusedxml support as preferred option for parsing ()
65
0.7.1: - add extra_requires for plugins deps and defusedxml
76
0.7.1: - Remove code duplication in sudo_run and sudo_run_background from process.py
8-
0.7.1: - Merge PR79 from Shouren to fix empty nmap outputs
7+
0.7.1: - Fix empty nmap outputs due to subprocess race condition (Merge PR79 from @Shouren)
8+
0.7.1: - Added banner_dict support + unittest (Merge edited PR from @cfoulds)
9+
0.7.2: - add CSV backend support
910
0.7.2: improve API for NSE scripts
1011
0.7.2: add support for post,pre and host scripts
1112
0.7.2: add a Contribution guideline page

libnmap/objects/host.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def __hash__(self):
113113
return (
114114
hash(self.status)
115115
^ hash(self.address)
116+
^ hash(self._mac_addr)
116117
^ hash(frozenset(self._services))
117118
^ hash(frozenset(" ".join(self._hostnames)))
118119
)

libnmap/test/files/1_host_ping.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE nmaprun>
3+
<?xml-stylesheet href="file:///usr/bin/../share/nmap/nmap.xsl" type="text/xsl"?>
4+
<!-- Nmap 7.91 scan initiated Sun Dec 6 19:15:35 2020 as: nmap -sP -oX /tmp/a.xml dionaea -->
5+
<nmaprun scanner="nmap" args="nmap -sP -oX /tmp/a.xml dionaea" start="1607282135" startstr="Sun Dec 6 19:15:35 2020" version="7.91" xmloutputversion="1.05">
6+
<verbose level="0"/>
7+
<debugging level="0"/>
8+
<host><status state="up" reason="arp-response" reason_ttl="0"/>
9+
<address addr="172.28.1.3" addrtype="ipv4"/>
10+
<address addr="02:42:AC:1C:01:03" addrtype="mac"/>
11+
<hostnames>
12+
<hostname name="dionaea" type="user"/>
13+
<hostname name="dionaea.honeynet_testing_net" type="PTR"/>
14+
</hostnames>
15+
<times srtt="44" rttvar="5000" to="100000"/>
16+
</host>
17+
<runstats><finished time="1607282136" timestr="Sun Dec 6 19:15:36 2020" summary="Nmap done at Sun Dec 6 19:15:36 2020; 1 IP address (1 host up) scanned in 0.19 seconds" elapsed="0.19" exit="success"/><hosts up="1" down="0" total="1"/>
18+
</runstats>
19+
</nmaprun>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE nmaprun>
3+
<?xml-stylesheet href="file:///usr/bin/../share/nmap/nmap.xsl" type="text/xsl"?>
4+
<!-- Nmap 7.91 scan initiated Sun Dec 6 19:15:35 2020 as: nmap -sP -oX /tmp/a.xml dionaea -->
5+
<nmaprun scanner="nmap" args="nmap -sP -oX /tmp/a.xml dionaea" start="1607282135" startstr="Sun Dec 6 19:15:35 2020" version="7.91" xmloutputversion="1.05">
6+
<verbose level="0"/>
7+
<debugging level="0"/>
8+
<host><status state="up" reason="arp-response" reason_ttl="0"/>
9+
<address addr="172.28.1.3" addrtype="ipv4"/>
10+
<address addr="02:42:AC:1C:01:04" addrtype="mac"/>
11+
<hostnames>
12+
<hostname name="dionaea" type="user"/>
13+
<hostname name="dionaea.honeynet_testing_net" type="PTR"/>
14+
</hostnames>
15+
<times srtt="44" rttvar="5000" to="100000"/>
16+
</host>
17+
<runstats><finished time="1607282136" timestr="Sun Dec 6 19:15:36 2020" summary="Nmap done at Sun Dec 6 19:15:36 2020; 1 IP address (1 host up) scanned in 0.19 seconds" elapsed="0.19" exit="success"/><hosts up="1" down="0" total="1"/>
18+
</runstats>
19+
</nmaprun>

libnmap/test/test_host.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
import os
45
import unittest
56

67
from libnmap.parser import NmapParser
@@ -111,6 +112,7 @@
111112
<times srtt="2100" rttvar="688" to="100000"/>
112113
</host>
113114
"""
115+
114116
host4 = """
115117
<host starttime="77" endtime="13">
116118
<status state="up" reason="locaonse"/>
@@ -151,7 +153,7 @@
151153
class TestNmapHost(unittest.TestCase):
152154
def setUp(self):
153155
self.fdir = os.path.dirname(os.path.realpath(__file__))
154-
self.dionaea_path = "{0}/files/dionaea_scan.xml"
156+
self.dionaea_path = "{0}/files/dionaea_scan.xml".format(self.fdir)
155157

156158
def test_eq_host(self):
157159
h1 = NmapParser.parse(host1)

libnmap/test/test_report.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,28 @@ def test_host_address_unchanged(self):
249249
"NmapService::tcp.3306",
250250
"address",
251251
"NmapService::tcp.25",
252+
"mac_addr",
252253
]
253254
),
254255
)
255256

257+
def test_diff_mac(self):
258+
fdir = os.path.dirname(os.path.realpath(__file__))
259+
host_ping = "{0}/files/1_host_ping.xml".format(fdir)
260+
host_ping_mac_changed = (
261+
"{0}/files/diff_1_host_ping_mac_changed.xml".format(fdir)
262+
)
263+
264+
report_mac_original = NmapParser.parse_fromfile(host_ping)
265+
report_mac_changed = NmapParser.parse_fromfile(host_ping_mac_changed)
266+
267+
report_diff = report_mac_original.diff(report_mac_changed)
268+
self.assertEqual(report_diff.changed(), set(["NmapHost::172.28.1.3"]))
269+
host_original = report_mac_original.hosts[0]
270+
host_mac_changed = report_mac_changed.hosts[0]
271+
host_diff = host_original.diff(host_mac_changed)
272+
self.assertEqual(host_diff.changed(), set(["mac_addr"]))
273+
256274

257275
if __name__ == "__main__":
258276
test_suite = [

0 commit comments

Comments
 (0)