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: warn user for .NET samples #1864

Closed
mr-tz opened this issue Nov 27, 2023 · 36 comments · Fixed by #2568
Closed

dynamic: warn user for .NET samples #1864

mr-tz opened this issue Nov 27, 2023 · 36 comments · Fixed by #2568
Labels
dynamic related to dynamic analysis flavor good first issue Good for newcomers

Comments

@mr-tz
Copy link
Collaborator

mr-tz commented Nov 27, 2023

warn for .NET samples because runtime API trace and called .NET APIs differ

results may be confusing / misleading

@mr-tz mr-tz added the dynamic related to dynamic analysis flavor label Nov 27, 2023
@mr-tz mr-tz added the good first issue Good for newcomers label Feb 5, 2024
@v1bh475u
Copy link
Contributor

Hello, I am totally new here. I wanted to work on this issue. Can you please give more insights?

@mr-tz
Copy link
Collaborator Author

mr-tz commented Dec 18, 2024

Welcome! For dynamic analysis we should look into "file limitations" like e.g. .NET samples.

The first step here would be to identify a .NET sample from the sandbox files (for VMRay and CAPE first), example files can be found in https://github.com/mandiant/capa-testfiles/tree/master/dynamic.

Then we could handle it analogous to static file limitations (e.g. here

def find_file_limitations_from_cli(args, rules: RuleSet, file_extractors: list[FeatureExtractor]) -> bool:
) or come up with a new mechanism.

@v1bh475u
Copy link
Contributor

v1bh475u commented Dec 18, 2024

So currently, is it unable to detect .NET samples?

@mr-tz
Copy link
Collaborator Author

mr-tz commented Dec 18, 2024

Yes, that's the first idea here.

@v1bh475u
Copy link
Contributor

Can you please explain the issue in more detail? I am still unable to understand where the problem is ( this is my first time seeing the internals of capa).
Here's what i have interpreted:

  • runtime APIs used and APIs written in code differ
  • currently from the sandbox files, capa is unable to detect a .NET sample

Hence, we need to make capa identify .NET sample(no idea how it's done) and warn users if it is .NET and that's all. Is this correct?

@mr-tz
Copy link
Collaborator Author

mr-tz commented Dec 23, 2024

Yes, sounds like you got it.

For static analysis we already handle cases where the analysis may be confusing or wrong (so I provided that as an example).

For dynamic analysis there is probably multiple ways to identify .NET from the analysis archives. For CAPE and VMRay e.g. from the static analysis results (pefile, etc.).

@v1bh475u
Copy link
Contributor

How do I know which ones inside the capa-testfiles/dynamic/capa are dotnet ones? I know for vmray, all are dotnet samples as they that dotnet_version field in their summaries.

@williballenthin
Copy link
Collaborator

you could use file to check if the file contains a .NET assembly. or perhaps check the imports to see if mscoree.dll is loaded. there's a capa rule for this.

@v1bh475u
Copy link
Contributor

the files in capa-testfiles/dynamic/cape(pardon me for my mispell) are zips of json data and being new to this codebase, I currently have little to no idea how to interpret it.

@williballenthin
Copy link
Collaborator

ah, sorry, i misread the file path!

@williballenthin
Copy link
Collaborator

i don't know off the top of my head. it may require a little novel research.

for example, you could compare the either the module load or API calls (probably the first few) of .NET vs not .NET in the VMRay dataset, to figure out what a good charactistic/pattern is, and then see if it applies to CAPE too.

again, i think loading mscoree.dll is probably a good indicator, but im not totally sure.

@v1bh475u
Copy link
Contributor

The problem that I am seeing is I don't know a perfect way of knowing if it's a .NET by looking at the json from cape. If there is, then please tell. After that, I can try to find a pattern of how to identify if it is .NET.

@williballenthin
Copy link
Collaborator

williballenthin commented Dec 27, 2024

according to VT, 2ebadd04f0ada89c36c1409b6e96423a68dd77b513db8db3da203c36d3753e5f is a .NET assembly:
image

however, the file output embedded in the CAPE report doesn't support this:
image

but we should be able to use this field to find .NET files.

@williballenthin
Copy link
Collaborator

none of the CAPE reports in our existing testfiles repo are for .NET samples:
image

we'll have to find a new one and add it to the repo.

@v1bh475u
Copy link
Contributor

v1bh475u commented Jan 2, 2025

What I feel is checking if mscoree.dll is loaded or not should be a good enough indication of it being .NET. However, I can see that some normal PEs can deliberately load that dll and not use it giving out a false alarm. So, should I take the above into consideration and start implementing the logic?

@mr-tz
Copy link
Collaborator Author

mr-tz commented Jan 2, 2025

I'd start by obtaining a CAPE report for a .NET sample and inspecting the static fields, i.e. the mentioned file data - additionally, it may make sense to inspect the API trace (but it's probably not even necessary).

@v1bh475u
Copy link
Contributor

v1bh475u commented Jan 2, 2025

Okay. Will start implementing this.

@v1bh475u
Copy link
Contributor

v1bh475u commented Jan 5, 2025

can I use the check that if mscoree.dll is present in the imports in case of vmray as the current structure does not have any other explicit field that can be used for .NET detection? So, I am thinking of searching through imports for dll and if we find mscoree.dll,then we can generate the warning. This will work for vmray but cannot say as such for cape as we don't have cape .NET samples.

@williballenthin
Copy link
Collaborator

I think it's important that we find a test case for CAPE before going too far along. I'll be back working again tomorrow (sorry been off for two weeks) and can help collect the test file.

@v1bh475u
Copy link
Contributor

v1bh475u commented Jan 6, 2025

Also, we would need some more samples in vmray too as currently there is only 1 .NET pe in the testfiles.

@williballenthin
Copy link
Collaborator

@doomedraven can you suggest a source of public CAPE sandbox results? VT doesn't share the raw report archive.

@doomedraven
Copy link
Contributor

i will provide you one tomorrow from TCR, today is last holiday here

@williballenthin
Copy link
Collaborator

dotnet.json.zip

@williballenthin
Copy link
Collaborator

image

@v1bh475u
Copy link
Contributor

v1bh475u commented Jan 9, 2025

Great

@v1bh475u
Copy link
Contributor

@mr-tz @williballenthin shall I continue with approach of finding imports and iterating through them to find if it has imported mscoree.dll? The reason being although cape-analysis provide the details about file but the VMRay samples as I have seen do not. So, for keeping the structure of code symmetric, I feel it would be better to use the same idea for all samples. Also, please guide me if there is any particular rule that must be maintained like "a certain module should not be imported in another" or so.

@v1bh475u
Copy link
Contributor

Also, please tell what the warning message should be.

@williballenthin
Copy link
Collaborator

Yes let's move forward with the import strategy. You can use the existing capa rule format and logic to specify "import: mscoree" (or perhaps the function from that DLL) and make it only used for dynamic analysis ("scope: dynamic: file, static: unsupported").

please see how the existing static analysis file limitation rules and warnings are emitted for an example.

@v1bh475u
Copy link
Contributor

v1bh475u commented Jan 14, 2025

Shall I create a new rule under internal/limitation/file or something other directory or try to use some existing rule ( I feel it would be difficult to get a rule specific to our requirements and conditions, especially with scope)?

rule:
  meta:
    name: .NET dynamic sample
    namespace: internal/limitation/file
    authors:
      - "@v1bh475u"
    scopes:
      static: unsupported
      dynamic: file
    examples: # yet to be tested and found
  features:
     - or:
      - format: dotnet
      - import: mscoree._CorExeMain
      - import: mscoree._corexemain
      - import: mscoree._CorDllMain
      - import: mscoree._cordllmain

The above rule is almost same the rule compiled to the .NET platform but I have made it so that it can only be used by dynamic samples. I feel making such a directory for dynamic samples similar to the one present for static samples can help in making it easy to add more limitations for dynamic samples.

@v1bh475u
Copy link
Contributor

@williballenthin what is the password for the cape sample zip that you have shared? I have implemented some code and want to test it against it but capa does not support json.zip format. It works for vmray sample.

@williballenthin
Copy link
Collaborator

try "infected"

@v1bh475u
Copy link
Contributor

@williballenthin It worked! Now, I need to make PRs on capa as well as capa-rules both. I have made and uploaded the changes in the forked repos. Can you please guide me for how shall I proceed with the PRs? Also, is it fine if I continue to communicate like this on github or would you prefer mail for communicating on future issues that I try to solve?
https://github.com/v1bh475u/capa/tree/feat/warn-for-dynamic-dotnet
https://github.com/v1bh475u/capa-rules/tree/feat/dynamic-limitations

@williballenthin
Copy link
Collaborator

Working here on Github works well for me, as long as it does for you, too.

Nice job on the updates! Let's get PRs created for these changes. Have you done this before?

If not, you can use the button here to create a PR:

Image

and in the PR text you can reference this issue, like "closes #1864".

Once that is open, we can review and address any further recommendations, and merge when its ready. Thank you!

@v1bh475u
Copy link
Contributor

@williballenthin I have made the PRs. Please review them whenever you are free.

@v1bh475u
Copy link
Contributor

v1bh475u commented Feb 3, 2025

@williballenthin, I have also made the PR on capa-rules that restructures the limitation rules as per the expected namespace. Please review that too. mandiant/capa-rules#983

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dynamic related to dynamic analysis flavor good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants