@ai-workbench/connect 0.7.55-rc.3 → 0.7.55-rc.5
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.
package/helper/remote-access.ps1
CHANGED
|
@@ -99,7 +99,9 @@ function Write-AiwbFile([string]$Path, [string]$Text, [string]$Sddl) {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
# entry.ps1(ForceCommand):cd 到 AW_EMP_CWD(失败回 $HOME);shell 选法同 DSH pwsh-local(PowerShell 7 安装位置 → PATH 中 pwsh.exe → Windows PowerShell 5.1);
|
|
102
|
-
# 有 SSH_ORIGINAL_COMMAND
|
|
102
|
+
# 有 SSH_ORIGINAL_COMMAND 则先关进度流、固定 UTF-8 输出(DSH pwsh-local 的前缀)再以 -EncodedCommand 执行并透传退出码,无则交互;stdin 原样继承。
|
|
103
|
+
# 关进度流:stderr 被管道重定向时,Windows PowerShell 5.1 把进度记录(如模块自动加载的“正在准备首次使用模块”)序列化成 CLIXML 写到 stderr,
|
|
104
|
+
# 混入主机 agent 读到的输出;在内层命令前设 $ProgressPreference='SilentlyContinue'(about_Preference_Variables)即不再产生(实机:外层设无效、内层设即无)。
|
|
103
105
|
function Get-AiwbEntry {
|
|
104
106
|
return @'
|
|
105
107
|
$ErrorActionPreference = 'Continue'
|
|
@@ -112,7 +114,7 @@ $candidates += "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
|
|
|
112
114
|
$shell = 'pwsh'
|
|
113
115
|
foreach ($c in $candidates) { if ([IO.File]::Exists($c)) { $shell = $c; break } }
|
|
114
116
|
if ($env:SSH_ORIGINAL_COMMAND) {
|
|
115
|
-
$pre = '[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); $OutputEncoding = [System.Text.UTF8Encoding]::new($false); '
|
|
117
|
+
$pre = '$ProgressPreference = ''SilentlyContinue''; [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); $OutputEncoding = [System.Text.UTF8Encoding]::new($false); '
|
|
116
118
|
$enc = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($pre + $env:SSH_ORIGINAL_COMMAND))
|
|
117
119
|
& $shell -NoLogo -NoProfile -NonInteractive -EncodedCommand $enc
|
|
118
120
|
exit $LASTEXITCODE
|
package/lib/private-files.js
CHANGED
|
@@ -12,7 +12,7 @@ async function windowsPrivate(path, directory, before) {
|
|
|
12
12
|
if (s.isSymbolicLink() || (directory ? !s.isDirectory() : !s.isFile() || s.nlink !== 1))
|
|
13
13
|
throw new Error(directory ? 'PRIVATE_DIRECTORY_REQUIRED' : 'PRIVATE_FILE_REQUIRED');
|
|
14
14
|
const acl = await host.security.inspect(path);
|
|
15
|
-
if (acl.reparse || !privateSddl(acl.sddl, host.security.sid()))
|
|
15
|
+
if (acl.reparse || !privateSddl(acl.sddl, host.security.sid(), directory))
|
|
16
16
|
throw new Error(directory ? 'PRIVATE_DIRECTORY_REQUIRED' : 'PRIVATE_FILE_REQUIRED');
|
|
17
17
|
return s;
|
|
18
18
|
}
|
package/lib/remote-access.d.ts
CHANGED
|
@@ -31,9 +31,17 @@ export declare function windowsSetSegment(text: string, user: string, programDat
|
|
|
31
31
|
export declare function windowsSegmentOk(text: string | undefined, user: string, programData: string): boolean;
|
|
32
32
|
/** 内联进 -EncodedCommand 的脚本正文:去掉整行注释、空行与行首缩进(只为缩短命令行,语句不变)。 */
|
|
33
33
|
export declare function inlineScript(script: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* -EncodedCommand 的内层正文:校验过的三个常量 + 一小段引导,引导把 gzip 后 base64 内联的脚本(UTF-8)在内存里解压,
|
|
36
|
+
* 以 scriptblock 在同一作用域执行(脚本末尾 exit 即进程退出码)。只为缩短命令行(CreateProcess/ShellExecuteEx 上限 32767 字符),
|
|
37
|
+
* 脚本语句不变、仍整段内联、不落盘。依据:.NET IO.Compression.GZipStream(RFC 1952,Node zlib.gzipSync 同格式)。
|
|
38
|
+
*/
|
|
39
|
+
export declare function windowsAdminBody(script: string, user: string, sid: string, key: string): string;
|
|
40
|
+
/** Windows PowerShell 命令行(CreateProcess)上限 32767 字符;断言与回报按其一半留余量。 */
|
|
41
|
+
export declare const WINDOWS_COMMAND_LINE_MAX = 32767;
|
|
34
42
|
/**
|
|
35
43
|
* Windows UAC 提权(设计 C):外层 powershell -Command 以 ShellExecuteEx “runas” 动词(ProcessStartInfo Verb=runas、UseShellExecute、隐藏窗口,
|
|
36
|
-
* 即 Start-Process -Verb RunAs 的同一底层调用)启动管理员 powershell
|
|
44
|
+
* 即 Start-Process -Verb RunAs 的同一底层调用)启动管理员 powershell 并等待,透传其退出码;内层正文见 windowsAdminBody(以 UTF-16LE base64 经 -EncodedCommand 内联),
|
|
37
45
|
* 参数为已校验的常量,不落到用户可写位置。用户在 UAC 点“否”时抛 Win32Exception 1223(ERROR_CANCELLED),外层以其 NativeErrorCode 退出。
|
|
38
46
|
* 不用 Start-Process 本身:Windows PowerShell 5.1 的 Start-Process 把 Win32Exception 换成只带本地化文字的 InvalidOperationException,丢了错误码(实机核过)。
|
|
39
47
|
*/
|
package/lib/remote-access.js
CHANGED
|
@@ -3,6 +3,7 @@ import { execFile } from 'node:child_process';
|
|
|
3
3
|
import { stat } from 'node:fs/promises';
|
|
4
4
|
import { connect } from 'node:net';
|
|
5
5
|
import { win32 } from 'node:path';
|
|
6
|
+
import { gzipSync } from 'node:zlib';
|
|
6
7
|
import { shellQuote } from "./ssh.js";
|
|
7
8
|
import { windowsSecurity } from "./windows-security.js";
|
|
8
9
|
import { NETWORK_DEFAULTS } from "./defaults.js";
|
|
@@ -75,9 +76,20 @@ export function windowsSegmentOk(text, user, programData) { return text !== unde
|
|
|
75
76
|
/** 内联进 -EncodedCommand 的脚本正文:去掉整行注释、空行与行首缩进(只为缩短命令行,语句不变)。 */
|
|
76
77
|
export function inlineScript(script) { return script.split(/\r?\n/).map(line => line.trim()).filter(line => line && !line.startsWith('#')).join('\n'); }
|
|
77
78
|
const psLiteral = (value) => "'" + value.replaceAll(/['\u2018\u2019\u201A\u201B]/g, match => match + match) + "'";
|
|
79
|
+
/**
|
|
80
|
+
* -EncodedCommand 的内层正文:校验过的三个常量 + 一小段引导,引导把 gzip 后 base64 内联的脚本(UTF-8)在内存里解压,
|
|
81
|
+
* 以 scriptblock 在同一作用域执行(脚本末尾 exit 即进程退出码)。只为缩短命令行(CreateProcess/ShellExecuteEx 上限 32767 字符),
|
|
82
|
+
* 脚本语句不变、仍整段内联、不落盘。依据:.NET IO.Compression.GZipStream(RFC 1952,Node zlib.gzipSync 同格式)。
|
|
83
|
+
*/
|
|
84
|
+
export function windowsAdminBody(script, user, sid, key) {
|
|
85
|
+
const packed = gzipSync(Buffer.from(inlineScript(script), 'utf8'), { level: 9 }).toString('base64');
|
|
86
|
+
return `$AiwbUser=${psLiteral(user)};$AiwbSid=${psLiteral(sid)};$AiwbKey=${psLiteral(key)};$z=New-Object IO.Compression.GZipStream((New-Object IO.MemoryStream(,[Convert]::FromBase64String('${packed}'))),[IO.Compression.CompressionMode]::Decompress);$t=(New-Object IO.StreamReader($z,[Text.Encoding]::UTF8)).ReadToEnd();. ([scriptblock]::Create($t))`;
|
|
87
|
+
}
|
|
88
|
+
/** Windows PowerShell 命令行(CreateProcess)上限 32767 字符;断言与回报按其一半留余量。 */
|
|
89
|
+
export const WINDOWS_COMMAND_LINE_MAX = 32767;
|
|
78
90
|
/**
|
|
79
91
|
* Windows UAC 提权(设计 C):外层 powershell -Command 以 ShellExecuteEx “runas” 动词(ProcessStartInfo Verb=runas、UseShellExecute、隐藏窗口,
|
|
80
|
-
* 即 Start-Process -Verb RunAs 的同一底层调用)启动管理员 powershell
|
|
92
|
+
* 即 Start-Process -Verb RunAs 的同一底层调用)启动管理员 powershell 并等待,透传其退出码;内层正文见 windowsAdminBody(以 UTF-16LE base64 经 -EncodedCommand 内联),
|
|
81
93
|
* 参数为已校验的常量,不落到用户可写位置。用户在 UAC 点“否”时抛 Win32Exception 1223(ERROR_CANCELLED),外层以其 NativeErrorCode 退出。
|
|
82
94
|
* 不用 Start-Process 本身:Windows PowerShell 5.1 的 Start-Process 把 Win32Exception 换成只带本地化文字的 InvalidOperationException,丢了错误码(实机核过)。
|
|
83
95
|
*/
|
|
@@ -87,8 +99,7 @@ export function windowsAdminCommand(script, user, sid, key, env = process.env) {
|
|
|
87
99
|
const normalized = employeeKey(key);
|
|
88
100
|
if (normalized === undefined || normalized !== key)
|
|
89
101
|
throw Error('AW_REMOTE_ACCESS_KEY_INVALID');
|
|
90
|
-
const
|
|
91
|
-
const encoded = Buffer.from(body, 'utf16le').toString('base64');
|
|
102
|
+
const encoded = Buffer.from(windowsAdminBody(script, user, sid, key), 'utf16le').toString('base64');
|
|
92
103
|
const outer = `try{$s=New-Object Diagnostics.ProcessStartInfo;$s.FileName=(Join-Path $PSHOME 'powershell.exe');$s.Arguments='-NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand ${encoded}';$s.Verb='runas';$s.UseShellExecute=$true;$s.WindowStyle='Hidden';$p=[Diagnostics.Process]::Start($s);$p.WaitForExit();exit $p.ExitCode}catch{$e=$_.Exception;while($e){if($e -is [ComponentModel.Win32Exception]){exit $e.NativeErrorCode};$e=$e.InnerException};exit 1}`;
|
|
93
104
|
return { command: `${env.SYSTEMROOT || env.windir || 'C:\\Windows'}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`, args: ['-NoProfile', '-NonInteractive', '-Command', outer] };
|
|
94
105
|
}
|
|
@@ -14,6 +14,10 @@ export interface WindowsSecurity {
|
|
|
14
14
|
export declare function parseWhoami(text: string): string | undefined;
|
|
15
15
|
/** SID 末段(RID):Windows 上与 macOS uid 同用途(节点名后缀)。 */
|
|
16
16
|
export declare function rid(sid: string): string;
|
|
17
|
-
/**
|
|
18
|
-
|
|
17
|
+
/**
|
|
18
|
+
* SDDL 判定:所有者是本人、每条 ACE 都是“允许”且对象只在 本人/SYSTEM/Administrators 之内;目录还须 DACL 受保护(不继承)。
|
|
19
|
+
* 文件不要求受保护:L 读的私有文件都在 L 已核为受保护私有的目录里,其继承项只能来自该目录;N 写入交接配置(network-config\config.json)
|
|
20
|
+
* 用“临时文件 + 改名”,新文件继承所在私有目录的 ACE(awtest 实机读回 D:AI(A;ID;FA;;;BA)(A;ID;FA;;;SY)(A;ID;FA;;;本人)),有效访问者同样只有三方。
|
|
21
|
+
*/
|
|
22
|
+
export declare function privateSddl(sddl: string, sid: string, directory?: boolean): boolean;
|
|
19
23
|
export declare const windowsSecurity: WindowsSecurity;
|
package/lib/windows-security.js
CHANGED
|
@@ -10,13 +10,17 @@ export function parseWhoami(text) { return /"(S-1-\d+(?:-\d+)+)"\s*$/.exec(text.
|
|
|
10
10
|
/** SID 末段(RID):Windows 上与 macOS uid 同用途(节点名后缀)。 */
|
|
11
11
|
export function rid(sid) { const last = /-(\d+)$/.exec(sid)?.[1]; if (!last)
|
|
12
12
|
throw Error('WINDOWS_SID_INVALID'); return last; }
|
|
13
|
-
/**
|
|
14
|
-
|
|
13
|
+
/**
|
|
14
|
+
* SDDL 判定:所有者是本人、每条 ACE 都是“允许”且对象只在 本人/SYSTEM/Administrators 之内;目录还须 DACL 受保护(不继承)。
|
|
15
|
+
* 文件不要求受保护:L 读的私有文件都在 L 已核为受保护私有的目录里,其继承项只能来自该目录;N 写入交接配置(network-config\config.json)
|
|
16
|
+
* 用“临时文件 + 改名”,新文件继承所在私有目录的 ACE(awtest 实机读回 D:AI(A;ID;FA;;;BA)(A;ID;FA;;;SY)(A;ID;FA;;;本人)),有效访问者同样只有三方。
|
|
17
|
+
*/
|
|
18
|
+
export function privateSddl(sddl, sid, directory = true) {
|
|
15
19
|
const owner = /^O:([^:]+?)(?=[GDS]:|$)/.exec(sddl)?.[1];
|
|
16
20
|
if (owner === undefined || (ALIAS[owner] ?? owner) !== sid)
|
|
17
21
|
return false;
|
|
18
22
|
const dacl = /D:([A-Z_]*)((?:\([^)]*\))*)$/.exec(sddl);
|
|
19
|
-
if (!dacl || !dacl[1].includes('P') || dacl[1].includes('NO_ACCESS_CONTROL'))
|
|
23
|
+
if (!dacl || directory && !dacl[1].includes('P') || dacl[1].includes('NO_ACCESS_CONTROL'))
|
|
20
24
|
return false;
|
|
21
25
|
const allowed = new Set([sid, SYSTEM, ADMINS]);
|
|
22
26
|
for (const ace of dacl[2].match(/\([^)]*\)/g) ?? []) {
|