SSH.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.math.BigInteger;
  3. import java.nio.ByteBuffer;
  4. import java.security.KeyFactory;
  5. import java.security.KeyPair;
  6. import java.security.KeyPairGenerator;
  7. import java.security.MessageDigest;
  8. import java.security.PublicKey;
  9. import java.security.SecureRandom;
  10. import java.security.Signature;
  11. import java.security.interfaces.DSAPublicKey;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import javax.crypto.KeyAgreement;
  15. import javax.crypto.interfaces.DHPublicKey;
  16. import javax.crypto.spec.DHParameterSpec;
  17. import javax.crypto.spec.DHPublicKeySpec;
  18. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  19. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  20. /**
  21. * SSH protocol.
  22. * @author Wulf Pfeiffer
  23. */
  24. public class SSH implements Protocol {
  25. /**
  26. * Represents the states of the protocol.
  27. */
  28. private enum STATE {
  29. NONE,
  30. SERVER_VERSION,
  31. CLIENT_VERSION,
  32. KEX_INIT,
  33. CLOSED
  34. }
  35. /**
  36. * Denotes in which state the protocol is right now.
  37. */
  38. private STATE connectionState = STATE.NONE;
  39. /** Denotes in which state the protocol is right now */
  40. private STATE state = STATE.NONE;
  41. public int getPort() {
  42. return 22;
  43. }
  44. public TALK_FIRST whoTalksFirst() {
  45. return TALK_FIRST.SERVER;
  46. }
  47. public List<Packet> processMessage(Packet packet) {
  48. List<Packet> response = new ArrayList<Packet>();
  49. byte[] request = null;
  50. if(packet != null) {
  51. request = packet.getMessage();
  52. }
  53. switch(connectionState) {
  54. case NONE:
  55. response.add(new Packet(serverVersion + serverType + "\r\n"));
  56. connectionState = STATE.SERVER_VERSION;
  57. break;
  58. case SERVER_VERSION:
  59. extractType(request);
  60. extractCookie(request);
  61. response.add(kexInit());
  62. connectionState = STATE.CLIENT_VERSION;
  63. break;
  64. case CLIENT_VERSION:
  65. extractPubKey(request);
  66. response.add(dhKexReply());
  67. //FIXME signature in dhKexReply seems to be wrong
  68. response.add(newKeys());
  69. connectionState = STATE.KEX_INIT;
  70. break;
  71. case KEX_INIT:
  72. connectionState = STATE.CLOSED;
  73. break;
  74. case CLOSED:
  75. break;
  76. default:
  77. connectionState = STATE.CLOSED;
  78. break;
  79. }
  80. return response;
  81. }
  82. public boolean isClosed() {
  83. return (state == STATE.CLOSED);
  84. }
  85. public boolean isSecure() {
  86. return false;
  87. }
  88. public Class<byte[]> getType() {
  89. return byte[].class;
  90. }
  91. public String toString() {
  92. return "SSH";
  93. }
  94. /**
  95. * Wraps the packets with packet length and padding.
  96. * @param packet content that is wrapped.
  97. * @return wrapped packet.
  98. */
  99. private Packet wrapPacket(byte[] packet) {
  100. int packetLength = 5 + packet.length; //4 byte packet length, 1 byte padding length, payload length
  101. int paddingLengthCBS = cipherBlockSize - (packetLength % cipherBlockSize);
  102. int paddingLength8 = 8 - (packetLength % 8);
  103. int paddingLength = paddingLengthCBS > paddingLength8 ? paddingLengthCBS : paddingLength8;
  104. if(paddingLength < 4) paddingLength += cipherBlockSize;
  105. packetLength = packetLength + paddingLength - 4; //add padding string length to packet length
  106. byte[] packetLen = ByteBuffer.allocate(4).putInt(packetLength).array();
  107. byte[] paddingLen = {(byte) paddingLength};
  108. byte[] paddingString = new byte[paddingLength];
  109. for(int i = 0; i < paddingLength; i++) {
  110. SecureRandom rndm = new SecureRandom();
  111. paddingString[i] = (byte) rndm.nextInt(255);
  112. }
  113. return new Packet(HelperUtils.concat(packetLen, paddingLen, packet, paddingString));
  114. }
  115. /**
  116. * Builds the Kex Init packet that contains all the allowed algorithms by the server.
  117. * @return Kex Init packet.
  118. */
  119. private Packet kexInit() {
  120. byte[] msgCode = {0x14};
  121. I_S = randomBytes(16);
  122. byte[] kexLength = ByteBuffer.allocate(4).putInt(kex_alg.getBytes().length).array();
  123. byte[] serverLength = ByteBuffer.allocate(4).putInt(server_alg.getBytes().length).array();
  124. byte[] encrypt_c_Length = ByteBuffer.allocate(4).putInt(encrypt_alg_c.getBytes().length).array();
  125. byte[] encrypt_s_Length = ByteBuffer.allocate(4).putInt(encrypt_alg_s.getBytes().length).array();
  126. byte[] mac_c_Length = ByteBuffer.allocate(4).putInt(mac_alg_c.getBytes().length).array();
  127. byte[] mac_s_Length = ByteBuffer.allocate(4).putInt(mac_alg_s.getBytes().length).array();
  128. byte[] comp_c_Length = ByteBuffer.allocate(4).putInt(comp_alg_c.getBytes().length).array();
  129. byte[] comp_s_Length = ByteBuffer.allocate(4).putInt(comp_alg_s.getBytes().length).array();
  130. byte[] language_c_s = {0x00, 0x00, 0x00, 0x00};
  131. byte[] language_s_c = {0x00, 0x00, 0x00, 0x00};
  132. byte[] kexFirsPckt = {0x00};
  133. byte[] reserved = {0x00, 0x00, 0x00, 0x00};
  134. byte[] response = HelperUtils.concat(msgCode, I_S, kexLength, kex_alg.getBytes(), serverLength, server_alg.getBytes(),
  135. encrypt_c_Length, encrypt_alg_c.getBytes(), encrypt_s_Length, encrypt_alg_s.getBytes(), mac_c_Length, mac_alg_c.getBytes(),
  136. mac_s_Length, mac_alg_s.getBytes(), comp_c_Length, comp_alg_c.getBytes(), comp_s_Length, comp_alg_s.getBytes(),
  137. language_c_s, language_s_c, kexFirsPckt, reserved);
  138. return wrapPacket(response);
  139. }
  140. /**
  141. * Builds the Diffie-Hellman Kex Reply, containing the host key,f and the signature.
  142. * @return Diffie-Hellman Kex Reply packet.
  143. */
  144. private Packet dhKexReply() {
  145. generateDHKeys();
  146. generateHostKey();
  147. generateSha1Hash();
  148. generateSignature();
  149. byte[] msgCode = {0x1f};
  150. byte[] hostKeyLength = ByteBuffer.allocate(4).putInt(K_S.length).array();
  151. byte[] fDHLength = ByteBuffer.allocate(4).putInt(f.length).array();
  152. byte[] signatureLength = ByteBuffer.allocate(4).putInt(sig.length).array();
  153. byte[] server_algLength = ByteBuffer.allocate(4).putInt(server_alg.getBytes().length).array();
  154. byte[] payloadLength = ByteBuffer.allocate(4).putInt(server_algLength.length + signatureLength.length + sig.length + server_alg.getBytes().length).array();
  155. byte[] response = HelperUtils.concat(msgCode, hostKeyLength, K_S,
  156. fDHLength, f, payloadLength, server_algLength, server_alg.getBytes(), signatureLength, sig);
  157. return wrapPacket(response);
  158. }
  159. /**
  160. * New Keys response.
  161. * @return New Keys response.
  162. */
  163. private Packet newKeys() {
  164. byte[] msgCode = {0x15};
  165. return wrapPacket(msgCode);
  166. }
  167. /**
  168. * Generates the required Diffie-Hellman keys with p and g from Oakley Group 1.
  169. */
  170. private void generateDHKeys() {
  171. try {
  172. KeyPairGenerator myKpairGen = KeyPairGenerator.getInstance("DH");
  173. KeyAgreement myKeyAgree = KeyAgreement.getInstance("DH");
  174. BigInteger p = new BigInteger(this.p);
  175. BigInteger g = new BigInteger(this.g);
  176. BigInteger e = new BigInteger(this.e);
  177. DHParameterSpec dhParamSpec = new DHParameterSpec(p, g);
  178. myKpairGen.initialize(dhParamSpec);
  179. KeyPair myKpair = myKpairGen.generateKeyPair();
  180. myKeyAgree.init(myKpair.getPrivate());
  181. BigInteger f = ((DHPublicKey) (myKpair.getPublic())).getY();
  182. this.f = f.toByteArray();
  183. KeyFactory myKeyFac = KeyFactory.getInstance("DH");
  184. DHPublicKeySpec keySpec = new DHPublicKeySpec(e, p, g);
  185. PublicKey yourPubKey = myKeyFac.generatePublic(keySpec);
  186. myKeyAgree.doPhase(yourPubKey, true);
  187. byte[] mySharedSecret = myKeyAgree.generateSecret();
  188. k = mySharedSecret;
  189. } catch (Exception e) {
  190. e.printStackTrace();
  191. }
  192. }
  193. /**
  194. * Generates the Host Key based on the DSA algorithm
  195. */
  196. private void generateHostKey() {
  197. try {
  198. KeyPairGenerator generator = KeyPairGenerator.getInstance("DSA");
  199. dsa = generator.generateKeyPair();
  200. byte[] string = "ssh-dss".getBytes();
  201. byte[] stringLength = ByteBuffer.allocate(4).putInt(string.length).array();
  202. byte[] p = ((DSAPublicKey) dsa.getPublic()).getParams().getP().toByteArray();
  203. if(p[0] != 0x00) p = HelperUtils.concat(new byte[]{0x00}, p);
  204. byte[] pLength = ByteBuffer.allocate(4).putInt(p.length).array();
  205. byte[] q = ((DSAPublicKey) dsa.getPublic()).getParams().getQ().toByteArray();
  206. if(q[0] != 0x00) q = HelperUtils.concat(new byte[]{0x00}, q);
  207. byte[] qLength = ByteBuffer.allocate(4).putInt(q.length).array();
  208. byte[] g = ((DSAPublicKey) dsa.getPublic()).getParams().getG().toByteArray();
  209. if(g[0] != 0x00) g = HelperUtils.concat(new byte[]{0x00}, g);
  210. byte[] gLength = ByteBuffer.allocate(4).putInt(g.length).array();
  211. byte[] y = ((DSAPublicKey) dsa.getPublic()).getY().toByteArray();
  212. if(y[0] != 0x00) y = HelperUtils.concat(new byte[]{0x00}, y);
  213. byte[] yLength = ByteBuffer.allocate(4).putInt(y.length).array();
  214. K_S = HelperUtils.concat(stringLength, string, pLength, p, qLength, q, gLength, g, yLength, y);
  215. } catch (Exception e) {
  216. e.printStackTrace();
  217. }
  218. }
  219. /**
  220. * Generates the SHA-1 Hash from several values
  221. */
  222. private void generateSha1Hash() {
  223. try {
  224. MessageDigest sha = MessageDigest.getInstance("SHA-1");
  225. sha.update(V_C);
  226. sha.update(V_S);
  227. sha.update(I_C);
  228. sha.update(I_S);
  229. sha.update(K_S);
  230. sha.update(e);
  231. sha.update(f);
  232. sha.update(k);
  233. h = sha.digest();
  234. } catch (Exception e) {
  235. e.printStackTrace();
  236. }
  237. }
  238. /**
  239. * Generates the signature of the hash using DSA algorithm with SHA-1
  240. */
  241. private void generateSignature() {
  242. //FIXME something is wrong with this signature.. maybe one of the used components is generated wrong?!
  243. try {
  244. Signature sig = Signature.getInstance("SHA1withDSA");
  245. sig.initVerify(dsa.getPublic());
  246. sig.initSign(dsa.getPrivate());
  247. sig.update(h);
  248. this.sig = extractSignature(sig.sign());
  249. } catch (Exception e) {
  250. e.printStackTrace();
  251. }
  252. }
  253. /**
  254. * Extracts the type of the client
  255. * @param request containing the clients type
  256. */
  257. private void extractType(byte[] request) {
  258. int length = 0;
  259. for(int i = 8; i < request.length; i++, length++) { //start at 8 because "SSH-2.0-" is not part of type
  260. if(request[i] == 0x0d) break; //find the end of the type: '\r'
  261. }
  262. V_C = new byte[length];
  263. System.arraycopy(request, 8, V_C, 0, length);
  264. }
  265. /**
  266. * Extracts the cookie from the Kex Init client request
  267. * @param request containing the clients cookie
  268. */
  269. private void extractCookie(byte[] request) {
  270. int pos = 0;
  271. if(request[5] != 0x14) { //if type packet is in front of kex init
  272. pos = 1; //start behind the end of type message
  273. for(int i = 0; i < request.length; i++, pos++) {
  274. if(request[i] == 0x0a) break; //find end of type message: '\n'
  275. }
  276. }
  277. I_C = new byte[16];
  278. System.arraycopy(request, 6+pos, I_C, 0, 16); //srcLen: headersize+position after type packet
  279. }
  280. /**
  281. * Extracts the public key from the DH Kex Request
  282. * @param request containing the clients public key
  283. */
  284. private void extractPubKey(byte[] request) {
  285. e = new byte[byteToInt(new byte[] {request[6], request[7], request[8], request[9]})];
  286. for(int i = 0; i < e.length; i++) {
  287. e[i] = request[i+10];
  288. }
  289. }
  290. /**
  291. * Converts a byte[] to int
  292. * @param bytes that are converted
  293. * @return converted byte[] as int
  294. */
  295. private static int byteToInt(byte[] bytes) {
  296. int ret = 0;
  297. for (int i=0; i < bytes.length; i++) {
  298. ret <<= 8;
  299. ret |= bytes[i] & 0xFF;
  300. }
  301. return ret;
  302. }
  303. /**
  304. * Generates a random byte[] of a specified size
  305. * @param size of the byte[]
  306. * @return random byte[]
  307. */
  308. private byte[] randomBytes(int size) {
  309. byte[] bytes = new byte[size];
  310. SecureRandom rdm = new SecureRandom();
  311. rdm.nextBytes(bytes);
  312. return bytes;
  313. }
  314. /**
  315. * Extracts r and s from a DSA-signature
  316. * @param signature
  317. * @return r and s as byte[]
  318. */
  319. private byte[] extractSignature(byte[] signature) {
  320. int length = 0;
  321. int index = 3;
  322. length = signature[index++] & 0xff;
  323. byte[] r = new byte[length];
  324. System.arraycopy(signature, index, r, 0, r.length);
  325. index = index + length + 1;
  326. length = signature[index++] & 0xff;
  327. byte[] s = new byte[length];
  328. System.arraycopy(signature, index, s, 0, s.length);
  329. byte[] result = new byte[40];
  330. // result must be 40 bytes, but r and s may be longer than 20
  331. System.arraycopy(r,
  332. (r.length > 20) ? 1 : 0,
  333. result,
  334. (r.length > 20) ? 0 : 20 - r.length,
  335. (r.length > 20) ? 20 : r.length);
  336. System.arraycopy(s,
  337. (s.length > 20) ? 1 : 0,
  338. result,
  339. (s.length > 20) ? 20 : 40 - s.length,
  340. (s.length > 20) ? 20 : s.length);
  341. return result;
  342. }
  343. private String serverVersion = ProtocolSettings.getSshVersion();
  344. private String serverType = ProtocolSettings.getSshType();
  345. //Diffie-Hellman-Group-1 p and g
  346. private final byte[] p = {
  347. (byte)0x00,
  348. (byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,
  349. (byte)0xC9,(byte)0x0F,(byte)0xDA,(byte)0xA2,(byte)0x21,(byte)0x68,(byte)0xC2,(byte)0x34,
  350. (byte)0xC4,(byte)0xC6,(byte)0x62,(byte)0x8B,(byte)0x80,(byte)0xDC,(byte)0x1C,(byte)0xD1,
  351. (byte)0x29,(byte)0x02,(byte)0x4E,(byte)0x08,(byte)0x8A,(byte)0x67,(byte)0xCC,(byte)0x74,
  352. (byte)0x02,(byte)0x0B,(byte)0xBE,(byte)0xA6,(byte)0x3B,(byte)0x13,(byte)0x9B,(byte)0x22,
  353. (byte)0x51,(byte)0x4A,(byte)0x08,(byte)0x79,(byte)0x8E,(byte)0x34,(byte)0x04,(byte)0xDD,
  354. (byte)0xEF,(byte)0x95,(byte)0x19,(byte)0xB3,(byte)0xCD,(byte)0x3A,(byte)0x43,(byte)0x1B,
  355. (byte)0x30,(byte)0x2B,(byte)0x0A,(byte)0x6D,(byte)0xF2,(byte)0x5F,(byte)0x14,(byte)0x37,
  356. (byte)0x4F,(byte)0xE1,(byte)0x35,(byte)0x6D,(byte)0x6D,(byte)0x51,(byte)0xC2,(byte)0x45,
  357. (byte)0xE4,(byte)0x85,(byte)0xB5,(byte)0x76,(byte)0x62,(byte)0x5E,(byte)0x7E,(byte)0xC6,
  358. (byte)0xF4,(byte)0x4C,(byte)0x42,(byte)0xE9,(byte)0xA6,(byte)0x37,(byte)0xED,(byte)0x6B,
  359. (byte)0x0B,(byte)0xFF,(byte)0x5C,(byte)0xB6,(byte)0xF4,(byte)0x06,(byte)0xB7,(byte)0xED,
  360. (byte)0xEE,(byte)0x38,(byte)0x6B,(byte)0xFB,(byte)0x5A,(byte)0x89,(byte)0x9F,(byte)0xA5,
  361. (byte)0xAE,(byte)0x9F,(byte)0x24,(byte)0x11,(byte)0x7C,(byte)0x4B,(byte)0x1F,(byte)0xE6,
  362. (byte)0x49,(byte)0x28,(byte)0x66,(byte)0x51,(byte)0xEC,(byte)0xE6,(byte)0x53,(byte)0x81,
  363. (byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF
  364. };
  365. private final byte[] g = {0x02};
  366. //SSH Parameters for Kex etc.
  367. private byte[] V_S = serverType.getBytes();
  368. private byte[] V_C;
  369. private byte[] I_S;
  370. private byte[] I_C;
  371. private byte[] e;
  372. private byte[] f;
  373. private byte[] k;
  374. private byte[] h;
  375. private byte[] K_S;
  376. private byte[] sig;
  377. //Keys for signature
  378. private KeyPair dsa;
  379. //allowed algorithms for kexinit
  380. private String kex_alg = "diffie-hellman-group1-sha1";
  381. private String server_alg = "ssh-dss";
  382. private String encrypt_alg_c = "aes128-ctr";
  383. private String encrypt_alg_s = "aes128-ctr";
  384. private String mac_alg_c = "hmac-sha1";
  385. private String mac_alg_s = "hmac-sha1";
  386. private String comp_alg_c = "none";
  387. private String comp_alg_s = "none";
  388. private int cipherBlockSize = 16;
  389. }