mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-30 18:50:22 +08:00
fix: 全仓审计加固与死代码清理,版本 1.1.87
- 回滚/备份:空 bak 改名 .bad 跳过;升级刷新 bak 补上单数 marker 补丁;统一原子写 - 补丁命中:optional 命中不符记软失败;#42 插入型替换加探针防重复插入 - Windows:进程枚举改用 | 分隔 + UTF-8,修复中文路径换包不杀旧进程;换包失败拉起原版 - 安全:asar 解包防越界;红队路径校验 realpath 防空 symlink;更新仅 https;报告回放去换行注入 - 提示词钩子:停用后 rewriteFn 返回原装配文本并清理 assemble guard;缓存签名纳入红队/策略/override - 红队:listEngagements 默认不算统计;listVulns/报告支持 evidenceLimit - 子代理名额占位即时生效并回退;--apply 有失败项退出码 1 - 清理:删除 lib/mnemon-settings.js 及 13 处死代码/未使用 import;移除 test 目录与 test 脚本
This commit is contained in:
1 parent
b46c952584
commit
1a8195b4d4
48 files changed
+890
-1652
No files matched your search
+17
-81
@@ -41,27 +41,6 @@ export function writeRecord(dshHome, port, pid, extra = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
export function readRecord(dshHome, port) {
|
||||
try {
|
||||
const raw = JSON.parse(fs.readFileSync(recordPath(dshHome, port), "utf8"));
|
||||
if (!raw || typeof raw !== "object") return null;
|
||||
const pid = Number(raw.pid) || 0;
|
||||
if (!pid) return null;
|
||||
return { pid, port: Number(raw.port) || Number(port) || 3080, at: String(raw.at || "") };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function clearRecord(dshHome, port) {
|
||||
try {
|
||||
fs.unlinkSync(recordPath(dshHome, port));
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令行长什么样才算「我们自己起的 dsh web」。
|
||||
* 只认 bin.js 入口 + 带 web 子命令,避免误杀同机其它 node 进程。
|
||||
@@ -75,7 +54,7 @@ export function looksLikeDshWeb(cmdline) {
|
||||
/** win32 netstat -ano 的 LISTENING 行 → [{ address, port, pid }] */
|
||||
export function parseNetstatListeners(output) {
|
||||
const out = [];
|
||||
for (const line of String(output || "").split(/\r?\n/)) {
|
||||
for (const line of String(output || '').split(/\r?\n/)) {
|
||||
const m = line.match(/^\s*TCP\s+(\S+):(\d+)\s+\S+\s+LISTENING\s+(\d+)\s*$/i);
|
||||
if (!m) continue;
|
||||
out.push({ address: m[1], port: Number(m[2]), pid: Number(m[3]) });
|
||||
@@ -83,6 +62,17 @@ export function parseNetstatListeners(output) {
|
||||
return out;
|
||||
}
|
||||
|
||||
/** ss -ltnp 的输出里取监听某端口的 pid(users:(("node",pid=123,fd=22))) */
|
||||
export function parseSsListeners(output, port) {
|
||||
const want = Number(port) || 3080;
|
||||
const pids = [];
|
||||
for (const line of String(output || "").split(/\r?\n/)) {
|
||||
if (!line.includes(`:${want} `) && !line.includes(`:${want}\t`)) continue;
|
||||
for (const m of line.matchAll(/pid=(\d+)/g)) pids.push(Number(m[1]));
|
||||
}
|
||||
return [...new Set(pids.filter((n) => Number.isInteger(n) && n > 0))];
|
||||
}
|
||||
|
||||
/** lsof -nP -iTCP:PORT -sTCP:LISTEN -t 的输出 → pid 数组 */
|
||||
export function parseLsofPids(output) {
|
||||
return String(output || "")
|
||||
@@ -113,7 +103,11 @@ export function listenerPids(port) {
|
||||
const rows = parseNetstatListeners(run("netstat", ["-ano"]));
|
||||
return [...new Set(rows.filter((r) => r.port === p).map((r) => r.pid))];
|
||||
}
|
||||
return parseLsofPids(run("lsof", ["-nP", `-iTCP:${p}`, "-sTCP:LISTEN", "-t"]));
|
||||
const viaLsof = parseLsofPids(run("lsof", ["-nP", `-iTCP:${p}`, "-sTCP:LISTEN", "-t"]));
|
||||
if (viaLsof.length > 0) return viaLsof;
|
||||
/* 精简容器里常常没有 lsof(只有 ss):没有它时以前直接返回空,回收逻辑静默失效。 */
|
||||
const ss = run("ss", ["-ltnp"]);
|
||||
return parseSsListeners(ss, p);
|
||||
}
|
||||
|
||||
/** 取某个 pid 的完整命令行(拿不到就返回空串) */
|
||||
@@ -155,61 +149,3 @@ export function killPid(pid) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动前回收:先按记录杀,记录缺失/失效时再按端口兜底。
|
||||
* @returns {{ killed: number[], source: "record" | "port" | "" }}
|
||||
*/
|
||||
export function reclaimStaleWebServer({ port, dshHome, logger } = {}) {
|
||||
const p = Number(port) || 3080;
|
||||
const home = dshHome || dshHomeDir();
|
||||
const log = typeof logger === "function" ? logger : () => {};
|
||||
const killed = [];
|
||||
|
||||
const rec = readRecord(home, p);
|
||||
const listeners = listenerPids(p);
|
||||
let recStillAlive = false;
|
||||
if (rec && pidAlive(rec.pid)) {
|
||||
const cmd = cmdlineOf(rec.pid);
|
||||
// 命令行拿不到时,必须证明这个 pid 仍占着端口才杀:记录早于进程退出时,
|
||||
// pid 被系统复用后盲目 kill 会打到无关进程。
|
||||
const ownsPort = listeners.includes(rec.pid);
|
||||
if (looksLikeDshWeb(cmd) || (!cmd && ownsPort)) {
|
||||
if (killPid(rec.pid)) killed.push(rec.pid);
|
||||
}
|
||||
recStillAlive = pidAlive(rec.pid);
|
||||
}
|
||||
// 杀不掉就保留记录,下一次启动再试。
|
||||
if (rec && !recStillAlive) clearRecord(home, p);
|
||||
if (killed.length) {
|
||||
waitGone(killed);
|
||||
return { killed, source: "record" };
|
||||
}
|
||||
|
||||
for (const pid of listeners) {
|
||||
if (pid === process.pid) continue;
|
||||
const cmd = cmdlineOf(pid);
|
||||
if (!looksLikeDshWeb(cmd)) continue;
|
||||
if (killPid(pid)) killed.push(pid);
|
||||
}
|
||||
if (killed.length) {
|
||||
waitGone(killed);
|
||||
log(`[dsh-purge] reclaimed stale web server on :${p} (pid ${killed.join(",")})`);
|
||||
}
|
||||
return { killed, source: killed.length ? "port" : "" };
|
||||
}
|
||||
|
||||
/** taskkill / SIGKILL 返回不代表端口已释放;绑端口前等一下进程真正退出。 */
|
||||
export function waitGone(pids, timeoutMs = 1500) {
|
||||
const list = Array.isArray(pids) ? pids.filter((pid) => Number(pid) > 0) : [];
|
||||
if (!list.length) return;
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
const sleeper = new Int32Array(new SharedArrayBuffer(4));
|
||||
while (Date.now() < deadline && list.some((pid) => pidAlive(pid))) {
|
||||
try {
|
||||
Atomics.wait(sleeper, 0, 0, 80);
|
||||
} catch {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user