diff --git a/src/ssm.js b/src/ssm.js index 3f94821..88ca5e5 100644 --- a/src/ssm.js +++ b/src/ssm.js @@ -1,5 +1,7 @@ import sha256 from "./sha256.js"; +const HANDSHAKE_RESPONSE = 6; +const CHALLENGE_RESPONSE = 9; const ACK_TYPE = 3; const INPUT_TYPE = 1; //const OUTPUT_TYPE = 0; @@ -224,10 +226,10 @@ function uuidv4() { var ssm = { decode: function (buffer) { /* - var buf = new Uint8Array([ - 0x00,0x00,0x00,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x69,0x6e,0x70,0x75,0x74,0x5f,0x73,0x74,0x72,0x65,0x61,0x6d,0x5f,0x64,0x61,0x74,0x61,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x1e,0x5b,0x37,0xe2,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x73,0x0f,0x66,0x49,0x14,0x53,0x4e,0x2a,0x9f,0x01,0xa3,0x7d,0xbf,0xfe,0xfc,0xe1,0x65,0x34,0x34,0x33,0x36,0x35,0x62,0x39,0x35,0x31,0x31,0x36,0x32,0x65,0x64,0x61,0x36,0x34,0x61,0x63,0x65,0x66,0x64,0x32,0x37,0x35,0x37,0x32,0x32,0x30,0x39,0x65,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x16,0x7b,0x22,0x63,0x6f,0x6c,0x73,0x22,0x3a,0x32,0x30,0x32,0x2c,0x22,0x72,0x6f,0x77,0x73,0x22,0x3a,0x31,0x38,0x7d]); - console.log(buf); - */ + var buf = new Uint8Array([ + 0x00,0x00,0x00,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x69,0x6e,0x70,0x75,0x74,0x5f,0x73,0x74,0x72,0x65,0x61,0x6d,0x5f,0x64,0x61,0x74,0x61,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x1e,0x5b,0x37,0xe2,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x73,0x0f,0x66,0x49,0x14,0x53,0x4e,0x2a,0x9f,0x01,0xa3,0x7d,0xbf,0xfe,0xfc,0xe1,0x65,0x34,0x34,0x33,0x36,0x35,0x62,0x39,0x35,0x31,0x31,0x36,0x32,0x65,0x64,0x61,0x36,0x34,0x61,0x63,0x65,0x66,0x64,0x32,0x37,0x35,0x37,0x32,0x32,0x30,0x39,0x65,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x16,0x7b,0x22,0x63,0x6f,0x6c,0x73,0x22,0x3a,0x32,0x30,0x32,0x2c,0x22,0x72,0x6f,0x77,0x73,0x22,0x3a,0x31,0x38,0x7d]); + console.log(buf); + */ var buf = new Uint8Array(buffer); var agentMessage = { headerLength: getInt(buf.slice(0, 4)), //4 @@ -259,6 +261,18 @@ var ssm = { sendInitMessage: function (connection, termOptions) { connection.send(ssm.buildInitMessage(termOptions)); }, + + sendHandshakeResponse: function (connection, reqClientActions, kmsCipherText) { + const handshakeResp = ssm.buildHandshakeResponse(reqClientActions, kmsCipherText) + connection.send(handshakeResp); + }, + + sendChallengeResponse: function (connection, challengePayload) { + console.log(challengePayload) + const challengeResponse = ssm.buildChallengeResponse(challengePayload) + connection.send(challengeResponse); + }, + buildTokenMessage: function (token) { return JSON.stringify({ MessageSchemaVersion: "1.0", @@ -267,6 +281,54 @@ var ssm = { }); }, + buildHandshakeResponse: function (reqClientActions, kmsCipherText) { + let handshakePayload = { + ClientVersion: "1.2.312.0", // latest when this was written + ProcessedClientActions: [], + Errors: [], + } + for (let action of reqClientActions) { + let processedAction = { + ActionType: action.ActionType, + } + if (action.ActionType == "KMSEncryption") { + if (kmsCipherText) { + processedAction.ActionStatus = 1 + processedAction.ActionResult = { + KMSCipherTextKey: Array.from(kmsCipherText), + } + } + else processedAction.ActionStatus = 2 + } + else if (action.ActionType == "SessionType") { + processedAction.ActionStatus = 1 + } + else processedAction.ActionStatus = 3 + handshakePayload.ProcessedClientActions.push(processedAction) + } + messageSequenceNumber++; + var handshakeResp = buildAgentMessage( + JSON.stringify(handshakePayload), + "input_stream_data", + messageSequenceNumber, + HANDSHAKE_RESPONSE, + 1 + ); + return agentMessageToBuffer(handshakeResp); + }, + + buildChallengeResponse: function (challengePayload) { + messageSequenceNumber++; + var challengeResp = buildAgentMessage( + challengePayload, + "input_stream_data", + messageSequenceNumber, + CHALLENGE_RESPONSE, + 1 + ); + return agentMessageToBuffer(challengeResp); + }, + buildInitMessage: function (options) { var payload = { cols: options.cols,