Skip to content

Commit

Permalink
Aborting on invalid signature selectors also
Browse files Browse the repository at this point in the history
  • Loading branch information
alterakey committed Mar 9, 2024
1 parent 5e15cf2 commit 2644fad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 4 additions & 1 deletion trueseeing/app/cmd/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ async def _scan(self, args: deque[str]) -> None:
context = await self._helper.get_context().analyze(level=3)
limit = self._helper.get_graph_size_limit(self._helper.get_modifiers(args))
sigsels = self._get_sigsels(self._helper.get_modifiers(args))
scanner = Scanner(context, sigsels=sigsels, max_graph_size=limit)
try:
scanner = Scanner(context, sigsels=sigsels, max_graph_size=limit)
except ValueError as e:
ui.fatal('invalid sigsels found: {}'.format(', '.join(sorted(e.args[0]))))

if not scanner.get_active_signatures():
ui.fatal('no signature selected')
Expand Down
24 changes: 23 additions & 1 deletion trueseeing/core/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from contextlib import contextmanager

if TYPE_CHECKING:
from typing import List, Optional, Iterator, Dict, Any
from typing import List, Optional, Iterator, Dict, Any, Set
from trueseeing.api import SignatureEntry, SignatureHelper, SignatureMap
from trueseeing.core.context import Context, ContextType
from trueseeing.core.db import Query
Expand Down Expand Up @@ -92,19 +92,41 @@ def _match(sigid: str, sel: str) -> Optional[bool]:
else:
return False

def _require_sigsels_fully_applied(self, sigsels: List[str], known: Set[str]) -> None:
def _regen(key: str, neg: bool) -> str:
return ('no-' if neg else '') + key
unknown = set()
for sel in set(sigsels):
neg = False
if sel.startswith('no-'):
sel = sel[3:]
neg = True
if sel != 'all':
if sel.endswith('-all'):
psel = sel[:-4]
if psel and not any((f'{psel}-' in k) for k in known):
unknown.add(_regen(sel, neg))
elif sel not in known:
unknown.add(_regen(sel, neg))
if unknown:
raise ValueError(unknown)

def _init_sigs(self, sigsels: List[str]) -> None:
from itertools import chain
from trueseeing.sig import discover
from trueseeing.core.ext import Extension
known = set()
for clazz in chain(discover(), Extension.get().get_signatures()):
matched = False
t = clazz.create(self._helper)
for k,v in t.get_sigs().items():
known.add(k)
if self._sigsel_matches(k, sigsels):
self._sigs[k] = v
matched = True
if matched:
self._confbag.update(t.get_configs())
self._require_sigsels_fully_applied(sigsels, known)

class SignatureHelperImpl:
def __init__(self, scanner: Scanner) -> None:
Expand Down

0 comments on commit 2644fad

Please sign in to comment.