| 1 | /** |
| 2 | * @description MeshCentral Intel(R) AMT Event Parser |
| 3 | * @author Ylian Saint-Hilaire & Bryan Roe |
| 4 | * @copyright Intel Corporation 2018-2022 |
| 5 | * @license Apache-2.0 |
| 6 | * @version v0.0.1 |
| 7 | */ |
| 8 | |
| 9 | /*jslint node: true */ |
| 10 | /*jshint node: true */ |
| 11 | /*jshint strict:false */ |
| 12 | /*jshint -W097 */ |
| 13 | /*jshint esversion: 6 */ |
| 14 | "use strict"; |
| 15 | |
| 16 | // Construct a MeshAgent object, called upon connection |
| 17 | module.exports.CreateAmtEventsHandler = function (parent) { |
| 18 | var obj = {}; |
| 19 | obj.parent = parent; |
| 20 | |
| 21 | // Private method |
| 22 | function ParseWsman(xml) { |
| 23 | try { |
| 24 | if (!xml.childNodes) xml = _turnToXml(xml); |
| 25 | var r = { Header: {} }, header = xml.getElementsByTagName("Header")[0], t; |
| 26 | if (!header) header = xml.getElementsByTagName("a:Header")[0]; |
| 27 | if (!header) return null; |
| 28 | for (var i = 0; i < header.childNodes.length; i++) { |
| 29 | var child = header.childNodes[i]; |
| 30 | r.Header[child.localName] = child.textContent; |
| 31 | } |
| 32 | var body = xml.getElementsByTagName("Body")[0]; |
| 33 | if (!body) body = xml.getElementsByTagName("a:Body")[0]; |
| 34 | if (!body) return null; |
| 35 | if (body.childNodes.length > 0) { |
| 36 | t = body.childNodes[0].localName; |
| 37 | var x = t.indexOf('_OUTPUT'); |
| 38 | if ((x != -1) && (x == (t.length - 7))) { t = t.substring(0, t.length - 7); } |
| 39 | r.Header.Method = t; |
| 40 | r.Body = _ParseWsmanRec(body.childNodes[0]); |
| 41 | } |
| 42 | return r; |
| 43 | } catch (e) { |
| 44 | console.log("Unable to parse XML: " + xml); |
| 45 | return null; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Private method |
| 50 | function _ParseWsmanRec(node) { |
| 51 | var data, r = {}; |
| 52 | for (var i = 0; i < node.childNodes.length; i++) { |
| 53 | var child = node.childNodes[i]; |
| 54 | if (child.childNodes == null) { data = child.textContent; } else { data = _ParseWsmanRec(child); } |
| 55 | if (data == 'true') data = true; // Convert 'true' into true |
| 56 | if (data == 'false') data = false; // Convert 'false' into false |
| 57 | |
| 58 | var childObj = data; |
| 59 | if (child.attributes != null) { |
| 60 | childObj = { 'Value': data }; |
| 61 | for (var j = 0; j < child.attributes.length; j++) { |
| 62 | childObj['@' + child.attributes[j].name] = child.attributes[j].value; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (r[child.localName] instanceof Array) { r[child.localName].push(childObj); } |
| 67 | else if (r[child.localName] == undefined) { r[child.localName] = childObj; } |
| 68 | else { r[child.localName] = [r[child.localName], childObj]; } |
| 69 | } |
| 70 | return r; |
| 71 | } |
| 72 | |
| 73 | // Parse and handle an event coming from Intel AMT |
| 74 | obj.handleAmtEvent = function (data, nodeid, amthost) { |
| 75 | var x = ParseWsman(data); |
| 76 | if (x != null) { |
| 77 | // TODO: Dispatch this event, we need to keep a running Intel AMT log for each machine. |
| 78 | console.log('Got Intel AMT event from ' + amthost + ', nodeid: ' + nodeid.substring(0, 8)); |
| 79 | //console.log(x); |
| 80 | } |
| 81 | return x; |
| 82 | }; |
| 83 | |
| 84 | // DEBUG: This is an example event, to test parsing and dispatching |
| 85 | //obj.handleAmtEvent('<?xml version="1.0" encoding="UTF-8"?><a:Envelope xmlns:a="http://www.w3.org/2003/05/soap-envelope" xmlns:b="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:c="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:d="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:e="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:f="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:g="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_AlertIndication" xmlns:h="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><a:Header><b:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</b:To><b:ReplyTo><b:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</b:Address></b:ReplyTo><c:AckRequested></c:AckRequested><b:Action a:mustUnderstand="true">http://schemas.dmtf.org/wbem/wsman/1/wsman/Event</b:Action><b:MessageID>uuid:00000000-8086-8086-8086-000000128538</b:MessageID><c:ResourceURI>http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_AlertIndication</c:ResourceURI></a:Header><a:Body><g:CIM_AlertIndication><g:AlertType>8</g:AlertType><g:AlertingElementFormat>2</g:AlertingElementFormat><g:AlertingManagedElement>Interop:CIM_ComputerSystem.CreationClassName="CIM_ComputerSystem",Name="Intel(r) AMT"</g:AlertingManagedElement><g:IndicationFilterName>Intel(r) AMT:AllEvents</g:IndicationFilterName><g:IndicationIdentifier>Intel(r):2950234687</g:IndicationIdentifier><g:IndicationTime><h:Datetime>2017-01-31T15:40:09.000Z</h:Datetime></g:IndicationTime><g:Message></g:Message><g:MessageArguments>0</g:MessageArguments><g:MessageArguments>Interop:CIM_ComputerSystem.CreationClassName=CIM_ComputerSystem,Name=Intel(r) AMT</g:MessageArguments><g:MessageID>iAMT0005</g:MessageID><g:OtherAlertingElementFormat></g:OtherAlertingElementFormat><g:OtherSeverity></g:OtherSeverity><g:OwningEntity>Intel(r) AMT</g:OwningEntity><g:PerceivedSeverity>2</g:PerceivedSeverity><g:ProbableCause>0</g:ProbableCause><g:SystemName>Intel(r) AMT</g:SystemName></g:CIM_AlertIndication></a:Body></a:Envelope>', 'aabbccdd', '1.2.3.4'); |
| 86 | |
| 87 | // This is a drop-in replacement to _turnToXml() that works without xml parser dependency. |
| 88 | try { Object.defineProperty(Array.prototype, "peek", { value: function () { return (this.length > 0 ? this[this.length - 1] : null); } }); } catch (ex) { } |
| 89 | function _treeBuilder() { |
| 90 | this.tree = []; |
| 91 | this.push = function (element) { this.tree.push(element); }; |
| 92 | this.pop = function () { var element = this.tree.pop(); if (this.tree.length > 0) { var x = this.tree.peek(); x.childNodes.push(element); x.childElementCount = x.childNodes.length; } return (element); }; |
| 93 | this.peek = function () { return (this.tree.peek()); } |
| 94 | this.addNamespace = function (prefix, namespace) { this.tree.peek().nsTable[prefix] = namespace; if (this.tree.peek().attributes.length > 0) { for (var i = 0; i < this.tree.peek().attributes; ++i) { var a = this.tree.peek().attributes[i]; if (prefix == '*' && a.name == a.localName) { a.namespace = namespace; } else if (prefix != '*' && a.name != a.localName) { var pfx = a.name.split(':')[0]; if (pfx == prefix) { a.namespace = namespace; } } } } } |
| 95 | this.getNamespace = function (prefix) { for (var i = this.tree.length - 1; i >= 0; --i) { if (this.tree[i].nsTable[prefix] != null) { return (this.tree[i].nsTable[prefix]); } } return null; } |
| 96 | } |
| 97 | function _turnToXml(text) { if (text == null) return null; return ({ childNodes: [_turnToXmlRec(text)], getElementsByTagName: _getElementsByTagName, getChildElementsByTagName: _getChildElementsByTagName, getElementsByTagNameNS: _getElementsByTagNameNS }); } |
| 98 | function _getElementsByTagNameNS(ns, name) { var ret = []; _xmlTraverseAllRec(this.childNodes, function (node) { if (node.localName == name && (node.namespace == ns || ns == '*')) { ret.push(node); } }); return ret; } |
| 99 | function _getElementsByTagName(name) { var ret = []; _xmlTraverseAllRec(this.childNodes, function (node) { if (node.localName == name) { ret.push(node); } }); return ret; } |
| 100 | function _getChildElementsByTagName(name) { var ret = []; if (this.childNodes != null) { for (var node in this.childNodes) { if (this.childNodes[node].localName == name) { ret.push(this.childNodes[node]); } } } return (ret); } |
| 101 | function _getChildElementsByTagNameNS(ns, name) { var ret = []; if (this.childNodes != null) { for (var node in this.childNodes) { if (this.childNodes[node].localName == name && (ns == '*' || this.childNodes[node].namespace == ns)) { ret.push(this.childNodes[node]); } } } return (ret); } |
| 102 | function _xmlTraverseAllRec(nodes, func) { for (var i in nodes) { func(nodes[i]); if (nodes[i].childNodes) { _xmlTraverseAllRec(nodes[i].childNodes, func); } } } |
| 103 | function _turnToXmlRec(text) { |
| 104 | var elementStack = new _treeBuilder(), lastElement = null, x1 = text.split('<'), ret = [], element = null, currentElementName = null; |
| 105 | for (var i in x1) { |
| 106 | var x2 = x1[i].split('>'), x3 = x2[0].split(' '), elementName = x3[0]; |
| 107 | if ((elementName.length > 0) && (elementName[0] != '?')) { |
| 108 | if (elementName[0] != '/') { |
| 109 | var attributes = [], localName, localname2 = elementName.split(' ')[0].split(':'), localName = (localname2.length > 1) ? localname2[1] : localname2[0]; |
| 110 | Object.defineProperty(attributes, "get", |
| 111 | { |
| 112 | value: function () { |
| 113 | if (arguments.length == 1) { |
| 114 | for (var a in this) { if (this[a].name == arguments[0]) { return (this[a]); } } |
| 115 | } |
| 116 | else if (arguments.length == 2) { |
| 117 | for (var a in this) { if (this[a].name == arguments[1] && (arguments[0] == '*' || this[a].namespace == arguments[0])) { return (this[a]); } } |
| 118 | } |
| 119 | else { |
| 120 | throw ('attributes.get(): Invalid number of parameters'); |
| 121 | } |
| 122 | } |
| 123 | }); |
| 124 | elementStack.push({ name: elementName, localName: localName, getChildElementsByTagName: _getChildElementsByTagName, getElementsByTagNameNS: _getElementsByTagNameNS, getChildElementsByTagNameNS: _getChildElementsByTagNameNS, attributes: attributes, childNodes: [], nsTable: {} }); |
| 125 | // Parse Attributes |
| 126 | if (x3.length > 0) { |
| 127 | var skip = false; |
| 128 | for (var j in x3) { |
| 129 | if (x3[j] == '/') { |
| 130 | // This is an empty Element |
| 131 | elementStack.peek().namespace = elementStack.peek().name == elementStack.peek().localName ? elementStack.getNamespace('*') : elementStack.getNamespace(elementStack.peek().name.substring(0, elementStack.peek().name.indexOf(':'))); |
| 132 | elementStack.peek().textContent = ''; |
| 133 | lastElement = elementStack.pop(); |
| 134 | skip = true; |
| 135 | break; |
| 136 | } |
| 137 | var k = x3[j].indexOf('='); |
| 138 | if (k > 0) { |
| 139 | var attrName = x3[j].substring(0, k); |
| 140 | var attrValue = x3[j].substring(k + 2, x3[j].length - 1); |
| 141 | var attrNS = elementStack.getNamespace('*'); |
| 142 | |
| 143 | if (attrName == 'xmlns') { |
| 144 | elementStack.addNamespace('*', attrValue); |
| 145 | attrNS = attrValue; |
| 146 | } else if (attrName.startsWith('xmlns:')) { |
| 147 | elementStack.addNamespace(attrName.substring(6), attrValue); |
| 148 | } else { |
| 149 | var ax = attrName.split(':'); |
| 150 | if (ax.length == 2) { attrName = ax[1]; attrNS = elementStack.getNamespace(ax[0]); } |
| 151 | } |
| 152 | var x = { name: attrName, value: attrValue } |
| 153 | if (attrNS != null) x.namespace = attrNS; |
| 154 | elementStack.peek().attributes.push(x); |
| 155 | } |
| 156 | } |
| 157 | if (skip) { continue; } |
| 158 | } |
| 159 | elementStack.peek().namespace = elementStack.peek().name == elementStack.peek().localName ? elementStack.getNamespace('*') : elementStack.getNamespace(elementStack.peek().name.substring(0, elementStack.peek().name.indexOf(':'))); |
| 160 | if (x2[1]) { elementStack.peek().textContent = x2[1]; } |
| 161 | } else { lastElement = elementStack.pop(); } |
| 162 | } |
| 163 | } |
| 164 | return lastElement; |
| 165 | } |
| 166 | |
| 167 | return obj; |
| 168 | }; |