@remoteshell/device 0.1.0-beta.6 → 0.1.0-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// #327 Windows owner/DACL query without starting nested PowerShell.
|
|
2
|
+
// Uses built-in cscript/WMI under the caller's OS token; read-only, fail closed.
|
|
3
|
+
import {spawnSync} from 'node:child_process';
|
|
4
|
+
import {win32} from 'node:path';
|
|
5
|
+
import {fileURLToPath} from 'node:url';
|
|
6
|
+
const DENY=()=>{throw Error('DEVICE_WINDOWS_ACL_DENIED');};
|
|
7
|
+
const SID=/^S-1-\d+(?:-\d+){1,15}$/;
|
|
8
|
+
export function parseWmiAclRows(output,user,{directory=false}={}){
|
|
9
|
+
if(typeof output!=='string'||output.length>8192||!SID.test(user||''))DENY();
|
|
10
|
+
const expected=directory?['file']:['parent','file'];
|
|
11
|
+
const lines=output.trim().split(/\r?\n/),result={};
|
|
12
|
+
let index=0;
|
|
13
|
+
for(const name of expected){
|
|
14
|
+
if(lines[index++]!==`begin=${name}`)DENY();
|
|
15
|
+
const ownerLine=lines[index++];
|
|
16
|
+
if(!ownerLine?.startsWith('owner='))DENY();
|
|
17
|
+
const owner=ownerLine.slice(6);
|
|
18
|
+
if(!SID.test(owner))DENY();
|
|
19
|
+
const protectedLine=lines[index++];
|
|
20
|
+
if(protectedLine!=='protected=true'&&protectedLine!=='protected=false')DENY();
|
|
21
|
+
const rules=[];
|
|
22
|
+
while(index<lines.length&&lines[index].startsWith('ace=')){
|
|
23
|
+
const match=/^ace=(S-1-\d+(?:-\d+){1,15}),(true|false),([01]),(\d{1,12})$/.exec(lines[index++]);
|
|
24
|
+
if(!match)DENY();
|
|
25
|
+
const rights=Number(match[4]);
|
|
26
|
+
if(!Number.isSafeInteger(rights))DENY();
|
|
27
|
+
rules.push({sid:match[1],inherited:match[2]==='true',
|
|
28
|
+
access:match[3]==='0'?'Allow':'Deny',rights});
|
|
29
|
+
if(rules.length>32)DENY();
|
|
30
|
+
}
|
|
31
|
+
if(lines[index++]!==`end=${name}`||rules.length===0)DENY();
|
|
32
|
+
result[name]={user,owner,protected:protectedLine==='protected=true',rules};
|
|
33
|
+
}
|
|
34
|
+
if(index!==lines.length)DENY();
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
export function inspectWmiAcl(target,{directory=false,run=spawnSync}={}){
|
|
38
|
+
const root=process.env.SystemRoot||'C:\\Windows';
|
|
39
|
+
const call=(name,args,timeout)=>run(win32.join(root,'System32',name),args,{
|
|
40
|
+
shell:false,windowsHide:true,encoding:'utf8',timeout,maxBuffer:8192,
|
|
41
|
+
stdio:['ignore','pipe','pipe']});
|
|
42
|
+
const who=call('whoami.exe',['/user','/fo','csv','/nh'],10000);
|
|
43
|
+
if(who.error||who.signal||who.status!==0)DENY();
|
|
44
|
+
const user=String(who.stdout||'').match(/S-1-\d+(?:-\d+){1,15}/)?.[0];
|
|
45
|
+
if(!SID.test(user||''))DENY();
|
|
46
|
+
const script=fileURLToPath(new URL('./windows-acl-wmi.js',import.meta.url));
|
|
47
|
+
const probe=call('cscript.exe',['//nologo',script,target,directory?'1':'0'],25000);
|
|
48
|
+
if(probe.error||probe.signal||probe.status!==0)DENY();
|
|
49
|
+
return parseWmiAclRows(probe.stdout,user,{directory});
|
|
50
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Windows JScript Host WMI ACL snapshot. Read-only; no ACL repair.
|
|
2
|
+
// Executes under the current Windows user token via cscript.exe.
|
|
3
|
+
try {
|
|
4
|
+
var target = String(WScript.Arguments.Item(0));
|
|
5
|
+
var directory = String(WScript.Arguments.Item(1)) === '1';
|
|
6
|
+
if (target.length > 1024 || !/^[A-Za-z]:\\/.test(target) ||
|
|
7
|
+
/['"\r\n]/.test(target) || (WScript.Arguments.Length !== 2))
|
|
8
|
+
throw new Error('PATH_DENIED');
|
|
9
|
+
var svc = new ActiveXObject('WbemScripting.SWbemLocator').ConnectServer('.', 'root\\cimv2');
|
|
10
|
+
var fso = new ActiveXObject('Scripting.FileSystemObject');
|
|
11
|
+
function inspect(path, label) {
|
|
12
|
+
var q = "SELECT * FROM Win32_LogicalFileSecuritySetting WHERE Path='" +
|
|
13
|
+
path.replace(/\\/g, '\\\\') + "'";
|
|
14
|
+
var found = new Enumerator(svc.ExecQuery(q)), count = 0, record = null;
|
|
15
|
+
for (; !found.atEnd(); found.moveNext()) { record = found.item(); count++; }
|
|
16
|
+
if (count !== 1) throw new Error('MATCH_DENIED');
|
|
17
|
+
var result = record.ExecMethod_('GetSecurityDescriptor');
|
|
18
|
+
if (result.Properties_.Item('ReturnValue').Value !== 0)
|
|
19
|
+
throw new Error('ACCESS_DENIED');
|
|
20
|
+
var descriptor = result.Properties_.Item('Descriptor').Value;
|
|
21
|
+
var owner = descriptor.Properties_.Item('Owner').Value.Properties_.Item('SIDString').Value;
|
|
22
|
+
var flags = descriptor.Properties_.Item('ControlFlags').Value;
|
|
23
|
+
var list = descriptor.Properties_.Item('DACL').Value.toArray();
|
|
24
|
+
if (list.length < 1 || list.length > 32) throw new Error('ACE_COUNT_DENIED');
|
|
25
|
+
WScript.Echo('begin=' + label);
|
|
26
|
+
WScript.Echo('owner=' + owner);
|
|
27
|
+
WScript.Echo('protected=' + ((flags & 4096) !== 0));
|
|
28
|
+
for (var i = 0; i < list.length; i++) {
|
|
29
|
+
var ace = list[i];
|
|
30
|
+
var sid = ace.Properties_.Item('Trustee').Value.Properties_.Item('SIDString').Value;
|
|
31
|
+
WScript.Echo('ace=' + sid + ',' +
|
|
32
|
+
((ace.Properties_.Item('AceFlags').Value & 16) !== 0) + ',' +
|
|
33
|
+
ace.Properties_.Item('AceType').Value + ',' +
|
|
34
|
+
ace.Properties_.Item('AccessMask').Value);
|
|
35
|
+
}
|
|
36
|
+
WScript.Echo('end=' + label);
|
|
37
|
+
}
|
|
38
|
+
if (!directory) inspect(fso.GetParentFolderName(target), 'parent');
|
|
39
|
+
inspect(target, 'file');
|
|
40
|
+
} catch (error) {
|
|
41
|
+
WScript.Echo('DEVICE_WINDOWS_ACL_DENIED');
|
|
42
|
+
WScript.Quit(1);
|
|
43
|
+
}
|
|
@@ -5,6 +5,7 @@ import {spawnSync} from 'node:child_process';
|
|
|
5
5
|
import {lstatSync,realpathSync} from 'node:fs';
|
|
6
6
|
import {win32} from 'node:path';
|
|
7
7
|
import {canonicalWindowsLocalFile,isWindowsChildPath} from './native-path-contract.mjs';
|
|
8
|
+
import {inspectWmiAcl} from './windows-acl-wmi-client.mjs';
|
|
8
9
|
const DENY=()=>{throw Error('DEVICE_WINDOWS_ACL_DENIED');};
|
|
9
10
|
const SID=/^S-1-\d+(?:-\d+){1,15}$/;
|
|
10
11
|
const SYSTEM='S-1-5-18';
|
|
@@ -67,6 +68,11 @@ export function inspectWindowsCredentialAcl({
|
|
|
67
68
|
stat.nlink!==1||stat.size<1||stat.size>8192)DENY();
|
|
68
69
|
if(win32.normalize(realpath(file)).toLowerCase()!==file.toLowerCase()||
|
|
69
70
|
win32.normalize(realpath(parent)).toLowerCase()!==parent.toLowerCase())DENY();
|
|
71
|
+
if(run===spawnSync){
|
|
72
|
+
const snapshot=inspectWmiAcl(file);
|
|
73
|
+
validateWindowsAclSnapshot(snapshot.parent);
|
|
74
|
+
return validateWindowsAclSnapshot(snapshot.file);
|
|
75
|
+
}
|
|
70
76
|
const systemRoot=process.env.SystemRoot||'C:\\Windows';
|
|
71
77
|
canonicalWindowsLocalFile(systemRoot+'\\System32');
|
|
72
78
|
const powerShell=win32.join(systemRoot,'System32','WindowsPowerShell','v1.0','powershell.exe');
|
|
@@ -93,6 +99,7 @@ export function inspectWindowsPrivateDirectoryAcl({directory,home,os=process.pla
|
|
|
93
99
|
const st=lstat(directory);
|
|
94
100
|
if(!st.isDirectory()||st.isSymbolicLink()||
|
|
95
101
|
win32.normalize(realpath(directory)).toLowerCase()!==directory.toLowerCase())DENY();
|
|
102
|
+
if(run===spawnSync)return validateWindowsAclSnapshot(inspectWmiAcl(directory,{directory:true}).file);
|
|
96
103
|
const systemRoot=process.env.SystemRoot||'C:\\Windows';
|
|
97
104
|
const ps=win32.join(systemRoot,'System32','WindowsPowerShell','v1.0','powershell.exe');
|
|
98
105
|
const child=run(ps,['-NoProfile','-NonInteractive','-EncodedCommand',
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remoteshell/device",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.8",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@remoteshell/device",
|
|
9
|
-
"version": "0.1.0-beta.
|
|
9
|
+
"version": "0.1.0-beta.8",
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@modelcontextprotocol/sdk": "1.32.1",
|
|
12
12
|
"@wonderwhy-er/desktop-commander": "0.3.1"
|
package/package.json
CHANGED
package/src/main.mjs
CHANGED
|
@@ -194,7 +194,9 @@ async function refreshDcCatalog({force=false}={}){
|
|
|
194
194
|
dcCatalogCheckedAt=Date.now();
|
|
195
195
|
const checkedAt=Date.now();
|
|
196
196
|
try{
|
|
197
|
-
|
|
197
|
+
// Windows DC startup and concurrent access inspection can delay first tools/list.
|
|
198
|
+
// Bound the wait without replacing or modifying original DC behavior.
|
|
199
|
+
const next=await discoverDcCatalog(client,{requestTimeoutMs:process.platform==='win32'?45000:12000});
|
|
198
200
|
console.info('RS_DC_DISCOVERY_TIMING',JSON.stringify({result:'ok',duration_ms:Date.now()-checkedAt,device_ref:deviceId.slice(0,8)}));
|
|
199
201
|
const diff=diffDcCatalog(dcCatalog||[],next);
|
|
200
202
|
const fingerprint=dcCatalogFingerprint(next);
|