Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Domain Scanner #1146

Merged
merged 46 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
ba4d38e
dns_resolver
kshitijk4poor Jul 12, 2024
1b370bb
Merge branch 'CERT-Polska:main' into main
kshitijk4poor Jul 12, 2024
a578fb4
linting
kshitijk4poor Jul 13, 2024
52098dc
lookup for NS and A records
kshitijk4poor Jul 17, 2024
4852348
fixed lint
kshitijk4poor Jul 17, 2024
bd5f25a
Merge branch 'CERT-Polska:main' into main
kshitijk4poor Jul 18, 2024
1c7aa6d
yeahhh
kshitijk4poor Jul 22, 2024
5876115
added task
kshitijk4poor Jul 22, 2024
cd7d4c0
Merge branch 'CERT-Polska:main' into main
kshitijk4poor Jul 27, 2024
9bac8ef
passing pre-commit
kshitijk4poor Jul 29, 2024
1101e33
updated logic
kshitijk4poor Jul 29, 2024
15d32cd
fixed whitespace-trail
kshitijk4poor Jul 29, 2024
d70c894
should fix lint
kshitijk4poor Jul 29, 2024
57d4a4e
added test
kshitijk4poor Jul 29, 2024
e255f09
implemented the module as a function
kshitijk4poor Aug 2, 2024
23bd878
cleanup
kshitijk4poor Aug 2, 2024
c9fd4d6
lint
kshitijk4poor Aug 2, 2024
7ce1ecd
fixed
kshitijk4poor Aug 2, 2024
78ee50c
using the payload
kshitijk4poor Aug 2, 2024
481a9d7
fixed param
kshitijk4poor Aug 2, 2024
e5dcdfe
fixed context
kshitijk4poor Aug 2, 2024
9c7b86b
update
kshitijk4poor Aug 2, 2024
480d432
fixed
kshitijk4poor Aug 3, 2024
a6cfe1a
lint pass
kshitijk4poor Aug 3, 2024
c887da8
finally working
kshitijk4poor Aug 3, 2024
e4678e2
fixed with better error handling
kshitijk4poor Aug 3, 2024
7b08d3c
better logging and documentation
kshitijk4poor Aug 3, 2024
01dbe78
.
kshitijk4poor Aug 3, 2024
0e969ef
fixed triling-whitespaces and end-of-file for docker-compose.yaml
kshitijk4poor Aug 3, 2024
bdf4c06
fixed test
kshitijk4poor Aug 5, 2024
2feb681
lint
kshitijk4poor Aug 5, 2024
dc01949
Merge branch 'CERT-Polska:main' into main
kshitijk4poor Aug 5, 2024
dc356d4
fixed return
kshitijk4poor Aug 9, 2024
594aecd
lint
kshitijk4poor Aug 9, 2024
da27aa4
fix
kshitijk4poor Aug 9, 2024
a0330a5
lint
kshitijk4poor Aug 9, 2024
1977c41
Merge branch 'CERT-Polska:main' into main
kshitijk4poor Aug 9, 2024
a81a1db
fixed
kshitijk4poor Aug 13, 2024
9003dc2
lint
kshitijk4poor Aug 13, 2024
26e65f3
lint
kshitijk4poor Aug 14, 2024
5fb5944
Merge branch 'CERT-Polska:main' into main
kshitijk4poor Aug 21, 2024
4fbec0a
fix
kazet Sep 12, 2024
13bb41f
.
kazet Sep 12, 2024
808fe37
Merge branch 'CERT-Polska:main' into main
kshitijk4poor Sep 19, 2024
60cda04
Merge
kazet Oct 2, 2024
6c7dd56
fix
kazet Oct 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion artemis/module_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from artemis.db import DB
from artemis.domains import is_domain
from artemis.redis_cache import RedisCache
from artemis.resolvers import lookup
from artemis.resolvers import NoAnswer, ResolutionException, lookup
from artemis.resource_lock import FailedToAcquireLockException, ResourceLock
from artemis.retrying_resolver import setup_retrying_resolver
from artemis.task_utils import (
Expand Down Expand Up @@ -123,6 +123,52 @@ def add_task(self, current_task: Task, new_task: Task) -> None:
else:
self.log.info("Task is not a new task, not adding: %s", new_task)

def add_task_if_domain_exists(self, current_task: Task, new_task: Task) -> None:
"""
Add a new task if the domain in the task payload exists.

Args:
current_task (Task): The current task being processed.
new_task (Task): The new task to potentially add.
"""
domain = new_task.payload.get("domain")
if not domain:
self.log.info("No domain found in new task payload - adding it, as it might be an IP task")
self.add_task(current_task, new_task)
return

if self.check_domain_exists(domain):
self.add_task(current_task, new_task)
else:
self.log.info("Skipping invalid domain: %s", domain)

def check_domain_exists(self, domain: str) -> bool:
"""
Check if a domain exists by looking up its NS and A records.

Args:
domain (str): The domain to check.

Returns:
bool: True if the domain exists, False otherwise.
"""
try:
# Check for NS records
try:
ns_records = lookup(domain, "NS")
if ns_records:
return True
except NoAnswer:
# No NS records, continue to check A records
pass

# Check for A records
a_records = lookup(domain, "A")
return len(a_records) > 0 # returns true if found

except ResolutionException:
return False

def loop(self) -> None:
"""
Differs from the original karton implementation: consumes the tasks in random order, so that
Expand Down
2 changes: 1 addition & 1 deletion artemis/modules/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def run(self, current_task: Task) -> None:
},
)

self.add_task(current_task, new_task)
self.add_task_if_domain_exists(current_task, new_task)


if __name__ == "__main__":
Expand Down
15 changes: 8 additions & 7 deletions artemis/modules/subdomain_enumeration.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,14 @@ def run(self, current_task: Task) -> None:

# We save the task as soon as we have results from a single tool so that other kartons can do something.
for subdomain in valid_subdomains_from_tool:
task = Task(
{"type": TaskType.DOMAIN},
payload={
"domain": subdomain,
},
)
self.add_task(current_task, task)
if subdomain != domain: # ensure we are not adding the parent domain again
task = Task(
{"type": TaskType.DOMAIN},
payload={
"domain": subdomain,
},
)
self.add_task_if_domain_exists(current_task, task)

valid_subdomains.update(valid_subdomains_from_tool)

Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ services:
restart: always
volumes: ["./docker/karton.ini:/etc/karton/karton.ini", "${DOCKER_COMPOSE_ADDITIONAL_SHARED_DIRECTORY:-./shared}:/shared/"]


karton-dashboard:
depends_on: [karton-system, karton-logger]
env_file: .env
Expand Down Expand Up @@ -163,7 +162,6 @@ services:
restart: always
volumes: ["./docker/karton.ini:/etc/karton/karton.ini", "${DOCKER_COMPOSE_ADDITIONAL_SHARED_DIRECTORY:-./shared}:/shared/"]


karton-http_service_to_url:
<<: *artemis-build-or-image
command: "python3 -m artemis.modules.http_service_to_url"
Expand Down
12 changes: 8 additions & 4 deletions test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import socket
import tempfile
from pathlib import Path
from typing import List
from typing import Any, List, Set
from unittest.mock import MagicMock, patch

from jinja2 import BaseLoader, Environment, StrictUndefined, Template
Expand Down Expand Up @@ -40,10 +40,14 @@ def setUp(self) -> None:
# Unfortunately, in the context of a test that is about to run and a respective module has already been
# imported, to mock lookup we need to mock it in modules it has been imported to,
# so we need to enumerate the locations it's used in in the list below.
def mock_lookup(host: str, *args: Any) -> Set[str]:
try:
return {socket.gethostbyname(host)}
except socket.gaierror:
return set() # Return an empty set if resolution fails

for item in ["artemis.module_base.lookup", "artemis.modules.port_scanner.lookup"]:
# We cannot use Artemis default DoH resolvers as they wouldn't be able to resolve
# internal test services' addresses.
self._lookup_mock = patch(item, MagicMock(side_effect=lambda host: {socket.gethostbyname(host)}))
self._lookup_mock = patch(item, MagicMock(side_effect=mock_lookup))
self._lookup_mock.__enter__()

self.mock_db = MagicMock()
Expand Down
Loading