不多说了,直接上手代码,也没有啥练手的,都是很熟悉的代码,水一篇,方便作为工作的小工具吧。试了一下,配合一个好点的字典,还是可以作为个人小工具使用的。

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3.  
  4. '''
  5. SSH服务弱口令扫描脚本
  6. 作者:陈然
  7. '''
  8.  
  9. #引入包文件
  10. import ipaddr
  11. import logging
  12. import datetime
  13. import paramiko
  14. import threading
  15. from optparse import OptionParser
  16.  
  17. #定义全局配置
  18. logging.basicConfig(format="%(message)s",level=logging.INFO)
  19.  
  20. #定义全局变量
  21. username_config_file = "../config/username.conf"
  22. password_config_file = "../config/password.conf"
  23. username_list = []
  24. password_list = []
  25. target_list = []
  26. result_list = []
  27. multi_thread = False
  28.  
  29. #定义全局接口函数
  30. def read_config_from_file():
  31. """从配置文件夹下的字典文件中读取爆破用户名和口令"""
  32. global username_list
  33. global password_list
  34. #读取用户名字典
  35. with open(username_config_file,"r") as fr:
  36. for line in fr.readlines():
  37. username = line.split("\n")[0].split("\r")[0]
  38. username_list.append(username)
  39. #读取口令字典
  40. with open(password_config_file,"r") as fr:
  41. for line in fr.readlines():
  42. password = line.split("\n")[0].split("\r")[0]
  43. password_list.append(password)
  44. #字典列表去重
  45. username_list = list(set(username_list))
  46. password_list = list(set(password_list))
  47.  
  48. def change_config_files(username_file=None,password_file=None):
  49. """指定用户名和口令的字典配置文件"""
  50. global username_config_file
  51. global password_config_file
  52. if username_file != None:
  53. username_config_file = username_file
  54. if password_file != None:
  55. password_config_file = password_file
  56.  
  57. def target_analyst(target):
  58. """对于目标网络地址分析并拆分其中的地址段 仅支持IPv4"""
  59. global target_list
  60. target = ipaddr.IPv4Network(target)
  61. hosts_list = target.iterhosts()
  62. for host in hosts_list:
  63. target_list.append(str(host))
  64.  
  65. def target_file_anylast(filename):
  66. """分析目标列表文件"""
  67. file_to_target = []
  68. with open(filename,"r") as fr:
  69. for line in fr.readlines():
  70. each_target = line.split("\n")[0].split("\r")[0]
  71. file_to_target.append(each_target)
  72. return file_to_target
  73.  
  74. def send_crack_packet(target,username,password,port=22,timeout=3):
  75. """发送爆破登录报文"""
  76. global result_list
  77. #局部变量
  78. flag = False#是否有漏洞的标志位,默认False
  79. #创建SSH对象并登陆
  80. logging.info("[+] 爆破对象 地址%s 端口:%s 用户名:%s 口令:%s"%(str(target),str(port),str(username),str(password)))
  81. ssh = paramiko.SSHClient()
  82. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  83. try:
  84. ssh.connect(hostname=target, port=port, username=username, password=password,timeout=timeout,allow_agent=False,look_for_keys = False)
  85. #执行命令
  86. stdin, stdout, stderr = ssh.exec_command('whoami',timeout=timeout)
  87. #获取命令结果
  88. result = stdout.read().split("\n")[0]
  89. if result == username:
  90. flag = True
  91. report_sting = "%s,%s,%s,%s,%s\n"%(str(target),"YES",str(port),str(username),str(password))
  92. result_list.append(report_sting)
  93. logging.info("[*] 爆破成功: 详细信息[地址:%s,端口:%s,用户名:%s,口令:%s]"%(str(target),str(port),str(username),str(password)))
  94. try:
  95. if multi_thread == False:
  96. continue_flag = raw_input("是否继续?[1]继续[2]退出")
  97. continue_flag = int(continue_flag)
  98. else:
  99. continue_flag = 1
  100. except Exception,ex:
  101. continue_flag = 2
  102. if continue_flag != 1:
  103. exit(0)
  104. except Exception,ex:
  105. pass
  106. #关闭连接
  107. ssh.close()
  108. return flag
  109.  
  110. def create_report():
  111. """生成报告文件"""
  112. time_string = str(datetime.datetime.now()).replace(" ","").replace(":","")
  113. fd = open("../result/%s.csv"%time_string,"w")
  114. fd.write("Target-IP,WEAK,PORT,USERNAME,PASSWORD\n")
  115. for result_string in result_list:
  116. fd.write(result_string)
  117. fd.close()
  118.  
  119. def parameter_checker(parameter):
  120. """参数检查函数"""
  121. if parameter in ["",None," ","null"]:
  122. return False
  123. else:
  124. return True
  125.  
  126. def list_devide(object_list,count):
  127. """列表拆分函数"""
  128. return_list = []
  129. if not isinstance(object_list,list):
  130. return []
  131. else:
  132. total = len(object_list)
  133. size = total/count + 1
  134. start = 0
  135. end = start + size
  136. while True:
  137. if end <= total:
  138. return_list.append(object_list[start:end])
  139. elif end > total and start < total:
  140. return_list.append(object_list[start:])
  141. elif start > total:
  142. break
  143. else:
  144. break
  145. start += size
  146. end += size
  147. return return_list
  148.  
  149. class cracker(threading.Thread):
  150. """多线程爆破类"""
  151. def __init__(self,target_list,timeout):
  152. """多线程爆破构造函数"""
  153. threading.Thread.__init__(self)
  154. self.__target_list = target_list
  155. self.__timeout = timeout
  156.  
  157. def run(self):
  158. for target in self.__target_list:
  159. for username in username_list:
  160. for password in password_list:
  161. send_crack_packet(target=target,username=username,password=password,timeout=self.__timeout)
  162.  
  163. if __name__ == '__main__':
  164. parser = OptionParser()
  165. parser.add_option("-a","--target",dest="target",help="Target IP Addresses!")
  166. parser.add_option("-i","--infile",dest="infile",help="Target IP Addresses File!")
  167. parser.add_option("-u","--user",dest="userfile",help="Username Dictionary File!")
  168. parser.add_option("-p","--pswd",dest="pswdfile",help="Password Dictionary File!")
  169. parser.add_option("-o","--outfile",dest="outfile",help="Create A Report File! If [Yes] Create Report!")
  170. parser.add_option("-n","--thread",dest="threadnum",help="Count Of Thread!")
  171. parser.add_option("-t","--timeout",dest="timeout",help="Timeout Of Seconds!")
  172. (options, arges) = parser.parse_args()
  173. try:
  174. options.threadnum = int(options.threadnum)
  175. except Exception,ex:
  176. options.threadnum = 1
  177. options.threadnum = 10 if options.threadnum > 10 else options.threadnum
  178. try:
  179. timeout = int(options.timeout)
  180. except Exception,ex:
  181. timeout = 3
  182. timeout = 60 if timeout >= 60 else timeout
  183. if (parameter_checker(options.target) or parameter_checker(options.infile)) == False:
  184. logging.error("[-] 输入参数错误!!!")
  185. exit(0)
  186. logging.info("[+] 目标初始化...")
  187. if options.infile != None:
  188. ret = target_file_anylast(options.infile)
  189. for item in ret:
  190. if item.find("/") >= 0 or item.find("-") >= 0:
  191. target_analyst(item)
  192. else:
  193. target_list.append(item)
  194. if options.target != None:
  195. if options.target.find("/") >= 0 or options.target.find("-") >= 0:
  196. target_analyst(options.target)
  197. else:
  198. target_list.append(options.target)
  199. logging.info("[+] 目标初始化完成!!!")
  200. if (parameter_checker(options.userfile) or parameter_checker(options.pswdfile)) == True:
  201. logging.info("[+] 配置字典文件!!!")
  202. change_config_files(username_file=options.userfile,password_file=options.pswdfile)
  203. read_config_from_file()
  204. logging.info("[+] 开始扫描")
  205. #单线程爆破
  206. if options.threadnum == 1:
  207. for target in target_list:
  208. for username in username_list:
  209. for password in password_list:
  210. send_crack_packet(target=target,username=username,password=password,timeout=timeout)
  211. #多线程爆破
  212. else:
  213. multi_thread = True
  214. thread_list = []
  215. thread_target_list = list_devide(target_list,options.threadnum)
  216. for thread_target in thread_target_list:
  217. thread_object = cracker(thread_target,timeout)
  218. thread_list.append(thread_object)
  219. for thread in thread_list:
  220. thread.start()
  221. for thread in thread_list:
  222. thread.join()
  223. if parameter_checker(options.outfile) and options.outfile == "yes":
  224. logging.info("[+] 生成报告中...")
  225. create_report()
  226. logging.info("[+] 报告已生成!!!")
  227. logging.info("[+] 扫描完成")

多线程sshd爆破程序代码的更多相关文章

  1. python多线程ssh爆破

    python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...

  2. 破解入门【OllyDebug爆破程序】

    逆向破解这块我也是个刚起步的小菜,入门都还算不上吧,看了点基础教程,先动手练习一下增加点兴趣.嘿嘿 工具: peid         //查壳工具 OllyDebug    //反汇编.动态调试工具 ...

  3. HOOK大法实现不修改程序代码给程序添加功能

    [文章标题]: HOOK大法实现不修改程序代码给程序添加功能[文章作者]: 0x18c0[软件名称]: Scylla[使用工具]: OD.Stub_PE.ResHacker[版权声明]: 本文原创于0 ...

  4. 程序代码中退出函数exit()与返回函数return ()的区别

    程序代码中退出函数exit()与返回函数return ()的区别   exit(0):正常运行程序并退出程序:   exit(1):非正常运行导致退出程序:   return():返回函数,若在主函数 ...

  5. Android入门(四):链接接口组件和程序代码

    编写好layout中的接口组件之后,下一步就是编写控制接口组件的程序代码.上一章,我们使用了三种接口组件,在使用者输入性别和年龄之后点击“健康建议按钮”,程序会读取用户所填入的性别和年龄,然后显示判断 ...

  6. WinPipe后门程序代码示例(仅限技术交流)

    具体怎么编译,生成执行程序,不懂得先学习C++程序代码编译和集成开发环境. 多的不说了,只有两个代码文件,一个头文件,一个源文件.不多说了,直接上干货. (恶意使用,或者商用,后果自负,与本人无关.) ...

  7. Bullet核心类介绍(Bullet 2.82 HelloWorld程序及其详解,附程序代码)

    实验平台:win7,VS2010 先上结果截图: 文章最后附有生成该图的程序. 1. 刚体模拟原理 Bullet作为一个物理引擎,其任务就是刚体模拟(还有可变形体模拟).刚体模拟,就是要计算预测物体的 ...

  8. 每周一书-编写高质量代码:改善C程序代码的125个建议

    首先说明,本周活动有效时间为2016年8月28日到2016年9月4日.本周为大家送出的书是由机械工业出版社出版,马伟编著的<编写高质量代码:改善C程序代码的125个建议>. 编辑推荐 10 ...

  9. 使用sencha cmd 一键生成你的应用程序代码

    一键生成你的应用程序代码: ------------------------------------------------------------ 我们的出发点就是使用命令来产生一个应用程序,执行以 ...

随机推荐

  1. Linux下connect超时处理

    1.前言 最近在写一个测试工具,要求快速的高效率的扫描出各个服务器开放了哪些端口.当时想了一下,ping只能检测ip,判断服务器的网络是连通的,而不能判断是否开放了端口.我们知道端口属于网络的传输层, ...

  2. js获取当前域名的方法

    一.获取当前域名 1.方法一 var domain = document.domain; 2.方法二 var domain = window.location.host; 但是获取到的domain在线 ...

  3. JavaScript 中 this 的用法

    在 JavaScript 中,this 是动态绑定,或称为运行期绑定的.一般而言,在Javascript中,this 指向函数执行时的当前对象. 由于其运行期绑定的特性,JavaScript 中的 t ...

  4. svnserver权限问题

    打开visualSVN server 右键Users,新建user/Create user 输入username.password.确认password.依据须要建立对应的用户 右键Groups,新建 ...

  5. [Jobdu] 题目1373:整数中1出现的次数(从1到n整数中1出现的次数)

    题目描述: 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直困扰着他,特此他向JOBDU发来求助信,希望亲们能帮帮他.问题是:求出1~13的整数中1出现的次数,并算出100~130 ...

  6. You-Get 视频下载工具 Python命令行下载工具

    You-Get 是一个命令行工具, 用来下载各大视频网站的视频, 是我目前知道的命令行下载工具中最好的一个, 之前使用过 youtube-dl, 但是 youtube-dl 吧, 下载好的视频是分段的 ...

  7. oracle autotrace

    --======================= -- 启用 AUTOTRACE功能 --======================= AUTOTRACE是一个SQL*Plus工具,用于跟踪SQL ...

  8. Unix系统编程()brk,sbrk

    在堆上分配内存 进程可以通过增加堆的大小来分配内存,所谓堆是一段长度可变的连续虚拟内存,始于进程的未初始化数据段末尾,随着内存的分配和释放而增减.通常将堆的当前内存边界称为"program ...

  9. saveFileDialog对话框

    private void button1_Click(object sender, EventArgs e) { saveFileDialog1.Filter = "*.txt|*.txt| ...

  10. PHP——数组2(数组函数,二维数组,正则表达式)

    <body> <?php //数组函数 $arr=array(1,2,3,4,5,6); print_r($arr); echo "<br />"; ...