SQL注入检测与防护全面指南
SQL注入是Web应用最常见的安全威胁之一,攻击者通过在用户输入中插入恶意SQL代码来操纵数据库查询。以下是检测和处理SQL注入的完整解决方案:
检测SQL注入的方法
1. 手工检测技术
// 基本检测方法:输入特殊字符观察响应
$testInputs = [
"' OR 1=1 -- ",
'" OR "a"="a',
'; SELECT SLEEP(5) -- ',
'UNION SELECT NULL, username, password FROM users -- '
];
foreach ($testInputs as $input) {
$result = $pdo->query("SELECT * FROM products WHERE id = '$input'");
// 观察异常响应、错误信息或延迟
}
2. 自动化检测工具
- SQLMap:自动化SQL注入检测和利用工具
sqlmap -u "http://example.com/product.php?id=1" --risk=3 --level=5
- OWASP ZAP:集成SQL注入扫描的Web应用安全测试工具
- Burp Suite:专业Web安全测试工具(商业版)
3. 日志分析检测
-- 监控异常SQL查询日志
SELECT * FROM mysql.general_log
WHERE argument LIKE '%union%select%'
OR argument LIKE '%;--%'
OR argument LIKE '%waitfor%delay%'
OR argument LIKE '%xp_cmdshell%';
SQL注入防护措施
1. 参数化查询(预处理语句) - 最有效的方法
// PDO 参数化查询示例
$id = $_GET['id'];
$status = 'active';
// 使用命名占位符
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id AND status = :status");
$stmt->execute([':id' => $id, ':status' => $status]);
// 使用问号占位符
$stmt = $pdo->prepare("SELECT * FROM products WHERE category = ? AND price > ?");
$stmt->execute([$category, $minPrice]);
2. 存储过程
// 调用存储过程
$stmt = $pdo->prepare("CALL GetUserByEmail(?)");
$stmt->execute([$email]);
3. 输入验证与净化
// 白名单验证
$allowedCategories = ['books', 'electronics', 'clothing'];
if (!in_array($category, $allowedCategories)) {
die("无效类别");
}
// 类型转换
$id = (int)$_GET['id'];
// 正则表达式验证
if (!preg_match('/^[a-zA-Z0-9_]{3,20}$/', $username)) {
die("用户名格式无效");
}
4. 最小权限原则
-- 数据库用户权限设置
CREATE USER 'webapp'@'localhost' IDENTIFIED BY 'StrongPassword!123';
GRANT SELECT, INSERT ON shop.products TO 'webapp'@'localhost';
GRANT SELECT ON shop.users TO 'webapp'@'localhost';
REVOKE DROP, ALTER, CREATE ON *.* FROM 'webapp'@'localhost';
5. 安全框架实践
// Laravel Eloquent ORM 示例
$user = User::where('email', $email)
->where('status', 'active')
->first();
// Symfony Doctrine 示例
$query = $entityManager->createQuery(
'SELECT p FROM Product p WHERE p.price > :price'
)->setParameter('price', $minPrice);
6. Web应用防火墙(WAF)
# Nginx + ModSecurity 配置示例
location / {
ModSecurityEnabled on;
ModSecurityConfig modsecurity.conf;
}
动态表名/列名的安全处理
// 只允许特定的表名
$allowedTables = ['users', 'products', 'orders'];
$table = $_GET['table'];
if (!in_array($table, $allowedTables)) {
die("无效表名");
}
// 使用白名单映射
$sortColumns = [
'name' => 'product_name',
'price' => 'unit_price',
'date' => 'created_at'
];
$sort = $_GET['sort'] ?? 'name';
$column = $sortColumns[$sort] ?? $sortColumns['name'];
// 安全查询
$stmt = $pdo->prepare("SELECT * FROM `" . str_replace("`", "``", $table) . "` ORDER BY $column");
$stmt->execute();
错误处理最佳实践
// 生产环境错误处理
try {
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$id]);
$user = $stmt->fetch();
} catch (PDOException $e) {
// 记录详细错误到日志
error_log("SQL Error: " . $e->getMessage());
// 用户友好消息
die("数据库请求失败,请稍后再试");
}
// 开发环境配置
ini_set('display_errors', '0'); // 禁用错误显示
ini_set('log_errors', '1'); // 启用错误日志
ini_set('error_log', '/path/to/php-errors.log');
SQL注入检测脚本
function detectSQLInjection($input) {
$patterns = [
// 常见注入模式
'/\b(union\s+select)\b/i',
'/\b(insert|update|delete|drop|alter|truncate)\b/i',
'/\b(exec|execute|xp_cmdshell|sp_executesql)\b/i',
'/\b(or\s+[\'"]?\d+[\'"]?\s*=\s*[\'"]?\d+)\b/i',
'/;\s*(--|#)/',
'/sleep\(\d+\)/i',
'/benchmark\(/i',
'/\b(outfile|dumpfile)\b/i',
'/@@version/i',
// 编码注入检测
'/%27|\x27/i', // 单引号
'/%22|\x22/i', // 双引号
'/%3B|\x3B/i', // 分号
'/%2D%2D|\x2D\x2D/i', // --
'/\b(0x[0-9a-f]+)\b/i' // 十六进制编码
];
foreach ($patterns as $pattern) {
if (preg_match($pattern, $input)) {
return true;
}
}
return false;
}
// 使用示例
if (detectSQLInjection($_POST['username'])) {
// 记录攻击尝试
$log = date('Y-m-d H:i:s') . " - SQLi attempt: " . $_SERVER['REMOTE_ADDR'] .
" - " . $_POST['username'] . "\n";
file_put_contents('security.log', $log, FILE_APPEND);
// 响应模糊信息
die("请求包含可疑内容");
}
多层防御策略
总结:SQL注入防护最佳实践
-
优先使用参数化查询
- 对所有动态值使用预处理语句
- 避免手动拼接SQL查询
-
实施最小权限原则
- 数据库用户只能访问必需的数据
- 禁止DROP、ALTER等高危权限
-
严格输入验证
- 白名单优于黑名单
- 类型转换和格式验证
-
安全错误处理
- 生产环境禁用详细错误显示
- 记录详细错误到安全日志
-
纵深防御策略
- Web应用防火墙
- 数据库防火墙
- 定期安全审计
-
持续安全维护
- 定期更新数据库和框架
- 渗透测试和安全扫描
- 安全培训和代码审查
通过实施这些措施,你可以显著降低应用遭受SQL注入攻击的风险。记住:安全不是一次性任务,而是需要持续关注和改进的过程。