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

  1. description = [[
  2. Shows SSH hostkeys.
  3.  
  4. Shows the target SSH server's key fingerprint and (with high enough verbosity level) the public key itself.  It records the discovered host keys in <code>nmap.registry</code> for use by other scripts.  Output can be controlled with the <code>ssh_hostkey</code> script argument.
  5. ]]
  6.  
  7. ---
  8. --@usage
  9. -- nmap host --script SSH-hostkey --script-args ssh_hostkey=full
  10. -- nmap host --script SSH-hostkey --script-args ssh_hostkey=all
  11. -- nmap host --script SSH-hostkey --script-args ssh_hostkey='visual bubble'
  12. --
  13. --@args ssh_hostkey Controls the output format of keys. Multiple values may be
  14. -- given, separated by spaces. Possible values are
  15. -- * <code>"full"</code>: The entire key, not just the fingerprint.
  16. -- * <code>"bubble"</code>: Bubble Babble output,
  17. -- * <code>"visual"</code>: Visual ASCII art representation.
  18. -- * <code>"all"</code>: All of the above.
  19. --
  20. --@output
  21. -- 22/tcp open  ssh
  22. -- |  ssh-hostkey: 2048 f0:58:ce:f4:aa:a4:59:1c:8e:dd:4d:07:44:c8:25:11 (RSA)
  23. -- 22/tcp open  ssh
  24. -- |  ssh-hostkey: 2048 f0:58:ce:f4:aa:a4:59:1c:8e:dd:4d:07:44:c8:25:11 (RSA)
  25. -- |  +--[ RSA 2048]----+
  26. -- |  |       .E*+      |
  27. -- |  |        oo       |
  28. -- |  |      . o .      |
  29. -- |  |       O . .     |
  30. -- |  |      o S o .    |
  31. -- |  |     = o + .     |
  32. -- |  |    . * o .      |
  33. -- |  |     = .         |
  34. -- |  |    o .          |
  35. -- |_ +-----------------+
  36. -- 22/tcp open  ssh
  37. -- |  ssh-hostkey: 2048 xuvah-degyp-nabus-zegah-hebur-nopig-bubig-difeg-hisym-rumef-cuxex (RSA)
  38. -- |_ ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAwVuv2gcr0maaKQ69VVIEv2ob4OxnuI64fkeOnCXD1lUx5tTA+vefXUWEMxgMuA7iX4irJHy2zer0NQ3Z3yJvr5scPgTYIaEOp5Uo/eGFG9Agpk5wE8CoF0e47iCAPHqzlmP2V7aNURLMODb3jVZuI07A2ZRrMGrD8d888E2ORVORv1rYeTYCqcMMoVFmX9l3gWEdk4yx3w5sD8v501Iuyd1v19mPfyhrI5E1E1nl/Xjp5N0/xP2GUBrdkDMxKaxqTPMie/f0dXBUPQQN697a5q+5lBRPhKYOtn6yQKCd9s1Q22nxn72Jmi1RzbMyYJ52FosDT755Qmb46GLrDMaZMQ==
  39. author = "Sven Klemm <sven@c3d2.de>"
  40. license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
  41. categories = {"safe","default","intrusive"}
  42.  
  43. require("shortport")
  44. require("stdnse")
  45.  
  46. -- openssl is required for this script
  47. if pcall(require,"openssl") then
  48.   require("ssh1")
  49.   require("ssh2")
  50. else
  51.   action = function()
  52.     stdnse.print_debug( 3, "Skipping %s script because OpenSSL is missing.", filename )
  53.   end 
  54. end
  55.  
  56. portrule = shortport.port_or_service(22, "ssh")
  57.  
  58.  
  59. --- put hostkey in the nmap registry for usage by other scripts
  60. --@param host nmap host table
  61. --@param key host key table
  62. local add_key_to_registry = function( host, key )
  63.   nmap.registry.sshhostkey = nmap.registry.sshhostkey or {}
  64.   nmap.registry.sshhostkey[host.ip] = nmap.registry.sshhostkey[host.ip] or {}
  65.   table.insert( nmap.registry.sshhostkey[host.ip], key )
  66. end
  67.  
  68. action = action or function(host, port)
  69.   local output = {}
  70.   local keys = {}
  71.   local _,key
  72.   local format = nmap.registry.args.ssh_hostkey or "hex"
  73.   local all_formats = format:find( 'all', 1, true )
  74.  
  75.   key = ssh1.fetch_host_key( host, port )
  76.   if key then table.insert( keys, key ) end
  77.  
  78.   key = ssh2.fetch_host_key( host, port, "ssh-dss" )
  79.   if key then table.insert( keys, key ) end
  80.  
  81.   key = ssh2.fetch_host_key( host, port, "ssh-rsa" )
  82.   if key then table.insert( keys, key ) end
  83.  
  84.   for _, key in ipairs( keys ) do
  85.     add_key_to_registry( host, key )
  86.     if format:find( 'hex', 1, true ) or all_formats then
  87.       table.insert( output, ssh1.fingerprint_hex( key.fingerprint, key.algorithm, key.bits ) )
  88.     end
  89.     if format:find( 'bubble', 1, true ) or all_formats then
  90.       table.insert( output, ssh1.fingerprint_bubblebabble( openssl.sha1(key.fp_input), key.algorithm, key.bits ) )
  91.     end
  92.     if format:find( 'visual', 1, true ) or all_formats then
  93.       -- insert empty line so table is not destroyed if this is the first
  94.       -- line of output
  95.       if #output == 0 then table.insert( output, " " ) end
  96.       table.insert( output, ssh1.fingerprint_visual( key.fingerprint, key.algorithm, key.bits ) )
  97.     end
  98.     if nmap.verbosity() > 1 or format:find( 'full', 1, true ) or all_formats then
  99.       table.insert( output, key.full_key )
  100.     end
  101.   end
  102.  
  103.   if #output > 0 then
  104.     return table.concat( output, '\n' )
  105.   end
  106. end
  107.  
  108.