amt-redir-ws-0.1.0.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * @description Intel AMT Redirection Transport Module - using websocket relay
  3. * @author Ylian Saint-Hilaire
  4. * @version v2.0.0
  5. */
  6. // Construct a MeshServer object
  7. var CreateAmtRedirect = function (module, authCookie) {
  8. var obj = {};
  9. obj.m = module; // This is the inner module (Terminal or Desktop)
  10. module.parent = obj;
  11. obj.authCookie = authCookie;
  12. obj.State = 0;
  13. obj.socket = null;
  14. // ###BEGIN###{!Mode-Firmware}
  15. obj.host = null;
  16. obj.port = 0;
  17. obj.user = null;
  18. obj.pass = null;
  19. obj.authuri = '/RedirectionService';
  20. obj.tlsv1only = 0;
  21. obj.inDataCount = 0;
  22. // ###END###{!Mode-Firmware}
  23. obj.connectstate = 0;
  24. obj.protocol = module.protocol; // 1 = SOL, 2 = KVM, 3 = IDER
  25. obj.acc = null;
  26. obj.amtsequence = 1;
  27. obj.amtkeepalivetimer = null;
  28. obj.onStateChanged = null;
  29. function arrToStr(arr) { return String.fromCharCode.apply(null, arr); }
  30. function randomHex(length) { var r = ''; for (var i = 0; i < length; i++) { r += 'abcdef0123456789'.charAt(Math.floor(Math.random() * 16)); } return r; }
  31. obj.Start = function (host, port, user, pass, tls) {
  32. obj.host = host;
  33. obj.port = port;
  34. obj.user = user;
  35. obj.pass = pass;
  36. obj.tls = tls;
  37. obj.connectstate = 0;
  38. obj.inDataCount = 0;
  39. var url = window.location.protocol.replace('http', 'ws') + '//' + window.location.host + window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')) + '/webrelay.ashx?p=2&host=' + host + '&port=' + port + '&tls=' + tls + ((user == '*') ? '&serverauth=1' : '') + ((typeof pass === 'undefined') ? ('&serverauth=1&user=' + user) : ''); // The 'p=2' indicates to the relay that this is a REDIRECTION session
  40. if ((authCookie != null) && (authCookie != '')) { url += '&auth=' + authCookie; }
  41. obj.socket = new WebSocket(url);
  42. obj.socket.binaryType = 'arraybuffer';
  43. obj.socket.onopen = obj.xxOnSocketConnected;
  44. obj.socket.onmessage = obj.xxOnMessage;
  45. obj.socket.onclose = obj.xxOnSocketClosed;
  46. obj.xxStateChange(1);
  47. }
  48. obj.xxOnSocketConnected = function () {
  49. obj.xxStateChange(2);
  50. if (obj.protocol == 1) obj.directSend(new Uint8Array([0x10, 0x00, 0x00, 0x00, 0x53, 0x4F, 0x4C, 0x20])); // SOL
  51. if (obj.protocol == 2) obj.directSend(new Uint8Array([0x10, 0x01, 0x00, 0x00, 0x4b, 0x56, 0x4d, 0x52])); // KVM
  52. if (obj.protocol == 3) obj.directSend(new Uint8Array([0x10, 0x00, 0x00, 0x00, 0x49, 0x44, 0x45, 0x52])); // IDER
  53. }
  54. obj.xxOnMessage = function (e) {
  55. if (!e.data || obj.connectstate == -1) return;
  56. obj.inDataCount++;
  57. // KVM traffic, forward it directly.
  58. if ((obj.connectstate == 1) && ((obj.protocol == 2) || (obj.protocol == 3))) {
  59. return obj.m.ProcessBinaryData ? obj.m.ProcessBinaryData(e.data) : obj.m.ProcessData(arrToStr(e.data));
  60. }
  61. // Append to accumulator
  62. if (obj.acc == null) {
  63. obj.acc = e.data;
  64. } else {
  65. var tmp = new Uint8Array(obj.acc.byteLength + e.data.byteLength);
  66. tmp.set(new Uint8Array(obj.acc), 0);
  67. tmp.set(new Uint8Array(e.data), obj.acc.byteLength);
  68. obj.acc = tmp.buffer;
  69. }
  70. //console.log('Redir Recv', obj.acc);
  71. while ((obj.acc != null) && (obj.acc.byteLength >= 1)) {
  72. var cmdsize = 0, accArray = new Uint8Array(obj.acc);
  73. switch (accArray[0]) {
  74. case 0x11: // StartRedirectionSessionReply (17)
  75. if (accArray.byteLength < 4) return;
  76. var statuscode = accArray[1];
  77. switch (statuscode) {
  78. case 0: // STATUS_SUCCESS
  79. if (accArray.byteLength < 13) return;
  80. var oemlen = accArray[12];
  81. if (accArray.byteLength < 13 + oemlen) return;
  82. obj.directSend(new Uint8Array([0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])); // Query for available authentication
  83. cmdsize = (13 + oemlen);
  84. break;
  85. default:
  86. obj.Stop(1);
  87. break;
  88. }
  89. break;
  90. case 0x14: // AuthenticateSessionReply (20)
  91. if (accArray.byteLength < 9) return;
  92. var authDataLen = new DataView(obj.acc).getUint32(5, true);
  93. if (accArray.byteLength < 9 + authDataLen) return;
  94. var status = accArray[1], authType = accArray[4], authData = [];
  95. for (i = 0; i < authDataLen; i++) { authData.push(accArray[9 + i]); }
  96. var authDataBuf = new Uint8Array(obj.acc.slice(9, 9 + authDataLen));
  97. cmdsize = 9 + authDataLen;
  98. if (authType == 0) {
  99. // Query
  100. if (authData.indexOf(4) >= 0) {
  101. // Good Digest Auth (With cnonce and all)
  102. obj.xxSend(String.fromCharCode(0x13, 0x00, 0x00, 0x00, 0x04) + IntToStrX(obj.user.length + obj.authuri.length + 8) + String.fromCharCode(obj.user.length) + obj.user + String.fromCharCode(0x00, 0x00) + String.fromCharCode(obj.authuri.length) + obj.authuri + String.fromCharCode(0x00, 0x00, 0x00, 0x00));
  103. }
  104. /*
  105. else if (authData.indexOf(3) >= 0) {
  106. // Bad Digest Auth (Not sure why this is supported, cnonce is not used!)
  107. obj.xxSend(String.fromCharCode(0x13, 0x00, 0x00, 0x00, 0x03) + IntToStrX(obj.user.length + obj.authuri.length + 7) + String.fromCharCode(obj.user.length) + obj.user + String.fromCharCode(0x00, 0x00) + String.fromCharCode(obj.authuri.length) + obj.authuri + String.fromCharCode(0x00, 0x00, 0x00));
  108. }
  109. else if (authData.indexOf(1) >= 0) {
  110. // Basic Auth (Probably a good idea to not support this unless this is an old version of Intel AMT)
  111. obj.xxSend(String.fromCharCode(0x13, 0x00, 0x00, 0x00, 0x01) + IntToStrX(obj.user.length + obj.pass.length + 2) + String.fromCharCode(obj.user.length) + obj.user + String.fromCharCode(obj.pass.length) + obj.pass);
  112. }
  113. */
  114. else obj.Stop(2);
  115. } else if (((authType == 3) || (authType == 4)) && (status == 1)) {
  116. var curptr = 0;
  117. // Realm
  118. var realmlen = authDataBuf[curptr];
  119. var realm = arrToStr(new Uint8Array(authDataBuf.buffer.slice(curptr + 1, curptr + 1 + realmlen)));
  120. curptr += (realmlen + 1);
  121. // Nonce
  122. var noncelen = authDataBuf[curptr];
  123. var nonce = arrToStr(new Uint8Array(authDataBuf.buffer.slice(curptr + 1, curptr + 1 + noncelen)));
  124. curptr += (noncelen + 1);
  125. // QOP
  126. var qoplen = 0;
  127. var qop = null;
  128. var cnonce = randomHex(32);
  129. var snc = '00000002';
  130. var extra = '';
  131. if (authType == 4) {
  132. qoplen = authDataBuf[curptr];
  133. qop = arrToStr(new Uint8Array(authDataBuf.buffer.slice(curptr + 1, curptr + 1 + qoplen)));
  134. curptr += (qoplen + 1);
  135. extra = snc + ':' + cnonce + ':' + qop + ':';
  136. }
  137. var digest = hex_md5(hex_md5(obj.user + ':' + realm + ':' + obj.pass) + ':' + nonce + ':' + extra + hex_md5('POST:' + obj.authuri));
  138. var totallen = obj.user.length + realm.length + nonce.length + obj.authuri.length + cnonce.length + snc.length + digest.length + 7;
  139. if (authType == 4) totallen += (qop.length + 1);
  140. var buf = String.fromCharCode(0x13, 0x00, 0x00, 0x00, authType) + IntToStrX(totallen) + String.fromCharCode(obj.user.length) + obj.user + String.fromCharCode(realm.length) + realm + String.fromCharCode(nonce.length) + nonce + String.fromCharCode(obj.authuri.length) + obj.authuri + String.fromCharCode(cnonce.length) + cnonce + String.fromCharCode(snc.length) + snc + String.fromCharCode(digest.length) + digest;
  141. if (authType == 4) buf += (String.fromCharCode(qop.length) + qop);
  142. obj.xxSend(buf);
  143. } else if (status == 0) { // Success
  144. switch (obj.protocol) {
  145. case 1: {
  146. // Serial-over-LAN: Send Intel AMT serial settings...
  147. var MaxTxBuffer = 10000;
  148. var TxTimeout = 100;
  149. var TxOverflowTimeout = 0;
  150. var RxTimeout = 10000;
  151. var RxFlushTimeout = 100;
  152. var Heartbeat = 0;//5000;
  153. obj.xxSend(String.fromCharCode(0x20, 0x00, 0x00, 0x00) + IntToStrX(obj.amtsequence++) + ShortToStrX(MaxTxBuffer) + ShortToStrX(TxTimeout) + ShortToStrX(TxOverflowTimeout) + ShortToStrX(RxTimeout) + ShortToStrX(RxFlushTimeout) + ShortToStrX(Heartbeat) + IntToStrX(0));
  154. break;
  155. }
  156. case 2: {
  157. // Remote Desktop: Send traffic directly...
  158. obj.directSend(new Uint8Array([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]));
  159. break;
  160. }
  161. case 3: {
  162. // Remote IDER: Send traffic directly...
  163. obj.connectstate = 1;
  164. obj.xxStateChange(3);
  165. break;
  166. }
  167. }
  168. } else obj.Stop(3);
  169. break;
  170. case 0x21: // Response to settings (33)
  171. if (accArray.byteLength < 23) break;
  172. cmdsize = 23;
  173. obj.xxSend(String.fromCharCode(0x27, 0x00, 0x00, 0x00) + IntToStrX(obj.amtsequence++) + String.fromCharCode(0x00, 0x00, 0x1B, 0x00, 0x00, 0x00));
  174. if (obj.protocol == 1) { obj.amtkeepalivetimer = setInterval(obj.xxSendAmtKeepAlive, 2000); }
  175. obj.connectstate = 1;
  176. obj.xxStateChange(3);
  177. break;
  178. case 0x29: // Serial Settings (41)
  179. if (accArray.byteLength < 10) break;
  180. cmdsize = 10;
  181. break;
  182. case 0x2A: // Incoming display data (42)
  183. if (accArray.byteLength < 10) break;
  184. var cs = (10 + (accArray[9] << 8) + accArray[8]);
  185. if (accArray.byteLength < cs) break;
  186. if (obj.m.ProcessBinaryData) { obj.m.ProcessBinaryData(new Uint8Array(accArray.buffer.slice(10, cs))); } else { obj.m.ProcessData(arrToStr(new Uint8Array(accArray.buffer.slice(10, cs)))); }
  187. cmdsize = cs;
  188. break;
  189. case 0x2B: // Keep alive message (43)
  190. if (accArray.byteLength < 8) break;
  191. cmdsize = 8;
  192. break;
  193. case 0x41:
  194. if (accArray.byteLength < 8) break;
  195. obj.connectstate = 1;
  196. obj.m.Start();
  197. // KVM traffic, forward rest of accumulator directly.
  198. if (accArray.byteLength > 8) {
  199. if (obj.m.ProcessBinaryData) { obj.m.ProcessBinaryData(new Uint8Array(accArray.buffer.slice(8))); } else { obj.m.ProcessData(arrToStr(new Uint8Array(accArray.buffer.slice(8)))); }
  200. }
  201. cmdsize = accArray.byteLength;
  202. break;
  203. case 0xF0:
  204. // console.log('Session is being recorded');
  205. obj.serverIsRecording = true;
  206. cmdsize = 1;
  207. break;
  208. default:
  209. console.log('Unknown Intel AMT command: ' + accArray[0] + ' acclen=' + accArray.byteLength);
  210. obj.Stop(4);
  211. return;
  212. }
  213. if (cmdsize == 0) return;
  214. if (cmdsize != obj.acc.byteLength) { obj.acc = obj.acc.slice(cmdsize); } else { obj.acc = null; }
  215. }
  216. }
  217. obj.directSend = function (arr) { try { obj.socket.send(arr.buffer); } catch (ex) { } }
  218. obj.xxSend = function (x) {
  219. if ((obj.socket != null) && (obj.socket.readyState == WebSocket.OPEN)) {
  220. var b = new Uint8Array(x.length);
  221. for (var i = 0; i < x.length; ++i) { b[i] = x.charCodeAt(i); }
  222. try { obj.socket.send(b.buffer); } catch (ex) { }
  223. }
  224. }
  225. obj.Send = obj.send = function (x) {
  226. if (obj.socket == null || obj.connectstate != 1) return;
  227. if (obj.protocol == 1) { obj.xxSend(String.fromCharCode(0x28, 0x00, 0x00, 0x00) + IntToStrX(obj.amtsequence++) + ShortToStrX(x.length) + x); } else { obj.xxSend(x); }
  228. }
  229. obj.xxSendAmtKeepAlive = function () { if (obj.socket != null) { obj.xxSend(String.fromCharCode(0x2B, 0x00, 0x00, 0x00) + IntToStrX(obj.amtsequence++)); } }
  230. obj.xxOnSocketClosed = function () {
  231. if ((obj.inDataCount == 0) && (obj.tlsv1only == 0)) {
  232. obj.tlsv1only = 1;
  233. obj.socket = new WebSocket(window.location.protocol.replace('http', 'ws') + '//' + window.location.host + window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')) + '/webrelay.ashx?p=2&host=' + obj.host + '&port=' + obj.port + '&tls=' + obj.tls + '&tls1only=1' + ((obj.user == '*') ? '&serverauth=1' : '') + ((typeof pass === 'undefined') ? ('&serverauth=1&user=' + obj.user) : '')); // The 'p=2' indicates to the relay that this is a REDIRECTION session
  234. obj.socket.binaryType = 'arraybuffer';
  235. obj.socket.onopen = obj.xxOnSocketConnected;
  236. obj.socket.onmessage = obj.xxOnMessage;
  237. obj.socket.onclose = obj.xxOnSocketClosed;
  238. } else {
  239. obj.Stop(5);
  240. }
  241. }
  242. obj.xxStateChange = function (newstate) {
  243. if (obj.State == newstate) return;
  244. obj.State = newstate;
  245. obj.m.xxStateChange(obj.State);
  246. if (obj.onStateChanged != null) obj.onStateChanged(obj, obj.State);
  247. }
  248. obj.Stop = function (x) {
  249. obj.xxStateChange(0);
  250. obj.connectstate = -1;
  251. obj.acc = null;
  252. if (obj.socket != null) { obj.socket.close(); obj.socket = null; }
  253. if (obj.amtkeepalivetimer != null) { clearInterval(obj.amtkeepalivetimer); obj.amtkeepalivetimer = null; }
  254. }
  255. return obj;
  256. }