Improved Intel AMT activation logging.

Ylian Saint-Hilaire committed Oct 22, 2020 at 14:35 UTC 5a3e68a93510ec68032fda51651fe1a41daf229d
5 files changed +29 -14
agents/meshcore.js
+1 -1
@@ -1093,7 +1093,7 @@ function createMeshCore(agent) {
1093 };
1094 apftunnel = require('apfclient')({ debug: false }, apfarg);
1095 apftunnel.onJsonControl = function (data) {
1096 - //if (data.action == 'console') { require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: data.msg }); } // Display a console message (DEBUG)
1096 + if (data.action == 'console') { require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: data.msg }); } // Display a console message (DEBUG)
1097 if (data.action == 'mestate') { getMeiState(15, function (state) { apftunnel.updateMeiState(state); }); } // Update the MEI state
1098 if (data.action == 'deactivate') { // Request CCM deactivation
1099 var amtMeiModule, amtMei;
amtmanager.js
+10 -2
@@ -1452,6 +1452,13 @@ module.exports.CreateAmtManager = function (parent) {
1452 if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
1453 if (status != 200) { dev.consoleMsg("Failed to get Intel AMT state."); removeAmtDevice(dev); return; }
1454 if (responses['IPS_HostBasedSetupService'].response['AllowedControlModes'].length != 2) { dev.consoleMsg("Client control mode activation not allowed."); removeAmtDevice(dev); return; }
1455 +
1456 + // Log the activation request, logging is a required step for activation.
1457 + if (parent.certificateOperations.logAmtActivation(domain, { time: new Date(), action: 'ccmactivate', domain: dev.domainid, amtUuid: dev.mpsConnection.tag.meiState.UUID, amtRealm: responses['AMT_GeneralSettings'].response['DigestRealm'], user: 'admin', password: dev.temp.pass, ipport: dev.mpsConnection.remoteAddr + ':' + dev.mpsConnection.remotePort, nodeid: dev.nodeid, meshid: dev.meshid, computerName: dev.name }) == false) {
1458 + dev.consoleMsg("Unable to log operation."); removeAmtDevice(dev); return;
1459 + }
1460 +
1461 + // Perform CCM activation
1462 dev.amtstack.IPS_HostBasedSetupService_Setup(2, hex_md5('admin:' + responses['AMT_GeneralSettings'].response['DigestRealm'] + ':' + dev.temp.pass).substring(0, 32), null, null, null, null, activateIntelAmtCcmEx2);
1463 }
1464
@@ -1539,8 +1546,9 @@ module.exports.CreateAmtManager = function (parent) {
1546
1547 // Sign the Intel AMT ACM activation request
1548 var info = { nonce: responses['IPS_HostBasedSetupService'].response['ConfigurationNonce'], realm: responses['AMT_GeneralSettings'].response['DigestRealm'], fqdn: dev.temp.acminfo.fqdn, hash: dev.temp.acminfo.hash, uuid: dev.mpsConnection.tag.meiState.UUID };
1542 - var acmdata = parent.certificateOperations.signAcmRequest(parent.config.domains[dev.domainid], info, 'admin', dev.temp.pass, obj.remoteaddrport, dev.nodeid, dev.meshid, dev.name, 0);
1543 - if ((acmdata == null) || (acmdata.error != null)) { dev.consoleMsg("Failed to sign ACM nonce."); removeAmtDevice(dev); return; }
1549 + var acmdata = parent.certificateOperations.signAcmRequest(parent.config.domains[dev.domainid], info, 'admin', dev.temp.pass, dev.mpsConnection.remoteAddr + ':' + dev.mpsConnection.remotePort, dev.nodeid, dev.meshid, dev.name, 0);
1550 + if (acmdata == null) { dev.consoleMsg("Failed to sign ACM nonce."); removeAmtDevice(dev); return; }
1551 + if (acmdata.error != null) { dev.consoleMsg(acmdata.errorText); removeAmtDevice(dev); return; }
1552
1553 // Log this activation event
1554 var event = { etype: 'node', action: 'amtactivate', nodeid: dev.nodeid, domain: dev.domainid, msgid: 58, msgArgs: [ dev.temp.acminfo.fqdn ], msg: 'Device requested Intel(R) AMT ACM activation, FQDN: ' + dev.temp.acminfo.fqdn };
certoperations.js
+9 -9
@@ -31,11 +31,11 @@ module.exports.CertificateOperations = function (parent) {
31 // Sign a Intel AMT ACM activation request
32 obj.signAcmRequest = function (domain, request, user, pass, ipport, nodeid, meshid, computerName, agentId) {
33 if ((domain == null) || (domain.amtacmactivation == null) || (domain.amtacmactivation.certs == null) || (request == null) || (request.nonce == null) || (request.realm == null) || (request.fqdn == null) || (request.hash == null)) return { 'action': 'acmactivate', 'error': 1, 'errorText': 'Invalid arguments' };
34 - if (parent.common.validateString(request.nonce, 16, 256) == false) return { 'action': 'acmactivate', 'error': 1, 'errorText': 'Invalid nonce argument' };
35 - if (parent.common.validateString(request.realm, 16, 256) == false) return { 'action': 'acmactivate', 'error': 1, 'errorText': 'Invalid realm argument' };
36 - if (parent.common.validateString(request.fqdn, 4, 256) == false) return { 'action': 'acmactivate', 'error': 1, 'errorText': 'Invalid FQDN argument' };
37 - if (parent.common.validateString(request.hash, 16, 256) == false) return { 'action': 'acmactivate', 'error': 1, 'errorText': 'Invalid hash argument' };
38 - if (parent.common.validateString(request.uuid, 36, 36) == false) return { 'action': 'acmactivate', 'error': 1, 'errorText': 'Invalid UUID argument' };
34 + if (parent.common.validateString(request.nonce, 16, 256) == false) return { 'action': 'acmactivate', 'error': 1, 'errorText': "Invalid nonce argument." };
35 + if (parent.common.validateString(request.realm, 16, 256) == false) return { 'action': 'acmactivate', 'error': 1, 'errorText': "Invalid realm argument." };
36 + if (parent.common.validateString(request.fqdn, 4, 256) == false) return { 'action': 'acmactivate', 'error': 1, 'errorText': "Invalid FQDN argument." };
37 + if (parent.common.validateString(request.hash, 16, 256) == false) return { 'action': 'acmactivate', 'error': 1, 'errorText': "Invalid hash argument." };
38 + if (parent.common.validateString(request.uuid, 36, 36) == false) return { 'action': 'acmactivate', 'error': 1, 'errorText': "Invalid UUID argument." };
39
40 // Look for the signing certificate
41 var signkey = null, certChain = null, hashAlgo = null, certIndex = null;
@@ -44,10 +44,10 @@ module.exports.CertificateOperations = function (parent) {
44 if ((certEntry.sha256 == request.hash) && ((certEntry.cn == '*') || (certEntry.cn == request.fqdn))) { hashAlgo = 'sha256'; signkey = certEntry.key; certChain = certEntry.certs; certIndex = i; break; }
45 if ((certEntry.sha1 == request.hash) && ((certEntry.cn == '*') || (certEntry.cn == request.fqdn))) { hashAlgo = 'sha1'; signkey = certEntry.key; certChain = certEntry.certs; certIndex = i; break; }
46 }
47 - if (signkey == null) return { 'action': 'acmactivate', 'error': 2, 'errorText': 'No signing certificate found' }; // Did not find a match.
47 + if (signkey == null) return { 'action': 'acmactivate', 'error': 2, 'errorText': "No signing certificate found." }; // Did not find a match.
48
49 // If the matching certificate is a root cert, issue a leaf cert that matches the fqdn
50 - if (domain.amtacmactivation.certs[certIndex].cn == '*') return { 'action': 'acmactivate', 'error': 3, 'errorText': "Unsupported activation" }; // TODO: Add support for this mode
50 + if (domain.amtacmactivation.certs[certIndex].cn == '*') return { 'action': 'acmactivate', 'error': 3, 'errorText': "Unsupported activation." }; // TODO: Add support for this mode
51
52 // Setup both nonces, ready to be signed
53 const mcNonce = Buffer.from(obj.crypto.randomBytes(20), 'binary');
@@ -59,10 +59,10 @@ module.exports.CertificateOperations = function (parent) {
59 var signer = obj.crypto.createSign(hashAlgo);
60 signer.update(Buffer.concat([fwNonce, mcNonce]));
61 signature = signer.sign(signkey, 'base64');
62 - } catch (ex) { return { 'action': 'acmactivate', 'error': 4, 'errorText': "Unable to perform signature" }; }
62 + } catch (ex) { return { 'action': 'acmactivate', 'error': 4, 'errorText': "Unable to perform signature." }; }
63
64 // Log the activation request, logging is a required step for activation.
65 - if (obj.logAmtActivation(domain, { time: new Date(), action: 'acmactivate', domain: domain.id, amtUuid: request.uuid, certHash: request.hash, hashType: hashAlgo, amtRealm: request.realm, amtFqdn: request.fqdn, user: user, password: pass, ipport: ipport, nodeid: nodeid, meshid: meshid, computerName: computerName, agentId: agentId, tag: request.tag, name: request.name }) == false) return { 'action': 'acmactivate', 'error': 5, 'errorText': 'Unable to log operation' };
65 + if (obj.logAmtActivation(domain, { time: new Date(), action: 'acmactivate', domain: domain.id, amtUuid: request.uuid, certHash: request.hash, hashType: hashAlgo, amtRealm: request.realm, amtFqdn: request.fqdn, user: user, password: pass, ipport: ipport, nodeid: nodeid, meshid: meshid, computerName: computerName, agentId: agentId, tag: request.tag, name: request.name }) == false) return { 'action': 'acmactivate', 'error': 5, 'errorText': "Unable to log operation." };
66
67 // Return the signature with the computed account password hash
68 return { 'action': 'acmactivate', 'signature': signature, 'password': obj.crypto.createHash('md5').update(user + ':' + request.realm + ':' + pass).digest('hex'), 'nonce': mcNonce.toString('base64'), 'certs': certChain };
mpsserver.js
+8 -1
@@ -277,7 +277,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
277 return packet_len;
278 }
279
280 - obj.onWebSocketConnection = function (socket) {
280 + obj.onWebSocketConnection = function (socket, req) {
281 connectionCount++;
282 // connType: 0 = CIRA, 1 = Relay, 2 = LMS
283 socket.tag = { first: true, connType: 0, clientCert: null, accumulator: '', activetunnels: 0, boundPorts: [], websocket: true, socket: socket, host: null, nextchannelid: 4, channels: {}, nextsourceport: 0 };
@@ -288,6 +288,8 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
288 socket.ControlMsg = function ControlMsg(message) { return ControlMsg.parent.SendJsonControl(ControlMsg.conn, message); }
289 socket.ControlMsg.parent = obj;
290 socket.ControlMsg.conn = socket;
291 + socket.remoteAddr = req.clientIp;
292 + socket.remotePort = socket._socket.remotePort;
293 parent.debug('mps', "New CIRA websocket connection");
294
295 socket.on('message', function (data) {
@@ -330,6 +332,8 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
332 socket.ControlMsg = function ControlMsg(message) { return ControlMsg.parent.SendJsonControl(ControlMsg.conn, message); }
333 socket.ControlMsg.parent = obj;
334 socket.ControlMsg.conn = socket;
335 + socket.remoteAddr = cleanRemoteAddr(socket.remoteAddress);
336 + //socket.remotePort is already present, no need to set it.
337 socket.setEncoding('binary');
338 parent.debug('mps', "New CIRA connection");
339
@@ -1225,6 +1229,9 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
1229
1230 function guidToStr(g) { return g.substring(6, 8) + g.substring(4, 6) + g.substring(2, 4) + g.substring(0, 2) + "-" + g.substring(10, 12) + g.substring(8, 10) + "-" + g.substring(14, 16) + g.substring(12, 14) + "-" + g.substring(16, 20) + "-" + g.substring(20); }
1231
1232 + // Clean a IPv6 address that encodes a IPv4 address
1233 + function cleanRemoteAddr(addr) { if (typeof addr != 'string') { return null; } if (addr.indexOf('::ffff:') == 0) { return addr.substring(7); } else { return addr; } }
1234 +
1235 // Example, this will add a file to stream, served 2 times max and 3 minutes max.
1236 //obj.addHttpFileResponse('/a.png', 'c:\\temp\\MC2-LetsEncrypt.png', 2, 3);
1237
webserver.js
+1 -1
@@ -4850,7 +4850,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
4850 obj.app.post(url + 'uploadmeshcorefile.ashx', handleUploadMeshCoreFile);
4851 obj.app.get(url + 'userfiles/*', handleDownloadUserFiles);
4852 obj.app.ws(url + 'echo.ashx', handleEchoWebSocket);
4853 - obj.app.ws(url + 'apf.ashx', function (ws, req) { obj.parent.mpsserver.onWebSocketConnection(ws); })
4853 + obj.app.ws(url + 'apf.ashx', function (ws, req) { obj.parent.mpsserver.onWebSocketConnection(ws, req); })
4854 obj.app.get(url + 'webrelay.ashx', function (req, res) { res.send('Websocket connection expected'); });
4855 obj.app.get(url + 'health.ashx', function (req, res) { res.send('ok'); }); // TODO: Perform more server checking.
4856 obj.app.ws(url + 'webrelay.ashx', function (ws, req) { PerformWSSessionAuth(ws, req, false, handleRelayWebSocket); });