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

Add tests (and refactor) API extraction and symbol generation #2597

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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 @@ -16,6 +16,8 @@
### Bug Fixes
- cape: make some fields optional @williballenthin #2631 #2632
- lint: add WARN for regex features that contain unescaped dot #2635
- separate execution paths for all supported formats of `api` in `trim_dll_part` #1899 @v1bh475u
- add test for `trim_dll_part` #1899 @v1bh475u

### capa Explorer Web

Expand Down
8 changes: 5 additions & 3 deletions capa/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,13 @@ def trim_dll_part(api: str) -> str:
if ".#" in api:
return api

# .NET namespace, like System.Diagnostics.Debugger::IsLogging, keep the namespace part
if "::" in api:
return api

# kernel32.CreateFileA
if api.count(".") == 1:
if "::" not in api:
# skip System.Convert::FromBase64String
api = api.split(".")[1]
api = api.split(".")[1]
return api


Expand Down
12 changes: 12 additions & 0 deletions tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,3 +1653,15 @@ def test_circular_dependency():
]
with pytest.raises(capa.rules.InvalidRule):
list(capa.rules.get_rules_and_dependencies(rules, rules[0].name))


def test_trim_dll_part():
from capa.rules import trim_dll_part

assert trim_dll_part("GetModuleHandle") == "GetModuleHandle"
assert trim_dll_part("kernel32.CreateFileA") == "CreateFileA"
assert trim_dll_part("System.Convert::FromBase64String") == "System.Convert::FromBase64String"
assert trim_dll_part("System.Diagnostics.Debugger::IsLogging") == "System.Diagnostics.Debugger::IsLogging"
assert trim_dll_part("ws2_32.#1") == "ws2_32.#1"
Comment on lines +1662 to +1665
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems inconsistent to me:

assert trim_dll_part("kernel32.CreateFileA") == "CreateFileA" only export name.
assert trim_dll_part("ws2_32.#1") == "ws2_32.#1" both DLL and export name.

would you please continue to explore how this code is used (e.g., review the callers) and consider if further refactoring needs to be done to make the code less surprising?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so we can remove the dll part for ordinal function. Reason: ordinal is meaningless without the dll part. In case of named functions, only a single library contains them and exports them but ordinal is same for all libraries.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is used for api and count(api:<api_name>) statement building

assert trim_dll_part("Debugger::IsLogging") == "Debugger::IsLogging"
assert trim_dll_part("kernel32.ws2.#1") == "kernel32.ws2.#1"