-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_nmap_output_parser.py
More file actions
97 lines (77 loc) · 3.23 KB
/
test_nmap_output_parser.py
File metadata and controls
97 lines (77 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from unittest import TestCase
from injector_common.targets import TargetExtractionResult
from nmap.src.helpers.nmap_output_parser import NmapOutputParser
class NmapOutputParserTest(TestCase):
def setUp(self):
self.result_single_host = {
"nmaprun": {
"host": {
"address": {"@addr": "172.16.5.10"},
"ports": {
"port": [
{
"@portid": "21",
"state": {"@state": "open"},
"service": {"@name": "ftp"},
}
]
},
}
}
}
# -------------------------------
# Tests
# -------------------------------
def test_parse_target_assets(self):
"""Ensure target_selector='assets' uses asset_list and sets asset_id."""
data = {"injection": {"inject_content": {"target_selector": "assets"}}}
result = NmapOutputParser.parse(
data,
self.result_single_host,
TargetExtractionResult(
ip_to_asset_id_map={"172.16.5.10": "asset-123"}, targets=[]
),
)
scan = result["outputs"]["scan_results"][0]
self.assertEqual(scan["asset_id"], "asset-123")
self.assertEqual(scan["host"], "172.16.5.10")
self.assertEqual(scan["port"], 21)
self.assertEqual(scan["service"], "ftp")
def test_parse_target_asset_groups(self):
"""Ensure target_selector='asset-groups' also uses asset_list."""
data = {"injection": {"inject_content": {"target_selector": "asset-groups"}}}
result = NmapOutputParser.parse(
data,
self.result_single_host,
TargetExtractionResult(
ip_to_asset_id_map={"172.16.5.10": "group-asset-555"},
targets=["172.16.5.10"],
),
)
scan = result["outputs"]["scan_results"][0]
self.assertEqual(scan["asset_id"], "group-asset-555")
self.assertEqual(scan["host"], "172.16.5.10")
self.assertEqual(scan["port"], 21)
self.assertEqual(scan["service"], "ftp")
def test_parse_target_manual(self):
"""Ensure target_selector='manual' sets asset_id=None."""
data = {"injection": {"inject_content": {"target_selector": "manual"}}}
result = NmapOutputParser.parse(
data,
self.result_single_host,
TargetExtractionResult(ip_to_asset_id_map={}, targets=["172.16.5.10"]),
)
scan = result["outputs"]["scan_results"][0]
self.assertIsNone(scan["asset_id"])
self.assertEqual(scan["host"], "172.16.5.10")
def test_parse_target_manual_None_values(self):
"""Ensure target_selector='manual' sets asset_id=None."""
data = {"injection": {"inject_content": {"target_selector": "manual"}}}
result = NmapOutputParser.parse(
data,
self.result_single_host,
TargetExtractionResult(ip_to_asset_id_map={}, targets=[]),
)
scan = result["outputs"]["scan_results"][0]
self.assertIsNone(scan["asset_id"])
self.assertIsNone(scan["host"])