amt-redir-duk.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * @description Intel AMT Redirection Transport Module - using Node
  3. * @author Ylian Saint-Hilaire
  4. * @version v0.0.1f
  5. */
  6. // Construct a MeshServer object
  7. module.exports = function CreateAmtRedirect(module) {
  8. var obj = {};
  9. obj.m = module; // This is the inner module (Terminal or Desktop)
  10. module.parent = obj;
  11. obj.State = 0;
  12. obj.net = require('net');
  13. obj.tls = require('tls');
  14. obj.socket = null;
  15. obj.host = null;
  16. obj.port = 0;
  17. obj.user = null;
  18. obj.pass = null;
  19. obj.connectstate = 0;
  20. obj.protocol = module.protocol; // 1 = SOL, 2 = KVM, 3 = IDER
  21. obj.xtlsoptions = null;
  22. obj.amtaccumulator = Buffer.alloc(0);
  23. obj.amtsequence = 1;
  24. obj.amtkeepalivetimer = null;
  25. obj.authuri = '/RedirectionService';
  26. obj.digestRealmMatch = null;
  27. obj.onStateChanged = null;
  28. // Private method
  29. obj.Debug = function (msg) { console.log(msg); }
  30. var urlvars = null;
  31. obj.Start = function (host, port, user, pass, tls, tlsFingerprint, tlsoptions) {
  32. obj.host = host;
  33. obj.port = port;
  34. obj.user = user;
  35. obj.pass = pass;
  36. obj.xtls = tls;
  37. obj.xtlsoptions = tlsoptions;
  38. obj.xtlsFingerprint = tlsFingerprint;
  39. obj.connectstate = 0;
  40. if (tls == true) {
  41. obj.socket = obj.tls.connect({ host: host, port: port, rejectUnauthorized: false, checkServerIdentity: obj.onCheckServerIdentity }, obj.xxOnSocketConnected);
  42. } else {
  43. obj.socket = obj.net.createConnection({ host: host, port: port }, obj.xxOnSocketConnected);
  44. }
  45. obj.socket.on('data', obj.xxOnSocketData);
  46. obj.socket.on('close', obj.xxOnSocketClosed);
  47. obj.socket.on('error', obj.xxOnSocketClosed);
  48. obj.xxStateChange(1);
  49. }
  50. // Get the certificate of Intel AMT
  51. //obj.getPeerCertificate = function () { if (obj.xtls == true) { return obj.socket.getPeerCertificate(); } return null; }
  52. obj.onCheckServerIdentity = function (cert) {
  53. var f = cert[0].fingerprint.split(':').join('').toLowerCase();
  54. if ((obj.xtlsFingerprint != null) && (obj.xtlsFingerprint != f)) {
  55. console.log('Invalid TLS Cert, SHA384: ' + f);
  56. process.exit(2);
  57. return;
  58. } else {
  59. if (obj.xtlsFingerprint == null) {
  60. obj.xtlsFingerprint = f;
  61. console.log('TLS Cert SHA384: ' + f);
  62. }
  63. }
  64. }
  65. obj.xxOnSocketConnected = function () {
  66. if (obj.socket == null) return;
  67. /*
  68. if (obj.xtls == true) {
  69. obj.xtlsCertificate = obj.socket.getPeerCertificate();
  70. if ((obj.xtlsFingerprint != 0) && (obj.xtlsCertificate.fingerprint.split(':').join('').toLowerCase() != obj.xtlsFingerprint)) { obj.Stop(); return; }
  71. }
  72. */
  73. if (urlvars && urlvars['redirtrace']) { console.log('REDIR-CONNECTED'); }
  74. //obj.Debug("Socket Connected");
  75. obj.xxStateChange(2);
  76. if (obj.protocol == 1) obj.xxSend(obj.RedirectStartSol); // TODO: Put these strings in higher level module to tighten code
  77. else if (obj.protocol == 2) obj.xxSend(obj.RedirectStartKvm); // Don't need these is the feature if not compiled-in.
  78. else if (obj.protocol == 3) obj.xxSend(obj.RedirectStartIder);
  79. }
  80. obj.xxOnSocketData = function (data) {
  81. if (!data || obj.connectstate == -1) return;
  82. if (urlvars && urlvars['redirtrace']) { console.log('REDIR-RECV(' + data.length + '): ' + data.toString('hex')); }
  83. //obj.Debug("Recv(" + data.length + "): " + rstr2hex(data));
  84. if ((obj.protocol == 2 || obj.protocol == 3) && obj.connectstate == 1) { return obj.m.ProcessData(data); } // KVM or IDER traffic, forward it directly.
  85. obj.amtaccumulator = Buffer.concat([obj.amtaccumulator, data]);
  86. //obj.Debug("Recv(" + obj.amtaccumulator.length + "): " + obj.amtaccumulator.toString('hex'));
  87. while (obj.amtaccumulator.length > 0) {
  88. var cmdsize = 0;
  89. //console.log('CMD: ' + obj.amtaccumulator[0]);
  90. switch (obj.amtaccumulator[0]) {
  91. case 0x11: // StartRedirectionSessionReply (17)
  92. if (obj.amtaccumulator.length < 4) return;
  93. var statuscode = obj.amtaccumulator[1];
  94. switch (statuscode) {
  95. case 0: // STATUS_SUCCESS
  96. if (obj.amtaccumulator.length < 13) return;
  97. var oemlen = obj.amtaccumulator[12];
  98. if (obj.amtaccumulator.length < 13 + oemlen) return;
  99. obj.xxSend(String.fromCharCode(0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)); // Query authentication support
  100. cmdsize = (13 + oemlen);
  101. break;
  102. default:
  103. obj.Stop();
  104. break;
  105. }
  106. break;
  107. case 0x14: // AuthenticateSessionReply (20)
  108. if (obj.amtaccumulator.length < 9) return;
  109. var authDataLen = obj.amtaccumulator.readInt32LE(5);
  110. if (obj.amtaccumulator.length < 9 + authDataLen) return;
  111. var status = obj.amtaccumulator[1];
  112. var authType = obj.amtaccumulator[4];
  113. var authData = [];
  114. for (i = 0; i < authDataLen; i++) { authData.push(obj.amtaccumulator[9 + i]); }
  115. var authDataBuf = obj.amtaccumulator.slice(9, 9 + authDataLen);
  116. cmdsize = 9 + authDataLen;
  117. if (authType == 0) {
  118. // Query
  119. if (authData.indexOf(4) >= 0) {
  120. // Good Digest Auth (With cnonce and all)
  121. 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));
  122. }
  123. /*
  124. else if (authData.indexOf(3) >= 0) {
  125. // Bad Digest Auth (Not sure why this is supported, cnonce is not used!)
  126. 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));
  127. }
  128. else if (authData.indexOf(1) >= 0) {
  129. // Basic Auth (Probably a good idea to not support this unless this is an old version of Intel AMT)
  130. 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);
  131. }
  132. */
  133. else obj.Stop();
  134. }
  135. else if ((authType == 3 || authType == 4) && status == 1) {
  136. var curptr = 0;
  137. // Realm
  138. var realmlen = authDataBuf[curptr];
  139. var realm = authDataBuf.slice(curptr + 1, curptr + 1 + realmlen).toString();
  140. curptr += (realmlen + 1);
  141. // Check the digest realm. If it does not match, close the connection.
  142. if (obj.digestRealmMatch && (obj.digestRealmMatch != realm)) { obj.Stop(); return; }
  143. // Nonce
  144. var noncelen = authDataBuf[curptr];
  145. var nonce = authDataBuf.slice(curptr + 1, curptr + 1 + noncelen).toString();
  146. curptr += (noncelen + 1);
  147. // QOP
  148. var qoplen = 0;
  149. var qop = null;
  150. var cnonce = obj.xxRandomValueHex(32);
  151. var snc = '00000002';
  152. var extra = '';
  153. if (authType == 4) {
  154. qoplen = authDataBuf[curptr];
  155. qop = authDataBuf.slice(curptr + 1, curptr + 1 + qoplen).toString();
  156. curptr += (qoplen + 1);
  157. extra = snc + ':' + cnonce + ':' + qop + ':';
  158. }
  159. var digest = hex_md5(hex_md5(obj.user + ':' + realm + ':' + obj.pass) + ':' + nonce + ':' + extra + hex_md5('POST:' + obj.authuri));
  160. var totallen = obj.user.length + realm.length + nonce.length + obj.authuri.length + cnonce.length + snc.length + digest.length + 7;
  161. if (authType == 4) totallen += (qop.length + 1);
  162. var buf = Buffer.concat([new Buffer([0x13, 0x00, 0x00, 0x00, authType]), new Buffer([totallen & 0xFF, (totallen >> 8) & 0xFF, 0x00, 0x00]), new Buffer([obj.user.length]), new Buffer(obj.user), new Buffer([realm.length]), new Buffer(realm), new Buffer([nonce.length]), new Buffer(nonce), new Buffer([obj.authuri.length]), new Buffer(obj.authuri), new Buffer([cnonce.length]), new Buffer(cnonce), new Buffer([snc.length]), new Buffer(snc), new Buffer([digest.length]), new Buffer(digest)]);
  163. if (authType == 4) buf = Buffer.concat([buf, new Buffer([qop.length]), new Buffer(qop) ]);
  164. obj.xxSend(buf);
  165. }
  166. else if (status == 0) { // Success
  167. if (obj.protocol == 1) {
  168. // Serial-over-LAN: Send Intel AMT serial settings...
  169. var MaxTxBuffer = 10000;
  170. var TxTimeout = 100;
  171. var TxOverflowTimeout = 0;
  172. var RxTimeout = 10000;
  173. var RxFlushTimeout = 100;
  174. var Heartbeat = 0;//5000;
  175. obj.xxSend(String.fromCharCode(0x20, 0x00, 0x00, 0x00) + ToIntStr(obj.amtsequence++) + ToShortStr(MaxTxBuffer) + ToShortStr(TxTimeout) + ToShortStr(TxOverflowTimeout) + ToShortStr(RxTimeout) + ToShortStr(RxFlushTimeout) + ToShortStr(Heartbeat) + ToIntStr(0));
  176. }
  177. if (obj.protocol == 2) {
  178. // Remote Desktop: Send traffic directly...
  179. obj.xxSend(new Buffer([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]));
  180. }
  181. if (obj.protocol == 3) {
  182. // Remote IDER: Send traffic directly...
  183. obj.connectstate = 1;
  184. obj.xxStateChange(3);
  185. }
  186. } else obj.Stop();
  187. break;
  188. case 0x21: // Response to settings (33)
  189. if (obj.amtaccumulator.length < 23) break;
  190. cmdsize = 23;
  191. obj.xxSend(String.fromCharCode(0x27, 0x00, 0x00, 0x00) + ToIntStr(obj.amtsequence++) + String.fromCharCode(0x00, 0x00, 0x1B, 0x00, 0x00, 0x00));
  192. if (obj.protocol == 1) { obj.amtkeepalivetimer = setInterval(obj.xxSendAmtKeepAlive, 2000); }
  193. obj.connectstate = 1;
  194. obj.xxStateChange(3);
  195. break;
  196. case 0x29: // Serial Settings (41)
  197. if (obj.amtaccumulator.length < 10) break;
  198. cmdsize = 10;
  199. break;
  200. case 0x2A: // Incoming display data (42)
  201. if (obj.amtaccumulator.length < 10) break;
  202. var cs = (10 + ((obj.amtaccumulator[9] & 0xFF) << 8) + (obj.amtaccumulator[8] & 0xFF));
  203. if (obj.amtaccumulator.length < cs) break;
  204. obj.m.ProcessData(obj.amtaccumulator.slice(10, cs));
  205. cmdsize = cs;
  206. break;
  207. case 0x2B: // Keep alive message (43)
  208. if (obj.amtaccumulator.length < 8) break;
  209. cmdsize = 8;
  210. break;
  211. case 0x41:
  212. if (obj.amtaccumulator.length < 8) break;
  213. obj.connectstate = 1;
  214. obj.m.Start();
  215. // KVM traffic, forward rest of accumulator directly.
  216. if (obj.amtaccumulator.length > 8) { obj.m.ProcessData(obj.amtaccumulator.substring(8)); }
  217. cmdsize = obj.amtaccumulator.length;
  218. break;
  219. default:
  220. console.log('Unknown Intel AMT command: ' + obj.amtaccumulator[0] + ' acclen=' + obj.amtaccumulator.length);
  221. obj.Stop();
  222. return;
  223. }
  224. if (cmdsize == 0) return;
  225. obj.amtaccumulator = obj.amtaccumulator.slice(cmdsize);
  226. }
  227. }
  228. obj.xxSend = function (x) {
  229. if (urlvars && urlvars['redirtrace']) { console.log('REDIR-SEND(' + x.length + '): ' + rstr2hex(x)); }
  230. //obj.Debug('Send(' + x.length + '): ' + Buffer.from(x, 'binary').toString('hex'));
  231. if (typeof x == 'string') { obj.socket.write(Buffer.from(x, 'binary')); } else { obj.socket.write(x); }
  232. }
  233. // Send Serial-over-LAN ASCII characters
  234. obj.Send = function (x) {
  235. if (obj.socket == null || obj.connectstate != 1) return;
  236. if (obj.protocol == 1) { obj.xxSend(String.fromCharCode(0x28, 0x00, 0x00, 0x00) + ToIntStr(obj.amtsequence++) + ToShortStr(x.length) + x); } else { obj.xxSend(x); }
  237. }
  238. obj.xxSendAmtKeepAlive = function () {
  239. if (obj.socket == null) return;
  240. obj.xxSend(String.fromCharCode(0x2B, 0x00, 0x00, 0x00) + ToIntStr(obj.amtsequence++));
  241. }
  242. // Uses OpenSSL random to generate a hex string
  243. obj.xxRandomValueHex = function (len) {
  244. var t = [], l = Math.floor(len / 2);
  245. for (var i = 0; i < l; i++) { t.push(obj.tls.generateRandomInteger('0', '255')); }
  246. return new Buffer(t).toString('hex');
  247. }
  248. obj.xxOnSocketClosed = function () {
  249. obj.socket = null;
  250. if (urlvars && urlvars['redirtrace']) { console.log('REDIR-CLOSED'); }
  251. //obj.Debug('Socket Closed');
  252. obj.Stop();
  253. }
  254. obj.xxStateChange = function(newstate) {
  255. if (obj.State == newstate) return;
  256. obj.State = newstate;
  257. obj.m.xxStateChange(obj.State);
  258. if (obj.onStateChanged != null) obj.onStateChanged(obj, obj.State);
  259. }
  260. obj.Stop = function () {
  261. if (urlvars && urlvars['redirtrace']) { console.log('REDIR-CLOSED'); }
  262. //obj.Debug('Socket Stopped');
  263. obj.xxStateChange(0);
  264. obj.connectstate = -1;
  265. obj.amtaccumulator = Buffer.alloc(0);
  266. if (obj.socket != null) { obj.socket.destroy(); obj.socket = null; }
  267. if (obj.amtkeepalivetimer != null) { clearInterval(obj.amtkeepalivetimer); obj.amtkeepalivetimer = null; }
  268. }
  269. obj.RedirectStartSol = new Buffer([0x10, 0x00, 0x00, 0x00, 0x53, 0x4F, 0x4C, 0x20]);
  270. obj.RedirectStartKvm = new Buffer([0x10, 0x01, 0x00, 0x00, 0x4b, 0x56, 0x4d, 0x52]);
  271. obj.RedirectStartIder = new Buffer([0x10, 0x00, 0x00, 0x00, 0x49, 0x44, 0x45, 0x52]);
  272. return obj;
  273. }
  274. function ToIntStr(v) { return String.fromCharCode((v & 0xFF), ((v >> 8) & 0xFF), ((v >> 16) & 0xFF), ((v >> 24) & 0xFF)); }
  275. function ToShortStr(v) { return String.fromCharCode((v & 0xFF), ((v >> 8) & 0xFF)); }
  276. function ShortToStr(v) { return String.fromCharCode((v >> 8) & 0xFF, v & 0xFF); }
  277. function ShortToStrX(v) { return String.fromCharCode(v & 0xFF, (v >> 8) & 0xFF); }
  278. function IntToStr(v) { return String.fromCharCode((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF); }
  279. function IntToStrX(v) { return String.fromCharCode(v & 0xFF, (v >> 8) & 0xFF, (v >> 16) & 0xFF, (v >> 24) & 0xFF); }
  280. var md5hasher = require('MD5Stream').create();
  281. function hex_md5(a) { return md5hasher.syncHash(a).toString('hex').toLowerCase(); }