Skip to content

Progress towards tree-sitter feature#3102

Open
saniyafatima07 wants to merge 24 commits into
mandiant:masterfrom
saniyafatima07:script-feature
Open

Progress towards tree-sitter feature#3102
saniyafatima07 wants to merge 24 commits into
mandiant:masterfrom
saniyafatima07:script-feature

Conversation

@saniyafatima07

@saniyafatima07 saniyafatima07 commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

Addresses PR #1080 and #2931

This PR updates capa’s Tree-sitter support for script analysis and fixes the related integration issues so the feature works correctly in both source and packaged builds.

Changes include:

  • Updated few outdated tree-sitter queries
  • Ensured that all the CI tests pass
  • Fixed few import errors
  • Added Python test scripts
  • Added PyInstaller packaging support so the Tree-sitter signature JSON files are included in bundled builds.

Checklist

  • CHANGELOG updated
  • Few tests added
  • No documentation update needed currently.
  • This submission includes AI-generated code and I have provided details in the description.

Parts of this implementation were assisted using AI tools (Codex and ChatGPT).

AI was used for:

  • writing the test script for python
  • fixing few errors

All code was reviewed, modified and tested manually before submission.

Testing:
I built it using PyInstaller and tested the capabilities on script inputs. It functions as intended.
image

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces Tree-Sitter Script Analysis to capa, enabling feature extraction from script languages such as C#, Python, HTML, and ASPX templates. It adds a new Tree-Sitter-based feature extractor, auto-detection capabilities, and signature-based tools, along with comprehensive tests and updated dependencies. The code review feedback primarily addresses compatibility issues with the upgraded tree-sitter library (version 0.25.0), specifically pointing out that QueryCursor has been removed and the Parser instantiation has changed in tree-sitter versions >= 0.21.0. The feedback provides actionable suggestions to execute queries directly and update parser usage. Additionally, it identifies a bug in integer suffix parsing and recommends replacing a deprecated importlib.resources API.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread capa/features/extractors/ts/engine.py
Comment thread capa/features/extractors/ts/engine.py
Comment thread capa/features/extractors/ts/autodetect.py
Comment thread capa/features/extractors/ts/engine.py
Comment thread capa/features/extractors/ts/engine.py
Comment thread capa/features/extractors/ts/engine.py
Comment thread capa/features/extractors/ts/engine.py
Comment thread capa/features/extractors/ts/autodetect.py
Comment thread capa/features/extractors/ts/tools.py
Comment thread capa/features/extractors/ts/tools.py
@saniyafatima07
saniyafatima07 marked this pull request as ready for review June 10, 2026 18:58
@saniyafatima07
saniyafatima07 marked this pull request as draft June 10, 2026 18:58
@saniyafatima07
saniyafatima07 force-pushed the script-feature branch 2 times, most recently from e71ca37 to 5c19383 Compare June 17, 2026 16:41
Comment thread capa/capabilities/common.py
@saniyafatima07
saniyafatima07 force-pushed the script-feature branch 7 times, most recently from 2e75cd1 to 050ab84 Compare June 22, 2026 20:54
@saniyafatima07
saniyafatima07 force-pushed the script-feature branch 4 times, most recently from 46a6446 to 19308f7 Compare June 22, 2026 22:31
@saniyafatima07

Copy link
Copy Markdown
Collaborator Author

@mike-hunhoff @larchchen @Maijin The first version of the script analysis feature is ready for review.
Could you please take a look when you have a chance?

@mr-tz mr-tz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Initial review of the first few files

Comment thread capa/capabilities/static.py Outdated

def get_language_from_ext(path: str) -> str:
if path.endswith(EXT_ASPX):
return LANG_TEM

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Embedded template is not immediately clear to me, some doc on that would be good (maybe it is further down).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I haven’t added documentation yet, but I’ll add a section describing the script analysis feature in the near future.

buf = f.read()
return get_language_ts(buf)
except ValueError:
return get_language_from_ext(str(path))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I guess the order is debatable here, so flagging for further discussion

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

My thinking here was to prefer Tree-sitter’s language detection when possible, since it can inspect the file contents rather than relying only on the extension. The extension fallback is mainly for cases where content-based detection fails.

That said, I agree that the order is debatable. Happy to discuss more on this!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What's the performance difference if we switch the order? My concern is that Tree-sitter's language detection could be slow for large or heavily obfuscated scripts, while simply checking the extension won't be bogged down by script size or complexity.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I ran a small benchmark test with simple Python scripts of different file sizes. There is actually a huge difference in the time taken by content-based detection compared to extension-based detection.

  • Extension-based: 0.0005 ms
  • Content-based: 7.3845 ms

So, it would be optimal for us to have extension-based detection first. Thank you for pointing it out @mr-tz and @mike-hunhoff.

I will correct it in the next commit.

@Maijin

Maijin commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Kudo for making this huge pr work 👍, I have two high-level comments mainly:

  • Since automated detection may struggle with heavily obfuscated scripts, it might be beneficial to allow users to force specific language detection on the CLI. This would not only improve user experience but also serve as a useful tool for regression testing. I suggest considering this for the next PR.

  • Regarding the cs.json, py.json etc. signature files, what is the intended process for updating these as new malicious script patterns emerge? Since these are separated from capa-rules, we should document how rule authors can contribute new signatures. It may also be worth having an architectural discussion regarding the long-term management of these files tagging @mike-hunhoff for visibility on this.

@@ -0,0 +1,95 @@
{
"classes" : [
"System.Data.SqlClient.SqlCommand",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would recommend sorting all of these signatures everywhere. This will ease contributions.

@@ -0,0 +1,47 @@
{
"classes": [
"socket.socket",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here - sort everywhere

@saniyafatima07

Copy link
Copy Markdown
Collaborator Author

Kudo for making this huge pr work 👍, I have two high-level comments mainly:

* Since automated detection may struggle with heavily obfuscated scripts, it might be beneficial to allow users to force specific language detection on the CLI. This would not only improve user experience but also serve as a useful tool for regression testing. I suggest considering this for the next PR.

* Regarding the `cs.json`, `py.json` etc. signature files, what is the intended process for updating these as new malicious script patterns emerge? Since these are separated from `capa-rules`, we should document how rule authors can contribute new signatures. It may also be worth having an architectural discussion regarding the long-term management of these files tagging @mike-hunhoff for visibility on this.

Sure, Maxime. We can discuss about the architecture in the upcoming meets.

Comment thread capa/helpers.py Outdated
Comment thread capa/helpers.py Outdated
Comment thread capa/helpers.py Outdated
Comment thread tests/fixtures/features/binexport.json Outdated
Comment thread tests/fixtures/features/static.json Outdated

@mr-tz mr-tz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nice progress overall, thanks for all the work on getting this feature finally integrated :)

@saniyafatima07
saniyafatima07 force-pushed the script-feature branch 3 times, most recently from eefa6da to 3da5825 Compare June 28, 2026 09:28

@mike-hunhoff mike-hunhoff left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you @saniyafatima07 , I know this is a large PR to work with, so thank you for all you've done so far. I've left comments for your review. Mostly it appears that the PR still contains unrelated changes, likely from some rebase conflicts?

buf = f.read()
return get_language_ts(buf)
except ValueError:
return get_language_from_ext(str(path))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What's the performance difference if we switch the order? My concern is that Tree-sitter's language detection could be slow for large or heavily obfuscated scripts, while simply checking the extension won't be bogged down by script size or complexity.

Comment thread capa/features/extractors/ts/extractor.py Outdated
Comment thread capa/rules/__init__.py
Comment thread capa/helpers.py Outdated
Comment thread capa/helpers.py Outdated
Comment thread tests/test_dnfile_features.py Outdated
Comment thread tests/test_elf_os_detection.py
Comment thread tests/test_feature_snapshots.py
Comment thread tests/test_match.py
Comment thread CHANGELOG.md

### New Features

- Tree-Sitter Script Analysis

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

TODO: expand this description to include contributions from previous developers.

@saniyafatima07

saniyafatima07 commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator Author

Thank you @saniyafatima07 , I know this is a large PR to work with, so thank you for all you've done so far. I've left comments for your review. Mostly it appears that the PR still contains unrelated changes, likely from some rebase conflicts?

Thank you for the review, Mike.

I looked into all the comments. It seems like I messed up a few things during the rebase, which caused a lot of unrelated changes.

I will carefully look into all the unrelated changes and resolve them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants