-
Notifications
You must be signed in to change notification settings - Fork 717
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
Open
saniyafatima07
wants to merge
8
commits into
mandiant:feature/script-analysis
Choose a base branch
from
saniyafatima07:script-feature-bash
base: feature/script-analysis
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.
+3,661
−8
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7e83963
ci: trigger workflows for feature/script-analysis branch (#3142)
mike-hunhoff 01a9bf0
Add support for script analysis (#3102)
saniyafatima07 3a94b33
fix(extractors/ts): deduplicate qualified names and resolve module na…
mike-hunhoff 2af91b0
Add Bash support to script analysis
saniyafatima07 1cf1feb
Remove zipfile extraction
saniyafatima07 ea6637e
Extend global statement to include top-level constructs
saniyafatima07 8de61da
Remove duplicate imports, fix Text import in helpers
saniyafatima07 49be124
Fix minor errors
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
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.
Please provide context for this change.