-
Notifications
You must be signed in to change notification settings - Fork 627
os, arch, and format features #723
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
Merged
Merged
Changes from 51 commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
aefe97e
rules: fix typos
a1eca58
features: support characteristic(os/*) features
e797a67
features: define CHARACTERISTIC_OS constants for ease of use
06f8943
features: add format/pe and format/elf characteristics
20859d2
extractors: pefile: extract OS and format
97092c9
tests: assert absence of the wrong os/format
753b003
pep8
05f8e24
fixtures: add tests demonstrating extraction of features from ELF files
baaa8ba
scripts: add script to detect ELF OS
37bc47c
extractors: viv: extract from bytes not file path
7205862
helpers: move ELF and IDA helpers out of script and into common module
fa8b4a4
extractors: add common routine to extract OS from ELF
294f74b
extractors: viv: extract format and OS at all scopes
a7678e7
extractors: smda: extract format and OS characteristics at all scopes
769d354
detect-elf-os: remove extra print statement
c1910d4
move is_global_feature into capa.features.common
71d9ebd
extractors: ida: extract OS and file format characteristics at all sc…
34819b2
pep8
30d7425
changelog
d5c9a5c
mypy: ignore ida_loader
f013815
features: rename legacy term `arch` to `bitness`
ab1326f
features: move OS and Format to their own features, not characteristics
5405e18
features: move Format features to file scope
738fa91
fixtures: update tests to account for Format scope
8e689c3
features: add Arch feature at global scope
fd47b03
render: vverbose: don't render locations of global scope features
98c00bd
extractors: add missing global_.py files
0065876
extractors: ida: move os extraction to global module
92dfa99
extractors: log unsupported os/arch/format but don't except
909ffc1
Merge branch 'master' into feature-701
williballenthin ac5d163
pep8
0c3a38b
Merge branch 'feature-701' of github.com:fireeye/capa into feature-701
f1df29d
tests: xfail smda ELF API
a35f5a1
elf: detect FreeBSD via note
8960358
elf: add some doc
766ac7e
Merge branch 'master' of github.com:fireeye/capa into feature-701
249b849
pefile: extract Arch
e124115
Merge branch 'master' into feature-701
williballenthin f0a34fd
merge
cf17eba
Merge branch 'feature-701' of github.com:fireeye/capa into feature-701
45b6c8d
setup: bump SMDA dep ver
a96a5de
tests: re-enable SMDA ELF API tests
3cb7573
enable os/arch/format for capa explorer
mike-hunhoff dae7be0
elf: fix alignment calculation
williballenthin 04cc94a
main: detect invalid arch and os
3eaeb53
Merge branch 'feature-701' of github.com:fireeye/capa into feature-701
aef03b5
elf: fix type error caught by mypy!
1b9a6c3
main: collect os/format/arch into metadata and render it
b6ab12d
Update capa/features/common.py
williballenthin a90e93e
Update capa/main.py
williballenthin 2ba000a
Merge branch 'master' into feature-701
williballenthin c0fe042
changelog: tweak PR ref
6961fde
Merge branch 'feature-701' of github.com:fireeye/capa into feature-701
a1bf95e
features: formatting of OS constants
6482f67
elf: document unused OS constants
dab88e4
elf: add more explanation about ei_osabi
a729bdf
elf: more clearly set first detected OS
30a5493
tests: smda: remove unused import
fc73787
extractors: file extractor arg consistency via kwargs
a4b0954
viv: ignore mypy FP
56f9e16
tests: viv: disable ELF tests due to #735
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import io | ||
| import logging | ||
| import binascii | ||
| import contextlib | ||
|
|
||
| import pefile | ||
|
|
||
| import capa.features.extractors.elf | ||
| import capa.features.extractors.pefile | ||
| from capa.features.common import OS, FORMAT_PE, FORMAT_ELF, OS_WINDOWS, Arch, Format | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def extract_format(buf): | ||
| if buf.startswith(b"MZ"): | ||
| yield Format(FORMAT_PE), 0x0 | ||
| elif buf.startswith(b"\x7fELF"): | ||
| yield Format(FORMAT_ELF), 0x0 | ||
| else: | ||
| # we likely end up here: | ||
| # 1. handling a file format (e.g. macho) | ||
| # | ||
| # for (1), this logic will need to be updated as the format is implemented. | ||
| logger.debug("unsupported file format: %s", binascii.hexlify(buf[:4]).decode("ascii")) | ||
| return | ||
|
|
||
|
|
||
| def extract_arch(buf): | ||
| if buf.startswith(b"MZ"): | ||
| yield from capa.features.extractors.pefile.extract_file_arch(pefile.PE(data=buf), "hack: path not provided") | ||
williballenthin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| elif buf.startswith(b"\x7fELF"): | ||
| with contextlib.closing(io.BytesIO(buf)) as f: | ||
| arch = capa.features.extractors.elf.detect_elf_arch(f) | ||
|
|
||
| if arch == "unknown": | ||
| logger.debug("unsupported arch: %s", arch) | ||
| return | ||
|
|
||
| yield Arch(arch), 0x0 | ||
|
|
||
| else: | ||
| # we likely end up here: | ||
| # 1. handling shellcode, or | ||
| # 2. handling a new file format (e.g. macho) | ||
| # | ||
| # for (1) we can't do much - its shellcode and all bets are off. | ||
| # we could maybe accept a futher CLI argument to specify the arch, | ||
| # but i think this would be rarely used. | ||
| # rules that rely on arch conditions will fail to match on shellcode. | ||
| # | ||
| # for (2), this logic will need to be updated as the format is implemented. | ||
| logger.debug("unsupported file format: %s, will not guess Arch", binascii.hexlify(buf[:4]).decode("ascii")) | ||
| return | ||
|
|
||
|
|
||
| def extract_os(buf): | ||
| if buf.startswith(b"MZ"): | ||
| yield OS(OS_WINDOWS), 0x0 | ||
| elif buf.startswith(b"\x7fELF"): | ||
| with contextlib.closing(io.BytesIO(buf)) as f: | ||
| os = capa.features.extractors.elf.detect_elf_os(f) | ||
|
|
||
| yield OS(os), 0x0 | ||
| else: | ||
| # we likely end up here: | ||
| # 1. handling shellcode, or | ||
| # 2. handling a new file format (e.g. macho) | ||
| # | ||
| # for (1) we can't do much - its shellcode and all bets are off. | ||
| # we could maybe accept a futher CLI argument to specify the OS, | ||
| # but i think this would be rarely used. | ||
| # rules that rely on OS conditions will fail to match on shellcode. | ||
| # | ||
| # for (2), this logic will need to be updated as the format is implemented. | ||
| logger.debug("unsupported file format: %s, will not guess OS", binascii.hexlify(buf[:4]).decode("ascii")) | ||
| return | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.