Incredibly fast crawler designed for OSINT.
v1.0.0 (stable) release
Somdev Sangwanbb099f873b7c
diff --git a/photon.py b/photon.pyindex 20e14e6..5bdaa62 100644--- a/photon.py+++ b/photon.py@@ -1,201 +1,386 @@-#!/usr/bin/env python2-import mechanize-import threading-import argparse-import time-import re-import os+#!/usr/bin/env python3+# Colors and shit bro+end = '\033[1;m'+red = '\033[91m'white = '\033[1;97m'green = '\033[1;32m'-red = '\033[91m'yellow = '\033[1;33m'-end = '\033[1;m'-info = '\033[1;33m[!]\033[1;m'-que = '\033[1;34m[?]\033[1;m'+run = '\033[1;97m[~]\033[1;m'bad = '\033[1;31m[-]\033[1;m'good = '\033[1;32m[+]\033[1;m'-run = '\033[1;97m[~]\033[1;m'+info = '\033[1;33m[!]\033[1;m'+que = '\033[1;34m[?]\033[1;m'++# Let's import what we need+import os+import sys+import time+import random+import urllib3+import argparse+import threading+from requests import get, post+from re import search, findall+from urllib.parse import urlparse-print '''%s ____ __ __+print ('''%s ____ __ __/ %s__%s \/ /_ ____ / /_____ ____/ %s/_/%s / __ \/ %s__%s \/ __/ %s__%s \/ __ \\/ ____/ / / / %s/_/%s / /_/ %s/_/%s / / / /- /_/ /_/ /_/\____/\__/\____/_/ /_/ %s\n''' % (red, white, red, white, red, white, red, white, red, white, red, white, red, end)+ /_/ /_/ /_/\____/\__/\____/_/ /_/ %s\n''' % (red, white, red, white, red, white, red, white, red, white, red, white, red, end))+urllib3.disable_warnings()+# Processing command line argumentsparser = argparse.ArgumentParser()-parser.add_argument('target', help="target website")-parser.add_argument('--cookie', help="cookie", dest='cookie')-parser.add_argument('--output', help="output directory", dest='name')-parser.add_argument('--delay', help="request delay", dest='delay', type=float)+parser.add_argument('-u', '--url', help='root url', dest='root')+parser.add_argument('-l', '--level', help='levels to crawl', dest='level', type=int)+parser.add_argument('-t', '--threads', help='number of threads', dest='threads', type=int)+parser.add_argument('-d', '--delay', help='delay between requests', dest='delay', type=int)+parser.add_argument('-c', '--cookie', help='cookie', dest='cook')+parser.add_argument('-s', '--seeds', help='additional seed urls', dest='seeds')+parser.add_argument('-n', '--ninja', help='ninja mode', dest='ninja', action='store_true')args = parser.parse_args()-target = args.target-if 'http' not in target:- target = 'http://' + target+if args.root:+ main_inp = args.root+else:+ print ('\n' + parser.format_help().lower())+ quit()-br = mechanize.Browser() # Just shortening the calling function-br.set_handle_robots(False) # Don't follow robots.txt-br.set_handle_equiv(True) # I don't know what it does, but its some good shit-br.set_handle_redirect(True) # Follow redirects-br.set_handle_referer(True) # Include referrer+delay = 0 # Delay between requests+cook = None # Cookie+ninja = False # Ninja mode toggle+crawl_level = 2 # Crawling level+thread_count = 2 # Number of threads-delay = 0.5+if args.cook:+ cook = args.cook+if args.ninja:+ ninja = Trueif args.delay:delay = args.delay+if args.level:+ crawl_level = args.level+if args.threads:+ thread_count = args.threads-if args.cookie:- br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1'),-('Accept-Encoding', 'deflate'), ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'), ('Cookie', args.cookie)]-else:- br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1'),-('Accept-Encoding', 'deflate'), ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')]-+# Variables we are gonna use later to store stuff+files = set()+intel = set()+robots = set()+failed = set()storage = set()scripts = set()-params = set()+external = set()+fuzzable = set()endpoints = set()+processed = set()-if args.name:- name = args.name-else:- name = target.split('.')[0].strip('www.').strip('http').strip('https').strip('://')- if name == '':- name = 'result%i' % len(target)-storage.add(target)+seeds = ''+if args.seeds:+ seeds = args.seeds+ for seed in seeds.split(','):+ storage.add(seed)-def zap(url):+# If the user hasn't supplied the root url with http(s), we will handle it+if main_inp.startswith('http'):+ main_url = main_inp+else:try:- response = br.open(url+'/robots.txt').read().split('\n')- for line in response:- if 'Allow:' in line or 'Disallow:' in line and not line.endswith('/'):- storage.add(target + line.strip('Allow: ').strip('Disallow: '))- print '%s Links retrieved from robots.txt: %s' % (info, len(storage) - 1)+ get('https://' + main_inp)+ main_url = 'https://' + main_inpexcept:- pass+ main_url = 'http://' + main_inp++storage.add(main_url)++main_domain = urlparse(main_url).netloc # Extracts domain out of the url+name = main_domain.split('.')[-2] # Extracts example out of example.com++####+# This function makes requests to webpage and returns response body+####++user_agents = ['Mozilla/5.0 (X11; Linux i686; rv:60.0) Gecko/20100101 Firefox/60.0',+'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'+'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 OPR/43.0.2442.991']++def requester(url):+ processed.add(url)+ time.sleep(delay)try:- response = br.open(url+'/sitemap.xml').read()- matches = re.findall(r'<loc>[^<]*</loc>', response)- print '%s Links retrieved from sitemap.xml: %s' % (info, len(matches))- for match in matches:- storage.add(match.split('<loc>')[1][:-6])+ def normal(url):+ headers = {+ 'Host' : main_domain,+ 'User-Agent' : random.choice(user_agents),+ 'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',+ 'Accept-Language' : 'en-US,en;q=0.5',+ 'Accept-Encoding' : 'gzip',+ 'DNT' : '1',+ 'Connection' : 'close'}+ return get(url, cookies=cook, headers=headers, verify=False).text++ def pixlr(url):+ if url == main_url:+ url = main_url + '/'+ return get('https://pixlr.com/proxy/?url=' + url, headers={'Accept-Encoding' : 'gzip'}, verify=False).text++ def code_beautify(url):+ headers = {+ 'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0',+ 'Accept' : 'text/plain, */*; q=0.01',+ 'Accept-Encoding' : 'gzip',+ 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8',+ 'Origin' : 'https://codebeautify.org',+ 'Connection' : 'close'+ }+ return post('https://codebeautify.com/URLService', headers=headers, data='path=' + url, verify=False).text++ def photopea(url):+ return get('https://www.photopea.com/mirror.php?url=' + url, verify=False).text++ if ninja:+ return random.choice([photopea, normal, pixlr, code_beautify])(url)+ else:+ return normal(url)except:- pass+ failed.add(url)++####+# This function extracts links from robots.txt and sitemap.xml+####-def get_links(url):+def zap(url):+ response = requester(url + '/robots.txt')+ if '<body' not in response:+ matches = findall(r'Allow: (.*)|Disallow: (.*)', response)+ for match in matches:+ match = match[0] + match[1]+ if '/*/' not in match:+ url = main_url + match+ storage.add(url)+ robots.add(url)+ if '=' in url:+ fuzzable.add(url)+ if len(matches) > 0:+ print ('%s URLs retrieved from robots.txt: %s' % (good, len(robots)))++ response = requester(url + '/sitemap.xml')++ if '<body' not in response:+ matches = findall(r'<loc>[^<]*</loc>', response)+ if matches:+ print ('%s URLs retrieved from sitemap.xml: %s' % (good, len(matches)))+ for match in matches:+ storage.add(match.split('<loc>')[1][:-6])++####+# This functions checks if the found link is crawlable or not+####++def is_link(url):+ bad = ['xml', 'png', 'bmp', 'jpg', 'jpeg', 'pdf', 'css', 'ico', 'js', 'svg', 'json']+ conclusion = False++ if url not in processed:+ if '.' in url[-5:]:+ extension = url[-5:].split('.')[-1]+ if extension not in bad:+ conclusion = True+ else:+ files.add(main_url + url)+ else:+ return True+ return conclusion++####+# This function extracts stuff from the response body+####++def extractor(url):+ response = requester(url)try:- response = br.open(url).read()- for link in br.links():- link = link.url- if name in link:- if link.startswith('http'):+ matches = findall(r'href=[\'"](.*?)["\']', response)+ for link in matches:+ link = link.split('#')[0]+ if is_link(link):+ if link.startswith(main_url):storage.add(link)if '=' in link:- params.add(link)- elif link.startswith('/'):- storage.add(target + link)+ fuzzable.add(link)+ if link.startswith('http') or link.startswith('//'):+ if not link.startswith(main_url):+ external.add(link)+ elif link.startswith('#') or link.startswith('javascript') or link.startswith('{{'):+ pass+ elif link.startswith('/') and not link.startswith('//'):+ storage.add(main_url + link)if '=' in link:- params.add(link)- else:- storage.add(target + '/' + link)+ fuzzable.add(link)+ elif not link.startswith('http') and not link.startswith('//'):+ storage.add(main_url + '/' + link)if '=' in link:- params.add(link)- matches = re.findall(r'src=[\'"][^<]*\.js', response)+ fuzzable.add(link)++ matches = findall(r'src=[\'"](.*?\.js)["\']', response)for match in matches:- if 'http' in match:- scripts.add(match[5:])- elif match[5:].startswith('/'):- scripts.add(url + match[5:])- else:- scripts.add(url + '/' + match[5:])- except:- storage.remove(url)+ if match.startswith(main_url):+ scripts.add(match)+ elif match.startswith('/') and not match.startswith('//'):+ scripts.add(main_url + match)+ elif not match.startswith('http') and not match.startswith('//'):+ scripts.add(main_url + '/' + match)-def jscanner(url):- try:- response = br.open(url).read()- matches = re.findall(r'"/.*"|/\'.*\'|"http.*"|\'http.*\'', response)+ matches = findall(r'''([\w\.-]+s[\w\.-]+\.amazonaws\.com)|(github\.com/[\w\.-/]+)|(facebook\.com/.*?)[\'" ]|+(youtube\.com/.*?)[\'" ]|(linkedin\.com/.*?)[\'" ]|(twitter\.com/.*?)[\'" ]|([\w\.-]+@[\w\.-]+\.[\.\w]+)''', response)for match in matches:- if r'[}{><"\']' not in match:- endpoints.add(match.split(match[-1:])[1])+ for x in match:+ if x != '':+ intel.add(x)except:pass-def jsinit():- database = list(scripts.copy())- def part1():- for url in database[:len(scripts)/2]:- time.sleep(delay)- jscanner(url)- def part2():- for url in database[len(scripts)/2:]:- time.sleep(delay)- jscanner(url)- t1 = threading.Thread(target=part1) #Calls the part1 function via a thread- t2 = threading.Thread(target=part2) #Calls the part2 function via a thread- t1.start() #starts thread 1- t2.start() #starts thread 2- t1.join() #Joins both- t2.join() #of the threads--def initiate():- database = list(storage.copy())- def part1():- for url in database[:len(storage)/2]:- time.sleep(delay)- get_links(url)- def part2():- for url in database[len(storage)/2:]:- time.sleep(delay)- get_links(url)- t1 = threading.Thread(target=part1) #Calls the part1 function via a thread- t2 = threading.Thread(target=part2) #Calls the part2 function via a thread- t1.start() #starts thread 1- t2.start() #starts thread 2- t1.join() #Joins both- t2.join() #of the threads--zap(target)-get_links(target)-print '%s Links to crawl: %i' % (info, len(storage))-approx = (len(storage)/20)--if approx > 0:- print '%s Time required: ~%i minutes' % (info, approx)-else:- print '%s Time required: ~1 minute' % info+####+# This function extracts endpoints from JavaScript Code+####-initiate()+def jscanner(url):+ response = requester(url)+ matches = findall(r'[\'"](/.*?)[\'"]|[\'"](http.*?)[\'"]', response)+ for match in matches:+ if match[0]:+ match = match[0]+ elif match[1]:+ match = match[1]+ if not search(r'[}{><"\']', match) and not match == '/':+ endpoints.add(match)-print '%s Total URLs found: %i' % (good, len(storage))-print '%s Fuzzable URLs found: %i' % (good, len(params))-print '%s JavaScript files found: %i' % (good, len(scripts))-print '%s Scanning JavaScript files for endpoints' % run+####+# This function starts multiple threads for a function+####-approx = (len(scripts)/200)-if approx > 0:- print '%s Time required: ~%i minutes' % (info, approx)-else:- print '%s Time required: ~1 minute' % info+def threader(function, *urls):+ threads = []+ urls = urls[0]+ for url in urls:+ task = threading.Thread(target=function, args=(url,))+ threads.append(task)++ for thread in threads:+ thread.start()-jsinit()+ for thread in threads:+ thread.join()+ del threads[:]-print '%s Enpoints found: %i' % (good, len(endpoints))+####+# This function processes the urls and sends them to "threader" function+####++def flash(function, links):+ links = list(links)+ begin = 0+ end = thread_count+ for i in range((int(len(links)/thread_count)) + 1):+ splitted = links[begin:end]+ threader(function, splitted)+ begin += thread_count+ if (len(links) - end) >= thread_count:+ end += thread_count+ else:+ end += len(links) - begin+ progress = begin+ if progress > len(links):+ progress = len(links)+ sys.stdout.write('\r%s Progress: %i/%i' % (info, progress, len(links)))+ sys.stdout.flush()+ print ('')++# Step 1. Extract urls from robots.txt & sitemap.xml+zap(main_url)++# Step 2. Crawl recursively to the limit specified in "crawl_level"+for level in range(crawl_level):+ links = storage - processed+ if len(links) == 0:+ break+ if len(storage) <= len(processed):+ if len(storage) > 2 + len(seeds):+ break+ print ('%s Level %i: %i URLs' % (run, level + 1, len(links)))+ try:+ flash(extractor, links)+ except KeyboardInterrupt:+ print ('')+ break+# Step 3. Scan the JavaScript files for enpoints+print ('%s Crawling %i JavaScript files' % (run, len(scripts)))+flash(jscanner, scripts)++# Step 4. Save the resultsif os.path.exists(name):os.system('rm -r %s' % name)os.system('mkdir %s' % name)+with open('%s/links.txt' % name, 'w+') as f:for x in storage:f.write(x + '\n')-with open('%s/js.txt' % name, 'w+') as f:+f.close()++with open('%s/robots.txt' % name, 'w+') as f:+ for x in storage:+ f.write(x + '\n')+f.close()++with open('%s/scripts.txt' % name, 'w+') as f:for x in scripts:f.write(x + '\n')+f.close()+with open('%s/fuzzable.txt' % name, 'w+') as f:- for x in params:+ for x in fuzzable:f.write(x + '\n')+f.close()+with open('%s/endpoints.txt' % name, 'w+') as f:- for x in params:+ for x in endpoints:+ f.write(x + '\n')+f.close()++with open('%s/failed.txt' % name, 'w+') as f:+ for x in failed:+ f.write(x + '\n')+f.close()++with open('%s/external.txt' % name, 'w+') as f:+ for x in external:+ f.write(x + '\n')+f.close()++with open('%s/files.txt' % name, 'w+') as f:+ for x in files:f.write(x + '\n')-print '%s Results saved in \033[;1m%s\033[0m directory' % (good, name)+f.close()++with open('%s/intel.txt' % name, 'w+') as f:+ for x in intel:+ f.write(x + '\n')+f.close()++print ('''%s+%s URLs: %i+%s Intel: %i+%s Files: %i+%s Endpoints: %i+%s Fuzzable URLs: %i+%s JavaScript Files: %i+%s External References: %i+%s''' % ((('%s-%s' % (red, end)) * 50), good, len(storage), good,+len(intel), good, len(files), good, len(endpoints), good, len(fuzzable),+good, len(scripts), good, len(external),+(('%s-%s' % (red, end)) * 50)))++print ('%s Results saved in \033[;1m%s\033[0m directory' % (good, name))