hacktricks/network-services-pentesting/pentesting-web/code-review-tools.md

456 lines
16 KiB
Markdown
Raw Normal View History

# 源代码审查 / SAST 工具
2022-04-28 16:01:33 +00:00
<details>
<summary><strong>从零开始学习 AWS 黑客技术,成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTEHackTricks AWS 红队专家)</strong></a><strong></strong></summary>
2022-04-28 16:01:33 +00:00
支持 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 来**分享您的黑客技巧**。
2022-04-28 16:01:33 +00:00
</details>
## 指南和工具列表
2021-09-25 13:27:46 +00:00
* [**https://owasp.org/www-community/Source\_Code\_Analysis\_Tools**](https://owasp.org/www-community/Source\_Code\_Analysis\_Tools)
2023-08-03 19:12:22 +00:00
* [**https://github.com/analysis-tools-dev/static-analysis**](https://github.com/analysis-tools-dev/static-analysis)
## 多语言工具
2023-08-03 19:12:22 +00:00
### [Naxus - AI-Gents](https://www.naxusai.com/)
2023-08-03 19:12:22 +00:00
有一个**免费的套餐用于审查 PR**。
2023-08-03 19:12:22 +00:00
### [**Semgrep**](https://github.com/returntocorp/semgrep)
2023-08-03 19:12:22 +00:00
这是一个**开源工具**。
2023-08-03 19:12:22 +00:00
#### 支持的语言
2023-08-03 19:12:22 +00:00
| 类别 | 语言 |
| ------------ | ----------------------------------------------------------------------------------------------------- |
| 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 · |
2023-08-03 19:12:22 +00:00
#### 快速开始
2023-08-03 19:12:22 +00:00
{% code overflow="wrap" %}
```bash
# Install https://github.com/returntocorp/semgrep#option-1-getting-started-from-the-cli
brew install semgrep
2023-08-03 19:12:22 +00:00
# Go to your repo code and scan
cd repo
semgrep scan --config auto
2020-11-10 09:27:45 +00:00
```
{% endcode %}
2020-11-10 09:27:45 +00:00
您还可以使用[**semgrep VSCode Extension**](https://marketplace.visualstudio.com/items?itemName=Semgrep.semgrep)在VSCode中获取查找结果。
2023-08-03 19:12:22 +00:00
### [**SonarQube**](https://www.sonarsource.com/products/sonarqube/downloads/)
2023-08-03 19:12:22 +00:00
有一个可安装的**免费版本**。
2023-08-03 19:12:22 +00:00
#### 快速开始
2023-08-03 19:12:22 +00:00
{% code overflow="wrap" %}
2020-11-10 09:27:45 +00:00
```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>
2020-11-10 09:27:45 +00:00
```
{% endcode %}
2023-08-03 19:12:22 +00:00
### CodeQL
2023-08-03 19:12:22 +00:00
有一个**可安装的免费版本**,但根据许可证,您只能在**开源项目中使用免费的CodeQL版本**。
2023-08-03 19:12:22 +00:00
#### 安装
2023-08-03 19:12:22 +00:00
{% 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
2023-08-03 19:12:22 +00:00
# Move it to the destination folder
mkdir ~/codeql
mv codeql-bundle* ~/codeql
2023-08-03 19:12:22 +00:00
# Decompress it
cd ~/codeql
tar -xzvf codeql-bundle-*.tar.gz
rm codeql-bundle-*.tar.gz
2023-08-03 19:12:22 +00:00
# Add to path
echo 'export PATH="$PATH:/Users/username/codeql/codeql"' >> ~/.zshrc
2023-08-03 19:12:22 +00:00
# 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个数据库**来指示每种语言。
2023-08-03 19:12:22 +00:00
{% 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 %}
请记住,如果您使用了多种语言,在您指定的路径中将会为**每种语言创建一个数据库**。
```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 %}
2023-08-03 19:12:22 +00:00
您可以在[**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中获取发现。
2023-08-03 19:12:22 +00:00
### [Insider](https://github.com/insidersec/insider)
2023-08-03 19:12:22 +00:00
它是**开源**的,但看起来**未维护**。
2023-08-03 19:12:22 +00:00
#### 支持的语言
2023-08-03 19:12:22 +00:00
JavaMaven和AndroidKotlinAndroidSwiftiOS.NET完整框架C#和JavascriptNode.js
2023-08-03 19:12:22 +00:00
#### 快速入门
```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)&#x20;
2023-08-03 19:12:22 +00:00
免费用于**公共存储库**。
2023-08-03 19:12:22 +00:00
## NodeJS
2023-08-03 19:12:22 +00:00
* **`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
2023-08-03 19:12:22 +00:00
* [**electronegativity**](https://github.com/doyensec/electronegativity)**:** 这是一个用于识别 Electron 应用程序中的配置错误和安全反模式的工具。
2023-08-03 19:12:22 +00:00
## Python
2023-08-03 19:12:22 +00:00
* [**Bandit**](https://github.com/PyCQA/bandit)**:** Bandit 是一个旨在查找 Python 代码中常见安全问题的工具。为此Bandit 处理每个文件,从中构建 AST并针对 AST 节点运行适当的插件。一旦 Bandit 完成扫描所有文件,它就会生成一份报告。
```bash
# Install
pip3 install bandit
2023-08-03 19:12:22 +00:00
# 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): 未维护。
2023-08-03 19:12:22 +00:00
## .NET
2020-11-10 09:27:45 +00:00
```bash
# dnSpy
https://github.com/0xd4d/dnSpy
# .NET compilation
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe test.cs
```
## RUST
### RUST
Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It is a great language for writing low-level code, such as operating systems, file systems, and other performance-critical software.
Rust is known for its strong type system and ownership model, which allows for memory safety without garbage collection. It achieves this through a concept called "ownership," which ensures that memory is cleaned up properly without the need for a garbage collector.
Rust also provides excellent concurrency support through its ownership model, which allows for safe and efficient parallelism without data races.
Overall, Rust is a powerful language that combines the performance of low-level languages with the safety and ease of use of high-level languages. It is a great choice for projects where performance and safety are critical.
```bash
# Install
cargo install cargo-audit
# Run
cargo audit
#Update the Advisory Database
cargo audit fetch
```
2022-05-07 10:35:17 +00:00
## Java
## Java
2020-11-10 09:27:45 +00:00
```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
```
| 任务 | 命令 |
2022-04-11 00:18:13 +00:00
| --------------- | --------------------------------------------------------- |
| 执行 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] |
## 运行
2021-05-26 11:18:48 +00:00
```bash
https://github.com/securego/gosec
```
2022-05-07 10:35:17 +00:00
## 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 插件
2022-04-11 00:18:13 +00:00
2022-05-07 10:35:17 +00:00
[https://www.pluginvulnerabilities.com/plugin-security-checker/](https://www.pluginvulnerabilities.com/plugin-security-checker/)
2022-04-28 16:01:33 +00:00
2022-05-07 10:35:17 +00:00
## Solidity
* [https://www.npmjs.com/package/solium](https://www.npmjs.com/package/solium)
2022-04-28 16:01:33 +00:00
## 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`
### 静态分析
#### 反混淆/美化
2022-04-28 16:01:33 +00:00
* [https://prettier.io/playground/](https://prettier.io/playground/)
* [https://beautifier.io/](https://beautifier.io/)
2022-04-28 16:01:33 +00:00
#### 解混淆/解包
2022-04-28 16:01:33 +00:00
**注意**: 可能无法完全解混淆。
2022-04-28 16:01:33 +00:00
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”以减少代码中的注释。
* 确保在脚本之前没有空行,因为这可能会影响解混淆过程并导致不准确的结果。
3. 使用 console.log();
* 找到最后的返回值并将其更改为 `console.log(<packerReturnVariable>);`,以便打印解混淆的 js 而不是执行它。
* 然后将修改后仍然混淆的js 粘贴到 https://jsconsole.com/ 中,以查看解混淆的 js 在控制台中记录。
* 最后,将解混淆的输出粘贴到 https://prettier.io/playground/ 中进行美化以进行分析。
* **注意**: 如果仍然看到打包的但不同的js可能是递归打包。重复该过程。
参考
* https://www.youtube.com/watch?v=\_v8r\_t4v6hQ
* https://blog.nvisium.com/angular-for-pentesters-part-1
* https://blog.nvisium.com/angular-for-pentesters-part-2
工具
* 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/