脚本更新:
------20171128 新增了对 GET 请求的支持
对于现有的项目做了部分角度的安全测试。做个记录。
工具:sqlmap
介绍:sqlmap 很强大,但实际人为去操作的话,会比较耗时间,结合抓包工具的接口数据保存。因为 APP 主要都是 post 请求,所以自己写了个自动遍历 post 接口数据文件的脚本。
相关的参数命令可以自行百度,或者查看 github:https://github.com/sqlmapproject/sqlmap/wiki/Usage
har 包的来源很多,浏览器检查模式里右键生成,抓包工具导出等等。
步骤:
1.使用 charles 抓包所有需要检查 sql 注入的接口,把要测功能都过一遍。
(1)使用 charles 的过滤器只留下被测试应用的接口。
(2)全选被测接口,右键 ‘Export...’ -》保存为 ‘data.har’ 文件,放到下面的目录位置
(3)运行 makefile.py 脚本,生成测试接口数据文件
$python makefile.py
(4)运行 run_sqlmap.py 脚本执行接口的 sql 注入检查
$python run_sqlmap.py
我下面的脚本是直接在 dos 里把运行信息打印出来的,也可用注释掉那部分可以把运行 log 输出到文件分析。
但最终如果存在漏洞,sqlmap 会自己记录到:用户/.sqlmap/output/ip/log 文件中的。
2.脚本介绍
先简单说下我的目录结构:
--|sqlmap
----|interface_trace
--------|interface_post_file
--------|data.har
--------|makefile.py
--------|run_sqlmap.py
----|sqlmap.py
----...
makefile.py
#-*-coding:utf-8-*-
import time
__author__="orion-c"
import json,os
def loadFileData(fileName):
data = open(fileName).read()
data = json.loads(data)
return data
fileName = 'data.har'
all_data = loadFileData(fileName)
request_data = all_data['log']['entries']
for request_index in range(len(request_data)):
method = request_data[request_index]['request']['method']
if method == "POST":
url = request_data[request_index]['request']['url']
httpVersion = request_data[request_index]['request']['httpVersion']
path = url.replace('http://192.168.43.131:82/', '')
#path = url[25:url.rfind('?', 1)] #这里注意我的访问域名是xxxx:82,是25个字符,如果端口是8081等需改为27,如果是域名,改为23,下同
title_line = method + ' /' + path + ' ' + httpVersion
headers = request_data[request_index]['request']['headers']
new_headers = []
for item in headers:
new_headers.append(item['name'] + ': '+item['value'])
try:
str_new_postData = ''
postData = request_data[request_index]['request']['postData']['params']
new_postData = []
for params_item_index in range(len(postData)):
if params_item_index < len(postData) -1 :
new_postData.append(postData[params_item_index]['name']+'='+postData[params_item_index]['value']+'&')
else:
new_postData.append(postData[params_item_index]['name'] + '=' + postData[params_item_index]['value'])
for i in new_postData:
str_new_postData = str_new_postData + i
title = path.replace('/', '_')
filePath = "interface_post_file\\%s.txt" % title
int_tm = int(round(time.time() * 1000))
tm = str(int_tm)
if os.path.exists(filePath):
new_file = title + tm
filePath = "interface_post_file\\%s.txt" % new_file
with open(filePath, "a") as f: # 格式化字符串还能这么用!
f.writelines(title_line + '\n')
for i in new_headers:
f.writelines(i + '\n')
f.writelines(' ' + '\n')
f.writelines(str_new_postData)
except:
title = path.replace('/', '_')
with open("interface_post_file\\%s.txt" % title, "a") as f: # 格式化字符串还能这么用!
f.writelines(title_line + '\n')
for i in new_headers:
f.writelines(i + '\n')
if method == "GET":
url = request_data[request_index]['request']['url']
path = url[25:url.rfind('?', 1)]
path_and_parms = url[25:len(url)]
httpVersion = request_data[request_index]['request']['httpVersion']
title_line = method + ' /' + path_and_parms + ' ' + httpVersion
headers = request_data[request_index]['request']['headers']
new_headers = []
for item in headers:
new_headers.append(item['name'] + ': ' + item['value'])
try:
title = path.replace('/', '_')
filePath = "interface_post_file\\%s.txt" % title
int_tm = int(round(time.time() * 1000))
tm = str(int_tm)
if os.path.exists(filePath):
new_file = title + tm
filePath = "interface_post_file\\%s.txt" % new_file
with open(filePath, "a") as f:
f.writelines(title_line + '\n')
for i in new_headers:
f.writelines(i + '\n')
except:
title = path.replace('/', '_')
with open("interface_post_file\\%s.txt" % title, "a") as f:
f.writelines(title_line + '\n')
for i in new_headers:
f.writelines(i + '\n')
run_sqlmap.py
#-*-coding:utf-8-*-
__author__="orion-c.win"
import os
# 遍历指定目录,显示目录下的所有文件名
here = os.getcwd()
interface_post_file = here+'\interface_post_file'
pathDir = os.listdir(interface_post_file)
file_paths = []
for allDir in pathDir:
child = os.path.join('%s\%s' % (interface_post_file, allDir))
file_paths.append(child.decode('gbk')) # .decode('gbk')是解决中文显示乱码问题
os.chdir('..')
for file_path_index in range(len(file_paths)):
#os.system('python sqlmap.py -r %s --batch >> result.txt'%file_paths[file_path_index])
os.system('python sqlmap.py -r %s --batch'%file_paths[file_path_index])