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

  1. description = [[
  2. Retrieves the authentication scheme and realm of a web service that requires
  3. authentication.
  4. ]]
  5.  
  6. ---
  7. -- @output
  8. -- 80/tcp open  http
  9. -- |  http-auth: HTTP Service requires authentication
  10. -- |    Auth type: Basic, realm = Password Required
  11. -- |_   HTTP server may accept admin:admin combination for Basic authentication
  12.  
  13. -- HTTP authentication information gathering script
  14. -- rev 1.1 (2007-05-25)
  15. -- 2008-11-06 Vlatko Kosturjak <kost@linux.hr>
  16. -- * bug fixes against base64 encoded strings, more flexible auth/pass check,
  17. --   corrected sample output
  18.  
  19. author = "Thomas Buchanan <tbuchanan@thecompassgrp.net>"
  20.  
  21. license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
  22.  
  23. categories = {"default", "auth", "intrusive"}
  24.  
  25. require "shortport"
  26. require "http"
  27. require "base64"
  28.  
  29. portrule = shortport.port_or_service({80, 443, 8080}, {"http","https"})
  30.  
  31. action = function(host, port)
  32.   local realm,scheme,result,authheader
  33.   local basic = false
  34.   local authcombinations= {"admin:", "admin:admin"}
  35.  
  36.   local answer = http.get( host, port, "/" )
  37.  
  38.   --- check for 401 response code
  39.   if answer.status == 401 then
  40.     result = "HTTP Service requires authentication\n"
  41.  
  42.     -- split www-authenticate header
  43.     local auth_headers = {}
  44.     local pcre = pcre.new('\\w+( (\\w+=("[^"]+"|\\w+), *)*(\\w+=("[^"]+"|\\w+)))?',0,"C")
  45.     local match = function( match ) table.insert(auth_headers, match) end
  46.     pcre:gmatch( answer.header['www-authenticate'], match )
  47.  
  48.     for _, value in pairs( auth_headers ) do
  49.       result = result .. "  Auth type: "
  50.       scheme, realm = string.match(value, "(%a+).-[Rr]ealm=\"(.-)\"")
  51.       if scheme == "Basic" then
  52.         basic = true
  53.       end
  54.       if realm ~= nil then
  55.         result = result .. scheme .. ", realm = " .. realm .. "\n"
  56.       else
  57.         result = result .. string.match(value, "(%a+)") .. "\n"
  58.       end
  59.     end
  60.   end
  61.  
  62.   if basic then
  63.     for _, combination in pairs (authcombinations) do 
  64.         authheader = "Basic " .. base64.enc(combination)
  65.         answer = http.get(host, port, '/', {header={Authorization=authheader}})
  66.         if answer.status ~= 401 and answer.status ~= 403 then
  67.           result = result .. "  HTTP server may accept " .. combination .. " combination for Basic authentication\n"
  68.         end
  69.     end
  70.   end
  71.  
  72.   return result
  73. end
  74.  
  75.