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

dynamic: add "span of calls" scope #2532

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

williballenthin
Copy link
Collaborator

@williballenthin williballenthin commented Dec 9, 2024

This PR implements the dynamic "span of calls" scope introduced here: mandiant/capa-rules#951

In summary, we want a way to match across calls (in dynamic mode) without resorting to the entire thread (which may be very long, like thousands of events). So, we add a new scope "span of calls" that represents the sliding 20-tuples of calls across each thread. Rules can match against any set of logic within each of these 20-tuples.

For example, consider the initial behavior of thread 3064 in our test CAPE file 0000a657:

image

This is a long thread with many calls, so yesterday it was tough to write a rule for any behavior that spans multiple calls without introducing false positives. Consider matching on the dynamic resolution and invocation of AddVectoredExceptionHandler. Now we can write a rule like:

image

So, within a region of 20 calls, match all this logic.

Here's what the output looks like:

image
image
image

The implementation is pretty easy: maintain a deque of the trailing 20 call events, merging and matching those features.

I picked 20 fairly randomly. I think we can tweak this number as necessary. Smaller and its harder to match logic. Larger and the performance might decrease a bit, and then there's more FP possibility. But I don't think this is too risky.

I think this will affect runtime a bit, since we're matching features twice for each call event (one for the precise call event, one for the sliding window).

There's probably some edge cases to work out around overlapping windows. Consider a rule that matches a single call event within a sequence: that call event is contained by 20 sequences (some covering the events before, some covering the events after). So, we may have to do a little more work (TODO) to not emit those matches twice. I'm not precisely sure of the behavior at this moment. I'll write a test for it.

Checklist

  • changelog update needed
  • documentation needed

@williballenthin williballenthin added enhancement New feature or request breaking-change introduces a breaking change that should be released in a major version dynamic related to dynamic analysis flavor labels Dec 9, 2024
github-actions[bot]

This comment was marked as resolved.

@github-actions github-actions bot dismissed their stale review December 9, 2024 13:38

CHANGELOG updated or no update needed, thanks! 😄

@williballenthin
Copy link
Collaborator Author

williballenthin commented Dec 9, 2024

we also may want to update the vverbose render to only show each call event once, leaving the match details to a separate section, maybe like:

sequence: processs1, pid, tid, calls{1, 2}
  and:
    api: CreateFile @ call{1}
    api: CloseFile @ call{2}
  referenced call events:
    call{1}: CreateFile
    call{2}: CloseFile

@williballenthin
Copy link
Collaborator Author

@jorik-utwente FYI

@williballenthin
Copy link
Collaborator Author

I realize I dropped this PR without much warning 😇 I went from "I wonder how this would work" to "huh, it seems to work OK" pretty quickly.

@williballenthin williballenthin marked this pull request as draft December 9, 2024 14:34
Copy link
Collaborator

@mr-tz mr-tz left a comment

Choose a reason for hiding this comment

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

awesome, this looks very promising already!!

major things to discuss include the naming and potentially handling of loops

capa/capabilities/dynamic.py Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
capa/capabilities/dynamic.py Outdated Show resolved Hide resolved
@williballenthin
Copy link
Collaborator Author

potentially handling of loops

Good point. I think we'd want to see how this works in practice against a large number of samples and the rules we can translate to use this construct. In particular, loops (like you say) such as you'd see in ransomware.

Copy link
Collaborator

@mike-hunhoff mike-hunhoff left a comment

Choose a reason for hiding this comment

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

Great work, I'm excited about where this is going for an initial implementation. I echo a few of @mr-tz 's comments/concerns. Additionally, the value 5 comes close to being too small for some of our existing rules, e.g. https://github.com/mandiant/capa-rules/blob/e033410c8910f8b46718a5eefd9f0c7768be1b99/communication/c2/shell/create-reverse-shell.yml#L19-L23 so we'll need to do some additional work to find the sweet spot.

@williballenthin williballenthin force-pushed the feat/dynamic-sequence-scope branch from d6106ea to 6d05d3c Compare December 10, 2024 12:55
Copy link
Collaborator

@mr-tz mr-tz left a comment

Choose a reason for hiding this comment

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

I spent a few moments focusing on the core extension here and added some places for additional documentation.

capa/capabilities/dynamic.py Outdated Show resolved Hide resolved
capa/capabilities/dynamic.py Outdated Show resolved Hide resolved
capa/capabilities/dynamic.py Outdated Show resolved Hide resolved
capa/capabilities/dynamic.py Outdated Show resolved Hide resolved
@williballenthin williballenthin force-pushed the feat/dynamic-sequence-scope branch 4 times, most recently from ea9daed to b10d591 Compare December 12, 2024 15:14
@williballenthin
Copy link
Collaborator Author

williballenthin commented Dec 12, 2024

computing the features for the sequence, which involves merging features from many calls, seems to take quite a bit of time:

image
image

i'll have to think on whether there's a creative way to optimize this


profile information

before: sequence length: 20

image

before: sequence length: 0

(convenient this works!)
image

optimized, sequence length 1 and 20:

image

conclusion:

So, there's a bit of overhead to use this new algorithm, but it's independent of SEQUENCE_LENGTH, which is desirable.

capa/capabilities/dynamic.py Outdated Show resolved Hide resolved
@mr-tz
Copy link
Collaborator

mr-tz commented Dec 16, 2024

TODO?!

  • test sequence scope with submatch (call scope)
  • test sequence scope with submatch (sequence scope)
  • test sequence scope with submatch (thread or other scope - error?)

@williballenthin williballenthin force-pushed the feat/dynamic-sequence-scope branch 2 times, most recently from 4683882 to 69f4728 Compare December 16, 2024 15:51
Copy link
Collaborator

@mike-hunhoff mike-hunhoff left a comment

Choose a reason for hiding this comment

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

Great work @williballenthin - this is looking great! The code looks sensible to me and I trust (and appreciate) the tests that you've added 🚀

# matches found at the span scope.
self.matches: MatchResults = collections.defaultdict(list)

# We matches spans as the sliding window of calls with size SPAN_SIZE.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# We matches spans as the sliding window of calls with size SPAN_SIZE.
# We match spans as the sliding window of calls with size SPAN_SIZE.

@mike-hunhoff mike-hunhoff self-requested a review January 22, 2025 23:05
Copy link
Collaborator

@mike-hunhoff mike-hunhoff left a comment

Choose a reason for hiding this comment

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

Collecting matched call ids still appears to be incorrect.

williballenthin added a commit that referenced this pull request Jan 28, 2025
#2532 (comment)

vverbose: fix collection of span-of-calls call match locations
@williballenthin williballenthin force-pushed the feat/dynamic-sequence-scope branch from aac2105 to 20985e3 Compare January 28, 2025 08:47
to ensure its not modified by reference after we expect it to be
addresses discussion in
mandiant/capa-rules#951

pep8

sequence: add test showing multiple sequences overlapping a single event
also, for repeating behavior, match only the first instance.
sequence: add more tests
contains the call ids for all the calls within the sequence, so we know
where to look for related matched.

sequence: refactor SequenceMatcher

sequence: don't use sequence addresses

sequence: remove sequence address
williballenthin added a commit that referenced this pull request Jan 28, 2025
#2532 (comment)

vverbose: fix collection of span-of-calls call match locations
@williballenthin williballenthin force-pushed the feat/dynamic-sequence-scope branch from 20985e3 to e8de7aa Compare January 28, 2025 08:51
@williballenthin williballenthin force-pushed the feat/dynamic-sequence-scope branch from e8de7aa to 1b854a1 Compare January 28, 2025 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change introduces a breaking change that should be released in a major version dynamic related to dynamic analysis flavor enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants