win-volumes.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright 2022 Intel Corporation
  3. @author Bryan Roe
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. function trimObject(j)
  15. {
  16. var i;
  17. for(i in j)
  18. {
  19. if (j[i] == null || (typeof(j[i])=='string' && j[i]=='') || i.startsWith('__')) { delete j[i];}
  20. }
  21. if (j['SerialNumber'] < 0) { var tmp = Buffer.alloc(4); tmp.writeInt32LE(j['SerialNumber']); j['SerialNumber'] = tmp.readUInt32LE(); }
  22. return (j);
  23. }
  24. function getVolumes()
  25. {
  26. var v = require('win-wmi').query('ROOT\\CIMV2', 'SELECT * FROM Win32_Volume');
  27. var i;
  28. var ret = {};
  29. for (i in v)
  30. {
  31. ret[v[i].DeviceID] = trimObject(v[i]);
  32. }
  33. try {
  34. v = require('win-wmi').query('ROOT\\CIMV2\\Security\\MicrosoftVolumeEncryption', 'SELECT * FROM Win32_EncryptableVolume');
  35. for (i in v)
  36. {
  37. var tmp = trimObject(v[i]);
  38. for (var k in tmp)
  39. {
  40. ret[tmp.DeviceID][k] = tmp[k];
  41. }
  42. }
  43. } catch (ex) { }
  44. return (ret);
  45. }
  46. function windows_volumes()
  47. {
  48. var promise = require('promise');
  49. var p1 = new promise(function (res, rej) { this._res = res; this._rej = rej; });
  50. var ret = {};
  51. var values = require('win-wmi').query('ROOT\\CIMV2', 'SELECT * FROM Win32_LogicalDisk', ['DeviceID', 'VolumeName', 'FileSystem', 'Size', 'FreeSpace', 'DriveType']);
  52. if(values[0]){
  53. for (var i = 0; i < values.length; ++i) {
  54. var drive = values[i]['DeviceID'].slice(0,-1);
  55. ret[drive] = {
  56. name: (values[i]['VolumeName'] ? values[i]['VolumeName'] : ""),
  57. type: (values[i]['FileSystem'] ? values[i]['FileSystem'] : "Unknown"),
  58. size: (values[i]['Size'] ? values[i]['Size'] : 0),
  59. sizeremaining: (values[i]['FreeSpace'] ? values[i]['FreeSpace'] : 0),
  60. removable: (values[i]['DriveType'] == 2),
  61. cdrom: (values[i]['DriveType'] == 5)
  62. };
  63. }
  64. }
  65. try {
  66. values = require('win-wmi').query('ROOT\\CIMV2\\Security\\MicrosoftVolumeEncryption', 'SELECT * FROM Win32_EncryptableVolume', ['DriveLetter','ConversionStatus','ProtectionStatus']);
  67. if(values[0]){
  68. for (var i = 0; i < values.length; ++i) {
  69. var drive = values[i]['DriveLetter'].slice(0,-1);
  70. var statuses = {
  71. 0: 'FullyDecrypted',
  72. 1: 'FullyEncrypted',
  73. 2: 'EncryptionInProgress',
  74. 3: 'DecryptionInProgress',
  75. 4: 'EncryptionPaused',
  76. 5: 'DecryptionPaused'
  77. };
  78. ret[drive].volumeStatus = statuses.hasOwnProperty(values[i].ConversionStatus) ? statuses[values[i].ConversionStatus] : 'FullyDecrypted';
  79. ret[drive].protectionStatus = (values[i].ProtectionStatus == 0 ? 'Off' : (values[i].ProtectionStatus == 1 ? 'On' : 'Unknown'));
  80. try {
  81. var foundIDMarkedLine = false, foundMarkedLine = false, identifier = '', password = '';
  82. var keychild = require('child_process').execFile(process.env['windir'] + '\\system32\\cmd.exe', ['/c', 'manage-bde -protectors -get ' + drive + ': -Type recoverypassword'], {});
  83. keychild.stdout.str = ''; keychild.stdout.on('data', function (c) { this.str += c.toString(); });
  84. keychild.waitExit();
  85. var lines = keychild.stdout.str.trim().split('\r\n');
  86. for (var x = 0; x < lines.length; x++) { // Loop each line
  87. var abc = lines[x].trim();
  88. var englishidpass = (abc !== '' && abc.includes('Numerical Password:')); // English ID
  89. var germanidpass = (abc !== '' && abc.includes('Numerisches Kennwort:')); // German ID
  90. var frenchidpass = (abc !== '' && abc.includes('Mot de passe num')); // French ID
  91. var englishpass = (abc !== '' && abc.includes('Password:') && !abc.includes('Numerical Password:')); // English Password
  92. var germanpass = (abc !== '' && abc.includes('Kennwort:') && !abc.includes('Numerisches Kennwort:')); // German Password
  93. var frenchpass = (abc !== '' && abc.includes('Mot de passe :') && !abc.includes('Mot de passe num')); // French Password
  94. if (englishidpass || germanidpass || frenchidpass|| englishpass || germanpass || frenchpass) {
  95. var nextline = lines[x + 1].trim();
  96. if (x + 1 < lines.length && (nextline !== '' && (nextline.startsWith('ID:') || nextline.startsWith('ID :')) )) {
  97. identifier = nextline.replace('ID:','').replace('ID :', '').trim();
  98. foundIDMarkedLine = true;
  99. }else if (x + 1 < lines.length && nextline !== '') {
  100. password = nextline;
  101. foundMarkedLine = true;
  102. }
  103. }
  104. }
  105. ret[drive].identifier = (foundIDMarkedLine ? identifier : ''); // Set Bitlocker Identifier
  106. ret[drive].recoveryPassword = (foundMarkedLine ? password : ''); // Set Bitlocker Password
  107. } catch(ex) { } // just carry on as we cant get bitlocker key
  108. }
  109. }
  110. p1._res(ret);
  111. } catch (ex) { p1._res(ret); } // just return volumes as cant get encryption/bitlocker
  112. return (p1);
  113. }
  114. module.exports = {
  115. getVolumes: function () { try { return (getVolumes()); } catch (x) { return ({}); } },
  116. volumes_promise: windows_volumes
  117. };