home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Networking / nmap-5.00-setup.exe / nselib / ssh1.lua < prev    next >
Text File  |  2009-07-06  |  6KB  |  173 lines

  1. --- Functions for the SSH-1 protocol.
  2. --
  3. -- This module also contains functions for formatting key fingerprints.
  4. -- @author Sven Klemm <sven@c3d2.de>
  5. -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html
  6.  
  7. module(... or "ssh1",package.seeall)
  8.  
  9. local bin = require "bin"
  10. local bit = require "bit"
  11. local math = require "math"
  12. local stdnse = require "stdnse"
  13. local openssl = require "openssl"
  14.  
  15. --- Fetch an SSH-1 host key.
  16. -- @param host Nmap host table.
  17. -- @param port Nmap port table.
  18. -- @return A table with the following fields: <code>exp</code>,
  19. -- <code>mod</code>, <code>bits</code>, <code>key_type</code>,
  20. -- <code>fp_input</code>, <code>full_key</code>, <code>algorithm</code>, and
  21. -- <code>fingerprint</code>.
  22. fetch_host_key = function(host, port)
  23.   local socket = nmap.new_socket()
  24.   local status
  25.  
  26.   status = socket:connect(host.ip, port.number)
  27.   if not status then return end
  28.   -- fetch banner
  29.   status = socket:receive_lines(1)
  30.   if not status then socket:close(); return end
  31.   -- send our banner
  32.   status = socket:send("SSH-1.5-Nmap-SSH1-Hostkey\r\n")
  33.   if not status then socket:close(); return end
  34.  
  35.   local data, packet_length, padding, offset
  36.   status,data = socket:receive()
  37.   socket:close()
  38.   if not status then return end
  39.  
  40.   offset, packet_length = bin.unpack( ">i", data )
  41.   padding = 8 - packet_length % 8
  42.   offset = offset + padding
  43.  
  44.   if padding + packet_length + 4 == data:len() then
  45.     -- seems to be a proper SSH1 packet
  46.     local msg_code,host_key_bits,exp,mod,length,fp_input
  47.     offset, msg_code = bin.unpack( ">c", data, offset )
  48.     if msg_code == 2 then -- 2 => SSH_SMSG_PUBLIC_KEY
  49.       -- ignore cookie and server key bits
  50.       offset, _, _ = bin.unpack( ">A8i", data, offset )
  51.       -- skip server key exponent and modulus
  52.       offset, length = bin.unpack( ">S", data, offset )
  53.       offset = offset + math.ceil( length / 8 )
  54.       offset, length = bin.unpack( ">S", data, offset )
  55.       offset = offset + math.ceil( length / 8 )
  56.  
  57.       offset, host_key_bits = bin.unpack( ">i", data, offset )
  58.       offset, length = bin.unpack( ">S", data, offset )
  59.       offset, exp = bin.unpack( ">A" .. math.ceil( length / 8 ), data, offset )
  60.       exp = openssl.bignum_bin2bn( exp )
  61.       offset, length = bin.unpack( ">S", data, offset )
  62.       offset, mod = bin.unpack( ">A" .. math.ceil( length / 8 ), data, offset )
  63.       mod = openssl.bignum_bin2bn( mod )
  64.  
  65.       fp_input = mod:tobin()..exp:tobin()
  66.  
  67.       return {exp=exp,mod=mod,bits=host_key_bits,key_type='rsa1',fp_input=fp_input,
  68.               full_key=exp:todec()..' '..mod:todec(),algorithm="RSA1",
  69.               fingerprint=openssl.md5(fp_input)}
  70.     end
  71.   end
  72. end
  73.  
  74. --- Format a key fingerprint in hexadecimal.
  75. -- @param fingerprint Key fingerprint.
  76. -- @param algorithm Key algorithm.
  77. -- @param bits Key size in bits.
  78. fingerprint_hex = function( fingerprint, algorithm, bits )
  79.   fingerprint = stdnse.tohex(fingerprint,{separator=":",group=2})
  80.   return ("%d %s (%s)"):format( bits, fingerprint, algorithm )
  81. end
  82.  
  83. --- Format a key fingerprint in Bubble Babble.
  84. -- @param fingerprint Key fingerprint.
  85. -- @param algorithm Key algorithm.
  86. -- @param bits Key size in bits.
  87. fingerprint_bubblebabble = function( fingerprint, algorithm, bits )
  88.   local vowels = {'a','e','i','o','u','y'}
  89.   local consonants = {'b','c','d','f','g','h','k','l','m','n','p','r','s','t','v','z','x'}
  90.   local s = "x"
  91.   local seed = 1
  92.  
  93.   for i=1,#fingerprint+2,2 do
  94.     local in1,in2,idx1,idx2,idx3,idx4,idx5
  95.     if i < #fingerprint or #fingerprint / 2 % 2 ~= 0 then
  96.       in1 = fingerprint:byte(i)
  97.       idx1 = (bit.band(bit.rshift(in1,6),3) + seed) % 6 + 1
  98.       idx2 = bit.band(bit.rshift(in1,2),15) + 1
  99.       idx3 = (bit.band(in1,3) + math.floor(seed/6)) % 6 + 1
  100.       s = s .. vowels[idx1] .. consonants[idx2] .. vowels[idx3]
  101.       if i < #fingerprint then
  102.         in2 = fingerprint:byte(i+1)
  103.         idx4 = bit.band(bit.rshift(in2,4),15) + 1
  104.         idx5 = bit.band(in2,15) + 1
  105.         s = s .. consonants[idx4] .. '-' .. consonants[idx5]
  106.         seed = (seed * 5 + in1 * 7 + in2) % 36
  107.       end
  108.     else
  109.       idx1 = seed % 6 + 1
  110.       idx2 = 16 + 1
  111.       idx3 = math.floor(seed/6) + 1
  112.       s = s .. vowels[idx1] .. consonants[idx2] .. vowels[idx3]
  113.     end
  114.   end
  115.   s = s .. 'x'
  116.   return ("%d %s (%s)"):format( bits, s, algorithm )
  117. end
  118.  
  119. --- Format a key fingerprint into a visual ASCII art representation.
  120. --
  121. -- Ported from http://www.openbsd.org/cgi-bin/cvsweb/~checkout~/src/usr.bin/ssh/key.c.
  122. -- @param fingerprint Key fingerprint.
  123. -- @param algorithm Key algorithm.
  124. -- @param bits Key size in bits.
  125. fingerprint_visual = function( fingerprint, algorithm, bits )
  126.   local i,j,field,characters,input,fieldsize_x,fieldsize_y,s
  127.   fieldsize_x, fieldsize_y = 17, 9
  128.   characters = {' ','.','o','+','=','*','B','O','X','@','%','&','#','/','^','S','E'}
  129.  
  130.   -- initialize drawing area
  131.   field = {}
  132.   for i=1,fieldsize_x do
  133.     field[i]={}
  134.     for j=1,fieldsize_y do field[i][j]=1 end
  135.   end
  136.  
  137.   -- we start in the center and mark it
  138.   x, y = math.ceil(fieldsize_x/2), math.ceil(fieldsize_y/2)
  139.   field[x][y] = #characters - 1;
  140.  
  141.   -- iterate over fingerprint 
  142.   for i=1,#fingerprint do
  143.     input = fingerprint:byte(i)
  144.     -- each byte conveys four 2-bit move commands 
  145.     for j=1,4 do
  146.       if bit.band( input, 1) == 1 then x = x + 1 else x = x - 1 end
  147.       if bit.band( input, 2) == 2 then y = y + 1 else y = y - 1 end
  148.  
  149.       x = math.max(x,1); x = math.min(x,fieldsize_x)
  150.       y = math.max(y,1); y = math.min(y,fieldsize_y)
  151.  
  152.       if field[x][y] < #characters - 2 then
  153.         field[x][y] = field[x][y] + 1
  154.       end
  155.       input = bit.rshift( input, 2 )
  156.     end
  157.   end
  158.  
  159.   -- mark end point
  160.   field[x][y] = #characters;
  161.  
  162.   -- build output
  163.   s = ('\n+--[%4s %4d]----+\n'):format( algorithm, bits )
  164.   for i=1,fieldsize_y do
  165.     s = s .. '|'
  166.     for j=1,fieldsize_x do s = s .. characters[ field[j][i] ] end
  167.     s = s .. '|\n'
  168.   end
  169.   s = s .. '+-----------------+\n'
  170.   return s
  171. end
  172.  
  173.