added domaininfo console command for windows

Bryan Roe committed Jun 5, 2022 at 16:50 UTC b167f2a7b73b35cfb2c790081ef9c8d9dfc56d75
1 file changed +121 -1
agents/meshcore.js
+121 -1
@@ -56,6 +56,73 @@ function bcdOK()
56 }
57 return (true);
58 }
59 +function getDomainInfo()
60 +{
61 + var hostname = require('os').hostname();
62 + var ret = { Name: hostname, Domain: "" };
63 +
64 + switch (process.platform)
65 + {
66 + case 'win32':
67 + try
68 + {
69 + ret = require('win-wmi').query('ROOT\\CIMV2', 'SELECT * FROM Win32_ComputerSystem', ['Name', 'Domain'])[0];
70 + }
71 + catch (x)
72 + {
73 + }
74 + break;
75 + case 'linux':
76 + var hasrealm = false;
77 +
78 + try
79 + {
80 + hasrealm = require('lib-finder').hasBinary('realm');
81 + }
82 + catch(x)
83 + {
84 + }
85 + if (hasrealm)
86 + {
87 + var child = require('child_process').execFile('/bin/sh', ['sh']);
88 + child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
89 + child.stdin.write("realm list | grep domain-name: | tr '\\n' '`' | ");
90 + child.stdin.write("awk -F'`' '{ ");
91 + child.stdin.write(' printf("[");');
92 + child.stdin.write(' ST="";');
93 + child.stdin.write(' for(i=1;i<NF;++i)');
94 + child.stdin.write(' {');
95 + child.stdin.write(' match($i,/domain-name: /);');
96 + child.stdin.write(' printf("%s\\"%s\\"", ST, substr($i, RSTART+RLENGTH));');
97 + child.stdin.write(' ST=",";');
98 + child.stdin.write(' }');
99 + child.stdin.write(' printf("]");');
100 + child.stdin.write(" }'");
101 + child.stdin.write('\nexit\n');
102 + child.waitExit();
103 + var names = [];
104 + try
105 + {
106 + names = JSON.parse(child.stdout.str);
107 + }
108 + catch(e)
109 + {
110 + }
111 + while(names.length>0)
112 + {
113 + if(hostname.endsWith('.' + names.peek()))
114 + {
115 + ret = { Name: hostname.substring(0, hostname.length - names.peek().length - 1), Domain: names.peek() };
116 + break;
117 + }
118 + names.pop();
119 + }
120 + }
121 + break;
122 + }
123 + return (ret);
124 +}
125 +
126
127
128 try {
@@ -3268,7 +3335,7 @@ function processConsoleCommand(cmd, args, rights, sessionid) {
3335 var response = null;
3336 switch (cmd) {
3337 case 'help': { // Displays available commands
3271 - var fin = '', f = '', availcommands = 'translations,agentupdate,errorlog,msh,timerinfo,coreinfo,coredump,service,fdsnapshot,fdcount,startupoptions,alert,agentsize,versions,help,info,osinfo,args,print,type,dbkeys,dbget,dbset,dbcompact,eval,parseuri,httpget,wslist,plugin,wsconnect,wssend,wsclose,notify,ls,ps,kill,netinfo,location,power,wakeonlan,setdebug,smbios,rawsmbios,toast,lock,users,openurl,getscript,getclip,setclip,log,av,cpuinfo,sysinfo,apf,scanwifi,wallpaper,agentmsg,task';
3338 + var fin = '', f = '', availcommands = 'domain,translations,agentupdate,errorlog,msh,timerinfo,coreinfo,coredump,service,fdsnapshot,fdcount,startupoptions,alert,agentsize,versions,help,info,osinfo,args,print,type,dbkeys,dbget,dbset,dbcompact,eval,parseuri,httpget,wslist,plugin,wsconnect,wssend,wsclose,notify,ls,ps,kill,netinfo,location,power,wakeonlan,setdebug,smbios,rawsmbios,toast,lock,users,openurl,getscript,getclip,setclip,log,av,cpuinfo,sysinfo,apf,scanwifi,wallpaper,agentmsg,task';
3339 if (require('os').dns != null) { availcommands += ',dnsinfo'; }
3340 try { require('linux-dhcp'); availcommands += ',dhcp'; } catch (ex) { }
3341 if (process.platform == 'win32')
@@ -3290,6 +3357,59 @@ function processConsoleCommand(cmd, args, rights, sessionid) {
3357 response = "Available commands: \r\n" + fin + ".";
3358 break;
3359 }
3360 + case 'domain':
3361 + response = getDomainInfo();
3362 + break;
3363 + case 'domaininfo':
3364 + {
3365 + if(process.platform != 'win32')
3366 + {
3367 + response = 'Unknown command "cs", type "help" for list of avaialble commands.';
3368 + break;
3369 + }
3370 + if(global._domainQuery != null)
3371 + {
3372 + response = "There is already an outstanding Domain Controller Query... Please try again later...";
3373 + break;
3374 + }
3375 +
3376 + sendConsoleText('Querying Domain Controller... This can take up to 60 seconds. Please wait...', sessionid);
3377 + global._domainQuery = require('win-wmi').queryAsync('ROOT\\CIMV2', 'SELECT * FROM Win32_NTDomain');
3378 + global._domainQuery.session = sessionid;
3379 + global._domainQuery.then(function (v)
3380 + {
3381 + var results = [];
3382 + if (Array.isArray(v))
3383 + {
3384 + var i;
3385 + var r;
3386 + for (i = 0; i < v.length; ++i)
3387 + {
3388 + r = {};
3389 + if (v[i].DomainControllerAddress != null) { r.DomainControllerAddress = v[i].DomainControllerAddress.split('\\').pop(); }
3390 + if (r.DomainControllerName != null) { r.DomainControllerName = v[i].DomainControllerName.split('\\').pop(); }
3391 + r.DomainGuid = v[i].DomainGuid;
3392 + r.DomainName = v[i].DomainName;
3393 + if (r.DomainGuid != null)
3394 + {
3395 + results.push(r);
3396 + }
3397 + }
3398 + }
3399 + if (results.length > 0)
3400 + {
3401 + sendConsoleText('Domain Controller Results:', this.session);
3402 + sendConsoleText(JSON.stringify(results, null, 1), this.session);
3403 + sendConsoleText('End of results...', this.session);
3404 + }
3405 + else
3406 + {
3407 + sendConsoleText('Domain Controller: No results returned. Is the domain controller reachable?', this.session);
3408 + }
3409 + global._domainQuery = null;
3410 + });
3411 + break;
3412 + }
3413 case 'translations': {
3414 response = JSON.stringify(coretranslations, null, 2);
3415 break;