Improved Intel AMT within MeshCMD

Ylian Saint-Hilaire committed Jul 16, 2021 at 18:43 UTC 596e3e20d4b02cc4dc01671ebfb33c8ef03fc588
2 files changed +30 -20
agents/meshcmd.js
+28 -19
@@ -202,13 +202,13 @@ function run(argv) {
202 console.log(' AmtRPE - Intel AMT Remote Platform Erase.');
203 console.log('\r\nHelp on a specific action using:\r\n');
204 console.log(' meshcmd help [action]');
205 - exit(1); return;
205 + exit(0); return;
206 }
207 if (settings.action == 'help') {
208 if (argv.length <= 2) {
209 actions.shift();
210 console.log('Help usage:\r\n\r\n MeshCmd help [action]\r\n\r\nValid actions are: ' + actions.join(', ') + '.');
211 - exit(1); return;
211 + exit(0); return;
212 }
213 var action = argv[2].toLowerCase();
214 if (action == 'route') {
@@ -231,6 +231,8 @@ function run(argv) {
231 console.log(' --json Display all Intel AMT state in JSON format.');
232 } else if (action == 'amthashes') {
233 console.log('Amthashes will display all trusted activations hashes for Intel AMT on this computer. The command must be run on a computer with Intel AMT, must run as administrator and the Intel management driver must be installed. These certificates hashes are used by Intel AMT when performing activation into ACM mode. Example usage:\r\n\r\n meshcmd amthashes');
234 + console.log('\r\nPossible arguments:\r\n');
235 + console.log(' --json Display all Intel AMT hashes in JSON format.');
236 } else if ((action == 'microlms') || (action == 'lms') || (action == 'amtlms')) {
237 console.log('Starts MicroLMS on this computer, allowing local access to Intel AMT on TCP ports 16992 and 16993 when applicable. The command must be run on a computer with Intel AMT, must run as administrator and the Intel management driver must be installed. These certificates hashes are used by Intel AMT when performing activation into ACM mode. Example usage:\r\n\r\n meshcmd microlms');
238 console.log('\r\nPossible arguments:\r\n');
@@ -413,7 +415,7 @@ function run(argv) {
415 actions.shift();
416 console.log('Invalid action, usage:\r\n\r\n meshcmd help [action]\r\n\r\nValid actions are: ' + actions.join(', ') + '.');
417 }
416 - exit(1); return;
418 + exit(0); return;
419 }
420 settings.action = settings.action.toLowerCase();
421 debug(1, "Settings: " + JSON.stringify(settings));
@@ -438,7 +440,7 @@ function run(argv) {
440 else if (state == 49) { console.log("Certificate not ready."); }
441 else if (state == 0) { console.log("Success."); }
442 else { console.log("Unknown status: " + state); }
441 - exit(1);
443 + exit(state);
444 });
445 } else if (settings.action == 'amtstopconfig') {
446 // Stop Intel AMT configuration
@@ -450,16 +452,16 @@ function run(argv) {
452 else if (state == 1) { console.log("Intel AMT internal error."); }
453 else if (state == 0) { console.log("Success."); }
454 else { console.log("Unknown status: " + state); }
453 - exit(1);
455 + exit(state);
456 });
457 } else if (settings.action == 'smbios') {
458 // Display SM BIOS tables in raw form
459 SMBiosTables = require('smbios');
460 SMBiosTables.get(function (data) {
461 var r = SMBiosTables.parse(data);
460 - var out = objToString(r, 0, '\r\n');
462 + var out = JSON.stringify(r, null, 2);
463 if (settings.output == null) { console.log(out); } else { var file = fs.openSync(settings.output, 'w'); fs.writeSync(file, Buffer.from(out, 'utf8')); fs.closeSync(file); }
462 - exit(1);
464 + exit(0);
465 });
466 } else if (settings.action == 'rawsmbios') {
467 // Display SM BIOS tables in raw form
@@ -468,7 +470,7 @@ function run(argv) {
470 var out = '';
471 for (var i in data) { var header = false; for (var j in data[i]) { if (data[i][j].length > 0) { if (header == false) { out += ('Table type #' + i + ((SMBiosTables.smTableTypes[i] == null) ? '' : (', ' + SMBiosTables.smTableTypes[i]))) + '\r\n'; header = true; } out += (' ' + data[i][j].toString('hex')) + '\r\n'; } } }
472 if (settings.output == null) { console.log(out); } else { var file = fs.openSync(settings.output, 'w'); fs.writeSync(file, Buffer.from(out, 'utf8')); fs.closeSync(file); }
471 - exit(1);
473 + exit(0);
474 });
475 } else if (settings.action == 'route') {
476 // MeshCentral Router, port map local TCP port to a remote computer
@@ -503,11 +505,12 @@ function run(argv) {
505 console.log(val.Versions[version].Description + " = " + val.Versions[version].Version + extras);
506 }
507 }
506 - exit(1); return;
508 + exit(0);
509 + return;
510 });
511 } else if (settings.action == 'amthashes') {
512 // Display Intel AMT list of trusted hashes
510 - var amtMeiModule, amtMei;
513 + var amtMeiModule, amtMei, amtHashes = [];
514 try { amtMeiModule = require('amt-mei'); amtMei = new amtMeiModule(); } catch (ex) { console.log(ex); exit(1); return; }
515 amtMei.on('error', function (e) { console.log('ERROR: ' + e); exit(1); return; });
516 amtMei.getHashHandles(function (handles) {
@@ -517,8 +520,9 @@ function run(argv) {
520 var certState = [];
521 if (result.isDefault) { certState.push('Default'); }
522 if (result.isActive) { certState.push('Active'); } else { certState.push('Disabled'); }
520 - console.log(result.name + ', (' + certState.join(', ') + ')\r\n ' + result.hashAlgorithmStr + ': ' + result.certificateHash);
521 - if (--exitOnCount == 0) { exit(1); }
523 + amtHashes.push(result);
524 + if (!args.json) { console.log(result.name + ', (' + certState.join(', ') + ')\r\n ' + result.hashAlgorithmStr + ': ' + result.certificateHash); }
525 + if (--exitOnCount == 0) { if (args.json) { console.log(JSON.stringify(amtHashes, null, 2)); } exit(0); }
526 });
527 }
528 });
@@ -595,10 +599,11 @@ function run(argv) {
599 }
600 }
601 console.log(str + '.');
602 + exit(0);
603 } else {
604 console.log('Intel(R) AMT not supported.');
605 + exit(1);
606 }
601 - exit(1);
607 });
608 } else {
609 console.log("Unable to perform MEI operations, try running as " + ((process.platform == 'win32')?"administrator.":"root."));
@@ -608,7 +613,7 @@ function run(argv) {
613 } catch (ex) { console.log("Unable to perform MEI operations, try running as " + ((process.platform == 'win32')?"administrator.":"root.")); exit(1); return; }
614 } else if (settings.action == 'amtinfojson') {
615 // Display Intel AMT version and activation state
611 - getMeiState(15, function (state) { console.log(JSON.stringify(state, null, 2)); exit(1); }); // Flags: 1 = Versions, 2 = OsAdmin, 4 = Hashes, 8 = Network
616 + getMeiState(15, function (state) { console.log(JSON.stringify(state, null, 2)); exit(0); }); // Flags: 1 = Versions, 2 = OsAdmin, 4 = Hashes, 8 = Network
617 } else if (settings.action == 'amtsavestate') {
618 // Save the entire state of Intel AMT info a JSON file
619 if ((settings.password == null) || (typeof settings.password != 'string') || (settings.password == '')) { console.log('No or invalid \"password\" specified, use --password [password].'); exit(1); return; }
@@ -697,7 +702,7 @@ function run(argv) {
702 r = 'No Intel AMT found.';
703 }
704 console.log(r);
700 - exit(1);
705 + exit(0);
706 });
707 } else if (settings.action == 'amtauditlog') { // Read the Intel AMT audit log
708 if (settings.hostname != null) {
@@ -933,7 +938,7 @@ function readAmtEventLogEx2(stack, messages) {
938 var file = fs.openSync(settings.output, 'w');
939 fs.writeSync(file, Buffer.from(out));
940 fs.closeSync(file);
936 - exit(1);
941 + exit(0);
942 }
943 else if (settings.uuidoutput) {
944 var destpath = null; //Dest path where messagelog file will be saved
@@ -948,9 +953,11 @@ function readAmtEventLogEx2(stack, messages) {
953 var file = fs.openSync(eventlogsfile, 'w');
954 fs.writeSync(file, Buffer.from(out));
955 fs.closeSync(file);
956 + exit(0);
957 } else {
958 console.log('Intel AMT is not available or not activated, status = ' + status + '.');
953 - } exit(1);
959 + exit(1);
960 + }
961 });
962 }
963 else {
@@ -1012,7 +1019,7 @@ function readAmtAuditLogEx2(stack, response, status) {
1019 var file = fs.openSync(settings.output, 'w');
1020 fs.writeSync(file, Buffer.from(out));
1021 fs.closeSync(file);
1015 - exit(1);
1022 + exit(0);
1023 }
1024 else if (settings.uuidoutput) {
1025 var destpath = null; //Dest path where auditlog file will be saved
@@ -1027,9 +1034,11 @@ function readAmtAuditLogEx2(stack, response, status) {
1034 var file = fs.openSync(auditlogsfile, 'w');
1035 fs.writeSync(file, Buffer.from(out));
1036 fs.closeSync(file);
1037 + exit(0);
1038 } else {
1039 console.log('Intel AMT is not available or not activated, status = ' + status + '.');
1032 - } exit(1);
1040 + exit(1);
1041 + }
1042 });
1043 }
1044 else {
agents/modules_meshcmd/amt-wsman-duk.js
+2 -1
@@ -80,11 +80,12 @@ function CreateWsmanComm(/*host, port, user, pass, tls, extra*/) {
80 }
81 obj.digest.http = require('http');
82 }
83 - var request = { protocol: (obj.tls == 1 ? 'https:' : 'http:'), method: 'POST', host: obj.host, path: '/wsman', port: obj.port, rejectUnauthorized: false, checkServerIdentity: function (cert) { console.log('checkServerIdentity', JSON.stringify(cert)); } };
83 + var request = { protocol: (obj.tls == 1 ? 'https:' : 'http:'), method: 'POST', host: obj.host, path: '/wsman', port: obj.port, rejectUnauthorized: false, checkServerIdentity: function (cert) { /*console.log('checkServerIdentity', JSON.stringify(cert));*/ } };
84 var req = obj.digest.request(request);
85 //console.log('Request ' + (obj.RequestCount++));
86 req.on('error', function (e) { obj.gotNextMessagesError({ status: 600 }, 'error', null, [postdata, callback, tag]); });
87 req.on('response', function (response) {
88 + //console.log(JSON.stringify(response, null, 2));
89 if (globalDebugFlags & 1) { console.log('Response: ' + response.statusCode); }
90 if (response.statusCode != 200) {
91 if (globalDebugFlags & 1) { console.log('ERR:' + JSON.stringify(response)); }