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 sequence scope #2532

Draft
wants to merge 12 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 "sequence 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 "sequence" 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 5 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
@@ -4,6 +4,8 @@

### New Features

- add dynamic sequence scope for matching nearby calls within a thread #2532 @williballenthin
Copy link
Collaborator

Choose a reason for hiding this comment

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

naming alternatives to sequence (matching occurs in any order): span, ngram, group/cluster

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 cluster

Copy link
Collaborator Author

@williballenthin williballenthin Dec 12, 2024

Choose a reason for hiding this comment

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

"window", "slice", "range"

Copy link
Collaborator

Choose a reason for hiding this comment

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

math: multiset (or bag, or mset) - https://en.wikipedia.org/wiki/Multiset

  • multiple instances of same object
  • order doesn't matter

Copy link
Collaborator

Choose a reason for hiding this comment

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

optionally prefix with "call", e.g., callbag, callcluster?

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 2 times, most recently from c0af878 to a8abb16 Compare December 12, 2024 14:19
@williballenthin williballenthin force-pushed the feat/dynamic-sequence-scope branch from a8abb16 to 37f6ccb Compare December 12, 2024 14:38
also, for repeating behavior, match only the first instance.
@williballenthin williballenthin force-pushed the feat/dynamic-sequence-scope branch 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.

@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 from 269a2e0 to a31cfd9 Compare December 16, 2024 14:03
@williballenthin williballenthin force-pushed the feat/dynamic-sequence-scope branch from 4683882 to 69f4728 Compare December 16, 2024 15:51
@williballenthin
Copy link
Collaborator Author

williballenthin commented Dec 17, 2024

I've run into some bugs where sequence scoped rules matching sequence scoped rules that are hammered can't be tracked well. Currently thinking this through and figuring out a fix.


Details:

Sequence scope matches logic found within a sliding window of calls, currently of length 20. To avoid showing too many results when a program "hammers" a behavior, such as calling sleep in a tight loop, the sequence engine only "publishes" a match if it wasn't seen in the prior sequence. As long as the length of the sequence is larger than the behavior within a tight loop, capa shows the match at the first loop iteration and does not "publish" the subsequent run of duplicate behavior.

By "publish" we mean that capa reports on a match, recording it within the result document and rendering it to the user. The sequence engine still recognizes all the matches within the tight loop, and other rules can match against those matches, they just aren't propagated into the final results.

We think this behavior is generally agreeable and intuitive; however, there's a subtle bug. When we build the result document to show vverbose output, which prints the precise data used to make a match, a rule that depends on another rule that has been hammered can't be easily resolved. If we haven't published the hammered rule, then we can't easily show how the dependent rule matched, because the match details aren't available.

For example, in 0000a... for PID 1852 and thread 2596, there's a tight loop of NtAllocateVirtualMemory that triggers allocate or change RWX memory on the first match (id: 301), but is recognized to "hammer" and therefore subsequent matches are suppressed. Way later, at id 1257, are calls to WriteProcessMemory and CreateRemoteThread, which along with a match for allocate or change RWX memory, trigger inject thread. But when we go to render the results for inject thread the match details for allocate or change RWX memory are not available within 20 (or even 900) calls.


My current theory for a fix is: when there's a newly recognized rule (that is, not hammered), walk it's logic, and if it depends on any other rules, ensure they're published (even if they were hammered). This will take a bit more code, and will require some inline documentation like I've added above, but should be enough to ensure we can always prove why capa matched some logic.

@mr-tz
Copy link
Collaborator

mr-tz commented Dec 17, 2024

I've run into some bugs where sequence scoped rules matching sequence scoped rules that are hammered can't be tracked well. Currently thinking this through and figuring out a fix.

I think that's expected. If it's an issue that will take longer to fix we should handle it separately. The benefits of the sequence scope (way less FPs vs. thread scope) outweigh the shortcomings here.

contains the call ids for all the calls within the sequence, so we know
where to look for related matched.
@williballenthin williballenthin force-pushed the feat/dynamic-sequence-scope branch from ded0e27 to 6dde963 Compare December 18, 2024 12:54
Comment on lines +62 to +71
value: Union[
# for absolute, relative, file
int,
# for DNToken, Process, Thread, Call
tuple[int, ...],
# for sequence
tuple[int, int, int, int, tuple[int, ...]],
# for NO_ADDRESS,
None,
] = None # None default value to support deserialization of NO_ADDRESS
Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks, the documentation helps a lot here
we could also add documentation what the values are: ppid, pid, tid, id_, calls or use a dataclass?

@@ -181,6 +182,55 @@ def test_dynamic_sequence_scope_length():
assert r.name not in capabilities.matches


# show that the DynamicSequenceAddress has the correct structure.
# temporarily uses a sequence of length 2, for simplicity.
Copy link
Collaborator

Choose a reason for hiding this comment

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

great!

@williballenthin
Copy link
Collaborator Author

late night idea: to handle the link between dependent rules in the presence of hammering, I think we can just pick the most recent earlier match (in the same thread). We don't need to track this precisely, such as with specialized sequence addresses, so we can delete some of the recent code.

@mr-tz
Copy link
Collaborator

mr-tz commented Dec 20, 2024

to handle the link between dependent rules in the presence of hammering...

Nice, this is a good solution for API hammering across/between dependent rule matches.

I think we still have to come up with a solution for API hammering within a rule?
Example trace:

...
WantedApi1
...
many other calls (n > MAX_SEQUENCE_LENGTH)
...
WantedApi2
features:
  - and:
    - api: WantedApi1
    - api: WantedApi2

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.

3 participants