Advertisement
Guest User

bitch

a guest
Oct 26th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.08 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: latin-1 -*- ######################################################
  3. #                ____                     _ __                                 #
  4. #     ___  __ __/ / /__ ___ ______ ______(_) /___ __                           #
  5. #    / _ \/ // / / (_-</ -_) __/ // / __/ / __/ // /                           #
  6. #   /_//_/\_,_/_/_/___/\__/\__/\_,_/_/ /_/\__/\_, /                            #
  7. #                                            /___/ team                        #
  8. #                                                                              #
  9. # crawlb0y.py - fast and easy to use url + parameter crawler                   #
  10. #                                                                              #
  11. # FILE                                                                         #
  12. # crawlb0y.py                                                                  #
  13. #                                                                              #
  14. # DATE                                                                         #
  15. # 2013-10-31                                                                   #
  16. #                                                                              #
  17. # DESCRIPTION                                                                  #
  18. # 'crawlb0y.py' is a very fast url + parameter crawler ...                     #
  19. # ... Faster, Harder, Craaaawleer!!1!1                                         #
  20. #                                                                              #
  21. # AUTHOR                                                                       #
  22. # pigtail23 aka pgt                                                            #
  23. #                                                                              #
  24. ################################################################################
  25.  
  26.  
  27. import threading
  28. import urllib2
  29. import time
  30. import os
  31. import argparse
  32. from socket import *
  33.  
  34.  
  35. # crawlb0y.py version string
  36. VERSION="v0.1.1"
  37.  
  38.  
  39. # print our nice banner ;)
  40. def banner():
  41.     print '--==[ crawlb0y.py by pigtail23@nullsecurity.net ]==--'
  42.  
  43. # print version
  44. def version():
  45.     print '[+] crawlb0y.py %s' % (VERSION)
  46.     exit(0)
  47.  
  48. # the best code part ;)
  49. def finished():
  50.     print '\n[!] h4ppy 0wn1ng'
  51.  
  52. # defines the command line parameter and help page
  53. def argspage():
  54.     parser = argparse.ArgumentParser(
  55.     usage='\n   ./%(prog)s -u <arg> [options]',
  56.     formatter_class=argparse.RawDescriptionHelpFormatter,
  57.     epilog=
  58.     'examples:\n\n' \
  59.  
  60.     '  crawl also links with other domain name\n' \
  61.     '  usage: ./%(prog)s -u https://www.xxx.com/ -s api.xxx.com\n\n' \
  62.  
  63.     '  show links with parameters only\n' \
  64.     '  usage: ./%(prog)s -u www.xxx.com -O',
  65.     add_help=False
  66.     )
  67.  
  68.     options = parser.add_argument_group('options', '')
  69.     options.add_argument('-u', default=False, metavar='<url>',
  70.             help='url to test')
  71.     options.add_argument('-s', default='gimmaapibro', metavar='<url>',
  72.             help='grab and crawl other found urls also (e.g. api.xxx.com)')
  73.     options.add_argument('-P', default=False, metavar='<num>',
  74.             type=int, help='port')
  75.     options.add_argument('-d', default=1, metavar='<num>',
  76.             type=int, help='crawl depth level (default: 1)')
  77.     options.add_argument('-e', default='w00tw00tn00b', metavar='<string>',
  78.             help='urls must contain given <string> (e.g .php)')
  79.     options.add_argument('-N', const=True, action='store_const',
  80.             help='print urls without parameters')
  81.     options.add_argument('-O', const=True, action='store_const',
  82.             help='print urls with parameters only')
  83.     options.add_argument('-A', default=False, metavar='<string>',
  84.             help='http basic auth: username:password')
  85.     options.add_argument('-t', default=4, metavar='<num>',
  86.             type=int, help='threads (default: 4)')
  87.     options.add_argument('-T', default=3, metavar='<sec>',
  88.             type=int, help='timeout in seconds (default: 3)')
  89.     options.add_argument('-o', default=False, metavar='<filename>',
  90.             help='write urls to file')
  91.     options.add_argument('-p', default=False, metavar='<filename>',
  92.             help='write parameters to file')
  93.     options.add_argument('-V', action='store_true',
  94.             help='print version of crawlb0y.py and exit')
  95.  
  96.     args = parser.parse_args()
  97.  
  98.     if args.V:
  99.         version()
  100.  
  101.     if (args.u == False):
  102.         print ''
  103.         parser.print_help()
  104.         exit(0)
  105.  
  106.     return args
  107.  
  108. # write urls and parameters to file
  109. def writetofile(args, content, check):
  110.    
  111.     if (args.o != False) and (check == 0):
  112.         filename = args.o
  113.     elif (args.p != False) and (check == 1):
  114.         filename = args.p
  115.     else:
  116.         return
  117.  
  118.     content = '%s\n' % (content)
  119.  
  120.     try:
  121.         outfile = open(filename, 'a')
  122.         outfile.write(content)
  123.         outfile.close()
  124.     except:
  125.         print '[-] cannot write to file'
  126.         os._exit(1)
  127.  
  128. # print what?
  129. def print_baby(what):
  130.     print '[+] %s' % (what)
  131.  
  132. # print found urls
  133. def printurls(args, urls):
  134.    
  135.     if args.N:
  136.         noparams = []
  137.         for url in urls:
  138.             url = url.split('?')
  139.             noparams.append(url[0])
  140.         urls = set(noparams)
  141.  
  142.     print  '\n[*] crawled urls\n================\n'
  143.     for url in urls:
  144.         if (url.find(args.e) == -1) and (args.e != 'w00tw00tn00b'):
  145.             continue
  146.         if args.O == True:
  147.             if (url.find('?') != -1) and (url.find('=') != -1):
  148.                 print_baby(url)
  149.                 writetofile(args, url, 0)
  150.         else:
  151.             print_baby(url)
  152.             writetofile(args, url, 0)
  153.     print '\n'
  154.  
  155. # grabbing and printing parameters (will be refactored)
  156. def parameters(args, filtered):
  157.     params = []
  158.     for url in filtered:
  159.         u = 0
  160.         url = url.replace('&amp;', '&')
  161.         while u < len(url):
  162.             begin = url[u:].find('?')
  163.             if (begin == -1):
  164.                 begin_two = url[u:].find('&')
  165.                 if (begin_two != -1):
  166.                     begin_two += u
  167.                     end = url[begin_two+1:].find('=')
  168.                     if end == -1:
  169.                         break
  170.                     u = end + begin_two
  171.                     params.append(url[begin_two+1:u+1])
  172.                 else:
  173.                     u = len(url)
  174.             else:
  175.                 if not url[begin+1:]:
  176.                     break
  177.                 end = url[begin:].find('=')
  178.                 if end == -1:
  179.                     break
  180.                 u = begin + end
  181.                 params.append(url[begin+1:u])
  182.                 url = url.replace('?', '%3f')
  183.        
  184.     params = set(params)
  185.     print  '\n[*] grabbed parameters\n======================\n'
  186.     for param in params:
  187.         if (param.find('http://') == -1) and (param.find('https://') == -1):
  188.             writetofile(args, param, 1)
  189.             print_baby(param)
  190.  
  191. # blacklist for url content
  192. def checkblack(url):
  193.     blacklist = (['.css', '.ico', '.jpg', 'mailto:', 'javascript:', '.doc',
  194.         '.pdf', '.png'])
  195.     for black in blacklist:
  196.         if url.find(black) != -1:
  197.             return True
  198.  
  199. # sub / any domain grabbing
  200. def subdomain(subdomain, url):
  201.      begin = url.find(subdomain)
  202.      if begin != -1:
  203.          return True
  204.  
  205. # filter all found urls
  206. def filterurls(args):
  207.     filtered = []
  208.     start_url = args.u
  209.     for url in URLLIST:
  210.         url = url.replace('&amp;', '&')
  211.         if len(url) < 2:
  212.             continue
  213.         elif url == start_url:
  214.             continue
  215.         elif checkblack(url) == True:
  216.             continue
  217.         elif url.find(start_url) != -1:
  218.             filtered.append(url)
  219.         elif (url.find('http://') == -1) and (url.find('https://') == -1):
  220.             if url[0] == '/':
  221.                 url = url[1:]
  222.             url = '%s%s' % (start_url, url)
  223.             filtered.append(url)
  224.         elif subdomain(args.s, url) == True:
  225.             filtered.append(url)
  226.     return set(filtered)
  227.  
  228. # find urls after request
  229. def findurls(response):
  230.     u = 0
  231.     while u < len(response):
  232.         begin = response[u:].find('href="')
  233.         if begin == -1:
  234.             return 1
  235.         begin += 6 + u
  236.         end = response[begin:].find('"') + begin + 1
  237.         URLLIST.append(response[begin-1:end].replace('"', ''))
  238.         u = end
  239.  
  240. # send http/https request to server
  241. def scan(url, to, i):
  242.     request = urllib2.Request(url)
  243.     try:
  244.         response = urllib2.urlopen(request, timeout = to)
  245.         findurls(response.read())
  246.     except urllib2.HTTPError, e:
  247.         return
  248.     except urllib2.URLError, e:
  249.         reason = '%s' % (e.reason)
  250.         if reason == '[Errno 61] Connection refused':
  251.             print '[-] Connection refused: %s' % (url)
  252.             os._exit(1)
  253.         if reason == 'timed out':
  254.             print '[-] %s timed out' % (url)
  255.             if i < 10:
  256.                 i =+ 1
  257.                 scan(url, to, i)
  258.             return
  259.     except:
  260.         if i < 10:
  261.             i =+ 1
  262.             scan(url, to, i)
  263.         return
  264.  
  265. # w3 <3 7hr34d1ng
  266. def crawlmore(args, urls):
  267.     to = args.T
  268.     threads = args.t
  269.  
  270.     for url in urls:
  271.         Run = threading.Thread(target=scan, args=(url, to, 0,))
  272.         Run.start()
  273.         # checks that we a max number of threads
  274.         while threading.activeCount() > threads:
  275.             time.sleep(0.01)
  276.         time.sleep(0.001)
  277.  
  278.     # waiting for the last running threads
  279.     while threading.activeCount() > 1:
  280.         time.sleep(0.1)
  281.  
  282. # test for open http/https port (better than urllib2 stuff)
  283. def test_http(target, timeout):
  284.     try:
  285.         s = socket(AF_INET, SOCK_STREAM)
  286.         s.settimeout(timeout)
  287.         result = s.connect_ex((target, 80))
  288.         s.close()
  289.         if result == 0:
  290.             return 0
  291.         s = socket(AF_INET, SOCK_STREAM)
  292.         s.settimeout(timeout)
  293.         result = s.connect_ex((target, 443))
  294.         s.close()
  295.         if result == 0:
  296.             return 1
  297.         return
  298.     except:
  299.        print '[-] cannot resolve ip'
  300.        os._exit(1)
  301.  
  302. # check url format, make it valid for urllib2
  303. def checkformat(args):
  304.     url = args.u
  305.     to = args.T
  306.     if (url.find('http://') == -1) and (url.find('https://') == -1):
  307.         if test_http(url, to) == 0:
  308.             url = 'http://%s' % (url)
  309.         elif test_http(url, to) == 1:
  310.             url = 'https://%s' % (url)
  311.         else:
  312.             print '[-] cannot connect'
  313.             os._exit(1)
  314.     if url[len(url) - 1].find('/') == -1:
  315.         url = '%s/' % (url)
  316.     return url
  317.  
  318. # 57up1d m41n
  319. def main():
  320.     banner()
  321.     args = argspage()
  322.     args.u = checkformat(args)
  323.     urls = [ args.u ]
  324.     print '\n[*] crawling %s' % (args.u)
  325.     for i in range(args.d):
  326.         crawlmore(args, urls)
  327.         urls = filterurls(args)
  328.     printurls(args, urls)
  329.     parameters(args, urls)
  330.     finished()
  331.  
  332. if __name__ == '__main__':
  333.     URLLIST = []
  334.     try:
  335.         main()
  336.     except KeyboardInterrupt:
  337.         print '\nbye bye!!!'
  338.         time.sleep(0.01)
  339.         os._exit(1)
  340.  
  341. # EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement