Skip to content

Commit

Permalink
Handle nmap command crash
Browse files Browse the repository at this point in the history
  • Loading branch information
ErebusZ committed Jan 4, 2024
1 parent b389c7f commit 656aba2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
13 changes: 10 additions & 3 deletions agent/nmap_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ def process(self, message: msg.Message) -> None:
"target %s/%s was processed before, exiting", host, mask
)
return
scan_results, normal_results = self._scan_host(host, mask)
try:
scan_results, normal_results = self._scan_host(host, mask)
except subprocess.CalledProcessError:
logger.error("Nmap command failed to scan host %s", host)
continue
logger.info("scan results %s", scan_results)

self._emit_services(scan_results, domain_name)
Expand All @@ -141,8 +145,11 @@ def process(self, message: msg.Message) -> None:
return
if self._is_domain_in_scope(domain_name) is False:
return

scan_results, normal_results = self._scan_domain(domain_name)
try:
scan_results, normal_results = self._scan_domain(domain_name)
except subprocess.CalledProcessError:
logger.error("Nmap command failed to scan domain name %s", domain_name)
return
logger.info("scan results %s", scan_results)

self._emit_services(scan_results, domain_name)
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,11 @@ def nmap_agent_all_ports(

agent = nmap_agent.NmapAgent(definition, settings)
return agent


@pytest.fixture
def invalid_domain_msg() -> message.Message:
"""Creates a dummy message of type v3.asset.domain_name for testing purposes."""
return message.Message.from_data(
selector="v3.asset.domain_name", data={"name": "-ostorlab.co"}
)
18 changes: 18 additions & 0 deletions tests/nmap_agent_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Unittests for Nmap agent."""
import json
from typing import List, Dict, Union
import subprocess

import requests_mock as rq_mock
from ostorlab.agent.message import message
Expand Down Expand Up @@ -669,3 +670,20 @@ def testNmapAgent_whenIpv6AboveLimit_agentShouldRaiseError(

assert len(agent_mock) == 0
assert error_message.value.args[0] == "Subnet mask below 112 is not supported"


def testAgentNmap_whenInvalidDomainName_doesNotCrash(
nmap_test_agent: nmap_agent.NmapAgent,
agent_mock: List[message.Message],
invalid_domain_msg: message.Message,
mocker: plugin.MockerFixture,
) -> None:
"""Unit test for testing agent handling of an invalid domain name."""
mocker.patch(
"subprocess.run",
side_effect=subprocess.CalledProcessError(255, ""),
)

nmap_test_agent.process(invalid_domain_msg)

assert len(agent_mock) == 0

0 comments on commit 656aba2

Please sign in to comment.