amt-setupbin.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. Copyright 2020-2021 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @description Intel(R) AMT Setup.bin Parser
  15. * @author Ylian Saint-Hilaire
  16. * @version v0.1.0
  17. */
  18. var CreateAmtSetupBinStack = function () {
  19. var o = {};
  20. // Intel(R) AMT Setup.bin GUID's
  21. var AmtSetupBinSetupGuids = [
  22. '\xb5\x16\xfb\x71\x87\xcb\xf9\x4a\xb4\x41\xca\x7b\x38\x35\x78\xf9', // Version 1
  23. '\x96\xb2\x81\x58\xcf\x6b\x72\x4c\x8b\x91\xa1\x5e\x51\x2e\x99\xc4', // Version 2
  24. '\xa7\xf7\xf6\xc6\x89\xc4\xf6\x47\x93\xed\xe2\xe5\x02\x0d\xa5\x1d', // Version 3
  25. '\xaa\xa9\x34\x52\xe1\x29\xa9\x44\x8d\x4d\x08\x1c\x07\xb9\x63\x53' // Version 4
  26. ];
  27. // Notes about version 2 of setup.bin:
  28. // - Default "admin" must be followed by a new MEBx password
  29. // - ME_VARIABLE_IDENTIFIER_MANAGEABILITY_FEATURE_SELECTION may not appear after any CM settings
  30. // - CM_VARIABLE_IDENTIFIER_USER_DEFINED_CERT_ADD must be preceded by setting CM_VARIABLE_IDENTIFIER_USER_DEFINED_CERTS_CONFIG to (TODO!)
  31. // General notes:
  32. // - Setup.bin should always start with "CurrentMEBx Pwd", "newMebx Pwd", "manageability selection" (if present).
  33. // Intel(R) AMT variable identifiers
  34. // Type: 0 = Binar String, 1 = Char, 2 = Short, 3 = Int
  35. var AmtSetupBinVarIds =
  36. {
  37. 1: {
  38. 1: [0, "Current MEBx Password"],
  39. 2: [0, "New MEBx Password"],
  40. 3: [1, "Manageability Feature Selection",
  41. { 0: "None", 1: "Intel AMT" }],
  42. 4: [1, "Firmware Local Update", // 0 = Disabled, 1 = Enabled, 2 = Password Protected
  43. { 0: "Disabled", 1: "Enabled", 2: "Password Protected" }],
  44. 5: [1, "Firmware Update Qualifier", // 0 = Always, 1 = Never, 2 = Restricted
  45. { 0: "Always", 1: "Never", 2: "Restricted" }],
  46. 6: [4, "Power Package"] // GUID Length (16 bytes), Intel AMT version 2.1, 3 and 4
  47. },
  48. 2: {
  49. 1: [0, "Provisioning Preshared Key ID (PID)"],
  50. 2: [0, "Provisioning Preshared Key (PPS)"],
  51. 3: [0, "PKI DNS Suffix"], // 255 bytes max length
  52. 4: [0, "Configuration Server FQDN"], // 255 bytes max length
  53. 5: [1, "Remote Configuration Enabled (RCFG)", // 0 = Off, 1 = On
  54. { 0: "Off", 1: "On" }],
  55. 6: [1, "Pre-Installed Certificates Enabled", // 0 = Off, 1 = On
  56. { 0: "Off", 1: "On" }],
  57. 7: [1, "User Defined Certificate Configuration", // 0 = Disabled, 1 = Enabled, 2 = Delete
  58. { 0: "Disabled", 1: "Enabled", 2: "Delete" }],
  59. 8: [0, "User Defined Certificate Addition"], // 1 byte hash algo, 20 to 48 bytes hash, 1 byte name length, up to 32 bytes friendly name, 1 = SHA1 (20 bytes), 2 = SHA256 (32 bytes), 3 = SHA384 (48 bytes). Algo 2 & 3 are for version 3 and up.
  60. 10: [1, "SOL/IDER Redirection Configuration", {
  61. 0: "None", 1: "SOL only - User/Pass Disabled", 2: "IDER only - User/Pass Disabled", 3: "SOL+IDER - User/Pass Disabled",
  62. 4: "None - User/Pass Enabled", 5: "SOL only - User/Pass Enabled", 6: "IDER only - User/Pass Enabled", 7: "SOL+IDER - User/Pass Enabled"
  63. }],
  64. 11: [0, "Hostname"], // 63 bytes max length
  65. 12: [0, "Domain Name"], // 255 bytes max length
  66. 13: [1, "DHCP", { 1: "Disabled", 2: "Enabled" }],
  67. 14: [1, "Secure Firmware Update (SFWU)", // 0 = Disabled, 1 = Enabled
  68. { 0: "Disabled", 1: "Enabled" }],
  69. 15: [0, "ITO"],
  70. 16: [1, "Provisioning Mode (PM)", // 1 = Enterprise, 2 = Small Buisness (SMB)
  71. { 0: "Enterprise", 1: "Small Buisness" }],
  72. 17: [0, "Provisioning Server Address"],
  73. 18: [2, "Provision Server Port Number (PSPO)"],
  74. 19: [0, "Static IPv4 Parameters"],
  75. 20: [0, "VLAN"],
  76. 21: [0, "PASS Policy Flag"],
  77. 22: [0, "IPv6"], // Length is 204 bytes old format, 84 bytes new format, Version 3+ only
  78. 23: [1, "Shared/Dedicated FQDN", // 0 = Dedicated, 1 = Shared. This option is valid only if configuring the hostname as well
  79. { 0: "Dedicated", 1: "Shared" }],
  80. 24: [1, "Dynamic DNS Update", // 0 = Disabled, 1 = Enabled
  81. { 0: "Disabled", 1: "Enabled" }],
  82. 25: [1, "Remote Desktop (KVM) State", // 0 = Disabled, 1 = Enabled
  83. { 0: "Disabled", 1: "Enabled" }],
  84. 26: [1, "Opt-in User Consent Option", // 0 = Disabled, 1 = KVM, 0xFF = ALL
  85. { 0: "Disabled", 1: "KVM", 255: "All" }],
  86. 27: [1, "Opt-in Remote IT Consent Policy", // 0 = Disabled, 1 = Enabled. Allows user consent to be configured remotely.
  87. { 0: "Disabled", 1: "Enabled" }],
  88. 28: [1, "ME Provision Halt/Active", // 0 = Stop, 1 = Start. The "ME provisioning Halt/Activate" command must appear in the file only after "PKIDNSSuffix", "ConfigServerFQDN" and "Provisioning Server Address"
  89. { 0: "Stop", 1: "Start" }],
  90. 29: [1, "Manual Setup and Configuration", // 0 = Automated, 1 = Manual
  91. { 0: "Automated", 1: "Manual" }],
  92. 30: [3, "Support Channel Identifier"], // 4 bytes length. Support channel identifier (valid values: 1-65535)
  93. 31: [0, "Support Channel Description"], // 60 bytes max. Friendly name used to describe the party representedby the support channel identifier.
  94. 32: [0, "Service Account Number"], // 32 bytes max. Unique string identifier given to the end user by the service provider.
  95. 33: [0, "Enrollement Passcode"], // 32 bytes max
  96. 34: [3, "Service Type"], // 4 bytes length. 1 = Reactive, 2 = Proactive, 4 = One Time Session
  97. 35: [0, "Service Provider Identifier"] // GUID Length (16 bytes)
  98. }
  99. }
  100. // Parse the Setup.bin file
  101. o.AmtSetupBinCreate = function (version, flags) {
  102. var obj = {};
  103. obj.fileType = version;
  104. obj.recordChunkCount = 1; // TODO
  105. obj.recordHeaderByteCount = 46;
  106. obj.recordNumber = 0;
  107. obj.majorVersion = version;
  108. obj.minorVersion = 0;
  109. obj.flags = flags;
  110. obj.dataRecordsConsumed = 0;
  111. obj.dataRecordChunkCount = 1; // TODO
  112. obj.records = [];
  113. return obj;
  114. }
  115. // Parse the Setup.bin file
  116. o.AmtSetupBinDecode = function (file) {
  117. // Format of the setup file header:
  118. // FileTypeUUID(16) - uniquely identifies the file type. This identifier will remain valid and constant across all versions of the file type.
  119. // RecordChunkCount(2) - indicates the number of 512-byte chunks occupied by this record, including all header, body, and reserved fields.
  120. // RecordHeaderBytes(2) - indicates the length of the record header in bytes.
  121. // RecordNumber(4) - uniquely identifies the record among all records in the file. The field contains a non-negative ordinal value. The value of this field is always zero in the Local Provisioning File Header Record.
  122. // MajorVersion(1) - identifies the major version of the file format specification. This is a positive integer that is greater than or equal to 1. The Major Version number is incremented to indicate that changes have been introduced that will cause code written against a lower Major Version number to fail.
  123. // MinorVersion(1) - identifies the minor version of the file format specification. This is an integer that is greater than or equal to 0. The Minor Version number is incremented to indicate that changes have been introduced that will not cause code written against the same Major Version and a lower Minor Version number to fail. The purpose of this behavior is to allow a single local provisioning file to be used for multiple generations of Intel® AMT platform.
  124. // Flags (2) - file Flags, 1 = Do not consume records
  125. // DataRecordCount(4) - indicates the total number of data records written in the file when it was created.
  126. // DataRecordsConsumed(4) - is a counter value that begins at 0 and is incremented by 1 by each platform BIOS when it consumes a data record from the file. This value is used to determine the offset of the next data record in the file.
  127. // DataRecordChunkCount(2) - contains the number of 512-byte chunks in each data record. All data records are the same length.
  128. // Reserved (2) - reserved
  129. // ModuleList - contains a list of module identifiers. A module’s identifier appears in the list if and only if the data records contain entries for that module. Each module identifier is two bytes in length. The list is terminated by an identifier value of 0.
  130. var obj = {}, UUID = file.substring(0, 16);
  131. obj.fileType = 0;
  132. for (var i in AmtSetupBinSetupGuids) { if (UUID == AmtSetupBinSetupGuids[i]) obj.fileType = (+i + 1); }
  133. if (obj.fileType == 0) return; // Bad header
  134. obj.recordChunkCount = ReadShortX(file, 16);
  135. obj.recordHeaderByteCount = ReadShortX(file, 18);
  136. obj.recordNumber = ReadIntX(file, 20);
  137. obj.majorVersion = file.charCodeAt(24);
  138. obj.minorVersion = file.charCodeAt(25);
  139. obj.flags = ReadShortX(file, 26); // Flags: 1 = Do not consume records
  140. var dataRecordCount = ReadIntX(file, 28);
  141. obj.dataRecordsConsumed = ReadIntX(file, 32);
  142. obj.dataRecordChunkCount = ReadShortX(file, 36);
  143. obj.records = [];
  144. var ptr = 512;
  145. while (ptr + 512 <= file.length) {
  146. // Format of a data record header:
  147. // RecordTypeIdentifier(4) - identifies the type of record (in this case a data record). Record Identifiers: Invalid - 0, Data Record - 1
  148. // RecordFlags(4) - contains a set of bit flags that characterize the record.
  149. // RecordChunkCount(2) - contains the number of 512-byte chunks occupied by the record including all header, body, and reserved fields.
  150. // RecordHeaderByteCount(2) - indicates the length of the record header in bytes.
  151. // RecordNumber(4) - uniquely identifies the record among all records in the file, including invalid as well as valid records. The identifier is a non-negative integer.
  152. var r = {};
  153. r.typeIdentifier = ReadIntX(file, ptr);
  154. r.flags = ReadIntX(file, ptr + 4); // Flags: 1 = Valid, 2 = Scrambled
  155. r.chunkCount = ReadShortX(file, ptr + 8);
  156. r.headerByteCount = ReadShortX(file, ptr + 10);
  157. r.number = ReadIntX(file, ptr + 12);
  158. r.variables = [];
  159. var ptr2 = 0, recbin = file.substring(ptr + 24, ptr + 512);
  160. if ((r.flags & 2) != 0) { recbin = AmtSetupBinDescrambleRecordData(recbin); } // De-Scramble the record
  161. while (1) {
  162. // Format of a data record entry:
  163. // ModuleIdentifier(2) - identifies the target ME module for the entry.
  164. // VariableIdentifier(2) - an enumeration value that identifies the variable. Variable identifiers are unique to each ModuleIdentifier.
  165. // VariableLength(2) - is the length of the variable value in bytes.
  166. // VariableValue - is the value to be assigned to the variable.
  167. var v = {};
  168. v.moduleid = ReadShortX(recbin, ptr2);
  169. v.varid = ReadShortX(recbin, ptr2 + 2);
  170. if (v.moduleid == 0 || v.varid == 0) break;
  171. if (AmtSetupBinVarIds[v.moduleid][v.varid]) {
  172. v.length = ReadShortX(recbin, ptr2 + 4);
  173. v.type = AmtSetupBinVarIds[v.moduleid][v.varid][0];
  174. v.desc = AmtSetupBinVarIds[v.moduleid][v.varid][1];
  175. v.value = recbin.substring(ptr2 + 8, ptr2 + 8 + v.length);
  176. if (v.type == 1 && v.length == 1) v.value = v.value.charCodeAt(0); // 1 byte number
  177. else if (v.type == 2 && v.length == 2) v.value = ReadShortX(v.value, 0); // 2 byte number
  178. else if (v.type == 3 && v.length == 4) v.value = ReadIntX(v.value, 0); // 4 byte number
  179. else if (v.type == 4) v.value = guidToStr(rstr2hex(v.value)); // GUID
  180. r.variables.push(v);
  181. }
  182. ptr2 += (8 + (Math.floor((v.length + 3) / 4) * 4));
  183. }
  184. // Sort the variables
  185. r.variables.sort(AmtSetupBinVariableCompare);
  186. obj.records.push(r);
  187. ptr += 512;
  188. }
  189. if (dataRecordCount != obj.records.length) return; // Mismatch record count
  190. return obj;
  191. }
  192. // Construct a Setup.bin file
  193. o.AmtSetupBinEncode = function (obj) {
  194. if (obj.fileType < 1 && obj.fileType > AmtSetupBinSetupGuids.length) return null;
  195. var out = [], r = AmtSetupBinSetupGuids[obj.fileType - 1], reccount = 0;
  196. // Get the list of modules used
  197. var modulesInUse = [];
  198. for (var i in obj.records) { var rec = obj.records[i]; for (var j in rec.variables) { var v = rec.variables[j]; if (modulesInUse.indexOf(v.moduleid) == -1) { modulesInUse.push(v.moduleid); } } }
  199. r += ShortToStrX(obj.recordChunkCount);
  200. r += ShortToStrX(42 + (modulesInUse.length * 2)); // Header is 42 bytes long + 2 bytes for each additional modules in use.
  201. r += IntToStrX(obj.recordNumber);
  202. r += String.fromCharCode(obj.majorVersion, obj.minorVersion);
  203. r += ShortToStrX(obj.flags); // Flags: 1 = Do not consume records
  204. r += IntToStrX(obj.records.length);
  205. r += IntToStrX(obj.dataRecordsConsumed);
  206. r += ShortToStrX(obj.dataRecordChunkCount);
  207. r += ShortToStrX(0); // Reserved
  208. for (var i in modulesInUse) { r += ShortToStrX(modulesInUse[i]); } // Write each module in use. Needs to be null terminated, but the padding that follows will do that.
  209. while (r.length < 512) { r += '\0'; } // Pad the header
  210. out.push(r);
  211. // Write each record
  212. for (var i in obj.records) {
  213. var r2 = '', rec = obj.records[i];
  214. r2 += IntToStrX(rec.typeIdentifier);
  215. r2 += IntToStrX(rec.flags);
  216. r2 += IntToStrX(0); // Reserved
  217. r2 += IntToStrX(0); // Reserved
  218. r2 += ShortToStrX(1); // rec.chunkCount
  219. r2 += ShortToStrX(24); // rec.headerByteCount
  220. r2 += IntToStrX(++reccount);
  221. // Sort the variables
  222. rec.variables.sort(AmtSetupBinVariableCompare);
  223. /*
  224. // Change variable priority
  225. AmtSetupBinMoveToTop(r.variables, 1, 3); // Manageability Feature Selection
  226. AmtSetupBinMoveToTop(r.variables, 1, 2); // New MEBx password
  227. AmtSetupBinMoveToTop(r.variables, 1, 1); // Current MEBx password
  228. */
  229. // Write each variable
  230. for (var j in rec.variables) {
  231. var r3 = '', v = rec.variables[j], data = v.value;
  232. v.type = AmtSetupBinVarIds[v.moduleid][v.varid][0]; // Set the correct type if not alreay connect
  233. if ((v.type > 0) && (v.type < 4)) { // If this is a numeric value, encode it correctly
  234. data = parseInt(data);
  235. if (v.type == 1) data = String.fromCharCode(data);
  236. if (v.type == 2) data = ShortToStrX(data);
  237. if (v.type == 3) data = IntToStrX(data);
  238. }
  239. if (v.type == 4) { data = hex2rstr(guidToStr(data.split('-').join('')).split('-').join('')); }
  240. r3 += ShortToStrX(v.moduleid); // Module Identifier
  241. r3 += ShortToStrX(v.varid); // Variable Identifier
  242. r3 += ShortToStrX(data.length); // Variable Length
  243. r3 += ShortToStrX(0); // Reserved
  244. r3 += data; // Variable Data
  245. while (r3.length % 4 != 0) { r3 += '\0'; } // Pad the variable
  246. r2 += r3;
  247. }
  248. while (r2.length < 512) { r2 += '\0'; } // Pad the record
  249. if ((rec.flags & 2) != 0) { r2 = r2.substring(0, 24) + AmtSetupBinScrambleRecordData(r2.substring(24)); } // Scramble the record starting at byte 24, after the header
  250. out.push(r2);
  251. }
  252. return out.join('');
  253. }
  254. // Used to sort variables
  255. function AmtSetupBinVariableCompare(a, b) {
  256. if (a.moduleid > b.moduleid) return 1;
  257. if (a.moduleid < b.moduleid) return -1;
  258. if (a.varid > b.varid) return 1;
  259. if (a.varid < b.varid) return -1;
  260. return 0;
  261. }
  262. // Scramble and un-scramble records
  263. function AmtSetupBinScrambleRecordData(data) { var out = ''; for (var i = 0; i < data.length; i++) { out += String.fromCharCode((data.charCodeAt(i) + 17) & 0xFF); } return out; }
  264. function AmtSetupBinDescrambleRecordData(data) { var out = ''; for (var i = 0; i < data.length; i++) { out += String.fromCharCode((data.charCodeAt(i) + 0xEF) & 0xFF); } return out; }
  265. // Find a moduleid/varid in the variable list, if found, move it to the top
  266. //function AmtSetupBinMoveToTop(variables, moduleid, varid) { var i = -1; for (var j in variables) { if ((variables[j].moduleid == moduleid) && (variables[j].varid == varid)) { i = j; } } if (i > 1) { ArrayElementMove(variables, i, 0); } }
  267. function ShortToStrX(v) { return String.fromCharCode(v & 0xFF, (v >> 8) & 0xFF); };
  268. function IntToStrX(v) { return String.fromCharCode(v & 0xFF, (v >> 8) & 0xFF, (v >> 16) & 0xFF, (v >> 24) & 0xFF); };
  269. function ReadShortX(v, p) { return (v.charCodeAt(p + 1) << 8) + v.charCodeAt(p); };
  270. function ReadIntX(v, p) { return (v.charCodeAt(p + 3) * 0x1000000) + (v.charCodeAt(p + 2) << 16) + (v.charCodeAt(p + 1) << 8) + v.charCodeAt(p); };
  271. return o;
  272. };
  273. module.exports = CreateAmtSetupBinStack;