Added Intel AMT serial-over-lan terminal in meshcmd.

Ylian Saint-Hilaire committed Aug 2, 2022 at 16:24 UTC 9b05b27e06ee64f30f5569e22f23a6fdd2693eaa
5 files changed +95 -26
agents/MeshCmd-signed.exe
Binary files a/agents/MeshCmd-signed.exe and b/agents/MeshCmd-signed.exe differ
agents/MeshCmd64-signed.exe
Binary files a/agents/MeshCmd64-signed.exe and b/agents/MeshCmd64-signed.exe differ
agents/meshcmd.js
+52 -2
@@ -213,6 +213,7 @@ function run(argv) {
213 console.log(' AmtWake - Intel AMT Wake Alarms.');
214 console.log(' AmtRPE - Intel AMT Remote Platform Erase.');
215 console.log(' AmtDDNS - Intel AMT DDNS settings.');
216 + console.log(' AmtTerm - Intel AMT Serial-over-LAN terminal.');
217 console.log('\r\nHelp on a specific action using:\r\n');
218 console.log(' meshcmd help [action]');
219 exit(0); return;
@@ -441,6 +442,13 @@ function run(argv) {
442 console.log(' --set [disabled/dhcp/enabled] Set the dynamic DNS mode.');
443 console.log(' --interval [minutes] Set update interval in minutes, default is 1440, minimum is 20.');
444 console.log(' --ttl [seconds] Set time to live, default is 900.');
445 + } else if (action == 'amtterm') {
446 + console.log('AmtTerm is used to connect to the Serial-over-LAN port. Example usage:\r\n\r\n meshcmd amtterm --host 1.2.3.4 --user admin --pass mypassword');
447 + console.log('\r\nRequired arguments:\r\n');
448 + console.log(' --host [hostname] The IP address or DNS name of Intel AMT, 127.0.0.1 is default.');
449 + console.log(' --pass [password] The Intel AMT login password.');
450 + console.log('\r\nOptional arguments:\r\n');
451 + console.log(' --tls Specifies that TLS must be used.');
452 } else {
453 actions.shift();
454 console.log('Invalid action, usage:\r\n\r\n meshcmd help [action]\r\n\r\nValid actions are: ' + actions.join(', ') + '.');
@@ -829,6 +837,11 @@ function run(argv) {
837 if ((settings.password == null) || (typeof settings.password != 'string') || (settings.password == '')) { console.log('No or invalid \"password\" specified, use --password [password].'); exit(1); return; }
838 if ((settings.username == null) || (typeof settings.username != 'string') || (settings.username == '')) { settings.username = 'admin'; }
839 performAmtFeatureConfig(args);
840 + } else if (settings.action == 'amtterm') {
841 + if (settings.hostname == null) { settings.hostname = '127.0.0.1'; }
842 + if ((settings.password == null) || (typeof settings.password != 'string') || (settings.password == '')) { console.log('No or invalid \"password\" specified, use --password [password].'); exit(1); return; }
843 + if ((settings.username == null) || (typeof settings.username != 'string') || (settings.username == '')) { settings.username = 'admin'; }
844 + performAmtTerm(args);
845 } else if (settings.action == 'amtpower') { // Perform remote Intel AMT power operation
846 if ((settings.hostname == null) || (typeof settings.hostname != 'string') || (settings.hostname == '')) { console.log('No or invalid \"hostname\" specified, use --hostname [host].'); exit(1); return; }
847 if ((settings.password == null) || (typeof settings.password != 'string') || (settings.password == '')) { console.log('No or invalid \"password\" specified, use --password [password].'); exit(1); return; }
@@ -2406,8 +2419,8 @@ function OnMulticastMessage(msg, rinfo) {
2419 // IDER
2420 //
2421
2409 -ider = null;
2410 -iderIdleTimer = null;
2422 +var ider = null;
2423 +var iderIdleTimer = null;
2424
2425 // Perform IDER
2426 function performIder() {
@@ -2981,6 +2994,43 @@ function makeUefiBootParam(type, data, len) {
2994 function IntToStrX(v) { return String.fromCharCode(v & 0xFF, (v >> 8) & 0xFF, (v >> 16) & 0xFF, (v >> 24) & 0xFF); }
2995 function ShortToStrX(v) { return String.fromCharCode(v & 0xFF, (v >> 8) & 0xFF); }
2996
2997 +
2998 +//
2999 +// Intel AMT Serial-over-LAN
3000 +//
3001 +
3002 +var sol = null;
3003 +var solTimer = null;
3004 +
3005 +// Called to start serial-over-lan terminal
3006 +function performAmtTerm(args) {
3007 + try {
3008 + sol = require('amt-redir-duk')(require('amt-sol')());
3009 + sol.onStateChanged = onSolStateChange;
3010 + sol.m.onData = onSolData;
3011 + sol.m.debug = (settings.debuglevel > 0);
3012 + sol.Start(settings.hostname, (settings.tls == true) ? 16995 : 16994, settings.username ? 'admin' : settings.username, settings.password, settings.tls);
3013 + } catch (ex) { console.log(ex); }
3014 +}
3015 +
3016 +// Called when the serial-over-lan connection state changes
3017 +function onSolStateChange(stack, state) {
3018 + console.log(["Disconnected", "Connecting...", "Connected...", "Started Serial-over-LAN..."][state]);
3019 + if (state == 0) { exit(0); }
3020 + if (state == 3) {
3021 + // TODO: Serial-over-LAN is connected, we need to send stdin keys using sol.m.Send('abc');
3022 + // For now, we setup thie timer to send 'abc' at one second interval into serial-over-lan channel.
3023 + if (solTimer == null) { solTimer = setInterval(function () { sol.m.Send('abc'); }, 1000); }
3024 + } else {
3025 + // Serial-over-LAN is not active, stop any stdin key capture
3026 + if (solTimer != null) { clearInterval(solTimer); solTimer = null; }
3027 + }
3028 +}
3029 +
3030 +// This is called when serial-over-lan data come in from Intel AMT
3031 +function onSolData(stack, data) { console.log(data); }
3032 +
3033 +
3034 //
3035 // Intel AMT feature configuration action
3036 //
agents/modules_meshcmd/amt-redir-duk.js
+23 -24
@@ -21,10 +21,10 @@ module.exports = function CreateAmtRedirect(module) {
21 obj.protocol = module.protocol; // 1 = SOL, 2 = KVM, 3 = IDER
22 obj.xtlsoptions = null;
23
24 - obj.amtaccumulator = null;
24 + obj.amtaccumulator = Buffer.alloc(0);
25 obj.amtsequence = 1;
26 obj.amtkeepalivetimer = null;
27 - obj.authuri = "/RedirectionService";
27 + obj.authuri = '/RedirectionService';
28 obj.digestRealmMatch = null;
29
30 obj.onStateChanged = null;
@@ -80,7 +80,7 @@ module.exports = function CreateAmtRedirect(module) {
80 }
81 */
82
83 - if (urlvars && urlvars['redirtrace']) { console.log("REDIR-CONNECTED"); }
83 + if (urlvars && urlvars['redirtrace']) { console.log('REDIR-CONNECTED'); }
84 //obj.Debug("Socket Connected");
85 obj.xxStateChange(2);
86 if (obj.protocol == 1) obj.xxSend(obj.RedirectStartSol); // TODO: Put these strings in higher level module to tighten code
@@ -89,14 +89,14 @@ module.exports = function CreateAmtRedirect(module) {
89 }
90
91 obj.xxOnSocketData = function (data) {
92 - //console.log('xxOnSocketData: ' + data.toString('hex'), data.length);
92 if (!data || obj.connectstate == -1) return;
94 - if (urlvars && urlvars['redirtrace']) { console.log("REDIR-RECV(" + data.length + "): " + data.toString('hex')); }
93 + if (urlvars && urlvars['redirtrace']) { console.log('REDIR-RECV(' + data.length + '): ' + data.toString('hex')); }
94 //obj.Debug("Recv(" + data.length + "): " + rstr2hex(data));
95 if ((obj.protocol == 2 || obj.protocol == 3) && obj.connectstate == 1) { return obj.m.ProcessData(data); } // KVM or IDER traffic, forward it directly.
97 - if (obj.amtaccumulator == null) { obj.amtaccumulator = data; } else { obj.amtaccumulator = Buffer.concat(obj.amtaccumulator, data); }
98 - //obj.Debug("Recv(" + obj.amtaccumulator.length + "): " + rstr2hex(obj.amtaccumulator));
99 - while (obj.amtaccumulator != null) {
96 + obj.amtaccumulator = Buffer.concat([obj.amtaccumulator, data]);
97 + //obj.Debug("Recv(" + obj.amtaccumulator.length + "): " + obj.amtaccumulator.toString('hex'));
98 +
99 + while (obj.amtaccumulator.length > 0) {
100 var cmdsize = 0;
101 //console.log('CMD: ' + obj.amtaccumulator[0]);
102 switch (obj.amtaccumulator[0]) {
@@ -170,9 +170,9 @@ module.exports = function CreateAmtRedirect(module) {
170 qoplen = authDataBuf[curptr];
171 qop = authDataBuf.slice(curptr + 1, curptr + 1 + qoplen).toString();
172 curptr += (qoplen + 1);
173 - extra = snc + ":" + cnonce + ":" + qop + ":";
173 + extra = snc + ':' + cnonce + ':' + qop + ':';
174 }
175 - var digest = hex_md5(hex_md5(obj.user + ":" + realm + ":" + obj.pass) + ":" + nonce + ":" + extra + hex_md5("POST:" + obj.authuri));
175 + var digest = hex_md5(hex_md5(obj.user + ':' + realm + ':' + obj.pass) + ':' + nonce + ':' + extra + hex_md5('POST:' + obj.authuri));
176 var totallen = obj.user.length + realm.length + nonce.length + obj.authuri.length + cnonce.length + snc.length + digest.length + 7;
177 if (authType == 4) totallen += (qop.length + 1);
178 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)]);
@@ -181,7 +181,6 @@ module.exports = function CreateAmtRedirect(module) {
181 }
182 else if (status == 0) { // Success
183 if (obj.protocol == 1) {
184 - /*
184 // Serial-over-LAN: Send Intel AMT serial settings...
185 var MaxTxBuffer = 10000;
186 var TxTimeout = 100;
@@ -190,7 +189,6 @@ module.exports = function CreateAmtRedirect(module) {
189 var RxFlushTimeout = 100;
190 var Heartbeat = 0;//5000;
191 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));
193 - */
192 }
193 if (obj.protocol == 2) {
194 // Remote Desktop: Send traffic directly...
@@ -219,7 +217,7 @@ module.exports = function CreateAmtRedirect(module) {
217 if (obj.amtaccumulator.length < 10) break;
218 var cs = (10 + ((obj.amtaccumulator[9] & 0xFF) << 8) + (obj.amtaccumulator[8] & 0xFF));
219 if (obj.amtaccumulator.length < cs) break;
222 - obj.m.ProcessData(obj.amtaccumulator.substring(10, cs));
220 + obj.m.ProcessData(obj.amtaccumulator.slice(10, cs));
221 cmdsize = cs;
222 break;
223 case 0x2B: // Keep alive message (43)
@@ -235,21 +233,22 @@ module.exports = function CreateAmtRedirect(module) {
233 cmdsize = obj.amtaccumulator.length;
234 break;
235 default:
238 - console.log("Unknown Intel AMT command: " + obj.amtaccumulator[0] + " acclen=" + obj.amtaccumulator.length);
236 + console.log('Unknown Intel AMT command: ' + obj.amtaccumulator[0] + ' acclen=' + obj.amtaccumulator.length);
237 obj.Stop();
238 return;
239 }
240 if (cmdsize == 0) return;
243 - if (cmdsize == obj.amtaccumulator.length) { obj.amtaccumulator = null; } else { obj.amtaccumulator = obj.amtaccumulator.slice(cmdsize); }
241 + obj.amtaccumulator = obj.amtaccumulator.slice(cmdsize);
242 }
243 }
244
245 obj.xxSend = function (x) {
248 - if (urlvars && urlvars['redirtrace']) { console.log("REDIR-SEND(" + x.length + "): " + rstr2hex(x)); }
249 - //obj.Debug("Send(" + x.length + "): " + Buffer.from(x, "binary").toString('hex'));
250 - if (typeof x == 'string') { obj.socket.write(Buffer.from(x, "binary")); } else { obj.socket.write(x); }
246 + if (urlvars && urlvars['redirtrace']) { console.log('REDIR-SEND(' + x.length + '): ' + rstr2hex(x)); }
247 + //obj.Debug('Send(' + x.length + '): ' + Buffer.from(x, 'binary').toString('hex'));
248 + if (typeof x == 'string') { obj.socket.write(Buffer.from(x, 'binary')); } else { obj.socket.write(x); }
249 }
250
251 + // Send Serial-over-LAN ASCII characters
252 obj.Send = function (x) {
253 if (obj.socket == null || obj.connectstate != 1) return;
254 if (obj.protocol == 1) { obj.xxSend(String.fromCharCode(0x28, 0x00, 0x00, 0x00) + ToIntStr(obj.amtsequence++) + ToShortStr(x.length) + x); } else { obj.xxSend(x); }
@@ -263,14 +262,14 @@ module.exports = function CreateAmtRedirect(module) {
262 // Uses OpenSSL random to generate a hex string
263 obj.xxRandomValueHex = function (len) {
264 var t = [], l = Math.floor(len / 2);
266 - for (var i = 0; i < l; i++) { t.push(obj.tls.generateRandomInteger("0", "255")); }
265 + for (var i = 0; i < l; i++) { t.push(obj.tls.generateRandomInteger('0', '255')); }
266 return new Buffer(t).toString('hex');
267 }
268
269 obj.xxOnSocketClosed = function () {
270 obj.socket = null;
272 - if (urlvars && urlvars['redirtrace']) { console.log("REDIR-CLOSED"); }
273 - //obj.Debug("Socket Closed");
271 + if (urlvars && urlvars['redirtrace']) { console.log('REDIR-CLOSED'); }
272 + //obj.Debug('Socket Closed');
273 obj.Stop();
274 }
275
@@ -282,11 +281,11 @@ module.exports = function CreateAmtRedirect(module) {
281 }
282
283 obj.Stop = function () {
285 - if (urlvars && urlvars['redirtrace']) { console.log("REDIR-CLOSED"); }
286 - //obj.Debug("Socket Stopped");
284 + if (urlvars && urlvars['redirtrace']) { console.log('REDIR-CLOSED'); }
285 + //obj.Debug('Socket Stopped');
286 obj.xxStateChange(0);
287 obj.connectstate = -1;
289 - obj.amtaccumulator = "";
288 + obj.amtaccumulator = Buffer.alloc(0);
289 if (obj.socket != null) { obj.socket.destroy(); obj.socket = null; }
290 if (obj.amtkeepalivetimer != null) { clearInterval(obj.amtkeepalivetimer); obj.amtkeepalivetimer = null; }
291 }
agents/modules_meshcmd/amt-sol.js new
+20
@@ -0,0 +1,20 @@
1 +/**
2 +* @description Serial-over-LAN Handling Module
3 +* @author Ylian Saint-Hilaire
4 +*/
5 +
6 +// meshservice meshcmd.js amtterm --host 192.168.2.186 --pass P@ssw0rd
7 +
8 +// Construct a Intel AMT Serial-over-LAN object
9 +module.exports = function CreateAmtRemoteSol() {
10 + var obj = {};
11 + obj.protocol = 1; // Serial-over-LAN
12 + obj.debug = false;
13 + obj.onData = null;
14 + obj.xxStateChange = function (newstate) { if (obj.debug) console.log('SOL-StateChange', newstate); if (newstate == 0) { obj.Stop(); } if (newstate == 3) { obj.Start(); } }
15 + obj.Start = function () { if (obj.debug) { console.log('SOL-Start'); } }
16 + obj.Stop = function () { if (obj.debug) { console.log('SOL-Stop'); } }
17 + obj.ProcessData = function (data) { if (obj.debug) { console.log('SOL-ProcessData', data); } if (obj.onData) { obj.onData(obj, data); } }
18 + obj.Send = function(text) { if (obj.debug) { console.log('SOL-Send', text); } obj.parent.Send(text); }
19 + return obj;
20 +}
\ No newline at end of file