win-info.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. Copyright 2019-2020 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. var promise = require('promise');
  14. function qfe()
  15. {
  16. try {
  17. var tokens = require('win-wmi').query('ROOT\\CIMV2', 'SELECT * FROM Win32_QuickFixEngineering');
  18. if (tokens[0]){
  19. for (var index = 0; index < tokens.length; index++) {
  20. for (var key in tokens[index]) {
  21. if (key.startsWith('__')) delete tokens[index][key];
  22. }
  23. }
  24. return (tokens);
  25. } else {
  26. return ([]);
  27. }
  28. } catch (ex) {
  29. return ([]);
  30. }
  31. }
  32. function av()
  33. {
  34. var result = [];
  35. try {
  36. var tokens = require('win-wmi-fixed').query('ROOT\\SecurityCenter2', 'SELECT * FROM AntiVirusProduct');
  37. if (tokens.length == 0) { return ([]); }
  38. // Process each antivirus product
  39. for (var i = 0; i < tokens.length; ++i) {
  40. var product = tokens[i];
  41. var modifiedPath = product.pathToSignedProductExe || '';
  42. // Expand environment variables (e.g., %ProgramFiles%)
  43. var regex = /%([^%]+)%/g;
  44. var match;
  45. while ((match = regex.exec(product.pathToSignedProductExe)) !== null) {
  46. var envVar = match[1];
  47. var envValue = process.env[envVar] || '';
  48. if (envValue) {
  49. modifiedPath = modifiedPath.replace(match[0], envValue);
  50. }
  51. }
  52. // Check if the executable exists (unless it's Windows Defender pseudo-path)
  53. var flag = true;
  54. if (modifiedPath !== 'windowsdefender://') {
  55. try {
  56. if (!require('fs').existsSync(modifiedPath)) {
  57. flag = false;
  58. }
  59. } catch (ex) {
  60. flag = false;
  61. }
  62. }
  63. // Only include products with valid executables
  64. if (flag) {
  65. var status = {};
  66. status.product = product.displayName || '';
  67. status.updated = (parseInt(product.productState) & 0x10) == 0;
  68. status.enabled = (parseInt(product.productState) & 0x1000) == 0x1000;
  69. result.push(status);
  70. }
  71. }
  72. return (result);
  73. } catch (ex) {
  74. return ([]);
  75. }
  76. }
  77. function defrag(options)
  78. {
  79. var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; });
  80. var path = '';
  81. switch(require('os').arch())
  82. {
  83. case 'x64':
  84. if (require('_GenericMarshal').PointerSize == 4)
  85. {
  86. // 32 Bit App on 64 Bit Windows
  87. ret._rej('Cannot defrag volume on 64 bit Windows from 32 bit application');
  88. return (ret);
  89. }
  90. else
  91. {
  92. // 64 Bit App
  93. path = process.env['windir'] + '\\System32\\defrag.exe';
  94. }
  95. break;
  96. case 'ia32':
  97. // 32 Bit App on 32 Bit Windows
  98. path = process.env['windir'] + '\\System32\\defrag.exe';
  99. break;
  100. default:
  101. ret._rej(require('os').arch() + ' not supported');
  102. return (ret);
  103. break;
  104. }
  105. ret.child = require('child_process').execFile(process.env['windir'] + '\\System32\\defrag.exe', ['defrag', options.volume + ' /A']);
  106. ret.child.promise = ret;
  107. ret.child.promise.options = options;
  108. ret.child.stdout.str = ''; ret.child.stdout.on('data', function (c) { this.str += c.toString(); });
  109. ret.child.stderr.str = ''; ret.child.stderr.on('data', function (c) { this.str += c.toString(); });
  110. ret.child.on('exit', function (code)
  111. {
  112. var lines = this.stdout.str.trim().split('\r\n');
  113. var obj = { volume: this.promise.options.volume };
  114. for (var i in lines)
  115. {
  116. var token = lines[i].split('=');
  117. if(token.length == 2)
  118. {
  119. switch(token[0].trim().toLowerCase())
  120. {
  121. case 'volume size':
  122. obj['size'] = token[1];
  123. break;
  124. case 'free space':
  125. obj['free'] = token[1];
  126. break;
  127. case 'total fragmented space':
  128. obj['fragmented'] = token[1];
  129. break;
  130. case 'largest free space size':
  131. obj['largestFragment'] = token[1];
  132. break;
  133. }
  134. }
  135. }
  136. this.promise._res(obj);
  137. });
  138. return (ret);
  139. }
  140. function regQuery(H, Path, Key)
  141. {
  142. try
  143. {
  144. return(require('win-registry').QueryKey(H, Path, Key));
  145. }
  146. catch(e)
  147. {
  148. return (null);
  149. }
  150. }
  151. function pendingReboot()
  152. {
  153. var tmp = null;
  154. var ret = null;
  155. var HKEY = require('win-registry').HKEY;
  156. if(regQuery(HKEY.LocalMachine, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing', 'RebootPending') !=null)
  157. {
  158. ret = 'Component Based Servicing';
  159. }
  160. else if(regQuery(HKEY.LocalMachine, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate', 'RebootRequired'))
  161. {
  162. ret = 'Windows Update';
  163. }
  164. else if ((tmp=regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager', 'PendingFileRenameOperations'))!=null && tmp != 0 && tmp != '')
  165. {
  166. ret = 'File Rename';
  167. }
  168. else if (regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName', 'ComputerName') != regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName', 'ComputerName'))
  169. {
  170. ret = 'System Rename';
  171. }
  172. return (ret);
  173. }
  174. function installedApps()
  175. {
  176. var promise = require('promise');
  177. var ret = new promise(function (a, r) { this._resolve = a; this._reject = r; });
  178. var code = "\
  179. var reg = require('win-registry');\
  180. var result = [];\
  181. var val, tmp;\
  182. var items = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall');\
  183. for (var key in items.subkeys)\
  184. {\
  185. val = {};\
  186. try\
  187. {\
  188. val.name = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'DisplayName');\
  189. }\
  190. catch(e)\
  191. {\
  192. continue;\
  193. }\
  194. try\
  195. {\
  196. val.version = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'DisplayVersion');\
  197. if (val.version == '') { delete val.version; }\
  198. }\
  199. catch(e)\
  200. {\
  201. }\
  202. try\
  203. {\
  204. val.location = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'InstallLocation');\
  205. if (val.location == '') { delete val.location; }\
  206. }\
  207. catch(e)\
  208. {\
  209. }\
  210. try\
  211. {\
  212. val.installdate = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'InstallDate');\
  213. if (val.installdate == '') { delete val.installdate; }\
  214. }\
  215. catch(e)\
  216. {\
  217. }\
  218. result.push(val);\
  219. }\
  220. console.log(JSON.stringify(result,'', 1));process.exit();";
  221. ret.child = require('child_process').execFile(process.execPath, [process.execPath.split('\\').pop().split('.exe')[0], '-exec "' + code + '"']);
  222. ret.child.promise = ret;
  223. ret.child.stdout.str = ''; ret.child.stdout.on('data', function (c) { this.str += c.toString(); });
  224. ret.child.on('exit', function (c) { this.promise._resolve(JSON.parse(this.stdout.str.trim())); });
  225. return (ret);
  226. }
  227. function installedStoreApps(){
  228. try {
  229. var tokens = require('win-wmi-fixed').query('ROOT\\CIMV2', 'SELECT * FROM Win32_InstalledStoreProgram');
  230. if (tokens[0]){
  231. for (var index = 0; index < tokens.length; index++) {
  232. for (var key in tokens[index]) {
  233. if (key.startsWith('__')) delete tokens[index][key];
  234. }
  235. }
  236. return (tokens);
  237. } else {
  238. return ([]);
  239. };
  240. } catch (ex) {
  241. return ([]);
  242. }
  243. }
  244. function defender(){
  245. try {
  246. var tokens = require('win-wmi').query('ROOT\\Microsoft\\Windows\\Defender', 'SELECT * FROM MSFT_MpComputerStatus', ['RealTimeProtectionEnabled','IsTamperProtected','AntivirusSignatureVersion','AntivirusSignatureLastUpdated']);
  247. if (tokens[0]){
  248. var info = { RealTimeProtection: tokens[0].RealTimeProtectionEnabled, TamperProtected: tokens[0].IsTamperProtected };
  249. if (tokens[0].AntivirusSignatureVersion) { info.AntivirusSignatureVersion = tokens[0].AntivirusSignatureVersion; }
  250. if (tokens[0].AntivirusSignatureLastUpdated) { info.AntivirusSignatureLastUpdated = tokens[0].AntivirusSignatureLastUpdated; }
  251. return (info);
  252. } else {
  253. return ({});
  254. }
  255. } catch (ex) {
  256. return ({});
  257. }
  258. }
  259. if (process.platform == 'win32')
  260. {
  261. module.exports = { qfe: qfe, av: av, defrag: defrag, pendingReboot: pendingReboot, installedApps: installedApps, installedStoreApps: installedStoreApps, defender: defender };
  262. }
  263. else
  264. {
  265. var not_supported = function () { throw (process.platform + ' not supported'); };
  266. module.exports = { qfe: not_supported, av: not_supported, defrag: not_supported, pendingReboot: not_supported, installedApps: not_supported, installedStoreApps: not_supported, defender: not_supported };
  267. }