-
Notifications
You must be signed in to change notification settings - Fork 586
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
Extracts web domain and IP address, implements rendering functions and tests #1944
Closed
Closed
Changes from all commits
Commits
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 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 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,110 @@ | ||
from typing import Iterator, Tuple, List | ||
from pathlib import Path | ||
|
||
from capa.helpers import is_runtime_ida, get_auto_format, is_runtime_ghidra | ||
from capa.exceptions import UnsupportedFormatError | ||
from capa.features.common import FORMAT_PE, FORMAT_ELF, FORMAT_CAPE, String | ||
from capa.features.address import Address | ||
from capa.features.extractors import ( | ||
ida, | ||
ghidra, | ||
elffile, | ||
viv, | ||
pefile, | ||
binja, | ||
dnfile, | ||
cape, | ||
) | ||
|
||
from capa.render.result_document import ResultDocument | ||
from capa.features.extractors.base_extractor import FeatureExtractor | ||
|
||
CD = Path(__file__).resolve().parent.parent.parent | ||
|
||
# these constants are also defined in capa.main | ||
# defined here to avoid a circular import | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhpas define a script with all these constants inside? It is better than having repeated code |
||
BACKEND_VIV = "vivisect" | ||
BACKEND_DOTNET = "dotnet" | ||
BACKEND_BINJA = "binja" | ||
BACKEND_PEFILE = "pefile" | ||
|
||
|
||
def get_file_strings(doc: ResultDocument) -> Iterator[str]: | ||
"""extract strings from a given file""" | ||
extractor = get_extractor_from_doc(doc) | ||
if is_runtime_ida(): | ||
strings = fix_up(ida.helpers.extract_file_strings()) | ||
elif is_runtime_ghidra(): | ||
strings = fix_up(ghidra.helpers.extract_file_strings()) | ||
else: | ||
file = get_file_path(doc) | ||
format_ = get_auto_format(file) | ||
buf = file.read_bytes() | ||
if format_ == FORMAT_ELF: | ||
strings = fix_up(elffile.extract_file_strings(buf)) | ||
elif format_ == BACKEND_VIV: | ||
strings = fix_up(viv.file.extract_file_strings(buf)) | ||
elif format_ == BACKEND_PEFILE or format_ == FORMAT_PE: | ||
strings = fix_up(pefile.extract_file_strings(buf)) | ||
elif format_ == BACKEND_BINJA: | ||
strings = fix_up(binja.file.extract_file_strings(extractor.bv)) | ||
elif format_ == BACKEND_DOTNET: | ||
strings = fix_up(dnfile.file.extract_file_strings(extractor.pe)) | ||
elif format_ == FORMAT_CAPE: | ||
strings = fix_up(cape.file.extract_file_strings(extractor.report)) | ||
else: | ||
raise UnsupportedFormatError(f"Unknown file format! Format: {format_}") | ||
|
||
return strings | ||
|
||
|
||
def fix_up(obj: Iterator[Tuple[String, Address]]) -> List[str]: | ||
""" | ||
basically a wrapper for 'extract_file_strings' calls | ||
to actually get list of strings | ||
""" | ||
strings = [] | ||
for tuple in obj: | ||
strings.append(tuple[0]) | ||
|
||
return strings | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
|
||
def get_file_path(doc: ResultDocument) -> Path: | ||
return Path(doc.meta.sample.path) | ||
|
||
|
||
def get_extractor_from_doc(doc: ResultDocument) -> FeatureExtractor: | ||
import capa.main | ||
|
||
path = get_file_path(doc) | ||
format = doc.meta.analysis.format | ||
os = doc.meta.analysis.os | ||
|
||
_ = get_auto_format(get_file_path(doc)) | ||
if _ == BACKEND_VIV: | ||
backend = BACKEND_VIV | ||
elif _ == BACKEND_PEFILE: | ||
backend = BACKEND_PEFILE | ||
elif _ == BACKEND_BINJA: | ||
backend = BACKEND_BINJA | ||
elif _ == BACKEND_DOTNET: | ||
backend = BACKEND_DOTNET | ||
else: | ||
backend = BACKEND_VIV # according to capa.main this is the default | ||
|
||
sigpaths = [ | ||
CD / "tests" / "data" / "sigs" / "test_aulldiv.pat", | ||
CD / "tests" / "data" / "sigs" / "test_aullrem.pat.gz", | ||
CD / "sigs" / "1_flare_msvc_rtf_32_64.sig", | ||
CD / "sigs" / "2_flare_msvc_atlmfc_32_64.sig", | ||
CD / "sigs" / "3_flare_common_libs.sig", | ||
] | ||
|
||
return capa.main.get_extractor( | ||
path=path, | ||
format_=format, | ||
os_=os, | ||
backend=backend, | ||
sigpaths=sigpaths, | ||
) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CD is for current directory?