#!/usr/bin/python sig=""" -----BEGIN PGP SIGNED MESSAGE----- """ # # 2003/8/23 # # Read key from SSH-keyfile and store in a file. # Secret keys must have an empty passphrase in order to extract # the secret decryption value. # # SECRETKEY-Structure assumed: # # octets value meaning # 1-32 Formatstring "SSH PRIVATE KEY FILE FORMAT 1.1" # 33-40 Nullstring 8 octetts # 41/42 Length of Modulus # 43+x MPI: modulus n # 43+x+y MPI: encryption e # 43+x+y+2 0 Nullstring 2 octetts # 43+x+y+4 Length of User-ID # + L octetts User-ID # + 4 octetts unknown data # if secet key is unencrypted: # MPI: Decryption (secret key) # MPI: u multiplicative inverse of p mod q # MPI: p # MPI: q (q 0 and i < len(keyfile) : Length = Length * 256 + ord(keyfile[int(i)]) i = i + 1 N = N - 1 return Length ######################################################### def readMPI(): global i Length = 0 Length = readNbytes(2) # in bits Bytes = Length / 8 if Length % 8 != 0 : Bytes = Bytes + 1 # read L octets print "Reading " , pure.toString(Length), " bit or ", pure.toString(Bytes), " bytes" X = 1 MPI = 0L while X <= Bytes and i < len(keyfile) : MPI = MPI * 256 + ord(keyfile[int(i)]) i = i + 1 X = X + 1 return MPI ######################################################### if len(sys.argv) != 3 : print "usage: read-sshkey infile outfile" else: try: FILE = open (sys.argv[1], "r") keyfile = FILE.read() FILE.close() print "Reading " + pure.toString(len(keyfile)) + " bytes." except: print "Cannot read input file" sys.exit(3) # read first line Formatstring = "" while ord(keyfile[i]) != 10 : Formatstring = Formatstring + keyfile[i] i = i + 1 if Formatstring[:15] == "SSH PRIVATE KEY" : print Formatstring begin = readNbytes(10) i = i + 1 Modulus = 0L Encryption = 0L Modulus = readMPI() print "Modulus = ", pure.toString(Modulus) print Encryption = readMPI() print "Encryption = ", pure.toString(Encryption) print neu = readNbytes(2) Length = readNbytes(2) UserID = keyfile[i:int(i+Length)] print UserID print i = i + Length + 4 Decryption = 0L P = Q = U = 0L Length = 0 Length = ord(keyfile[int(i)]) * 256 + ord(keyfile[int(i+1)]) print "Looking for unencrypted PRIVATE KEY of length ", print pure.toString(Length) if Length/8 < len(keyfile) - i : Decryption = readMPI() print "Decryption = ", pure.toString(Decryption) print U = readMPI() print "u = ", pure.toString(U) P = readMPI() print "p = ", pure.toString(P) Q = readMPI() print "q = ", pure.toString(Q) else : print "Secret key is 3-DES encrypted." KEY = "" KEY = KEY + "Modulus = " + pure.toString(Modulus) + "\n" KEY = KEY + "Encryption = " + pure.toString(Encryption) + "\n" KEY = KEY + "Decryption = " + pure.toString(Decryption) + "\n" KEY = KEY + "Hashmodulus = 1" + "\n" KEY = KEY + "Generator = 1" + "\n" KEY = KEY + UserID + "\n" KEY = KEY + "Protection = None" + "\n" KEY = KEY + "Securityhash = None" + "\n" #------------------------------------------------------------------# else: print "PUBLIC KEY" Modulus = 0L M = "" Encryption = 0L E = "" UserID = "" i = 0 while ord(keyfile[i]) != 32: i = i + 1 i = i + 1 while ord(keyfile[i]) != 32: E = E + keyfile[i] i = i + 1 Encryption = pure.toString(E) i = i + 1 while ord(keyfile[i]) != 32: M = M + keyfile[i] i = i + 1 i = i + 1 Modulus = pure.toString(M) UserID = pure.Line(keyfile[i:]) KEY = "" KEY = KEY + "Modulus = " + pure.toString(Modulus) + "\n" KEY = KEY + "Encryption = " + pure.toString(Encryption) + "\n" KEY = KEY + "Hashmodulus = 1" + "\n" KEY = KEY + "Generator = 1" + "\n" KEY = KEY + UserID + "\n" KEY = KEY + "Securityhash = None" + "\n" print KEY #------------------------------------------------------------------# outfile = sys.argv[2] try: FILE = open (outfile, "w") FILE.write(KEY) FILE.close() except: print "Cannot write file to filesystem" sys.exit(3) print "EXIT" sys.exit(0) ################################################################## sig=""" -----BEGIN PGP SIGNATURE----- Version: 2.6.3in Charset: noconv iQEVAwUBP0cgDL6wVDeIE49tAQFSdgf/aUE6sJzJ6Pv41/yYJwXp5XQP3wJsdlaa DbuXfrSNd3XyBObswzKlI/0r8eqP0Tk2eJVeILGftYMv9xi6ZZ3dv2VpaIbZVDD6 A9vt9f80DdhQ2fJeNdB5sbsaDXAL/e/b4vrRMkpxJMG/71pB700/iByqa8sRbr/x E8aUK7yP8i4DzNi/YwbPJ+g9ez9Nf+X+A9FiZEnyKgS9oTaEA9hQYxyPgcqgMK1z IoQed5x/7wF+raqxS7Tc2yNGc7zj/bukp9mWSHvdt98OIegE7Kyw+wZ+quOpSjxW knFbBZ79L8bvkiFMCFGoVztYdfjIkX+zlwBnMrHWgww3SOOP/Z4ylQ== =cM54 -----END PGP SIGNATURE----- """