mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
417 lines
17 KiB
Markdown
417 lines
17 KiB
Markdown
# Revisione del codice sorgente / Strumenti SAST
|
||
|
||
{{#include ../../banners/hacktricks-training.md}}
|
||
|
||
## Linee guida e elenco di strumenti
|
||
|
||
- [**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)
|
||
|
||
## Strumenti Multi-Lingua
|
||
|
||
### [Naxus - AI-Gents](https://www.naxusai.com/)
|
||
|
||
C'è un **pacchetto gratuito per rivedere le PR**.
|
||
|
||
### [**Semgrep**](https://github.com/returntocorp/semgrep)
|
||
|
||
È uno **strumento Open Source**.
|
||
|
||
#### Lingue Supportate
|
||
|
||
| Categoria | Lingue |
|
||
| ------------ | ----------------------------------------------------------------------------------------------------- |
|
||
| GA | C# · Go · Java · JavaScript · JSX · JSON · PHP · Python · Ruby · Scala · Terraform · TypeScript · TSX |
|
||
| Beta | Kotlin · Rust |
|
||
| Sperimentale | Bash · C · C++ · Clojure · Dart · Dockerfile · Elixir · HTML · Julia · Jsonnet · Lisp · |
|
||
|
||
#### Inizio Veloce
|
||
```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
|
||
```
|
||
Puoi anche utilizzare l'[**estensione semgrep per VSCode**](https://marketplace.visualstudio.com/items?itemName=Semgrep.semgrep) per ottenere i risultati all'interno di VSCode.
|
||
|
||
### [**SonarQube**](https://www.sonarsource.com/products/sonarqube/downloads/)
|
||
|
||
Esiste una **versione gratuita** installabile.
|
||
|
||
#### Avvio Veloce
|
||
```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>
|
||
```
|
||
### CodeQL
|
||
|
||
Esiste una **versione gratuita installabile** ma secondo la licenza puoi **utilizzare solo la versione gratuita di codeQL in progetti Open Source**.
|
||
|
||
#### Installa
|
||
```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
|
||
```
|
||
#### Quick Start - Prepara il database
|
||
|
||
> [!TIP]
|
||
> La prima cosa che devi fare è **preparare il database** (creare l'albero del codice) in modo che in seguito le query vengano eseguite su di esso.
|
||
|
||
- Puoi consentire a codeql di identificare automaticamente il linguaggio del repo e creare il database.
|
||
```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
|
||
```
|
||
> [!CAUTION]
|
||
> Questo **di solito attiverà un errore** che dice che è stata specificata più di una lingua (o rilevata automaticamente). **Controlla le opzioni successive** per risolvere questo problema!
|
||
|
||
- Puoi farlo **manualmente indicando** il **repo** e la **lingua** ([elenco delle lingue](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))
|
||
```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
|
||
```
|
||
- Se il tuo repo utilizza **più di 1 lingua**, puoi anche creare **1 DB per lingua** indicando ciascuna lingua.
|
||
```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/*
|
||
```
|
||
- Puoi anche consentire a `codeql` di **identificare tutti i linguaggi** per te e creare un DB per linguaggio. Devi fornirgli un **GITHUB_TOKEN**.
|
||
```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/*
|
||
```
|
||
#### Quick Start - Analizza il codice
|
||
|
||
> [!TIP]
|
||
> Ora è finalmente il momento di analizzare il codice
|
||
|
||
Ricorda che se hai utilizzato più linguaggi, **un DB per linguaggio** sarebbe stato creato nel percorso che hai specificato.
|
||
```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
|
||
```
|
||
#### Avvio Veloce - Scriptato
|
||
```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
|
||
```
|
||
Puoi visualizzare i risultati in [**https://microsoft.github.io/sarif-web-component/**](https://microsoft.github.io/sarif-web-component/) o utilizzando l'estensione di VSCode [**SARIF viewer**](https://marketplace.visualstudio.com/items?itemName=MS-SarifVSCode.sarif-viewer).
|
||
|
||
Puoi anche utilizzare l'[**estensione di VSCode**](https://marketplace.visualstudio.com/items?itemName=GitHub.vscode-codeql) per ottenere i risultati all'interno di VSCode. Dovrai comunque creare un database manualmente, ma poi puoi selezionare qualsiasi file e cliccare su `Right Click` -> `CodeQL: Run Queries in Selected Files`
|
||
|
||
### [**Snyk**](https://snyk.io/product/snyk-code/)
|
||
|
||
C'è una **versione gratuita installabile**.
|
||
|
||
#### Quick Start
|
||
```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
|
||
```
|
||
Puoi anche utilizzare l'[**estensione snyk per VSCode**](https://marketplace.visualstudio.com/items?itemName=snyk-security.snyk-vulnerability-scanner) per ottenere risultati all'interno di VSCode.
|
||
|
||
### [Insider](https://github.com/insidersec/insider)
|
||
|
||
È **Open Source**, ma sembra **non mantenuto**.
|
||
|
||
#### Linguaggi Supportati
|
||
|
||
Java (Maven e Android), Kotlin (Android), Swift (iOS), .NET Full Framework, C# e Javascript (Node.js).
|
||
|
||
#### Inizio Veloce
|
||
```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)
|
||
|
||
Gratuito per **repo pubblici**.
|
||
|
||
## NodeJS
|
||
|
||
- **`yarn`**
|
||
```bash
|
||
# Install
|
||
brew install yarn
|
||
# Run
|
||
cd /path/to/repo
|
||
yarn install
|
||
yarn audit # In lower versions
|
||
yarn npm audit # In 2+ versions
|
||
|
||
npm audit
|
||
```
|
||
- **`pnpm`**
|
||
```bash
|
||
# Install
|
||
npm install -g pnpm
|
||
# Run
|
||
cd /path/to/repo
|
||
pnpm install
|
||
pnpm audit
|
||
```
|
||
- [**nodejsscan**](https://github.com/ajinabraham/nodejsscan)**:** Scanner di sicurezza del codice statico (SAST) per applicazioni Node.js alimentato da [libsast](https://github.com/ajinabraham/libsast) e [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)**:** L'obiettivo di Retire.js è aiutarti a rilevare l'uso di versioni di librerie JS con vulnerabilità note.
|
||
```bash
|
||
# Install
|
||
npm install -g retire
|
||
# Run
|
||
cd /path/to/repo
|
||
retire --colors
|
||
```
|
||
## Electron
|
||
|
||
- [**electronegativity**](https://github.com/doyensec/electronegativity)**:** È uno strumento per identificare misconfigurazioni e anti-pattern di sicurezza nelle applicazioni basate su Electron.
|
||
|
||
## Python
|
||
|
||
- [**Bandit**](https://github.com/PyCQA/bandit)**:** Bandit è uno strumento progettato per trovare problemi di sicurezza comuni nel codice Python. Per fare ciò, Bandit elabora ogni file, costruisce un AST da esso e esegue plugin appropriati contro i nodi dell'AST. Una volta che Bandit ha terminato di esaminare tutti i file, genera un rapporto.
|
||
```bash
|
||
# Install
|
||
pip3 install bandit
|
||
|
||
# Run
|
||
bandit -r <path to folder>
|
||
```
|
||
- [**safety**](https://github.com/pyupio/safety): Safety controlla le dipendenze Python per vulnerabilità di sicurezza note e suggerisce le opportune soluzioni per le vulnerabilità rilevate. Safety può essere eseguito su macchine di sviluppo, in pipeline CI/CD e su sistemi di produzione.
|
||
```bash
|
||
# Install
|
||
pip install safety
|
||
# Run
|
||
safety check
|
||
```
|
||
- [~~**Pyt**~~](https://github.com/python-security/pyt): Non mantenuto.
|
||
|
||
## .NET
|
||
```bash
|
||
# dnSpy
|
||
https://github.com/0xd4d/dnSpy
|
||
|
||
# .NET compilation
|
||
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe test.cs
|
||
```
|
||
## RUST
|
||
```bash
|
||
# Install
|
||
cargo install cargo-audit
|
||
|
||
# Run
|
||
cargo audit
|
||
|
||
#Update the Advisory Database
|
||
cargo audit fetch
|
||
```
|
||
## 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
|
||
```
|
||
| Compito | Comando |
|
||
| --------------- | -------------------------------------------------------- |
|
||
| Esegui Jar | java -jar \[jar] |
|
||
| Estrai Jar | unzip -d \[output directory] \[jar] |
|
||
| Crea Jar | jar -cmf META-INF/MANIFEST.MF \[output jar] \* |
|
||
| Base64 SHA256 | sha256sum \[file] \| cut -d' ' -f1 \| xxd -r -p \| base64 |
|
||
| Rimuovi Firma | rm META-INF/_.SF META-INF/_.RSA META-INF/\*.DSA |
|
||
| Elimina da Jar | zip -d \[jar] \[file to remove] |
|
||
| Decompila classe| procyon -o . \[path to class] |
|
||
| Decompila Jar | procyon -jar \[jar] -o \[output directory] |
|
||
| Compila classe | javac \[path to .java file] |
|
||
|
||
## Vai
|
||
```bash
|
||
https://github.com/securego/gosec
|
||
```
|
||
## PHP
|
||
|
||
[Psalm](https://phpmagazine.net/2018/12/find-errors-in-your-php-applications-with-psalm.html) e [PHPStan](https://phpmagazine.net/2020/09/phpstan-pro-edition-launched.html).
|
||
|
||
### Wordpress Plugins
|
||
|
||
[https://www.pluginvulnerabilities.com/plugin-security-checker/](https://www.pluginvulnerabilities.com/plugin-security-checker/)
|
||
|
||
## Solidity
|
||
|
||
- [https://www.npmjs.com/package/solium](https://www.npmjs.com/package/solium)
|
||
|
||
## JavaScript
|
||
|
||
### Discovery
|
||
|
||
1. Burp:
|
||
- Spider e scopri contenuti
|
||
- Sitemap > filtro
|
||
- Sitemap > clic destro sul dominio > Strumenti di ingaggio > Trova script
|
||
2. [WaybackURLs](https://github.com/tomnomnom/waybackurls):
|
||
- `waybackurls <domain> |grep -i "\.js" |sort -u`
|
||
|
||
### Static Analysis
|
||
|
||
#### Unminimize/Beautify/Prettify
|
||
|
||
- [https://prettier.io/playground/](https://prettier.io/playground/)
|
||
- [https://beautifier.io/](https://beautifier.io/)
|
||
- Vedi alcuni degli strumenti menzionati in 'Deobfuscate/Unpack' qui sotto.
|
||
|
||
#### Deobfuscate/Unpack
|
||
|
||
**Nota**: Potrebbe non essere possibile deobfuscate completamente.
|
||
|
||
1. Trova e usa i file .map:
|
||
- Se i file .map sono esposti, possono essere utilizzati per deobfuscate facilmente.
|
||
- Comunemente, foo.js.map mappa a foo.js. Cerca manualmente.
|
||
- Usa [JS Miner](https://github.com/PortSwigger/js-miner) per cercarli.
|
||
- Assicurati che venga condotta una scansione attiva.
|
||
- Leggi '[Tips/Notes](https://github.com/minamo7sen/burp-JS-Miner/wiki#tips--notes)'
|
||
- Se trovati, usa [Maximize](https://www.npmjs.com/package/maximize) per deobfuscate.
|
||
2. Senza file .map, prova JSnice:
|
||
- Riferimenti: [http://jsnice.org/](http://jsnice.org/) & [https://www.npmjs.com/package/jsnice](https://www.npmjs.com/package/jsnice)
|
||
- Suggerimenti:
|
||
- Se usi jsnice.org, clicca sul pulsante delle opzioni accanto al pulsante "Nicify JavaScript" e deseleziona "Infer types" per ridurre il disordine nel codice con commenti.
|
||
- Assicurati di non lasciare righe vuote prima dello script, poiché potrebbe influenzare il processo di deobfuscation e dare risultati imprecisi.
|
||
3. Per alcune alternative più moderne a JSNice, potresti voler guardare i seguenti:
|
||
|
||
- [https://github.com/pionxzh/wakaru](https://github.com/pionxzh/wakaru)
|
||
- > Javascript decompiler, unpacker e unminify toolkit Wakaru è il decompilatore Javascript per il frontend moderno. Riporta il codice originale da una sorgente impacchettata e transpiled.
|
||
- [https://github.com/j4k0xb/webcrack](https://github.com/j4k0xb/webcrack)
|
||
- > Deobfuscate obfuscator.io, unminify e unpack javascript impacchettato
|
||
- [https://github.com/jehna/humanify](https://github.com/jehna/humanify)
|
||
- > Un-minify codice Javascript usando ChatGPT Questo strumento utilizza modelli di linguaggio di grandi dimensioni (come ChatGPT & llama2) e altri strumenti per un-minify codice Javascript. Nota che i LLM non eseguono alcuna modifica strutturale – forniscono solo suggerimenti per rinominare variabili e funzioni. Il lavoro pesante è svolto da Babel a livello AST per garantire che il codice rimanga equivalente 1-1.
|
||
- [https://thejunkland.com/blog/using-llms-to-reverse-javascript-minification.html](https://thejunkland.com/blog/using-llms-to-reverse-javascript-minification.html)
|
||
- > Usando LLMs per invertire la minificazione dei nomi delle variabili JavaScript
|
||
|
||
3. Usa `console.log()`;
|
||
- Trova il valore di ritorno alla fine e cambialo in `console.log(<packerReturnVariable>);` in modo che il js deobfuscato venga stampato invece di essere eseguito.
|
||
- Poi, incolla il js modificato (e ancora obfuscato) in [https://jsconsole.com/](https://jsconsole.com/) per vedere il js deobfuscato registrato nella console.
|
||
- Infine, incolla l'output deobfuscato in [https://prettier.io/playground/](https://prettier.io/playground/) per abbellirlo per l'analisi.
|
||
- **Nota**: Se continui a vedere js impacchettato (ma diverso), potrebbe essere impacchettato ricorsivamente. Ripeti il processo.
|
||
|
||
#### References
|
||
|
||
- [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)'s [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)
|
||
|
||
#### Tools
|
||
|
||
- [https://portswigger.net/burp/documentation/desktop/tools/dom-invader](https://portswigger.net/burp/documentation/desktop/tools/dom-invader)
|
||
|
||
#### Less Used References
|
||
|
||
- [https://cyberchef.org/](https://cyberchef.org/)
|
||
- [https://olajs.com/javascript-prettifier](https://olajs.com/javascript-prettifier)
|
||
- [https://jshint.com/](https://jshint.com/)
|
||
- [https://github.com/jshint/jshint/](https://github.com/jshint/jshint/)
|
||
|
||
{{#include ../../banners/hacktricks-training.md}}
|