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

Generate hash value per execution and use it to identify fixed findings #328

Merged
merged 2 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/backend/executions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Execution(BaseModel):
enqueued_at = models.DateTimeField(blank=True, null=True)
start = models.DateTimeField(blank=True, null=True)
end = models.DateTimeField(blank=True, null=True)
hash = models.TextField(max_length=128, blank=True, null=True)
defectdojo_test_id = models.IntegerField(blank=True, null=True)

def __str__(self) -> str:
Expand Down
18 changes: 6 additions & 12 deletions src/backend/findings/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,9 @@ def consume(execution: Execution, findings: list[Finding]) -> None:
platform.process_alert(alert, finding)
break
if settings.auto_fix_findings:
# TODO: TEST!
completed_executions = Execution.objects.filter(
configuration=execution.configuration,
task__target=execution.task.target,
# task__wordlists__in=execution.task.wordlists.all(),
# task__input_technologies__in=execution.task.input_technologies.all(),
# task__input_vulnerabilities__in=execution.task.input_vulnerabilities.all(),
status=Status.COMPLETED,
).exclude(id=execution.id)
same_executions = Execution.objects.filter(
hash=execution.hash, status=Status.COMPLETED
)
for finding_type in [
OSINT,
Host,
Expand All @@ -83,7 +77,7 @@ def consume(execution: Execution, findings: list[Finding]) -> None:
Exploit,
]:
finding_type.objects.fix(
finding_type.objects.filter( # .exclude(executions__id=execution.id)
executions__in=completed_executions
).all()
finding_type.objects.exclude(executions__id=execution.id)
.filter(executions__in=same_executions)
.all()
)
18 changes: 15 additions & 3 deletions src/backend/tools/executors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from http_headers.models import HttpHeader
from parameters.models import InputTechnology, InputVulnerability
from rekono.settings import CONFIG
from security.cryptography.hashing import hash
from settings.models import Settings
from target_ports.models import TargetPort
from tools.models import Intensity
Expand All @@ -41,6 +42,7 @@ def __init__(self, execution: Execution) -> None:
/ f'{str(uuid.uuid4())}.{execution.configuration.tool.output_format or "txt"}'
)
self.arguments: list[str] = []
self.environment: dict[str, Any] = {}
self.findings_used_in_execution: dict[Any, BaseInput] = {}
self.authentication: Optional[Authentication] = None

Expand Down Expand Up @@ -256,8 +258,18 @@ def _on_completed(self, output: str) -> None:
if self.execution.configuration.tool.output_format and self.report.is_file():
self.execution.output_file = self.report
self.execution.output_plain = output
self.execution.hash = hash(
" ".join(
[f"{k}={v}" for k, v in self.environment.items()]
+ [
a
for a in self.arguments
if str(self.report).lower() not in a.lower()
]
).lower()
)
self.execution.save(
update_fields=["status", "end", "output_file", "output_plain"]
update_fields=["status", "end", "output_file", "output_plain", "hash"]
)
self._on_task_end()

Expand Down Expand Up @@ -288,10 +300,10 @@ def execute(
logger.error(f"[Tool] {str(error)}")
self._on_skip(str(error))
return
environment = self._get_environment()
self.environment = self._get_environment()
self._before_running()
try:
output = "" if CONFIG.testing else self._run(environment)
output = "" if CONFIG.testing else self._run(self.environment)
except (RuntimeError, Exception) as error:
logger.error(
f"[Tool] {self.execution.configuration.tool.name} execution finish with errors"
Expand Down
Loading