SafeNet 客户端附带的加密提供程序正在使用 SmardCardAPI (winscard.dll) 访问 USB 令牌。由于智能卡还用于身份验证/登录目的,RDP 堆栈将始终重定向对 RDP 客户端计算机的任何访问。
https://docs.microsoft.com/en-us/windows/security/identity-protection/smart-cards/smart-card-and-remote-desktop-services
对于像代码签名这样的场景,这种行为可能非常麻烦。我们使用专用的虚拟机进行签名,每个有权访问该机器的开发人员都应该能够执行签名过程。对于 COVID-19 和所有在家工作的开发人员,使用本地 USB 端口的想法是不可行的。我们的 USB-Token 连接到专用机器上。每次您通过 RDP 连接到这台机器时,加密狗都无法再访问,因为 SmartCardAPI 将重定向访问。
但是有一个解决这个问题的方法:
智能卡堆栈使用 API 调用
ProcessIdToSessionId
WinStationGetCurrentSessionCapabilities
确定当前进程是否在 RDP 会话中运行。通过将 DLL 注入 signtool 并使用 Detours 框架,您可以挂钩这些 API 调用并报告本地会话,因此 SmardCardAPI 将访问连接到远程虚拟机的加密狗。
https://github.com/microsoft/Detours
迂回的函数很简单(代码简化为重要的东西,所以你可以理解)
DWORD WINAPI ProcessIdToSessionIdLocal(DWORD dwProcessId, DWORD *pSessionId)
{
OutputDebugString("Detoured ProcessIdToSessionId\r\n");
if (pSessionId)
pSessionId = 0;
return TRUE;
}
BOOL WINAPI WinStationGetCurrentSessionCapabilitiesLocal(DWORD flags, DWORD *pOutBuffer)
{
BOOL bResult;
OutputDebugString("Detoured WinStationGetCurrentSessionCapabilities\r\n");
bResult = TrueGetCurStationCapabilities(flags,pOutBuffer);
if (bResult)
*pOutBuffer = 0;
return bResult;
}
BOOL WINAPI DllMain (haDLL, dwReason, lpReserved)
HANDLE haDLL;
DWORD dwReason;
LPVOID lpReserved;
{
LONG error;
TCHAR cBuffer[160];
wsprintf(cBuffer,"DllMain Entry %08x\r\n",dwReason);
OutputDebugString(cBuffer);
if (DetourIsHelperProcess()) {
return TRUE;
}
if (dwReason == DLL_PROCESS_ATTACH) {
OutputDebugString("Starting Detour API Calls \r\n");
hStaDLL = LoadLibrary("WINSTA.DLL");
TrueGetCurStationCapabilities = GetProcAddress(hStaDLL,"WinStationGetCurrentSessionCapabilities");
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&TrueProcessIdToSessionId, ProcessIdToSessionIdLocal);
DetourAttach((PVOID*)&TrueGetCurStationCapabilities, WinStationGetCurrentSessionCapabilitiesLocal);
error = DetourTransactionCommit();
if (error == NO_ERROR) {
OutputDebugString("Successfully Detoured API Calls \r\n");
}
else {
return FALSE;
}
}
else if (dwReason == DLL_PROCESS_DETACH) {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((PVOID*)&TrueProcessIdToSessionId, ProcessIdToSessionIdLocal);
DetourDetach((PVOID*)&TrueGetCurStationCapabilities, WinStationGetCurrentSessionCapabilitiesLocal);
error = DetourTransactionCommit();
FreeLibrary(hStaDLL);
}
return TRUE;
}
将 DLL 注入到 signtool 中也很容易:只需在 IMPORTs 部分添加一个新条目,该条目将加载带有迂回函数的 DLL。这可以使用名为“Lord PE”的工具(用于 32 位可执行文件)来完成。