home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Networking / nmap-5.00-setup.exe / scripts / http-open-proxy.nse < prev    next >
Text File  |  2009-07-06  |  5KB  |  157 lines

  1. description=[[
  2. Checks if an HTTP proxy is open.
  3.  
  4. The script attempts to connect to www.google.com through the (possible) proxy and checks
  5. for a valid HTTP response code.
  6.  
  7. Valid HTTP response codes are actually: 200, 301, 302.
  8.  
  9. If the target is an open proxy, this script causes the target to retrieve a
  10. web page from www.google.com.
  11. ]]
  12.  
  13. ---
  14. -- @args openproxy.url Url that will be requested to the proxy
  15. -- @args openproxy.pattern Pattern that will be searched inside the request results
  16. -- @output
  17. -- Interesting ports on scanme.nmap.org (64.13.134.52):
  18. -- PORT     STATE SERVICE
  19. -- 8080/tcp open  http-proxy
  20. -- |  proxy-open-http: Potentially OPEN proxy.
  21. -- |_ Methods succesfully tested: GET HEAD CONNECT
  22.  
  23. -- Arturo 'Buanzo' Busleiman <buanzo@buanzo.com.ar> / www.buanzo.com.ar / linux-consulting.buanzo.com.ar
  24. -- Changelog: Added explode() function. Header-only matching now works.
  25. --   * Fixed set_timeout
  26. --   * Fixed some \r\n's
  27. -- 2008-10-02 Vlatko Kosturjak <kost@linux.hr>
  28. --   * Match case-insensitively against "^Server: gws" rather than
  29. --     case-sensitively against "^Server: GWS/".
  30. -- 2009-05-14 Joao Correa <joao@livewire.com.br>
  31. --   * Included tests for HEAD and CONNECT methods
  32. --   * Included url and pattern arguments
  33. --   * Script now checks for http response status code, when url is used
  34. --   * If google is used, script checks for Server: gws
  35. -- 
  36. -- @usage
  37. -- nmap --script http-open-proxy.nse \
  38. --      --script-args 'openproxy={url=<url>,pattern=<pattern>}'
  39.  
  40.  
  41. author = "Arturo 'Buanzo' Busleiman <buanzo@buanzo.com.ar>"
  42. license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
  43. categories = {"default", "discovery", "external", "intrusive"}
  44. require "comm"
  45. require "shortport"
  46. require "stdnse"
  47. require "url"
  48.  
  49. --- check function, makes checkings for all valid returned status
  50. --- If any of the HTTP status below is found, the proxy is potentially open
  51. --@param result connection result
  52. --@return true if any of the status is found, otherwise false
  53. function check_code(result)
  54.     if string.match(result:lower(),"^http/%d\.%d%s*200") then return true end
  55.     if string.match(result:lower(),"^http/%d\.%d%s*30[12]") then return true end
  56.     return false
  57. end
  58.  
  59. --- check pattern, searches a pattern inside a response with multiple lines
  60. --@param result Connection result
  61. --@param pattern The pattern to be searched
  62. --@return true if pattern is found, otherwise false
  63. function check_pattern(result, pattern)
  64.     local lines = stdnse.strsplit("\n", result)
  65.     local i = 1
  66.     local n = table.getn(lines)
  67.     while true do
  68.         if i > n then return false end
  69.         if string.match(lines[i]:lower(),pattern) then return true end
  70.         i = i + 1
  71.     end
  72. end
  73.  
  74. --- check, decides what kind of check should be done on the response,
  75. --- depending if a specific pattern is being used
  76. --@param result Connection result
  77. --@param pattern The pattern that should be checked (must be false, in case of
  78. --code check)
  79. --@return true, if the performed check returns true, otherwise false
  80. function check(result, pattern)
  81.     if pattern 
  82.         then return check_pattern(result, pattern)
  83.         else return check_code(result)
  84.     end
  85. end
  86.  
  87.  
  88. portrule = shortport.port_or_service({8123,3128,8000,8080},{'polipo','squid-http','http-proxy'})
  89.  
  90. action = function(host, port)
  91.     local retval
  92.     local supported_methods = "\nMethods successfully tested: "
  93.     local fstatus = false
  94.     local test_url = "http://www.google.com"
  95.     local hostname = "www.google.com"
  96.     local pattern = "^server: gws"
  97.  
  98.     -- If arg url exists, use it as test url
  99.     if(nmap.registry.args.openproxy and nmap.registry.args.openproxy.url) then
  100.         test_url = nmap.registry.args.openproxy.url
  101.         pattern = false
  102.         if not string.match(test_url, "^http://.*") then 
  103.             test_url = "http://" .. test_url
  104.             stdnse.print_debug("URL missing scheme. URL concatenated to http://")
  105.         end
  106.         url_table = url.parse(test_url)
  107.         hostname = url_table.host
  108.     end
  109.     if(nmap.registry.args.openproxy and nmap.registry.args.openproxy.pattern) then pattern = ".*" .. nmap.registry.args.openproxy.pattern .. ".*" end
  110.      
  111.     -- Trying GET method!
  112.     req = "GET " .. test_url .. " HTTP/1.0\r\nHost: " .. hostname .. "\r\n\r\n"
  113.     stdnse.print_debug("GET Request: " .. req)
  114.     local status, result = comm.exchange(host, port, req, {lines=1,proto=port.protocol, timeout=10000})
  115.  
  116.     if status then    
  117.         lstatus = check(result, pattern)
  118.         if lstatus then    
  119.             supported_methods = supported_methods .. "GET "
  120.             fstatus = true
  121.         end
  122.     end
  123.  
  124.     -- Trying HEAD method
  125.     req = "HEAD " .. test_url .. " HTTP/1.0\r\nHost: " .. hostname .. "\r\n\r\n"
  126.     stdnse.print_debug("HEAD Request: " .. req)
  127.     local status, result = comm.exchange(host, port, req, {lines=1,proto=port.protocol, timeout=10000})
  128.  
  129.     if status then
  130.         lstatus = check(result, pattern)
  131.         if lstatus then    
  132.             supported_methods = supported_methods .. "HEAD "
  133.             fstatus = true
  134.         end
  135.     end 
  136.  
  137.     -- Trying CONNECT method
  138.     req = "CONNECT " .. hostname .. ":80 HTTP/1.0\r\n\r\n"
  139.     stdnse.print_debug("CONNECT Request: " .. req)
  140.     local status, result = comm.exchange(host, port, req, {lines=1,proto=port.protocol, timeout=10000})
  141.  
  142.     if status then
  143.         lstatus = check(result, false);
  144.         if lstatus then    
  145.             supported_methods = supported_methods .. "CONNECT"
  146.             fstatus = true
  147.         end
  148.     end
  149.  
  150.     -- If any of the tests were OK, then the proxy is potentially open
  151.     if fstatus then
  152.         retval = "Potentially OPEN proxy.\n" .. supported_methods
  153.         return retval
  154.     end
  155.     return
  156. end
  157.