This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make use of python-nmap * Add test for nmap result parse * Trick nmap wrapper to thing we have nmap installed
- Loading branch information
1 parent
77ed156
commit 9deef02
Showing
5 changed files
with
212 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env bash | ||
# This script is used to trick travis (actually the python-nmap lib) | ||
# to think that nmap is istalled (which is a requirement). | ||
|
||
echo 'Nmap version 7.80 ( https://nmap.org ) | ||
Platform: x86_64-apple-darwin19.5.0 | ||
Compiled with: nmap-liblua-5.3.5 openssl-1.1.1g nmap-libssh2-1.8.2 libz-1.2.11 nmap-libpcre-7.6 libpcap-1.9.1 nmap-libdnet-1.12 ipv6 | ||
Compiled without: | ||
Available nsock engines: kqueue poll select' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ setuptools>=41.0.1 | |
coverage==4.5.3 | ||
pytest-cov==2.7.1 | ||
termcolor==1.1.0 | ||
python-nmap==0.6.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import pytest | ||
import nmap | ||
from unittest.mock import MagicMock | ||
from illuminatio.illuminatio_runner import ( | ||
build_result_string, | ||
extract_results_from_nmap, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_input,expected", | ||
[ | ||
( | ||
{ | ||
"port": "80", | ||
"target": "test", | ||
"should_be_blocked": False, | ||
"was_blocked": False, | ||
}, | ||
"Test test:80 succeeded\nCould reach test on port 80. Expected target to be reachable", | ||
), | ||
( | ||
{ | ||
"port": "80", | ||
"target": "test", | ||
"should_be_blocked": False, | ||
"was_blocked": True, | ||
}, | ||
"Test test:80 failed\nCouldn't reach test on port 80. Expected target to be reachable", | ||
), | ||
( | ||
{ | ||
"port": "80", | ||
"target": "test", | ||
"should_be_blocked": True, | ||
"was_blocked": False, | ||
}, | ||
"Test test:-80 failed\nCould reach test on port 80. Expected target to not be reachable", | ||
), | ||
( | ||
{ | ||
"port": "80", | ||
"target": "test", | ||
"should_be_blocked": True, | ||
"was_blocked": True, | ||
}, | ||
"Test test:-80 succeeded\nCouldn't reach test on port 80. Expected target to not be reachable", | ||
), | ||
], | ||
) | ||
def test_build_result_string(test_input, expected): | ||
assert build_result_string(**test_input) == expected | ||
|
||
|
||
def create_nmap_mock(hosts: list()): | ||
nmap_mock = nmap.PortScanner() | ||
nmap_mock.all_hosts = MagicMock(return_value=hosts) | ||
nmap_mock._scan_result = MagicMock(return_value={"scan"}) | ||
if len(hosts) > 0: | ||
nmap_mock[hosts[0]].all_protocols = MagicMock(return_value=["tcp"]) | ||
nmap_mock[hosts[0]]["tcp"].keys = MagicMock(return_value=[80]) | ||
nmap_mock[hosts[0]].tcp = MagicMock( | ||
return_value={"state": "open", "reason": "syn-ack", "name": "http"} | ||
) | ||
|
||
return nmap_mock | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_input,expected", | ||
[ | ||
( | ||
{"nmap_res": create_nmap_mock([]), "port_on_nums": {}, "target": "test"}, | ||
{ | ||
"": { | ||
"error": "Found 0 hosts in nmap results, expected 1.", | ||
"success": False, | ||
} | ||
}, | ||
), | ||
( | ||
{ | ||
"nmap_res": create_nmap_mock(["123.321.123.321"]), | ||
"port_on_nums": {"80": "80"}, | ||
"target": "test", | ||
}, | ||
{ | ||
"80": { | ||
"nmap-state": "open", | ||
"string": "Test test:80 succeeded\n" | ||
"Could reach test on port 80. Expected target to be " | ||
"reachable", | ||
"success": True, | ||
} | ||
}, | ||
), | ||
( | ||
{ | ||
"nmap_res": create_nmap_mock(["123.321.123.321"]), | ||
"port_on_nums": {"80": "-80"}, | ||
"target": "test", | ||
}, | ||
{ | ||
"-80": { | ||
"nmap-state": "open", | ||
"string": "Test test:-80 failed\n" | ||
"Could reach test on port 80. Expected target to not be " | ||
"reachable", | ||
"success": False, | ||
} | ||
}, | ||
), | ||
( | ||
{ | ||
"nmap_res": create_nmap_mock(["::1"]), | ||
"port_on_nums": {"80": "-80"}, | ||
"target": "test", | ||
}, | ||
{ | ||
"-80": { | ||
"nmap-state": "open", | ||
"string": "Test test:-80 failed\n" | ||
"Could reach test on port 80. Expected target to not be " | ||
"reachable", | ||
"success": False, | ||
} | ||
}, | ||
), | ||
], | ||
) | ||
def test_extract_results_from_nmap(test_input, expected): | ||
assert extract_results_from_nmap(**test_input) == expected |