# Source code Review / SAST Tools

<details>

<summary><strong>从零开始学习 AWS 黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS 红队专家）</strong></a><strong>！</strong></summary>

支持 HackTricks 的其他方式：

* 如果您想看到您的**公司在 HackTricks 中做广告**或**下载 PDF 版的 HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
* 探索[**PEASS 家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord 群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或在 **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live) 上**关注**我们。
* 通过向 [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github 仓库提交 PR 来**分享您的黑客技巧**。

</details>

## 指南和工具列表

* [**https://owasp.org/www-community/Source\_Code\_Analysis\_Tools**](https://owasp.org/www-community/Source_Code_Analysis_Tools)
* [**https://github.com/analysis-tools-dev/static-analysis**](https://github.com/analysis-tools-dev/static-analysis)

## 多语言工具

### [Naxus - AI-Gents](https://www.naxusai.com/)

有一个**免费的套餐用于审查 PR**。

### [**Semgrep**](https://github.com/returntocorp/semgrep)

这是一个**开源工具**。

#### 支持的语言

| 类别           | 语言                                                                                                    |
| ------------ | ----------------------------------------------------------------------------------------------------- |
| GA           | C# · Go · Java · JavaScript · JSX · JSON · PHP · Python · Ruby · Scala · Terraform · TypeScript · TSX |
| Beta         | Kotlin · Rust                                                                                         |
| Experimental | Bash · C · C++ · Clojure · Dart · Dockerfile · Elixir · HTML · Julia · Jsonnet · Lisp ·               |

#### 快速开始

{% code overflow="wrap" %}

```bash
# Install https://github.com/returntocorp/semgrep#option-1-getting-started-from-the-cli
brew install semgrep

# Go to your repo code and scan
cd repo
semgrep scan --config auto
```

{% endcode %}

您还可以使用[**semgrep VSCode Extension**](https://marketplace.visualstudio.com/items?itemName=Semgrep.semgrep)在VSCode中获取查找结果。

### [**SonarQube**](https://www.sonarsource.com/products/sonarqube/downloads/)

有一个可安装的**免费版本**。

#### 快速开始

{% code overflow="wrap" %}

```bash
# Run the paltform in docker
docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:latest
# Install cli tool
brew install sonar-scanner

# Go to localhost:9000 and login with admin:admin or admin:sonar
# Generate a local project and then a TOKEN for it

# Using the token and from the folder with the repo, scan it
cd path/to/repo
sonar-scanner \
-Dsonar.projectKey=<project-name> \
-Dsonar.sources=. \
-Dsonar.host.url=http://localhost:9000 \
-Dsonar.token=<sonar_project_token>
```

{% endcode %}

### CodeQL

CodeQL有一个可安装的免费版本，但根据许可证，您只能在开源项目中使用免费的CodeQL版本。

#### 安装

{% code overflow="wrap" %}

```bash
# Download your release from https://github.com/github/codeql-action/releases
## Example
wget https://github.com/github/codeql-action/releases/download/codeql-bundle-v2.14.3/codeql-bundle-osx64.tar.gz

# Move it to the destination folder
mkdir ~/codeql
mv codeql-bundle* ~/codeql

# Decompress it
cd ~/codeql
tar -xzvf codeql-bundle-*.tar.gz
rm codeql-bundle-*.tar.gz

# Add to path
echo 'export PATH="$PATH:/Users/username/codeql/codeql"' >> ~/.zshrc

# Check it's correctly installed
## Open a new terminal
codeql resolve qlpacks #Get paths to QL packs
```

{% endcode %}

#### 快速入门 - 准备数据库

{% hint style="success" %}
你需要做的第一件事是**准备数据库**（创建代码树），以便稍后对其运行查询。
{% endhint %}

* 您可以允许 codeql 自动识别存储库的语言并创建数据库

{% code overflow="wrap" %}

```bash
codeql database create <database> --language <language>

# Example
codeql database create /path/repo/codeql_db --source-root /path/repo
## DB will be created in /path/repo/codeql_db
```

{% endcode %}

{% hint style="danger" %}
这通常会触发一个错误，指出指定了多种语言（或自动检测到）。检查下一个选项来修复这个问题！
{% endhint %}

* 您可以手动指定**存储库**和**语言**（[语言列表](https://docs.github.com/en/code-security/codeql-cli/getting-started-with-the-codeql-cli/preparing-your-code-for-codeql-analysis#running-codeql-database-create))

{% code overflow="wrap" %}

```bash
codeql database create <database> --language <language> --source-root </path/to/repo>

# Example
codeql database create /path/repo/codeql_db --language javascript --source-root /path/repo
## DB will be created in /path/repo/codeql_db
```

{% endcode %}

* 如果您的存储库使用**多于1种语言**，您还可以为每种语言创建**1个数据库**来指示每种语言。

{% code overflow="wrap" %}

```bash
export GITHUB_TOKEN=ghp_32849y23hij4...
codeql database create <database> --source-root /path/to/repo --db-cluster --language "javascript,python"

# Example
export GITHUB_TOKEN=ghp_32849y23hij4...
codeql database create /path/repo/codeql_db --source-root /path/to/repo --db-cluster --language "javascript,python"
## DBs will be created in /path/repo/codeql_db/*
```

{% endcode %}

* 您还可以允许 `codeql` 为您**识别所有语言**并为每种语言创建一个数据库。您需要提供一个**GITHUB\_TOKEN**。

{% code overflow="wrap" %}

```bash
export GITHUB_TOKEN=ghp_32849y23hij4...
codeql database create <database> --db-cluster --source-root </path/to/repo>

# Example
export GITHUB_TOKEN=ghp_32849y23hij4...
codeql database create /tmp/codeql_db --db-cluster --source-root /path/repo
## DBs will be created in /path/repo/codeql_db/*
```

{% endcode %}

#### 快速开始 - 分析代码

{% hint style="success" %}
现在终于是分析代码的时候了
{% endhint %}

请记住，如果您使用了多种语言，**每种语言一个数据库** 将会在您指定的路径中创建。

{% code overflow="wrap" %}

```bash
# Default analysis
codeql database analyze <database> --format=<format> --output=</out/file/path>
# Example
codeql database analyze /tmp/codeql_db/javascript --format=sarif-latest --output=/tmp/graphql_results.sarif

# Specify QL pack to use in the analysis
codeql database analyze <database> \
<qls pack> --sarif-category=<language> \
--sarif-add-baseline-file-info \ --format=<format> \
--output=/out/file/path>
# Example
codeql database analyze /tmp/codeql_db \
javascript-security-extended --sarif-category=javascript \
--sarif-add-baseline-file-info --format=sarif-latest \
--output=/tmp/sec-extended.sarif
```

{% endcode %}

#### 快速开始 - 脚本化

{% code overflow="wrap" %}

```bash
export GITHUB_TOKEN=ghp_32849y23hij4...
export REPO_PATH=/path/to/repo
export OUTPUT_DIR_PATH="$REPO_PATH/codeql_results"
mkdir -p "$OUTPUT_DIR_PATH"
export FINAL_MSG="Results available in: "

echo "Creating DB"
codeql database create "$REPO_PATH/codeql_db" --db-cluster --source-root "$REPO_PATH"
for db in `ls "$REPO_PATH/codeql_db"`; do
echo "Analyzing $db"
codeql database analyze "$REPO_PATH/codeql_db/$db" --format=sarif-latest --output="${OUTPUT_DIR_PATH}/$db).sarif"
FINAL_MSG="$FINAL_MSG ${OUTPUT_DIR_PATH}/$db.sarif ,"
echo ""
done

echo $FINAL_MSG
```

{% endcode %}

您可以在[**https://microsoft.github.io/sarif-web-component/**](https://microsoft.github.io/sarif-web-component/)上查看发现结果，或者使用VSCode扩展[**SARIF viewer**](https://marketplace.visualstudio.com/items?itemName=MS-SarifVSCode.sarif-viewer)。

您还可以使用[**VSCode扩展**](https://marketplace.visualstudio.com/items?itemName=GitHub.vscode-codeql)在VSCode中获取发现结果。您仍然需要手动创建数据库，然后可以选择任何文件，单击`右键` -> `CodeQL: Run Queries in Selected Files`

### [**Snyk**](https://snyk.io/product/snyk-code/)

有一个**可安装的免费版本**。

#### 快速入门

```bash
# Install
sudo npm install -g snyk

# Authenticate (you can use a free account)
snyk auth

# Test for open source vulns & license issues
snyk test [--all-projects]

# Test for code vulnerabilities
## This will upload your code and you need to enable this option in: Settings > Snyk Code
snyk test code

# Test for vulns in images
snyk container test [image]

# Test for IaC vulns
snyk iac test
```

你也可以使用[snyk VSCode Extension](https://marketplace.visualstudio.com/items?itemName=snyk-security.snyk-vulnerability-scanner)在VSCode中获取发现。

### [Insider](https://github.com/insidersec/insider)

它是**开源**的，但看起来**未维护**。

#### 支持的语言

Java（Maven和Android），Kotlin（Android），Swift（iOS），.NET完整框架，C＃和Javascript（Node.js）。

#### 快速入门

```bash
# Check the correct release for your environment
$ wget https://github.com/insidersec/insider/releases/download/2.1.0/insider_2.1.0_linux_x86_64.tar.gz
$ tar -xf insider_2.1.0_linux_x86_64.tar.gz
$ chmod +x insider
$ ./insider --tech javascript  --target <projectfolder>
```

### [**DeepSource**](https://deepsource.com/pricing)

免费用于**公共存储库**。

## NodeJS

* **`yarn`**

```bash
# Install
brew install yarn
# Run
cd /path/to/repo
yarn audit
npm audit
```

* **`pnpm`**

```bash
# Install
npm install -g pnpm
# Run
cd /path/to/repo
pnpm audit
```

* [**nodejsscan**](https://github.com/ajinabraham/nodejsscan)**:** 用于 Node.js 应用程序的静态安全代码扫描器（SAST），由[libsast](https://github.com/ajinabraham/libsast)和[semgrep](https://github.com/returntocorp/semgrep)驱动。

```bash
# Install & run
docker run -it -p 9090:9090 opensecurity/nodejsscan:latest
# Got to localhost:9090
# Upload a zip file with the code
```

* [**RetireJS**](https://github.com/RetireJS/retire.js)**:** Retire.js 的目标是帮助您检测使用已知漏洞的 JS 库版本。

```bash
# Install
npm install -g retire
# Run
cd /path/to/repo
retire --colors
```

## Electron

* [**electronegativity**](https://github.com/doyensec/electronegativity)**:** 这是一个用于识别 Electron 应用程序中的配置错误和安全反模式的工具。

## Python

* [**Bandit**](https://github.com/PyCQA/bandit)**:** Bandit 是一个旨在查找 Python 代码中常见安全问题的工具。为此，Bandit 处理每个文件，从中构建 AST，并针对 AST 节点运行适当的插件。一旦 Bandit 完成扫描所有文件，它就会生成一份报告。

```bash
# Install
pip3 install bandit

# Run
bandit -r <path to folder>
```

* [**safety**](https://github.com/pyupio/safety): Safety检查Python依赖项中已知的安全漏洞，并提出适当的漏洞修复建议。Safety可以在开发者机器上运行，在CI/CD流水线中运行，也可以在生产系统上运行。

```bash
# Install
pip install safety
# Run
safety check
```

* [~~**Pyt**~~](https://github.com/python-security/pyt): 未维护。

## .NET

```bash
# dnSpy
https://github.com/0xd4d/dnSpy

# .NET compilation
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe test.cs
```

## RUST

## RUST

```bash
# Install
cargo install cargo-audit

# Run
cargo audit

#Update the Advisory Database
cargo audit fetch
```

## Java

## Java

```bash
# JD-Gui
https://github.com/java-decompiler/jd-gui

# Java compilation step-by-step
javac -source 1.8 -target 1.8 test.java
mkdir META-INF
echo "Main-Class: test" > META-INF/MANIFEST.MF
jar cmvf META-INF/MANIFEST.MF test.jar test.class
```

| 任务            | 命令                                                        |
| ------------- | --------------------------------------------------------- |
| 执行 Jar        | java -jar \[jar]                                          |
| 解压 Jar        | unzip -d \[output directory] \[jar]                       |
| 创建 Jar        | jar -cmf META-INF/MANIFEST.MF \[output jar] \*            |
| Base64 SHA256 | sha256sum \[file] \| cut -d' ' -f1 \| xxd -r -p \| base64 |
| 移除签名          | rm META-INF/*.SF META-INF/*.RSA META-INF/\*.DSA           |
| 从 Jar 中删除     | zip -d \[jar] \[file to remove]                           |
| 反编译类          | procyon -o . \[path to class]                             |
| 反编译 Jar       | procyon -jar \[jar] -o \[output directory]                |
| 编译类           | javac \[path to .java file]                               |

## 运行

```bash
https://github.com/securego/gosec
```

## PHP

[Psalm](https://phpmagazine.net/2018/12/find-errors-in-your-php-applications-with-psalm.html) 和 [PHPStan](https://phpmagazine.net/2020/09/phpstan-pro-edition-launched.html)。

### Wordpress 插件

<https://www.pluginvulnerabilities.com/plugin-security-checker/>

## Solidity

* <https://www.npmjs.com/package/solium>

## JavaScript

### 探索

1. Burp:

* Spider and discover content
* Sitemap > filter
* Sitemap > right-click domain > Engagement tools > Find scripts

2. [WaybackURLs](https://github.com/tomnomnom/waybackurls):

* `waybackurls <domain> |grep -i "\.js" |sort -u`

### 静态分析

#### 反混淆/美化/格式化

* <https://prettier.io/playground/>
* <https://beautifier.io/>
* 也可以查看下面提到的“反混淆/解包”工具。

#### 反混淆/解包

**注意**：可能无法完全反混淆。

1. 查找并使用 .map 文件：

* 如果 .map 文件暴露出来，可以轻松地进行反混淆。
* 通常，foo.js.map 对应 foo.js。手动查找它们。
* 使用 [JS Miner](https://github.com/PortSwigger/js-miner) 查找它们。
* 确保进行主动扫描。
* 阅读 '[Tips/Notes](https://github.com/minamo7sen/burp-JS-Miner/wiki#tips--notes)'
* 如果找到了，使用 [Maximize](https://www.npmjs.com/package/maximize) 进行反混淆。

2. 没有 .map 文件，尝试使用 JSnice:

* 参考：<http://jsnice.org/> & <https://www.npmjs.com/package/jsnice>
* 提示：
* 如果使用 jsnice.org，请点击“Nicify JavaScript”按钮旁边的选项按钮，并取消选择“Infer types”以减少代码中的注释。
* 确保在脚本之前没有空行，因为这可能会影响反混淆过程并导致不准确的结果。

4. 对于一些现代的 JSNice 替代方案，您可能想看看以下内容：

* <https://github.com/pionxzh/wakaru>
* > Javascript decompiler, unpacker and unminify toolkit

> Wakaru 是用于现代前端的 Javascript 反编译器。它可以从捆绑和转译的源代码中恢复原始代码。

* <https://github.com/j4k0xb/webcrack>
* > Deobfuscate obfuscator.io, unminify and unpack bundled javascript
* <https://github.com/jehna/humanify>
* > Un-minify Javascript code using ChatGPT

> 该工具使用大型语言模型（如 ChatGPT 和 llama2）和其他工具来取消 JavaScript 代码的最小化。请注意，LLM 不执行任何结构更改 - 它们只提供重命名变量和函数的提示。通过 Babel 在 AST 级别执行繁重的工作，以确保代码保持 1-1 等效。

* <https://thejunkland.com/blog/using-llms-to-reverse-javascript-minification.html>
* > Using LLMs to reverse JavaScript variable name minification

3. 使用 `console.log()`;

* 找到最后的返回值并将其更改为 `console.log(<packerReturnVariable>);`，以便打印反混淆的 js 而不是执行它。
* 然后，将修改后（仍然混淆的）js 粘贴到 <https://jsconsole.com/> 中，以查看反混淆的 js 是否记录到控制台中。
* 最后，将反混淆的输出粘贴到 <https://prettier.io/playground/> 中进行美化以进行分析。
* **注意**：如果仍然看到打包的（但不同的）js，可能是递归打包。重复该过程。

#### 参考

* [YouTube: DAST - Javascript Dynamic Analysis](https://www.youtube.com/watch?v=_v8r_t4v6hQ)
* [https://blog.nvisium.com/angular-for-pentesters-part-1](https://web.archive.org/web/20221226054137/https://blog.nvisium.com/angular-for-pentesters-part-1)
* [https://blog.nvisium.com/angular-for-pentesters-part-2](https://web.archive.org/web/20230204012439/https://blog.nvisium.com/angular-for-pentesters-part-2)
* [devalias](https://twitter.com/_devalias) 的 [GitHub Gists](https://gist.github.com/0xdevalias):
* [Deobfuscating / Unminifying Obfuscated Web App Code](https://gist.github.com/0xdevalias/d8b743efb82c0e9406fc69da0d6c6581#deobfuscating--unminifying-obfuscated-web-app-code)
* [Reverse Engineering Webpack Apps](https://gist.github.com/0xdevalias/8c621c5d09d780b1d321bfdb86d67cdd#reverse-engineering-webpack-apps)
* [etc](https://gist.github.com/search?q=user:0xdevalias+javascript)

#### 工具

* <https://portswigger.net/burp/documentation/desktop/tools/dom-invader>

#### 较少使用的参考

* <https://cyberchef.org/>
* <https://olajs.com/javascript-prettifier>
* <https://jshint.com/>
* <https://github.com/jshint/jshint/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/network-services-pentesting/pentesting-web/code-review-tools.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
