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

display analysis information to users #2111

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- add function in capa/helpers to load plain and compressed JSON reports #1883 @Rohit1123
- document Antivirus warnings and VirusTotal false positive detections #2028 @RionEV @mr-tz
- replace Halo spinner with Rich #2086 @s-ff
- display analysis information for user #857 @s-ff

### Breaking Changes

Expand Down
3 changes: 1 addition & 2 deletions capa/capabilities/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def has_file_limitation(rules: RuleSet, capabilities: MatchResults, is_standalon
logger.warning(" %s", line)
logger.warning(" Identified via rule: %s", file_limitation_rule.name)
if is_standalone:
logger.warning(" ")
logger.warning(" Use -v or -vv if you really want to see the capabilities identified by capa.")
pass
fariss marked this conversation as resolved.
Show resolved Hide resolved
logger.warning("-" * 80)

# bail on first file limitation
Expand Down
4 changes: 4 additions & 0 deletions capa/capabilities/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from capa.rules import Scope, RuleSet
from capa.engine import FeatureSet, MatchResults
from capa.helpers import redirecting_print_to_tqdm
from capa.features.insn import API
from capa.capabilities.common import find_file_capabilities
from capa.features.extractors.base_extractor import BBHandle, InsnHandle, FunctionHandle, StaticFeatureExtractor

Expand Down Expand Up @@ -118,6 +119,9 @@ def find_code_capabilities(
features, bmatches, imatches = find_basic_block_capabilities(ruleset, extractor, fh, bb)
for feature, vas in features.items():
function_features[feature].update(vas)
if isinstance(feature, API):
# delcare a global variable (a set) and append to it here?
pass
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The goal here is to count how many API calls are made. I am thinking of declaring a global variable here (i.e. Set[API]) and appending to it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

once we have all the features collected and merged at the file level, we could enumerate them once to collect the API features and count them. We shouldn't need a distinct variable for this.

Copy link
Collaborator

@williballenthin williballenthin May 30, 2024

Choose a reason for hiding this comment

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

around line 231.

Though it's possible we don't construct the massive set like we do at lower scopes, I can't quite remember.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

once we have all the features collected and merged at the file level, we could enumerate them once to collect the API features and count them. We shouldn't need a distinct variable for this.

You're right, we don't. We collect all the features from the smaller blocks, but for functions we only return the len(function_features).


for rule_name, res in bmatches.items():
bb_matches[rule_name].extend(res)
Expand Down
17 changes: 2 additions & 15 deletions capa/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
FORMAT_RESULT,
)
from capa.capabilities.common import find_capabilities, has_file_limitation, find_file_capabilities
from capa.features.extractors.base_extractor import FeatureExtractor, StaticFeatureExtractor, DynamicFeatureExtractor
from capa.features.extractors.base_extractor import FeatureExtractor, DynamicFeatureExtractor

RULES_PATH_DEFAULT_STRING = "(embedded rules)"
SIGNATURES_PATH_DEFAULT_STRING = "(embedded signatures)"
Expand Down Expand Up @@ -666,16 +666,9 @@ def find_file_limitations_from_cli(args, rules: RuleSet, file_extractors: List[F
except (ELFError, OverflowError) as e:
logger.error("Input file '%s' is not a valid ELF file: %s", args.input_file, str(e))
raise ShouldExitError(E_CORRUPT_FILE) from e

# file limitations that rely on non-file scope won't be detected here.
# nor on FunctionName features, because pefile doesn't support this.
found_file_limitation = has_file_limitation(rules, pure_file_capabilities)
if found_file_limitation:
# bail if capa encountered file limitation e.g. a packed binary
# do show the output in verbose mode, though.
if not (args.verbose or args.vverbose or args.json):
logger.debug("file limitation short circuit, won't analyze fully.")
raise ShouldExitError(E_FILE_LIMITATION)
return found_file_limitation


Expand Down Expand Up @@ -804,7 +797,7 @@ def main(argv: Optional[List[str]] = None):
input_format = get_input_format_from_cli(args)
rules = get_rules_from_cli(args)
file_extractors = get_file_extractors_from_cli(args, input_format)
found_file_limitation = find_file_limitations_from_cli(args, rules, file_extractors)
_ = find_file_limitations_from_cli(args, rules, file_extractors)
Comment on lines 799 to +800
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The only reason I am keeping find_file_limitation_from_cli is that it prints a warning to the user if the sample is packed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'll have to do more thinking/testing on how to handle this. One the one hand the limitations are very valid, on the other hand often users still want to see the results. Ideally, we find a solution that helps with both.

except ShouldExitError as e:
return e.status_code

Expand Down Expand Up @@ -837,12 +830,6 @@ def main(argv: Optional[List[str]] = None):
meta = capa.loader.collect_metadata(argv, args.input_file, input_format, os_, args.rules, extractor, counts)
meta.analysis.layout = capa.loader.compute_layout(rules, extractor, capabilities)

if isinstance(extractor, StaticFeatureExtractor) and found_file_limitation:
# bail if capa's static feature extractor encountered file limitation e.g. a packed binary
# do show the output in verbose mode, though.
if not (args.verbose or args.vverbose or args.json):
return E_FILE_LIMITATION

if args.json:
print(capa.render.json.render(meta, rules, capabilities))
elif args.vverbose:
Expand Down
10 changes: 10 additions & 0 deletions capa/render/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from capa.render.utils import StringIO

tabulate.PRESERVE_WHITESPACE = True
MIN_LIBFUNCS_COUNT = 5
fariss marked this conversation as resolved.
Show resolved Hide resolved


def width(s: str, character_count: int) -> str:
Expand All @@ -29,6 +30,15 @@ def width(s: str, character_count: int) -> str:


def render_meta(doc: rd.ResultDocument, ostream: StringIO):
# check if analysis is Static analysis to inform users about
# potential false postive due to low number of library functions
if isinstance(doc.meta.analysis, rd.StaticAnalysis):
n_libs: int = len(doc.meta.analysis.library_functions)
if n_libs <= MIN_LIBFUNCS_COUNT:
ostream.write(
"Few library functions recognized by FLIRT signatures, results may contain false positives\n\n"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add a new color (orange?) to display such warnings.

)

rows = [
(width("md5", 22), width(doc.meta.sample.md5, 82)),
("sha1", doc.meta.sample.sha1),
Expand Down