You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3KB

  1. module CryptCheck
  2. module Ssh
  3. class Packet
  4. SSH_MSG_KEXINIT = 20
  5. def self.uint32(raw)
  6. raw.gets(4).unpack('N').first
  7. end
  8. def self.string(raw)
  9. length = self.uint32 raw
  10. raw.gets length
  11. end
  12. def self.strings(raw)
  13. self.string(raw).split ','
  14. end
  15. def self.read(socket)
  16. packet_length = socket.recv(4).unpack('N').first
  17. padding_length = socket.recv(1).unpack('C').first
  18. payload = socket.recv packet_length - padding_length - 1
  19. socket.recv padding_length
  20. StringIO.new payload
  21. end
  22. def self.read_kex_init(socket)
  23. payload = self.read socket
  24. msg_id = payload.gets(1).unpack('C').first
  25. raise "Not expected message id #{msg_id}" unless msg_id == SSH_MSG_KEXINIT
  26. payload.gets 16 # cookie
  27. key_algorithms = self.strings payload
  28. host_key_algorithms = self.strings payload
  29. encryption_algorithms = (self.strings(payload) + self.strings(payload)).uniq
  30. mac_algorithms = (self.strings(payload) + self.strings(payload)).uniq
  31. compression_algorithms = (self.strings(payload) + self.strings(payload)).uniq
  32. { kex: key_algorithms, host_key: host_key_algorithms, encryption: encryption_algorithms,
  33. mac: mac_algorithms, compression: compression_algorithms }
  34. end
  35. end
  36. end
  37. end