-
Notifications
You must be signed in to change notification settings - Fork 708
Script Analysis: Bash Support #3121
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
Draft
saniyafatima07
wants to merge
22
commits into
mandiant:master
Choose a base branch
from
saniyafatima07:script-feature-bash
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7bd2acd
Fix issues
saniyafatima07 f8d3848
fix: address PR review feedback (regex optimization, typos, duplicate…
EdoardoAllegrini bcfe22e
Update CHANGELOG.md
EdoardoAllegrini d86a44e
Fix outdated code
EdoardoAllegrini 3ebf4bd
Fix outdated code
EdoardoAllegrini 9b36aac
Modernize Tree-sitter: Replace local C-compilation with native PyPI w…
EdoardoAllegrini ddb701c
feat: revive script analysis ts feature
EdoardoAllegrini 772e55d
Fix: CI builds
saniyafatima07 fcca9ad
Fix import errors
saniyafatima07 88a98f4
Fix CI: trial-1
saniyafatima07 18342bb
Fix tree-sitter packaging
saniyafatima07 14610dc
Address valid gemini reviews
saniyafatima07 8057248
Minor fixes
saniyafatima07 291975a
Fix tests
saniyafatima07 652b6cf
Address comments
saniyafatima07 af4fd99
Sort json files
saniyafatima07 2b25c67
Remove few unnecessary changes
saniyafatima07 600659a
Initial bash support
saniyafatima07 a574ec9
Prioritize extension based detection
saniyafatima07 e043d58
Add bash query bindings and extractor support
saniyafatima07 3fa3004
Bash test sample
saniyafatima07 924dbff
Fix build fail
saniyafatima07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
.github/pyinstaller/hooks/hook-capa.features.extractors.ts.signatures.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from PyInstaller.utils.hooks import collect_data_files | ||
|
|
||
|
|
||
| # Tree-sitter signature lookups use importlib.resources, so PyInstaller must | ||
| # bundle the JSON files alongside the package. | ||
| datas = collect_data_files("capa.features.extractors.ts.signatures") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Copyright 2022 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import Tuple, Iterator | ||
|
|
||
| from capa.features.common import OS, OS_ANY, ARCH_ANY, FORMAT_SCRIPT, Arch, Format, Feature, ScriptLanguage | ||
| from capa.features.address import NO_ADDRESS, Address, FileOffsetRangeAddress | ||
|
|
||
| # Can be used to instantiate tree_sitter Language objects (see ts/query.py) | ||
| LANG_BASH = "bash" | ||
| LANG_CS = "c_sharp" | ||
| LANG_HTML = "html" | ||
| LANG_JS = "javascript" | ||
| LANG_PY = "python" | ||
| LANG_TEM = "embedded_template" | ||
|
|
||
| EXT_ASPX = ("aspx", "aspx_") | ||
| EXT_BASH = ("bash", "sh", "bash_", "sh_") | ||
| EXT_CS = ("cs", "cs_") | ||
| EXT_HTML = ("html", "html_") | ||
| EXT_PY = ("py", "py_") | ||
|
|
||
|
|
||
| LANGUAGE_FEATURE_FORMAT = { | ||
| LANG_BASH: "Bash", | ||
| LANG_CS: "C#", | ||
| LANG_HTML: "HTML", | ||
| LANG_JS: "JavaScript", | ||
| LANG_PY: "Python", | ||
| LANG_TEM: "Embedded Template", | ||
| } | ||
|
|
||
|
|
||
| def extract_arch() -> Iterator[Tuple[Feature, Address]]: | ||
| yield Arch(ARCH_ANY), NO_ADDRESS | ||
|
|
||
|
|
||
| def extract_language(language: str, addr: FileOffsetRangeAddress) -> Iterator[Tuple[Feature, Address]]: | ||
| yield ScriptLanguage(LANGUAGE_FEATURE_FORMAT[language]), addr | ||
|
|
||
|
|
||
| def extract_os() -> Iterator[Tuple[Feature, Address]]: | ||
| yield OS(OS_ANY), NO_ADDRESS | ||
|
|
||
|
|
||
| def extract_format() -> Iterator[Tuple[Feature, Address]]: | ||
| yield Format(FORMAT_SCRIPT), NO_ADDRESS |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Copyright 2022 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import Optional | ||
| from pathlib import Path | ||
|
|
||
| from tree_sitter import Node, Tree, Query, Parser, Language, QueryCursor | ||
|
|
||
| from capa.features.extractors.script import ( | ||
| EXT_CS, | ||
| EXT_PY, | ||
| LANG_CS, | ||
| LANG_PY, | ||
| EXT_ASPX, | ||
| EXT_BASH, | ||
| EXT_HTML, | ||
| LANG_TEM, | ||
| LANG_BASH, | ||
| LANG_HTML, | ||
| ) | ||
| from capa.features.extractors.ts.query import TS_LANGUAGES | ||
|
|
||
|
|
||
| def is_script(buf: bytes) -> bool: | ||
| try: | ||
| return bool(get_language_ts(buf)) | ||
| except ValueError: | ||
| return False | ||
|
|
||
|
|
||
| def _parse(ts_language: Language, buf: bytes) -> Optional[Tree]: | ||
| try: | ||
| parser = Parser(ts_language) | ||
| return parser.parse(buf) | ||
| except ValueError: | ||
| return None | ||
|
|
||
|
|
||
| def _contains_errors(ts_language, node: Node) -> bool: | ||
| query = Query(ts_language, "(ERROR) @error") | ||
| return bool(QueryCursor(query).captures(node)) | ||
|
|
||
|
|
||
| def get_language_ts(buf: bytes) -> str: | ||
| for language, ts_language in TS_LANGUAGES.items(): | ||
| if language == LANG_BASH: | ||
| continue | ||
| tree = _parse(ts_language, buf) | ||
| if tree and not _contains_errors(ts_language, tree.root_node): | ||
| return language | ||
| raise ValueError("failed to parse the language") | ||
|
|
||
|
|
||
| def get_template_language_ts(buf: bytes) -> str: | ||
| for language, ts_language in TS_LANGUAGES.items(): | ||
| if language in [LANG_TEM, LANG_HTML]: | ||
| continue | ||
| tree = _parse(ts_language, buf) | ||
| if tree and not _contains_errors(ts_language, tree.root_node): | ||
| return language | ||
| raise ValueError("failed to parse the language") | ||
|
|
||
|
|
||
| def get_language_from_ext(path: str) -> str: | ||
| if path.endswith(EXT_ASPX): | ||
| return LANG_TEM | ||
| if path.endswith(EXT_BASH): | ||
| return LANG_BASH | ||
| if path.endswith(EXT_CS): | ||
| return LANG_CS | ||
| if path.endswith(EXT_HTML): | ||
| return LANG_HTML | ||
| if path.endswith(EXT_PY): | ||
| return LANG_PY | ||
| raise ValueError(f"{path} has an unrecognized or an unsupported extension.") | ||
|
|
||
|
|
||
| def get_language(path: Path) -> str: | ||
| try: | ||
| return get_language_from_ext(str(path)) | ||
| except ValueError: | ||
| with path.open("rb") as f: | ||
| buf = f.read() | ||
| return get_language_ts(buf) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constant
FORMAT_SCRIPTis defined twice in this block (on line 498 and line 500). We should remove the duplicate definition.