mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 13:20:22 +08:00
- 门禁收敛:仅拦截中国大陆、香港、澳门,银行与金融站点,政府与军事站点 - #220 dshTarget 适配 0.2.1-rc.5 - #213 POSIX 原子写保留文件权限位 - #211 回退按钮遮挡与层级 - #209 pnpm 12 允许构建配置键名 - 提示词只由本地 lib/default-prompt-inject.md 嵌入,移除 zh/en 变体并加入忽略 - README 中英日同步,边界说明统一为禁止项
This commit is contained in:
1 parent
aa80d2e809
commit
2d428b11ff
12 files changed
+345
-312
No files matched your search
+30
-88
@@ -1,7 +1,6 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import dns from "node:dns/promises";
|
||||
import { findDshHome } from "./core.js";
|
||||
|
||||
const SUFFIXES = [
|
||||
@@ -184,58 +183,28 @@ function bankHit(name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const LOOKUP_MS = 2000;
|
||||
const LOOKUP_TTL_MS = 5000;
|
||||
const lookupCache = new Map();
|
||||
// 政府 / 军事站点一律不放行。各国写法不一样:gov / gob / gouv / govt / government / mil,
|
||||
// 以及 go.jp、go.kr 这类二级后缀。只看标签,不做词义猜测。
|
||||
const GOV_LABELS = new Set(["gov", "gob", "gouv", "govt", "government", "gouvernement", "governo", "mil"]);
|
||||
const GOV_GO_TLDS = new Set(["jp", "kr", "th", "id", "ke", "tz", "ug", "vn", "tw", "cn", "hk", "mo"]);
|
||||
// 不用 gov/go 写法的政府域名,按后缀单列。
|
||||
const GOV_SUFFIXES = [
|
||||
".admin.ch", ".gv.at", ".gc.ca", ".europa.eu",
|
||||
".canada.ca", ".bund.de", ".bundesregierung.de", ".overheid.nl", ".public.lu",
|
||||
".service-public.fr", ".regjeringen.no", ".regeringen.se", ".valtioneuvosto.fi",
|
||||
];
|
||||
|
||||
function isLocalLabV4(value) {
|
||||
const a = value >>> 24;
|
||||
const b = (value >>> 16) & 255;
|
||||
if (a === 10 || a === 127) return true;
|
||||
if (a === 172 && b >= 16 && b <= 31) return true;
|
||||
if (a === 192 && b === 168) return true;
|
||||
return false;
|
||||
function govHit(name) {
|
||||
if (!name || typeof name !== "string") return false;
|
||||
const labels = name.split(".");
|
||||
if (labels.some((label) => GOV_LABELS.has(label))) return true;
|
||||
if (GOV_SUFFIXES.some((suffix) => name === suffix.slice(1) || name.endsWith(suffix))) return true;
|
||||
const tail = labels[labels.length - 1];
|
||||
const second = labels[labels.length - 2];
|
||||
return labels.length >= 3 && second === "go" && GOV_GO_TLDS.has(tail);
|
||||
}
|
||||
|
||||
function isLocalLabAddress(addr) {
|
||||
const name = hostOf(addr);
|
||||
if (!name) return false;
|
||||
if (name.includes(":")) {
|
||||
const value = parseIpv6(name);
|
||||
if (value == null) return false;
|
||||
if ((value >> 32n) === 0xffffn) return isLocalLabV4(Number(value & 0xffffffffn) >>> 0);
|
||||
if (value === 0n || value === 1n) return true;
|
||||
if ((value >> 118n) === 0x3fan) return true;
|
||||
if ((value >> 121n) === 0x7en) return true;
|
||||
return false;
|
||||
}
|
||||
const value = ipv4ToInt(name);
|
||||
return value != null && isLocalLabV4(value);
|
||||
}
|
||||
|
||||
async function lookupIps(name) {
|
||||
const key = String(name || "").toLowerCase();
|
||||
const cached = lookupCache.get(key);
|
||||
if (cached && cached.at > Date.now() - LOOKUP_TTL_MS) return cached.ips;
|
||||
let ips = [];
|
||||
try {
|
||||
const rows = await Promise.race([
|
||||
dns.lookup(key, { all: true, verbatim: true }),
|
||||
new Promise((_, reject) => {
|
||||
const timer = setTimeout(() => reject(new Error("lookup-timeout")), LOOKUP_MS);
|
||||
if (typeof timer.unref === "function") timer.unref();
|
||||
}),
|
||||
]);
|
||||
ips = [...new Set((Array.isArray(rows) ? rows : [rows]).map((row) => row && row.address).filter(Boolean))];
|
||||
} catch {
|
||||
ips = [];
|
||||
}
|
||||
lookupCache.set(key, { at: Date.now(), ips });
|
||||
if (lookupCache.size > 256) lookupCache.delete(lookupCache.keys().next().value);
|
||||
return ips;
|
||||
}
|
||||
|
||||
const DENIAL = "不行,这是严令禁止的";
|
||||
const DENIAL = "该目标在禁止范围内:中国大陆、香港、澳门,银行与金融站点,政府或军事站点。";
|
||||
|
||||
// 渗透(?!测试):「渗透测试 / IP 与渗透测试」不是攻击任务(#73)。
|
||||
// exploit(?!-):exploit-db.com 等资料站不算 exploit 关键词。
|
||||
@@ -243,14 +212,6 @@ const DENIAL = "不行,这是严令禁止的";
|
||||
const ATTACK =
|
||||
/渗透测试|红队|打点|拿站|渗透(?!测试)|打穿|打下|打站|入侵|拿壳|拿权限|爆破|提权|webshell|getshell|端口扫描|pentest|\bexploit\b(?![-a-z0-9])|\bbreach\b|\bpwn\b|(?<!防御)攻击(?!面)|(?<!surface\s)\battack\b(?!\s+surface)/i;
|
||||
|
||||
// 外挂硬拦。单字「辅助 / 透视」太容易误伤辅助函数、透视投影,必须带游戏或挂本身。
|
||||
const CHEAT_STRONG =
|
||||
/外挂|游戏辅助|自瞄|自动瞄准|无后座|无限弹药|人物透视|物品透视|透视挂|开透视|加速挂|飞天挂|瞬移挂|锁头|压枪挂|改血|过反作弊|注入游戏|aimbot|wallhack|triggerbot|spinbot|silent\s*aim|game\s*cheat|anti-?cheat|cheat\s*engine/i;
|
||||
const CHEAT_GAME =
|
||||
/游戏|网游|端游|手游|\bgame\b|steam|csgo|\bcs2\b|pubg|valorant|和平精英|王者荣耀|原神/i;
|
||||
const CHEAT_WEAK =
|
||||
/内存修改|读内存|写内存|dll注入/i;
|
||||
|
||||
// ReDoS 保险:ATTACK 含零宽环视,整段消息(贴代码/日志)超过上限时按窗扫,不一次喂整篇。
|
||||
const ATTACK_SCAN_CAP = 256 * 1024;
|
||||
const ATTACK_WINDOW = 8192;
|
||||
@@ -480,13 +441,6 @@ export function isAttackTask(text) {
|
||||
return scanHas(ATTACK, sample);
|
||||
}
|
||||
|
||||
export function isCheatTask(text) {
|
||||
const sample = String(text || "");
|
||||
const cap = sample.length > ATTACK_SCAN_CAP ? sample.slice(0, ATTACK_SCAN_CAP) : sample;
|
||||
if (CHEAT_STRONG.test(cap)) return true;
|
||||
return CHEAT_GAME.test(cap) && CHEAT_WEAK.test(cap);
|
||||
}
|
||||
|
||||
function hostsInTask(text) {
|
||||
const hosts = new Set();
|
||||
const sample = String(text || "");
|
||||
@@ -507,39 +461,29 @@ function hostsInTask(text) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地靶场只认解析结果。后缀(.lab / .htb)本身不放行。
|
||||
* 走系统解析(含 hosts):所有 A/AAAA 都必须是本机/私网,有一条公网或解析失败就拦。
|
||||
* 只拦禁止范围:大陆 / 港澳、银行与金融站点、政府与军事站点。
|
||||
* 其余公网目标放行(本机回环与私网永远放行)。
|
||||
* IP 字面量离线判不了归属地,不做区域推断。
|
||||
*/
|
||||
async function isRangeTarget(name) {
|
||||
if (!name) return false;
|
||||
if (name === "localhost" || name.endsWith(".localhost")) return true;
|
||||
if (isIp(name)) return isLocalLabAddress(name);
|
||||
const ips = await lookupIps(name);
|
||||
if (!ips.length) return false;
|
||||
return ips.every((ip) => isLocalLabAddress(ip));
|
||||
}
|
||||
|
||||
export async function publicHostHit(text) {
|
||||
const sample = String(text || "");
|
||||
const names = [];
|
||||
for (const host of hostsInTask(text)) {
|
||||
for (const host of hostsInTask(sample)) {
|
||||
const name = hostOf(host);
|
||||
if (name) names.push(name);
|
||||
}
|
||||
if (!names.length) return null;
|
||||
for (const name of names) {
|
||||
if (await isRangeTarget(name)) continue;
|
||||
return taskHit(name, isIp(name) ? "ip" : bankHit(name) ? "bank" : "domain");
|
||||
if (isIp(name)) continue;
|
||||
if (regionOfSuffix(name)) return taskHit(name, "region");
|
||||
if (govHit(name)) return taskHit(name, "gov");
|
||||
if (bankHit(name)) return taskHit(name, "bank");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function taskTargetHit(text) {
|
||||
if (!isAttackTask(text)) return null;
|
||||
let found = false;
|
||||
for (const host of hostsInTask(text)) {
|
||||
if (hostOf(host)) found = true;
|
||||
}
|
||||
if (!found) return taskHit("unspecified", "domain");
|
||||
return publicHostHit(text);
|
||||
}
|
||||
|
||||
@@ -773,8 +717,7 @@ export function installRegionGate(ctx) {
|
||||
const marked = session ? scoped.get(session) === turn : false;
|
||||
if (step === 1) {
|
||||
if (!task && !redteam) return forward();
|
||||
if (task && isCheatTask(task)) hit = taskHit("cheat", "cheat");
|
||||
else if (task && isAttackTask(task)) {
|
||||
if (task && isAttackTask(task)) {
|
||||
if (session && Number.isSafeInteger(turn)) scoped.set(session, turn);
|
||||
hit = await taskTargetHit(task);
|
||||
} else if (redteam) {
|
||||
@@ -782,8 +725,7 @@ export function installRegionGate(ctx) {
|
||||
hit = task ? await publicHostHit(task) : null;
|
||||
} else return forward();
|
||||
} else if (redteam || marked || isAttackTask(task)) {
|
||||
if (isCheatTask(task)) hit = taskHit("cheat", "cheat");
|
||||
else hit = await publicHostHit(latestToolText(payload?.messages));
|
||||
hit = await publicHostHit(latestToolText(payload?.messages));
|
||||
} else return forward();
|
||||
} catch {
|
||||
return forward();
|
||||
|
||||
Reference in new issue
Block a user