CH-J Server Managerمدیریت سرور از طریق SSH
منو
منبع منتشرشده

CH-J Server Manager

پوشه‌ها و فایل‌های یک نسخهٔ مشخص از برنامه را مرور کنید.

دانلود ZIP منبع
CH-J / Server Manager 368 فایل
core-20261017T201717Z-057348c4a70f افزوده‌شده: تاریخ ثبت نشده است · ساخته‌شده:
windowsPrivilegedExec.js افزوده‌شده: تاریخ ثبت نشده است ۴٫۲ KB · 76 خط
کد دانلود فایل
1"use strict";
3const { SessionError } = require("./sessionManager");
5// Runs fixed PowerShell scripts over an existing SSH session to Windows OpenSSH. The command line is only
6// a short -EncodedCommand bootstrap (valid for cmd.exe, Windows PowerShell and PowerShell 7 default shells and
7// far below the 8191 character cmd.exe limit). The script and one JSON argument object follow on stdin as
8// base64 lines, so user input is never concatenated into a command and secrets never reach a command line.
9// Windows has no sudo: the result reports whether the session token is elevated, and the scripts refuse
10// administrative work otherwise instead of trying to bypass UAC.
11const BOOTSTRAP = "$ErrorActionPreference='Stop';$ProgressPreference='SilentlyContinue';$i=[Console]::In;"
12 + "$c=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($i.ReadLine()));"
13 + "$a=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($i.ReadLine()));"
14 + "& ([ScriptBlock]::Create($c)) $a";
15const POWERSHELL = "powershell.exe";
17function encodedCommand(source, executable = POWERSHELL) {
18 return `${executable} -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand ${Buffer.from(source, "utf16le").toString("base64")}`;
21function bootstrapInput(script, args) {
22 const encode = (text) => Buffer.from(text, "utf8").toString("base64");
23 return `${encode(script)}\n${encode(JSON.stringify(args))}\n`;
26function cleanMessage(text, limit = 600) {
27 return String(text || "").replace(/[\x00-\x08\x0b-\x1f\x7f]/g, " ").replace(/\s+/g, " ").trim().slice(0, limit);
30// Parses the single result line; everything else (CLIXML progress, banners) is ignored.
31function parseWindowsOutput(stdout, stderr, code, errorPrefix = "USERS_") {
32 const lines = String(stdout || "").split(/\r?\n/);
33 const result = lines.find((line) => line.startsWith("CHJ_RESULT:"));
34 if (result) {
35 try { return JSON.parse(Buffer.from(result.slice(11), "base64").toString("utf8")); }
36 catch { throw new SessionError(`${errorPrefix}PROTOCOL_ERROR`, "The Windows helper returned unreadable data."); }
37 }
38 const error = lines.find((line) => line.startsWith("CHJ_ERR:"));
39 if (error) {
40 const match = error.match(/^CHJ_ERR:([A-Z0-9_]{2,64}):([A-Za-z0-9+/=]*)\s*$/);
41 if (match) throw new SessionError(`${errorPrefix}${match[1]}`, cleanMessage(Buffer.from(match[2], "base64").toString("utf8")) || match[1]);
42 }
43 const text = `${stdout || ""}\n${stderr || ""}`;
44 if (code === 9009 || /is not recognized as (an internal or external command|the name of a cmdlet)|powershell(\.exe)?: (command )?not found/i.test(text)) {
45 throw new SessionError(`${errorPrefix}POWERSHELL_UNAVAILABLE`, "Windows PowerShell (powershell.exe) could not be started over this SSH session.");
46 }
47 if (/language mode|ConstrainedLanguage|cannot be loaded because running scripts is disabled/i.test(text)) {
48 throw new SessionError(`${errorPrefix}POWERSHELL_RESTRICTED`, "PowerShell is restricted by policy (Constrained Language Mode or script blocking); CH-J does not bypass it.");
49 }
50 throw new SessionError(`${errorPrefix}PROTOCOL_ERROR`, `The Windows helper did not return a result (exit ${code}). ${cleanMessage(stderr.replace(/<[^>]+>/g, " "), 300)}`);
53class WindowsExecutor {
54 constructor(options) {
55 this.sessionManager = options.sessionManager;
56 this.logger = options.logger;
57 }
59 // args.op selects one fixed operation inside the script; the script validates every field again.
60 async run(sessionId, { script, args, timeoutMs = 120000, purpose, errorPrefix = "USERS_" }) {
61 const record = this.sessionManager._requireConnected(sessionId);
62 const channel = this.sessionManager._execStream(record, encodedCommand(BOOTSTRAP), {
63 timeoutMs,
64 outputLimit: 16 * 1024 * 1024,
65 onUpdate: (controller, kind) => { if (kind === "open") { controller.write(bootstrapInput(script, args)); controller.end(); } }
66 });
67 const result = await channel.done;
68 try { return parseWindowsOutput(result.stdout, result.stderr, result.code, errorPrefix); }
69 catch (error) {
70 this.logger?.warn("Windows remote operation failed.", { sessionId, purpose, code: error.code });
71 throw error;
72 }
73 }
76module.exports = { BOOTSTRAP, WindowsExecutor, bootstrapInput, encodedCommand, parseWindowsOutput };
SHA-2568fcc1951fc6d0294caed0b07d6311ded5c5d81452e13e8718727ca6a04b92153

CH-J Proprietary Software License 1.14 · کد منبع تحت CH-J Proprietary Software License 1.14 ارائه می‌شود. در دسترس بودن آن شرایط مجوز را تغییر نمی‌دهد و حق اضافه‌ای اعطا نمی‌کند.

SHA-256 بایگانیcf4455b698f0f28e456a37b1834976a0194d8486acb7101052d59a838745cca3
فایل‌ها