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

rules: use Scope enum instead of constants #1764

Merged
merged 4 commits into from
Aug 25, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Breaking Changes

- remove the `SCOPE_*` constants in favor of the `Scope` enum #1764 @williballenthin

### New Rules (0)

-
Expand Down
8 changes: 4 additions & 4 deletions capa/ida/plugin/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,13 @@ def render_capa_doc_by_program(self, doc: rd.ResultDocument):
location = location_.to_capa()

parent2: CapaExplorerDataItem
if capa.rules.FILE_SCOPE in rule.meta.scopes:
if capa.rules.Scope.FILE in rule.meta.scopes:
parent2 = parent
elif capa.rules.FUNCTION_SCOPE in rule.meta.scopes:
elif capa.rules.Scope.FUNCTION in rule.meta.scopes:
parent2 = CapaExplorerFunctionItem(parent, location)
elif capa.rules.BASIC_BLOCK_SCOPE in rule.meta.scopes:
elif capa.rules.Scope.BASIC_BLOCK in rule.meta.scopes:
parent2 = CapaExplorerBlockItem(parent, location)
elif capa.rules.INSTRUCTION_SCOPE in rule.meta.scopes:
elif capa.rules.Scope.INSTRUCTION in rule.meta.scopes:
parent2 = CapaExplorerInstructionItem(parent, location)
else:
raise RuntimeError("unexpected rule scope: " + str(rule.meta.scopes.static))
Expand Down
4 changes: 2 additions & 2 deletions capa/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ def compute_dynamic_layout(rules, extractor: DynamicFeatureExtractor, capabiliti
matched_threads = set()
for rule_name, matches in capabilities.items():
rule = rules[rule_name]
if capa.rules.THREAD_SCOPE in rule.scopes:
if capa.rules.Scope.THREAD in rule.scopes:
for addr, _ in matches:
assert addr in processes_by_thread
matched_threads.add(addr)
Expand Down Expand Up @@ -1099,7 +1099,7 @@ def compute_static_layout(rules, extractor: StaticFeatureExtractor, capabilities
matched_bbs = set()
for rule_name, matches in capabilities.items():
rule = rules[rule_name]
if capa.rules.BASIC_BLOCK_SCOPE in rule.scopes:
if capa.rules.Scope.BASIC_BLOCK in rule.scopes:
for addr, _ in matches:
assert addr in functions_by_bb
matched_bbs.add(addr)
Expand Down
2 changes: 1 addition & 1 deletion capa/render/verbose.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def render_rules(ostream, doc: rd.ResultDocument):

rows.append((key, v))

if capa.rules.FILE_SCOPE not in rule.meta.scopes:
if capa.rules.Scope.FILE not in rule.meta.scopes:
locations = [m[0] for m in doc.rules[rule.meta.name].matches]
rows.append(("matches", "\n".join(map(format_address, locations))))

Expand Down
6 changes: 3 additions & 3 deletions capa/render/vverbose.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def render_rules(ostream, doc: rd.ResultDocument):

ostream.writeln(tabulate.tabulate(rows, tablefmt="plain"))

if capa.rules.FILE_SCOPE in rule.meta.scopes:
if capa.rules.Scope.FILE in rule.meta.scopes:
matches = doc.rules[rule.meta.name].matches
if len(matches) != 1:
# i think there should only ever be one match per file-scope rule,
Expand All @@ -379,13 +379,13 @@ def render_rules(ostream, doc: rd.ResultDocument):
ostream.write(" @ ")
ostream.write(capa.render.verbose.format_address(location))

if capa.rules.BASIC_BLOCK_SCOPE in rule.meta.scopes:
if capa.rules.Scope.BASIC_BLOCK in rule.meta.scopes:
ostream.write(
" in function "
+ capa.render.verbose.format_address(frz.Address.from_capa(functions_by_bb[location.to_capa()]))
)

if capa.rules.THREAD_SCOPE in rule.meta.scopes:
if capa.rules.Scope.THREAD in rule.meta.scopes:
ostream.write(
" in process "
+ capa.render.verbose.format_address(
Expand Down
Loading