51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1866|回复: 0
打印 上一主题 下一主题

安卓安全测试----《四》批量对接口执行 sqlmap 接口注入检查

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-7-16 14:48:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
《四》批量对接口执行sqlmap接口注入检查

脚本更新:
------20171128新增了对GET请求的支持

对于现有的项目做了部分角度的安全测试。做个记录。

工具:sqlmap
介绍:sqlmap很强大,但实际人为去操作的话,会比较耗时间,结合抓包工具的接口数据保存。因为APP主要都是post请求,所以自己写了个自动遍历post接口数据文件的脚本。
相关的参数命令可以自行百度,或者查看github:[color=#069d6 !important]https://github.com/sqlmapproject/sqlmap/wiki/Usage

har包的来源很多,浏览器检查模式里右键生成,抓包工具导出等等。

步骤:
1.使用charles抓包所有需要检查sql注入的接口,把要测功能都过一遍。
(1)使用charles的过滤器只留下被测试应用的接口。
(2)全选被测接口,右键‘Export...’ -》 保存为 ‘data.har’文件,放到下面的目录位置
(3)运行makefile.py 脚本,生成测试接口数据文件

  1. $python makefile.py
复制代码
(4)运行run_sqlmap.py 脚本执行接口的sql注入检查

  1. $python run_sqlmap.py
复制代码


我下面的脚本是直接在dos里把运行信息打印出来的,也可用注释掉那部分可以把运行log输出到文件分析。
但最终如果存在漏洞,sqlmap会自己记录到:用户/.sqlmap/output/ip/log 文件中的。

2.脚本介绍
先简单说下我的目录结构:

  1. <font color="rgb(34,37,39)"><font face="Helvetica, Arial,">--|sqlmap
  2. ----|interface_trace
  3. --------|interface_post_file
  4. --------|data.har
  5. --------|makefile.py
  6. --------|run_sqlmap.py
  7. ----|sqlmap.py
  8. ----...</font></font>
复制代码
makefile.py


  1. #-*-coding:utf-8-*-
  2. import time

  3. __author__="orion-c"

  4. import json,os

  5. def loadFileData(fileName):
  6.     data = open(fileName).read()
  7.     data = json.loads(data)
  8.     return data

  9. fileName = 'data.har'
  10. all_data = loadFileData(fileName)
  11. request_data = all_data['log']['entries']

  12. for request_index in range(len(request_data)):
  13.     method = request_data[request_index]['request']['method']
  14.     if method == "POST":
  15.         url = request_data[request_index]['request']['url']
  16.         httpVersion = request_data[request_index]['request']['httpVersion']
  17.         path = url.replace('http://192.168.43.131:82/', '')
  18.         #path = url[25:url.rfind('?', 1)]  #这里注意我的访问域名是xxxx:82,是25个字符,如果端口是8081等需改为27,如果是域名,改为23,下同
  19.         title_line = method + ' /' + path + ' ' + httpVersion
  20.         headers = request_data[request_index]['request']['headers']
  21.         new_headers = []
  22.         for item in headers:
  23.             new_headers.append(item['name'] + ': '+item['value'])
  24.         try:
  25.             str_new_postData = ''
  26.             postData = request_data[request_index]['request']['postData']['params']
  27.             new_postData = []
  28.             for params_item_index in range(len(postData)):
  29.                 if params_item_index < len(postData) -1 :
  30.                     new_postData.append(postData[params_item_index]['name']+'='+postData[params_item_index]['value']+'&')
  31.                 else:
  32.                     new_postData.append(postData[params_item_index]['name'] + '=' + postData[params_item_index]['value'])
  33.             for i in new_postData:
  34.                 str_new_postData = str_new_postData + i
  35.             title = path.replace('/', '_')
  36.             filePath = "interface_post_file\\%s.txt" % title
  37.             int_tm = int(round(time.time() * 1000))
  38.             tm = str(int_tm)
  39.             if os.path.exists(filePath):
  40.                 new_file = title + tm
  41.                 filePath = "interface_post_file\\%s.txt" % new_file
  42.             with open(filePath, "a") as f:  # 格式化字符串还能这么用!
  43.                 f.writelines(title_line + '\n')
  44.                 for i in new_headers:
  45.                     f.writelines(i + '\n')
  46.                 f.writelines(' ' + '\n')
  47.                 f.writelines(str_new_postData)
  48.         except:
  49.             title = path.replace('/', '_')
  50.             with open("interface_post_file\\%s.txt" % title, "a") as f:  # 格式化字符串还能这么用!
  51.                 f.writelines(title_line + '\n')
  52.                 for i in new_headers:
  53.                     f.writelines(i + '\n')
  54.     if method == "GET":
  55.         url = request_data[request_index]['request']['url']
  56.         path = url[25:url.rfind('?', 1)]
  57.         path_and_parms = url[25:len(url)]
  58.         httpVersion = request_data[request_index]['request']['httpVersion']
  59.         title_line = method + ' /' + path_and_parms + ' ' + httpVersion
  60.         headers = request_data[request_index]['request']['headers']
  61.         new_headers = []
  62.         for item in headers:
  63.             new_headers.append(item['name'] + ': ' + item['value'])
  64.         try:
  65.             title = path.replace('/', '_')
  66.             filePath = "interface_post_file\\%s.txt" % title
  67.             int_tm = int(round(time.time() * 1000))
  68.             tm = str(int_tm)
  69.             if os.path.exists(filePath):
  70.                 new_file = title + tm
  71.                 filePath = "interface_post_file\\%s.txt" % new_file
  72.             with open(filePath, "a") as f:
  73.                 f.writelines(title_line + '\n')
  74.                 for i in new_headers:
  75.                     f.writelines(i + '\n')
  76.         except:
  77.             title = path.replace('/', '_')
  78.             with open("interface_post_file\\%s.txt" % title, "a") as f:
  79.                 f.writelines(title_line + '\n')
  80.                 for i in new_headers:
  81.                     f.writelines(i + '\n')
复制代码


run_sqlmap.py
  1. #-*-coding:utf-8-*-
  2. __author__="orion-c.win"

  3. import os
  4. # 遍历指定目录,显示目录下的所有文件名
  5. here = os.getcwd()
  6. interface_post_file = here+'\interface_post_file'
  7. pathDir = os.listdir(interface_post_file)
  8. file_paths = []
  9. for allDir in pathDir:
  10.     child = os.path.join('%s\%s' % (interface_post_file, allDir))
  11.     file_paths.append(child.decode('gbk')) # .decode('gbk')是解决中文显示乱码问题
  12. os.chdir('..')
  13. for file_path_index in range(len(file_paths)):
  14.     #os.system('python sqlmap.py -r %s --batch >> result.txt'%file_paths[file_path_index])
  15.     os.system('python sqlmap.py -r %s --batch'%file_paths[file_path_index])
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-19 15:28 , Processed in 0.061404 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表