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

+26 -7
View File
@@ -375,10 +375,21 @@ bool TlsAnchorSummary(std::string& out); // "builtin 2036-11-01 sha1:1a2b3c4d5
只写一行 `Mprintf`,不中断下发。注意传 `outSize` 而不是 `dwFileSize`——扫描循环已经把后者
递减成「剩余未扫描长度」。
`COMMAND_UPDATE` 清 `tls_disabled` 落在**客户端**(`KernelManager.cpp`):收到新镜像后扫一遍
payload,`IsAnchorBlockValid && length > 0` 才清。**不做无条件清除**——否则一台已被要求走明文
的主机会重新发 ClientHello,撞上明文服务端就是不可恢复的失联,正是 §3.5 要避免的那个失败。
被 UPX / AES 包过的镜像扫不到锚,按「没带锚」处理(保持既有行为)。
`COMMAND_UPDATE` **不再**顺带清 `tls_disabled`(2026-11-02 修订,见 §6.5)。服务端新增
「高级→DLL内嵌TLS证书」开关后,凡开启期间下发的 ServerDll 都带锚,「镜像里带锚」不再代表
运维特意为这台主机启用 TLS;照旧清除会把运维明确设过的「取消 TLS」在升级时静默翻掉。
重新启用仍走显式命令 `COMMAND_TLS_ENABLE`(在线列表右键「TLS 传输」),那条路径照常清标记。
**DLL 内嵌开关(2026-11-02 新增)**:主对话框「高级→DLL内嵌TLS证书」,默认关,勾选状态存
`[settings] EmbedTlsInDll`。勾选后 `ReadKernelDll()` 在 `SetServer()` 之后、`CalcMD5FromBytes()`
之前调用 `EmbedTlsAnchorInto()`,把服务端当前证书写进 ServerDll / ShellCode 的零值锚块(x86/x64
各一份);TinyRun 没有锚块,返回 `NoBlock` 属正常且不刷日志。锚里写的是 `server->szServerIP`
(纯主机名)而不是 `"ip:port"`——客户端 `EmbeddedAnchorAppliesTo` 按地址集合匹配,带端口两层
都匹配不上。锚必须在 md5 之前写入,否则 md5 不变、客户端会当作「内容没变」不重下。
MDLL 加载器(`client/main.c`)本身仍是明文:它只要 DLL,拿到后 MemoryModule 加载,之后 DLL 自己
新建的会话才是 TLS。切换开关或换证书后 `ReloadKernelPayloads()` 重生成六份载荷(替换指针,持
`m_csPayload`,网络线程读载荷处同样持锁),无需重启服务端;`OnTlsCertificate` 在确认后调用它。
### 4.9 服务端在线列表与「取消 TLS」
@@ -473,7 +484,14 @@ Linux 客户端编的是 `client/TlsChannelLinux.cpp`,不含 `LoginServer.cpp`
注册表以 **exe 全路径哈希**为键(`common/iniFile.h:54` `GetExeHashStr()`)。同一个安装路径重新生成或在线更新后,客户端的注册表状态**保留**,包括 `tls_disabled=1`。后果:运维刚内嵌的锚被上一轮的抑制标记悄悄作废,连接回到明文且无提示。
处理:`COMMAND_UPDATE` / `COMMAND_UPLOAD_EXEC` 下发新客户端时顺带清除 `tls_disabled`;且内嵌锚主机被抑制时,客户端日志与在线列表都要能看到(§4.9 的能力位)。
处理(v0.3 原案):`COMMAND_UPDATE` / `COMMAND_UPLOAD_EXEC` 下发新客户端时顺带清除 `tls_disabled`;且内嵌锚主机被抑制时,客户端日志与在线列表都要能看到(§4.9 的能力位)。
**2026-11-02 修订**:`COMMAND_UPDATE` 的自动清除已撤销(能力位上报不受影响)。服务端新增
「高级→DLL内嵌TLS证书」开关后,开启期间下发的 ServerDll 一律带锚,「镜像带锚」不再是「运维
特意为这台主机启用 TLS」的信号;继续自动清除,等于运维一升级被明确要求走明文的主机就把它翻回
TLS——对明文链路是不可恢复的失联。现在只有显式命令会清标记:`COMMAND_TLS_ENABLE` 与
`COMMAND_SET_PROTO`(WSS,`certLen > 0`)。代价是:被抑制过的主机在重新生成/更新后仍保持明文,
要切到内嵌锚得由运维再下发一次「TLS 传输」——刻意选的保守方向。
---
@@ -514,7 +532,8 @@ Linux 客户端编的是 `client/TlsChannelLinux.cpp`,不含 `LoginServer.cpp`
11. **分享实例隔离**:主控 A 分享一台主机到 B,B 对该分享连接下发"启用 TLS" → 客户端日志出现 `[TLS] ignored: shared instance`,注册表 `tls_cert` 不变,主实例与 A 的连接**不受影响**。反向(`COMMAND_TLS_DISABLE`)同样。
12. `UDP`/`KCP`/`随机` 协议下复选框置灰(§3.7)。
13. mem-DLL 场景:`ServerDll.dll` 内嵌锚,`TestRun.exe` 运行后仍为首连 TLS;**故意用过期的 `ServerDll.dll`** 验证会明确报错而不是静默明文。
14. 在线更新(`COMMAND_UPDATE`)后:新内嵌锚生效,旧 `tls_disabled` 被清除(§6.5)。
14. 在线更新(`COMMAND_UPDATE`)后:`tls_disabled` **保持不变**,被抑制的主机仍是明文(§6.5 修订);
要切到内嵌锚须显式下发「TLS 传输」。
**回归**
15. 老客户端(无锚)连新服务端,明文正常。
@@ -625,7 +644,7 @@ P1(Windows fullchain 循环加载):约 30 行 + 测试,半天。
| `common/tls_anchor.h`(新) | `EmbeddedTlsAnchor` 结构、`kAnchorVersion/ kAnchorCapacity`、`Fnv1a32`、`IsAnchorMagic`、`IsAnchorBlockValid`、`static_assert(sizeof == 4228)` |
| `client/EmbeddedTlsAnchor.h/.cpp`(新) | `volatile EmbeddedTlsAnchor g_embeddedTlsAnchor`(独立 TU、`.data` 落地) |
| `client/TlsChannel.cpp/.h` | `ReadTrustAnchor` 四级优先级(`tls_disabled` → `tls_cert` → 文件 → 内嵌 → 明文);P1 的 `DecodeAllCerts`(按 BEGIN/END 切块循环解码,见 §14.5);**按连接的两套缓存状态** `g_state[2]` + `ResolveStateIndex()` 地址集合匹配 + `TlsEnabledFor()` / `TlsLoadErrorFor()` / `AnchorEmbedded()`(见 §14.6) |
| `client/KernelManager.cpp` | `SetTlsDisabledMarker()`;启停 TLS 的分享实例隔离;`COMMAND_SET_PROTO` 清标记;`COMMAND_UPDATE` 按 payload 是否带锚清标记 |
| `client/KernelManager.cpp` | `SetTlsDisabledMarker()`;启停 TLS 的分享实例隔离;`COMMAND_SET_PROTO` 清标记;`COMMAND_UPDATE` **不清**标记(§6.5 修订) |
| `client/ClientDll.cpp` | 不再有进程级判定(删掉 `SetEmbeddedAnchorEnabled()` 那一套);`GetLoginInfo(..., IsTlsActive(), IsTlsAnchorEmbedded())` |
| `client/IOCPClient.h/.cpp` | `IsTlsActive()`(按实连上报,不看 `protoType`);`IsTlsAnchorEmbedded()`(取本连接握手时的锚来源);`ConnectServer` 改用 `TlsEnabledFor(m_sCurIP, m_Domain.GetAddress())` |
| `client/domain_pool.h` | `GetAddress()`:取配置里的原始地址串(可能分号多地址/域名) |