encodelib.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Python module to make it easy to manually encode SSH packets, by
  2. # supporting the various uint32, string, mpint primitives.
  3. #
  4. # The idea of this is that you can use it to manually construct key
  5. # exchange sequences of interesting kinds, for testing purposes.
  6. import struct, random
  7. def boolean(b):
  8. return "\1" if b else "\0"
  9. def byte(b):
  10. assert 0 <= b < 0x100
  11. return chr(b)
  12. def uint32(u):
  13. assert 0 <= u < 0x100000000
  14. return struct.pack(">I", u)
  15. def uint64(u):
  16. assert 0 <= u < 0x10000000000000000
  17. return struct.pack(">L", u)
  18. def string(s):
  19. return uint32(len(s)) + s
  20. def mpint(m):
  21. s = ""
  22. lastbyte = 0
  23. while m > 0:
  24. lastbyte = m & 0xFF
  25. s = chr(lastbyte) + s
  26. m >>= 8
  27. if lastbyte & 0x80:
  28. s = "\0" + s
  29. return string(s)
  30. def name_list(ns):
  31. s = ""
  32. for n in ns:
  33. assert "," not in n
  34. if s != "":
  35. s += ","
  36. s += n
  37. return string(s)
  38. def ssh_rsa_key_blob(modulus, exponent):
  39. return string(string("ssh-rsa") + mpint(modulus) + mpint(exponent))
  40. def ssh_rsa_signature_blob(signature):
  41. return string(string("ssh-rsa") + mpint(signature))
  42. def greeting(string):
  43. # Greeting at the start of an SSH connection.
  44. return string + "\r\n"
  45. # Packet types.
  46. SSH2_MSG_DISCONNECT = 1
  47. SSH2_MSG_IGNORE = 2
  48. SSH2_MSG_UNIMPLEMENTED = 3
  49. SSH2_MSG_DEBUG = 4
  50. SSH2_MSG_SERVICE_REQUEST = 5
  51. SSH2_MSG_SERVICE_ACCEPT = 6
  52. SSH2_MSG_KEXINIT = 20
  53. SSH2_MSG_NEWKEYS = 21
  54. SSH2_MSG_KEXDH_INIT = 30
  55. SSH2_MSG_KEXDH_REPLY = 31
  56. SSH2_MSG_KEX_DH_GEX_REQUEST = 30
  57. SSH2_MSG_KEX_DH_GEX_GROUP = 31
  58. SSH2_MSG_KEX_DH_GEX_INIT = 32
  59. SSH2_MSG_KEX_DH_GEX_REPLY = 33
  60. SSH2_MSG_KEXRSA_PUBKEY = 30
  61. SSH2_MSG_KEXRSA_SECRET = 31
  62. SSH2_MSG_KEXRSA_DONE = 32
  63. SSH2_MSG_USERAUTH_REQUEST = 50
  64. SSH2_MSG_USERAUTH_FAILURE = 51
  65. SSH2_MSG_USERAUTH_SUCCESS = 52
  66. SSH2_MSG_USERAUTH_BANNER = 53
  67. SSH2_MSG_USERAUTH_PK_OK = 60
  68. SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ = 60
  69. SSH2_MSG_USERAUTH_INFO_REQUEST = 60
  70. SSH2_MSG_USERAUTH_INFO_RESPONSE = 61
  71. SSH2_MSG_GLOBAL_REQUEST = 80
  72. SSH2_MSG_REQUEST_SUCCESS = 81
  73. SSH2_MSG_REQUEST_FAILURE = 82
  74. SSH2_MSG_CHANNEL_OPEN = 90
  75. SSH2_MSG_CHANNEL_OPEN_CONFIRMATION = 91
  76. SSH2_MSG_CHANNEL_OPEN_FAILURE = 92
  77. SSH2_MSG_CHANNEL_WINDOW_ADJUST = 93
  78. SSH2_MSG_CHANNEL_DATA = 94
  79. SSH2_MSG_CHANNEL_EXTENDED_DATA = 95
  80. SSH2_MSG_CHANNEL_EOF = 96
  81. SSH2_MSG_CHANNEL_CLOSE = 97
  82. SSH2_MSG_CHANNEL_REQUEST = 98
  83. SSH2_MSG_CHANNEL_SUCCESS = 99
  84. SSH2_MSG_CHANNEL_FAILURE = 100
  85. SSH2_MSG_USERAUTH_GSSAPI_RESPONSE = 60
  86. SSH2_MSG_USERAUTH_GSSAPI_TOKEN = 61
  87. SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE = 63
  88. SSH2_MSG_USERAUTH_GSSAPI_ERROR = 64
  89. SSH2_MSG_USERAUTH_GSSAPI_ERRTOK = 65
  90. SSH2_MSG_USERAUTH_GSSAPI_MIC = 66
  91. def clearpkt(msgtype, *stuff):
  92. # SSH-2 binary packet, in the cleartext format used for initial
  93. # setup and kex.
  94. s = byte(msgtype)
  95. for thing in stuff:
  96. s += thing
  97. padlen = 0
  98. while padlen < 4 or len(s) % 8 != 3:
  99. padlen += 1
  100. s += byte(random.randint(0,255))
  101. s = byte(padlen) + s
  102. return string(s)