Skip to content

Commit

Permalink
Merge pull request #45 from Ostorlab/fix/whatweb_http
Browse files Browse the repository at this point in the history
Fix whatweb scanning non HTTP services.
  • Loading branch information
3asm authored Aug 9, 2024
2 parents f7eeb0d + e0acd75 commit 54a070f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
6 changes: 4 additions & 2 deletions agent/whatweb_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class IPTarget(BaseTarget):
def target(self) -> str:
"""Prepare target."""
url = ""
if self.schema is not None:
if self.schema is not None and self.schema in ("https", "http"):
url += f"{self.schema}://"

url += self.name
Expand Down Expand Up @@ -163,16 +163,18 @@ def process(self, message: msg.Message) -> None:
"""
logger.info("processing message of selector : %s", message.selector)
targets = self._prepare_targets(message)
logger.info("Generated targets %s", targets)
if self._should_target_be_processed(message) is False:
return

for target in targets:
try:
logger.info("Scanning target %s", target)
with tempfile.NamedTemporaryFile() as fp:
self._start_scan(target, fp.name)
self._parse_emit_result(target, io.BytesIO(fp.read()))
except subprocess.CalledProcessError as e:
logger.error(e)
logger.error("Error scanning target `%s`: %s", target, e)

def _prepare_targets(self, message: msg.Message) -> List[IPTarget | DomainTarget]:
"""Returns a list of target objects to be scanned."""
Expand Down
10 changes: 8 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,16 @@ def ip_msg_with_port_schema_mask() -> m.Message:


@pytest.fixture
def ip_msg_with_port_schema_mask_2() -> m.Message:
def ip_tcp_message() -> m.Message:
"""Creates a dummy message of type v3.asset.ip.v4.port.service for testing purposes."""
input_selector = "v3.asset.ip.v4.port.service"
input_data = {"host": "192.168.0.0", "port": 80, "mask": "32", "protocol": "http"}
input_data = {
"host": "192.168.0.0",
"port": 80,
"mask": "32",
"protocol": "tcp",
"version": 4,
}
message = m.Message.from_data(selector=input_selector, data=input_data)
return message

Expand Down
22 changes: 22 additions & 0 deletions tests/whatweb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,25 @@ def testWhatWebAgent_whenIPAssetHasIncorrectVersion_raiseValueError(
"""Test the CIDR Limit in case IP has incorrect version."""
with pytest.raises(ValueError, match="Incorrect ip version 5."):
test_agent.process(scan_message_ipv_with_incorrect_version)


def testWhatWebAgent_whenSchemeIsNotHTTP_defaultToNoScheme(
agent_mock: List[message.Message],
whatweb_test_agent: whatweb_agent.AgentWhatWeb,
ip_tcp_message: message.Message,
mocker: plugin.MockerFixture,
) -> None:
run_mock = mocker.patch("subprocess.run", return_value=None)
with tempfile.TemporaryFile() as fp:
mocker.patch("tempfile.NamedTemporaryFile", return_value=fp)
with open(f"{pathlib.Path(__file__).parent}/ip_output.json", "rb") as op:
fp.write(op.read())
fp.seek(0)
whatweb_test_agent.process(ip_tcp_message)

assert run_mock.call_count == 1
assert run_mock.call_args[0][0] == [
"./whatweb",
"--log-json-verbose=11",
"192.168.0.0:80",
]

0 comments on commit 54a070f

Please sign in to comment.