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

  1. description = [[
  2. Sends an HTTP TRACE request and shows header fields that were modified in the
  3. response.
  4. ]]
  5.  
  6. ---
  7. -- @output
  8. -- 80/tcp open  http
  9. -- |  http-trace: Response differs from request.  First 5 additional lines:
  10. -- |  Cookie: UID=d4287aa38d02f409841b4e0c0050c131...
  11. -- |  Country: us
  12. -- |  Ip_is_advertise_combined: yes
  13. -- |  Ip_conntype-Confidence: -1
  14. -- |_ Ip_line_speed: medium
  15.  
  16. -- 08/31/2007
  17.  
  18. author = "Kris Katterjohn <katterjohn@gmail.com>"
  19.  
  20. license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
  21.  
  22. categories = {"discovery"}
  23.  
  24. require "comm"
  25. require "shortport"
  26. require "stdnse"
  27.  
  28. --- Truncates and formats the first 5 elements of a table.
  29. --@param tab The table to truncate.
  30. --@return Truncated, formatted table.
  31. local truncate = function(tab)
  32.     local str = ""
  33.     str = str .. tab[1] .. "\n"
  34.     str = str .. tab[2] .. "\n"
  35.     str = str .. tab[3] .. "\n"
  36.     str = str .. tab[4] .. "\n"
  37.     str = str .. tab[5] .. "\n"
  38.     return str
  39. end
  40.  
  41. --- Validates the HTTP response and checks for modifications.
  42. --@param response The HTTP response from the server.
  43. --@param original The original HTTP request sent to the server.
  44. --@return A string describing the changes (if any) between the response and
  45. -- request.
  46. local validate = function(response, original)
  47.     local start, stop
  48.     local body
  49.  
  50.     if not response:match("HTTP/1.[01] 200") or
  51.        not response:match("TRACE / HTTP/1.0") then
  52.         return
  53.     end
  54.  
  55.     start, stop = response:find("\r\n\r\n")
  56.     body = response:sub(stop + 1)
  57.  
  58.     if original ~= body then
  59.         local output =  "Response differs from request.  "
  60.  
  61.         if body:match("^TRACE / HTTP/1.0\r\n") then
  62.             local extra = body:sub(19) -- skip TRACE line
  63.             local tab = {}
  64.  
  65.             -- Skip extra newline at the end (making sure it's there)
  66.             extra = extra:gsub("\r\n\r\n$", "\r\n")
  67.  
  68.             tab = stdnse.strsplit("\r\n", extra)
  69.  
  70.             if #tab > 5 then
  71.                 output = output .. "First 5 additional lines:\n"
  72.                 return output .. truncate(tab)
  73.             end
  74.  
  75.             output = output .. "Additional lines:\n"
  76.             return output .. extra .. "\n"
  77.         end
  78.  
  79.         -- This shouldn't happen
  80.  
  81.         output = output .. "Full response:\n"
  82.         return output .. body .. "\n"
  83.     end
  84.  
  85.     return
  86. end
  87.  
  88. portrule = shortport.port_or_service({80, 8080}, "http")
  89.  
  90. action = function(host, port)
  91.     local cmd = "TRACE / HTTP/1.0\r\n\r\n"
  92.  
  93.     local status, response = comm.exchange(host, port, cmd, {lines=1,timeout=5000})
  94.  
  95.     if not status then
  96.         return
  97.     end
  98.  
  99.     return validate(response, cmd)
  100. end
  101.  
  102.