Improved TCP/TLS detection of Intel AMT

Ylian Saint-Hilaire committed May 18, 2018 at 17:29 UTC 7ff2f95a72ea98e215080ac6fe2ae6ef87c649b1
3 files changed +60 -23
agents/meshcore.js
+12 -10
@@ -1292,16 +1292,18 @@ function createMeshCore(agent) {
1292 // Get Intel AMT information using MEI
1293 function getAmtInfo(func) {
1294 if (amtMei == null || amtMeiConnected != 2) { if (func != null) { func(null); } return; }
1295 - amtMeiTmpState = { Flags: 0 }; // Flags: 1=EHBC, 2=CCM, 4=ACM
1296 - amtMei.getProtocolVersion(function (result) { if (result != null) { amtMeiTmpState.MeiVersion = result; } });
1297 - amtMei.getVersion(function (val) { amtMeiTmpState.Versions = {}; for (var version in val.Versions) { amtMeiTmpState.Versions[val.Versions[version].Description] = val.Versions[version].Version; } });
1298 - amtMei.getProvisioningMode(function (result) { amtMeiTmpState.ProvisioningMode = result.mode; });
1299 - amtMei.getProvisioningState(function (result) { amtMeiTmpState.ProvisioningState = result.state; });
1300 - amtMei.getEHBCState(function (result) { if ((result != null) && (result.EHBC == true)) { amtMeiTmpState.Flags += 1; } });
1301 - amtMei.getControlMode(function (result) { if (result != null) { if (result.controlMode == 1) { amtMeiTmpState.Flags += 2; } if (result.controlMode == 2) { amtMeiTmpState.Flags += 4; } } });
1302 - amtMei.getUuid(function (result) { if ((result != null) && (result.uuid != null)) { amtMeiTmpState.UUID = result.uuid; } });
1303 - //amtMei.getMACAddresses(function (result) { amtMeiTmpState.mac = result; });
1304 - amtMei.getDnsSuffix(function (result) { if (result != null) { amtMeiTmpState.dns = result; } if (func != null) { func(amtMeiTmpState); } });
1295 + try {
1296 + amtMeiTmpState = { Flags: 0 }; // Flags: 1=EHBC, 2=CCM, 4=ACM
1297 + amtMei.getProtocolVersion(function (result) { if (result != null) { amtMeiTmpState.MeiVersion = result; } });
1298 + amtMei.getVersion(function (val) { amtMeiTmpState.Versions = {}; for (var version in val.Versions) { amtMeiTmpState.Versions[val.Versions[version].Description] = val.Versions[version].Version; } });
1299 + amtMei.getProvisioningMode(function (result) { amtMeiTmpState.ProvisioningMode = result.mode; });
1300 + amtMei.getProvisioningState(function (result) { amtMeiTmpState.ProvisioningState = result.state; });
1301 + amtMei.getEHBCState(function (result) { if ((result != null) && (result.EHBC == true)) { amtMeiTmpState.Flags += 1; } });
1302 + amtMei.getControlMode(function (result) { if (result != null) { if (result.controlMode == 1) { amtMeiTmpState.Flags += 2; } if (result.controlMode == 2) { amtMeiTmpState.Flags += 4; } } });
1303 + amtMei.getUuid(function (result) { if ((result != null) && (result.uuid != null)) { amtMeiTmpState.UUID = result.uuid; } });
1304 + //amtMei.getMACAddresses(function (result) { amtMeiTmpState.mac = result; });
1305 + amtMei.getDnsSuffix(function (result) { if (result != null) { amtMeiTmpState.dns = result; } if (func != null) { func(amtMeiTmpState); } });
1306 + } catch (e) { if (func != null) { func(null); } return; }
1307 }
1308
1309 // Called on MicroLMS Intel AMT user notification
amtscanner.js
+47 -12
@@ -12,7 +12,9 @@ module.exports.CreateAmtScanner = function (parent) {
12 obj.active = false;
13 obj.parent = parent;
14 obj.net = require('net');
15 + obj.tls = require('tls');
16 obj.dns = require('dns');
17 + obj.constants = require('constants');
18 obj.dgram = require('dgram');
19 obj.common = require('./common.js');
20 obj.servers = {};
@@ -131,10 +133,14 @@ module.exports.CreateAmtScanner = function (parent) {
133 obj.parent.ClearConnectivityState(scaninfo.nodeinfo.meshid, scaninfo.nodeinfo._id, 4); // Clear connectivity state
134 } else if ((scaninfo.tcp == null) && ((scaninfo.state == 0) || isNaN(delta) || (delta > PeriodicScanTime))) {
135 // More than 30 seconds without a response, try TCP detection
134 - obj.checkTcpPresence(host, (doc.intelamt.tls == 1) ? 16993 : 16992, scaninfo, function (tag, result) {
136 + obj.checkTcpPresence(host, (doc.intelamt.tls == 1) ? 16993 : 16992, scaninfo, function (tag, result, version) {
137 if (result == false) return;
138 tag.lastpong = Date.now();
137 - if (tag.state == 0) { tag.state = 1; obj.parent.SetConnectivityState(tag.nodeinfo.meshid, tag.nodeinfo._id, tag.lastpong, 4, 7); } // Report power state as "present" (7).
139 + if (tag.state == 0) {
140 + tag.state = 1;
141 + obj.parent.SetConnectivityState(tag.nodeinfo.meshid, tag.nodeinfo._id, tag.lastpong, 4, 7); // Report power state as "present" (7).
142 + if (version != null) { obj.changeAmtState(tag.nodeinfo._id, version, 2, tag.nodeinfo.intelamt.tls); }
143 + }
144 });
145 }
146 }
@@ -298,17 +304,46 @@ module.exports.CreateAmtScanner = function (parent) {
304 // Check that we can connect TCP to a given port
305 obj.checkTcpPresence = function (host, port, scaninfo, func) {
306 //console.log('checkTcpPresence(' + host + ':' + port + ')');
301 - var client = new obj.net.Socket();
302 - client.scaninfo = scaninfo;
303 - client.func = func;
304 - client.setTimeout(10000);
305 - client.connect(port, host, function () { if (this.scaninfo.tcp != null) { delete this.scaninfo.tcp; try { this.destroy(); } catch (ex) { } this.func(this.scaninfo, true); } });
306 - client.on('data', function (data) { });
307 - client.on('close', function () { if (this.scaninfo.tcp != null) { delete this.scaninfo.tcp; try { this.destroy(); } catch (ex) { } this.func(this.scaninfo, false); } });
308 - client.on('error', function () { if (this.scaninfo.tcp != null) { delete this.scaninfo.tcp; try { this.destroy(); } catch (ex) { } this.func(this.scaninfo, false); } });
309 - client.on('timeout', function () { if (this.scaninfo.tcp != null) { delete this.scaninfo.tcp; try { this.destroy(); } catch (ex) { } this.func(this.scaninfo, false); } });
310 - scaninfo.tcp = client;
307 + try {
308 + var client;
309 + if (port == 16992) {
310 + // Connect using TCP
311 + client = new obj.net.Socket();
312 + client.connect(port, host, function () { this.write('GET / HTTP/1.1\r\nhost: ' + host + '\r\n\r\n'); });
313 + } else {
314 + // Connect using TLS, we will switch from default TLS to TLS1-only and back if we get a connection error to support older Intel AMT.
315 + if (scaninfo.tlsoption == null) { scaninfo.tlsoption = 0; }
316 + client = obj.tls.connect(port, host, scaninfo.tlsoption == 1 ? { secureProtocol: 'TLSv1_method', rejectUnauthorized: false, ciphers: 'RSA+AES:!aNULL:!MD5:!DSS', secureOptions: obj.constants.SSL_OP_NO_SSLv2 | obj.constants.SSL_OP_NO_SSLv3 | obj.constants.SSL_OP_NO_COMPRESSION | obj.constants.SSL_OP_CIPHER_SERVER_PREFERENCE } : { rejectUnauthorized: false, ciphers: 'RSA+AES:!aNULL:!MD5:!DSS', secureOptions: obj.constants.SSL_OP_NO_SSLv2 | obj.constants.SSL_OP_NO_SSLv3 | obj.constants.SSL_OP_NO_COMPRESSION | obj.constants.SSL_OP_CIPHER_SERVER_PREFERENCE }, function () { this.write('GET / HTTP/1.1\r\nhost: ' + host + '\r\n\r\n'); });
317 + }
318 + client.scaninfo = scaninfo;
319 + client.func = func;
320 + client.port = port;
321 + client.setTimeout(10000);
322 + client.on('data', function (data) { var version = obj.getIntelAmtVersionFromHeaders(data.toString()); if (this.scaninfo.tcp != null) { delete this.scaninfo.tcp; try { this.destroy(); } catch (ex) { } this.func(this.scaninfo, version != null, version); } });
323 + client.on('error', function () { if (this.scaninfo.tlsoption == 0) { this.scaninfo.tlsoption = 1; } else if (this.scaninfo.tlsoption == 1) { this.scaninfo.tlsoption = 0; } if (this.scaninfo.tcp != null) { delete this.scaninfo.tcp; try { this.destroy(); } catch (ex) { } this.func(this.scaninfo, false); } });
324 + client.on('timeout', function () { if (this.scaninfo.tcp != null) { delete this.scaninfo.tcp; try { this.destroy(); } catch (ex) { } this.func(this.scaninfo, false); } });
325 + client.on('close', function () { if (this.scaninfo.tcp != null) { delete this.scaninfo.tcp; try { this.destroy(); } catch (ex) { } this.func(this.scaninfo, false); } });
326 + client.on('end', function () { if (this.scaninfo.tcp != null) { delete this.scaninfo.tcp; try { this.destroy(); } catch (ex) { } this.func(this.scaninfo, false); } });
327 + scaninfo.tcp = client;
328 + } catch (ex) { console.log(ex); }
329 }
330
331 + // Return the Intel AMT version from the HTTP headers. Return null if nothing is found.
332 + obj.getIntelAmtVersionFromHeaders = function (headers) {
333 + if (headers == null || headers.length == 0) return null;
334 + var lines = headers.split('\r\n');
335 + for (var i in lines) {
336 + // Look for the Intel AMT version
337 + if (lines[i].substring(0, 46) == 'Server: Intel(R) Active Management Technology ') {
338 + // We need to check that the Intel AMT version is correct, in the "a.b.c" format
339 + var ver = lines[i].substring(46), splitver = ver.split('.');
340 + if ((splitver.length == 3 || splitver.length == 4) && ('' + parseInt(splitver[0]) === splitver[0]) && ('' + parseInt(splitver[1]) === splitver[1]) && ('' + parseInt(splitver[2]) === splitver[2])) { return (splitver[0] + '.' + splitver[1] + '.' + splitver[2]); }
341 + }
342 + }
343 + return null;
344 + }
345 +
346 + //console.log(obj.getIntelAmtVersionFromHeaders("HTTP/1.1 303 See Other\r\nLocation: /logon.htm\r\nContent-Length: 0\r\nServer: Intel(R) Active Management Technology 7.1.91\r\n\r\n"));
347 +
348 return obj;
349 }
\ No newline at end of file
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.1.7-m",
3 + "version": "0.1.7-n",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",