ssh.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import sys
  2. import struct
  3. import itertools
  4. assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
  5. def nbits(n):
  6. # Mimic mp_get_nbits for ordinary Python integers.
  7. assert 0 <= n
  8. smax = next(s for s in itertools.count() if (n >> (1 << s)) == 0)
  9. toret = 0
  10. for shift in reversed([1 << s for s in range(smax)]):
  11. if n >> shift != 0:
  12. n >>= shift
  13. toret += shift
  14. assert n <= 1
  15. if n == 1:
  16. toret += 1
  17. return toret
  18. def ssh_byte(n):
  19. return struct.pack("B", n)
  20. def ssh_uint32(n):
  21. return struct.pack(">L", n)
  22. def ssh_uint64(n):
  23. return struct.pack(">Q", n)
  24. def ssh_string(s):
  25. return ssh_uint32(len(s)) + s
  26. def ssh1_mpint(x):
  27. bits = nbits(x)
  28. bytevals = [0xFF & (x >> (8*n)) for n in range((bits-1)//8, -1, -1)]
  29. return struct.pack(">H" + "B" * len(bytevals), bits, *bytevals)
  30. def ssh2_mpint(x):
  31. bytevals = [0xFF & (x >> (8*n)) for n in range(nbits(x)//8, -1, -1)]
  32. return struct.pack(">L" + "B" * len(bytevals), len(bytevals), *bytevals)
  33. def decoder(fn):
  34. def decode(s, return_rest = False):
  35. item, length_consumed = fn(s)
  36. if return_rest:
  37. return item, s[length_consumed:]
  38. else:
  39. return item
  40. return decode
  41. @decoder
  42. def ssh_decode_byte(s):
  43. return struct.unpack_from("B", s, 0)[0], 1
  44. @decoder
  45. def ssh_decode_uint32(s):
  46. return struct.unpack_from(">L", s, 0)[0], 4
  47. @decoder
  48. def ssh_decode_uint64(s):
  49. return struct.unpack_from(">Q", s, 0)[0], 8
  50. @decoder
  51. def ssh_decode_string(s):
  52. length = ssh_decode_uint32(s)
  53. assert length + 4 <= len(s)
  54. return s[4:length+4], length+4
  55. @decoder
  56. def ssh1_get_mpint(s): # returns it unconsumed, still in wire encoding
  57. nbits = struct.unpack_from(">H", s, 0)[0]
  58. nbytes = (nbits + 7) // 8
  59. assert nbytes + 2 <= len(s)
  60. return s[:nbytes+2], nbytes+2
  61. @decoder
  62. def ssh1_decode_mpint(s):
  63. nbits = struct.unpack_from(">H", s, 0)[0]
  64. nbytes = (nbits + 7) // 8
  65. assert nbytes + 2 <= len(s)
  66. data = s[2:nbytes+2]
  67. v = 0
  68. for b in struct.unpack("B" * len(data), data):
  69. v = (v << 8) | b
  70. return v, nbytes+2
  71. AGENT_MAX_MSGLEN = 262144
  72. SSH1_AGENTC_REQUEST_RSA_IDENTITIES = 1
  73. SSH1_AGENT_RSA_IDENTITIES_ANSWER = 2
  74. SSH1_AGENTC_RSA_CHALLENGE = 3
  75. SSH1_AGENT_RSA_RESPONSE = 4
  76. SSH1_AGENTC_ADD_RSA_IDENTITY = 7
  77. SSH1_AGENTC_REMOVE_RSA_IDENTITY = 8
  78. SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES = 9
  79. SSH_AGENT_FAILURE = 5
  80. SSH_AGENT_SUCCESS = 6
  81. SSH_AGENT_EXTENSION_FAILURE = 28
  82. SSH2_AGENTC_REQUEST_IDENTITIES = 11
  83. SSH2_AGENT_IDENTITIES_ANSWER = 12
  84. SSH2_AGENTC_SIGN_REQUEST = 13
  85. SSH2_AGENT_SIGN_RESPONSE = 14
  86. SSH2_AGENTC_ADD_IDENTITY = 17
  87. SSH2_AGENTC_REMOVE_IDENTITY = 18
  88. SSH2_AGENTC_REMOVE_ALL_IDENTITIES = 19
  89. SSH2_AGENTC_EXTENSION = 27
  90. SSH_AGENT_RSA_SHA2_256 = 2
  91. SSH_AGENT_RSA_SHA2_512 = 4