Feature: Embed the server TLS certificate in delivered DLLs

Add a "高级 → DLL内嵌TLS证书" toggle (off by default, persisted as
[settings] EmbedTlsInDll). When it is on, ReadKernelDll patches the server's
current TLS anchor into the ServerDll and ShellCode payloads, so clients that
come online through the loaders start their own session over TLS without
waiting for an online push. The plaintext DLL loader in client/main.c is
untouched; only the DLL's fresh connection is protected. The write happens
after SetServer and before CalcMD5FromBytes, and the anchor records the host
part of the master address rather than "ip:port" because the client matches
the anchor against its configured address set. TinyRun has no anchor block and
keeps working as before.

Toggling the switch, and confirming the certificate dialog, rebuild the six
payloads in place, so no server restart is needed after a certificate change.
The payload pointers are replaced as a whole, and the network threads that read
them (SendServerDll and TOKEN_GETVERSION) now take a dedicated critical section
for the full duration of use.

COMMAND_UPDATE no longer clears the tls_disabled marker on the client. Now that
any delivered DLL may carry the anchor, "the payload has an anchor" no longer
means the operator opted this host into TLS, and clearing the marker would
silently flip a host that was explicitly set to plaintext onto TLS against a
plaintext path. Re-enabling TLS stays an explicit action through
COMMAND_TLS_ENABLE and COMMAND_SET_PROTO, which still clear the marker.

Menu and notice strings were added to en_US, zh_TW and de_DE, and the embedded
TLS design document records the new switch and the revised update semantics.

The new menu entry also takes a feature-limits slot so a subordinate master
can hide it like every other 高级 item (see 8807321):
HIDE_MENU_EMBED_TLS_ANCHOR in UIBranding.h, MF_EMBED_TLS_ANCHOR at bit 57
in FeatureFlags.h (the first free one after MF_FILE_SPEED), a row in the
主菜单 tab of CFeatureLimitsDlg, and the matching DeleteMenu on the 高级
menu. The entry still has no bitmap of its own, so it renders without an
icon next to its three siblings.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-02 18:30:37 +01:00
1 parent 8b92f32c47
commit c66f410831
12 files changed
+210 -45

No files matched your search

+6 -36
View File
@@ -824,26 +824,6 @@ static void SetTlsDisabledMarker(bool disabled)
RegCloseKey(hKey);
}
// 扫一段镜像里有没有「带正文的锚块」。生成端写锚时会连同 .rdata 里的 magic 字面量
// 一起留在二进制里,所以不能只认第一处命中,必须逐个做结构校验(同 common/tls_anchor.h
// 的说明)。被 UPX/AES 包过的镜像扫不到——那就按「没带锚」处理,退回原有行为。
static bool PayloadCarriesAnchor(const BYTE* data, size_t size)
{
if (!data || size < sizeof(EmbeddedTlsAnchor))
return false;
const size_t last = size - sizeof(EmbeddedTlsAnchor);
for (size_t i = 0; i <= last; ++i) {
const char* p = (const char*)data + i;
if (p[0] != 'Y' || !IsAnchorMagic(p))
continue;
EmbeddedTlsAnchor block;
memcpy(&block, p, sizeof(block)); // 对齐安全:不直接解引用未对齐指针
if (IsAnchorBlockValid(block) && block.length > 0)
return true;
}
return false;
}
// 信任锚已写入/删除:热重载 TLS 状态,只在传输方式真正变化(明文 ↔ TLS)时断开重连;
// 仅换证书(仍是 TLS)不打断现有会话,新锚从下一条新连接开始生效。与 Linux 版的
// linux/main.cpp::ApplyTlsAnchorChange 同一语义。
@@ -1700,22 +1680,12 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
case COMMAND_UPDATE: {
auto typ = m_conn->ClientType();
// 本次下发的镜像若带着内嵌信任锚,就先清掉本机的「取消 TLS」抑制标记:否则重装/
// 更新之后,运维刚内嵌的锚会被上一轮「取消 TLS」留下的标记悄悄作废(§6.5)。
// 只在镜像里真有可用锚时才清——反过来一律清除,会让一台已被要求走明文的主机
// 重新发 ClientHello,撞上明文服务端就是不可恢复的失联。
{
ULONGLONG size = 0;
if (ulLength > 1 + sizeof(ULONGLONG)) {
memcpy(&size, (const char*)szBuffer + 1, sizeof(ULONGLONG));
const size_t avail = (size_t)ulLength - 1 - sizeof(ULONGLONG);
if (size > 0 && (ULONGLONG)avail >= size &&
PayloadCarriesAnchor(szBuffer + 1 + sizeof(ULONGLONG), (size_t)size)) {
SetTlsDisabledMarker(false);
Mprintf("[TLS] update payload carries an embedded anchor; cleared tls_disabled\n");
}
}
}
// 在线更新不再顺带清除 tls_disabled 抑制标记(原设计文档 §6.5 的行为)。
// 服务端现在可以给所有下发的 ServerDll 内嵌锚(高级→DLL内嵌TLS证书,见
// 2015RemoteDlg::ReadKernelDll),于是「镜像里带锚」不再代表运维特意为这台主机
// 启用 TLS——照旧清除会把运维明确设过的「取消 TLS」在升级时静默翻掉,对走明文
// 链路的主机就是不可恢复的失联。重新启用 TLS 走显式命令:在线列表右键
//「TLS 传输」→ COMMAND_TLS_ENABLE,那条路径仍会清标记。
if (typ == CLIENT_TYPE_DLL || typ == CLIENT_TYPE_MODULE) {
ULONGLONG size = 0;
memcpy(&size, (const char*)szBuffer + 1, sizeof(ULONGLONG));