v0mit

Untitled

Aug 5th, 2011
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.52 KB | None | 0 0
  1. '''
  2. Created on Aug 5, 2011
  3.  
  4. @author: v0mit
  5. '''
  6. import urllib, urllib2, random, sys, urlparse
  7. from BeautifulSoup import BeautifulSoup
  8. from BeautifulSoup import BeautifulStoneSoup
  9.  
  10. if len(sys.argv) == 3:
  11.     url = sys.argv[1]
  12.     log_file = sys.argv[2]
  13.    
  14. elif len(sys.argv) == 2:
  15.     url = sys.argv[1]
  16.     log_file = "vulnerable.txt"
  17.  
  18. else:
  19.     print("Usage: pyxsser.py <target> <output>")
  20.  
  21.    
  22. header = """
  23. ______      __   _______ _____          
  24. | ___ \    \ \ / /  ___/  ___|          
  25. | |_/ /_   _ \ V /\ `--.\ `--.  ___ _ __
  26. |  __/| | | |/   \ `--. \`--. \/ _ \ '__|
  27. | |   | |_| / /^\ |\__/ /\__/ /  __/ |  
  28. \_|    \__, \/   \|____/\____/ \___|_|  
  29.        __/ |                            
  30.       |___/        v0.1
  31. """
  32. print(header)    
  33.  
  34. injection_str = ""
  35. for x in range(0,8):
  36.     injection_str += random.choice("abcdefghi1234567890")
  37.  
  38. encoded_injection_str = urllib.urlencode({"":";!--\"<%s>=&{()}" % injection_str})
  39. #encoded_injection_str = "=lol"
  40.    
  41. class _http_handler():
  42.     def __init__(self):
  43.         self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
  44.         urllib2.install_opener(self.opener)
  45.  
  46.     def request(self, url, data=None):
  47.  
  48.         req = urllib2.Request(url)
  49.         req.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.2; en-GB; rv:1.8.1.18) Gecko/20081029 Firefox/2.0.0.18')
  50.        
  51.         if data != None:
  52.             data = urllib.urlencode(data)
  53.             try:
  54.                 response = self.opener.open(req,data)
  55.             except urllib2.URLError as errno:
  56.                 print(err)
  57.                 print("[!]urllib2.URLError({0})\n".format(errno))
  58.                
  59.                 return
  60.                
  61.             return response.read()
  62.         else:
  63.             try:
  64.                 response = self.opener.open(req)
  65.             except urllib2.URLError as errno:
  66.                 print("[!]urllib2.URLError({0})\n".format(errno))
  67.                
  68.                 return
  69.                
  70.             except ValueError as errno:
  71.                 print("[!]ValueError({0}\n".format(errno))
  72.                
  73.                 return
  74.            
  75.             return response.read()
  76.            
  77. def get_forms(url):
  78.     h = _http_handler()
  79.     data = h.request(url)
  80.     if data == None:
  81.         return None
  82.    
  83.     soup = BeautifulSoup(data)
  84.     forms = soup.findAll("form", attrs={"method":"get"})
  85.     forms += soup.findAll("form", attrs={"method":"post"})
  86.     valid_forms = []
  87.     for form in forms:
  88.         action = form.get("action")
  89.         if action != None:
  90.             inputs = form.findAll("input", attrs={"type":"text"})
  91.             names = []
  92.             for input in inputs:
  93.                 name = input.get("name")
  94.                 if input != None:
  95.                     names.append(str(name))
  96.                    
  97.             valid_forms.append([str(action), names])
  98.                    
  99.     return valid_forms
  100.    
  101. def generate_links(forms):
  102.     urls = []
  103.     for form in forms:
  104.         for para in range(0, len(form[1])):
  105.             if form[0] == "#":
  106.                 new_url = url
  107.             else:
  108.                 new_url = form[0]
  109.             if not new_url.endswith("?"):
  110.                 new_url += "?"
  111.                
  112.             buff = ""
  113.             for x in form[1][:para]:
  114.                 buff += "&{0}=1".format(x)
  115.                
  116.             buff += "&{0}{1}".format(form[1][para], encoded_injection_str)
  117.            
  118.             for x in form[1][para+1:]:
  119.                 buff += "&{0}=1".format(x)
  120.             new_url += buff[1:]  
  121.             urls.append(urlparse.urljoin(url,new_url))
  122.            
  123.     return urls
  124.    
  125.  
  126. forms =  get_forms(url)
  127. if forms == None:
  128.     print("Cannot continue, could not open site to parse.")
  129.     sys.exit(0)
  130. urls = generate_links(forms)
  131.  
  132. vulnerable = []
  133. for url in urls:
  134.     print("[+]Testing:{0}".format(url))
  135.     h = _http_handler()
  136.     data = h.request(url)
  137.    
  138.     if data == None:
  139.         continue
  140.    
  141.     if injection_str in data:
  142.         print("[!]Possible XSS found!")
  143.         print("[+]URL:{0}\n".format(url))
  144.         vulnerable.append(url)
  145.  
  146. print("\n[+]Scann completed.")
  147.  
  148. if not len(vulnerable):
  149.     print("[+]Nothing found.")
  150.     sys.exit(0)
  151.  
  152. try:
  153.     out = open(log_file, "wb")
  154. except IOError as errno:
  155.     print("\n[!]IOError: [0}".format(errno))
  156.     sys.exit(0)
  157.    
  158. for url in vulnerable:
  159.     out.write("{0}\n".format(url))
  160.    
  161. out.close()
  162.  
  163. print("[+]Report saved to: {0}".format(log_file))
Advertisement
Add Comment
Please, Sign In to add comment