From 3736d71bb1bb4d4a896b39604fd0406854729701 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Wed, 27 Apr 2022 18:22:08 +0200 Subject: [PATCH] Implement 'net_encryptionEnable' ConVar This ConVar governs the use of encryption on game packets. Also removed 'net_toggletrace' ConCommand and added 'net_tracePayload' ConVar as we can no longer hook on-demand as we bound the encryption parameter of the function to a ConVar. --- r5dev/engine/host_state.cpp | 2 +- r5dev/engine/net.cpp | 30 ++++++---------- r5dev/engine/net.h | 2 -- r5dev/networksystem/serverlisting.h | 2 +- r5dev/tier1/IConVar.cpp | 5 +-- r5dev/tier1/cmd.cpp | 1 - r5dev/tier1/cvar.cpp | 5 +-- r5dev/tier1/cvar.h | 5 +-- r5dev/vstdlib/completion.cpp | 54 ----------------------------- r5dev/vstdlib/completion.h | 1 - 10 files changed, 22 insertions(+), 85 deletions(-) diff --git a/r5dev/engine/host_state.cpp b/r5dev/engine/host_state.cpp index 8807bd4b..a2db235b 100644 --- a/r5dev/engine/host_state.cpp +++ b/r5dev/engine/host_state.cpp @@ -191,7 +191,7 @@ FORCEINLINE void CHostState::Setup(void) const think.detach(); net_usesocketsforloopback->SetValue(1); - if (net_userandomkey->GetBool()) + if (net_useRandomKey->GetBool()) { NET_GenerateKey(); } diff --git a/r5dev/engine/net.cpp b/r5dev/engine/net.cpp index 2a51e5e4..a21ccc42 100644 --- a/r5dev/engine/net.cpp +++ b/r5dev/engine/net.cpp @@ -29,8 +29,8 @@ //----------------------------------------------------------------------------- bool NET_ReceiveDatagram(int iSocket, netpacket_s* pInpacket, bool bEncrypted) { - bool result = v_NET_ReceiveDatagram(iSocket, pInpacket, bEncrypted); - if (result) + bool result = v_NET_ReceiveDatagram(iSocket, pInpacket, net_encryptionEnable->GetBool()); + if (result && net_tracePayload->GetBool()) { // Log received packet data. HexDump("[+] NET_ReceiveDatagram", "netchan_packet_logger", &pInpacket->data[NULL], pInpacket->wiresize); @@ -44,13 +44,13 @@ bool NET_ReceiveDatagram(int iSocket, netpacket_s* pInpacket, bool bEncrypted) // *pPayload - // iLenght - // *pAdr - -// bEncrypted - +// bEncrypt - // Output : outgoing sequence number for this packet //----------------------------------------------------------------------------- -int NET_SendDatagram(SOCKET s, void* pPayload, int iLenght, v_netadr_t* pAdr, bool bEncrypted) +int NET_SendDatagram(SOCKET s, void* pPayload, int iLenght, v_netadr_t* pAdr, bool bEncrypt) { - int result = v_NET_SendDatagram(s, pPayload, iLenght, pAdr, bEncrypted); - if (result) + int result = v_NET_SendDatagram(s, pPayload, iLenght, pAdr, net_encryptionEnable->GetBool()); + if (result && net_tracePayload->GetBool()) { // Log transmitted packet data. HexDump("[+] NET_SendDatagram", "netchan_packet_logger", pPayload, iLenght); @@ -81,7 +81,7 @@ void NET_SetKey(const string& svNetKey) void NET_GenerateKey() { g_szNetKey.clear(); - net_userandomkey->SetValue(1); + net_useRandomKey->SetValue(1); BCRYPT_ALG_HANDLE hAlgorithm; if (BCryptOpenAlgorithmProvider(&hAlgorithm, L"RNG", 0, 0) < 0) @@ -228,6 +228,8 @@ const char* NET_ErrorString(int iCode) /////////////////////////////////////////////////////////////////////////////// void NET_Attach() { + DetourAttach((LPVOID*)&v_NET_ReceiveDatagram, &NET_ReceiveDatagram); + DetourAttach((LPVOID*)&v_NET_SendDatagram, &NET_SendDatagram); DetourAttach((LPVOID*)&v_NET_PrintFunc, &NET_PrintFunc); #ifndef DEDICATED DetourAttach((LPVOID*)&v_NET_Shutdown, &NET_Shutdown); @@ -236,24 +238,14 @@ void NET_Attach() void NET_Detach() { + DetourDetach((LPVOID*)&v_NET_ReceiveDatagram, &NET_ReceiveDatagram); + DetourDetach((LPVOID*)&v_NET_SendDatagram, &NET_SendDatagram); DetourDetach((LPVOID*)&v_NET_PrintFunc, &NET_PrintFunc); #ifndef DEDICATED DetourDetach((LPVOID*)&v_NET_Shutdown, &NET_Shutdown); #endif } -void NET_Trace_Attach() -{ - DetourAttach((LPVOID*)&v_NET_ReceiveDatagram, &NET_ReceiveDatagram); - DetourAttach((LPVOID*)&v_NET_SendDatagram, &NET_SendDatagram); -} - -void NET_Trace_Detach() -{ - DetourDetach((LPVOID*)&v_NET_ReceiveDatagram, &NET_ReceiveDatagram); - DetourDetach((LPVOID*)&v_NET_SendDatagram, &NET_SendDatagram); -} - /////////////////////////////////////////////////////////////////////////////// string g_szNetKey = "WDNWLmJYQ2ZlM0VoTid3Yg=="; uintptr_t g_pNetKey = NULL; diff --git a/r5dev/engine/net.h b/r5dev/engine/net.h index 2a217f3a..3c566e86 100644 --- a/r5dev/engine/net.h +++ b/r5dev/engine/net.h @@ -36,8 +36,6 @@ void NET_DisconnectClient(CBaseClient* pClient, int nIndex, const char* szReason void NET_Attach(); void NET_Detach(); -void NET_Trace_Attach(); -void NET_Trace_Detach(); /////////////////////////////////////////////////////////////////////////////// extern string g_szNetKey; diff --git a/r5dev/networksystem/serverlisting.h b/r5dev/networksystem/serverlisting.h index c0d1d760..31a686ad 100644 --- a/r5dev/networksystem/serverlisting.h +++ b/r5dev/networksystem/serverlisting.h @@ -6,7 +6,7 @@ struct ServerListing std::string svMapName = "mp_rr_canyonlands_staging"; std::string svIpAddress; std::string svPort; - std::string svPlaylist = "survival_dev"; + std::string svPlaylist = "dev_default"; bool bHidden{}; std::string svRemoteChecksum; std::string svVersion; diff --git a/r5dev/tier1/IConVar.cpp b/r5dev/tier1/IConVar.cpp index 6d21eb82..5c1bf21e 100644 --- a/r5dev/tier1/IConVar.cpp +++ b/r5dev/tier1/IConVar.cpp @@ -143,8 +143,9 @@ void ConVar::Init(void) const sq_showvmwarning = new ConVar("sq_showvmwarning" , "0", FCVAR_DEVELOPMENTONLY, "Prints the VM warning output to the console. 1 = Log to file. 2 = 1 + log to console.", false, 0.f, false, 0.f, nullptr, nullptr); //------------------------------------------------------------------------- // NETCHANNEL | - net_encryptpacket = new ConVar("net_encryptpacket" , "1" , FCVAR_DEVELOPMENTONLY, "Use encrpytion for in/out packets if set.", false, 0.f, false, 0.f, nullptr, nullptr); - net_userandomkey = new ConVar("net_userandomkey" , "1" , FCVAR_RELEASE , "Generates and sets a random base64 netkey for netchannel if set.", false, 0.f, false, 0.f, nullptr, nullptr); + net_tracePayload = new ConVar("net_tracePayload" , "0", FCVAR_DEVELOPMENTONLY | FCVAR_CHEAT , "Log the payload of the send/recv datagram to a file on the disk.", false, 0.f, false, 0.f, nullptr, nullptr); + net_encryptionEnable = new ConVar("net_encryptionEnable" , "1", FCVAR_DEVELOPMENTONLY | FCVAR_REPLICATED , "Use AES encryption on game packets.", false, 0.f, false, 0.f, nullptr, nullptr); + net_useRandomKey = new ConVar("net_useRandomKey" , "1" , FCVAR_RELEASE , "Use random base64 netkey for game packets.", false, 0.f, false, 0.f, nullptr, nullptr); r5net_matchmaking_hostname = new ConVar("r5net_matchmaking_hostname", "r5a-comp-sv.herokuapp.com", FCVAR_RELEASE , "Holds the R5Net matchmaking hostname.", false, 0.f, false, 0.f, nullptr, nullptr); r5net_show_debug = new ConVar("r5net_show_debug" , "1" , FCVAR_DEVELOPMENTONLY, "Shows debug output for R5Net.", false, 0.f, false, 0.f, nullptr, nullptr); //------------------------------------------------------------------------- diff --git a/r5dev/tier1/cmd.cpp b/r5dev/tier1/cmd.cpp index c842fb3e..a7863cc9 100644 --- a/r5dev/tier1/cmd.cpp +++ b/r5dev/tier1/cmd.cpp @@ -153,7 +153,6 @@ void ConCommand::Init(void) new ConCommand("pak_listpaks", "Display a list of the loaded Pak files.", FCVAR_DEVELOPMENTONLY, _Pak_ListPaks_f_CompletionFunc, nullptr); //------------------------------------------------------------------------- // NETCHANNEL | - new ConCommand("net_toggletrace", "Logs the sending and receiving datagram to a file on the disk.", FCVAR_CHEAT | FCVAR_DEVELOPMENTONLY, _NET_TraceNetChan_f_CompletionFunc, nullptr); new ConCommand("net_setkey", "Sets user specified base64 net key.", FCVAR_RELEASE, _NET_SetKey_f_CompletionFunc, nullptr); new ConCommand("net_generatekey", "Generates and sets a random base64 net key.", FCVAR_RELEASE, _NET_GenerateKey_f_CompletionFunc, nullptr); } diff --git a/r5dev/tier1/cvar.cpp b/r5dev/tier1/cvar.cpp index 968eee46..37f636bd 100644 --- a/r5dev/tier1/cvar.cpp +++ b/r5dev/tier1/cvar.cpp @@ -106,8 +106,9 @@ ConVar* sq_showvmoutput = nullptr; ConVar* sq_showvmwarning = nullptr; //----------------------------------------------------------------------------- // NETCHANNEL | -ConVar* net_encryptpacket = nullptr; -ConVar* net_userandomkey = nullptr; +ConVar* net_tracePayload = nullptr; +ConVar* net_encryptionEnable = nullptr; +ConVar* net_useRandomKey = nullptr; ConVar* net_usesocketsforloopback = nullptr; ConVar* r5net_matchmaking_hostname = nullptr; ConVar* r5net_show_debug = nullptr; diff --git a/r5dev/tier1/cvar.h b/r5dev/tier1/cvar.h index d28ebce4..855a2b6e 100644 --- a/r5dev/tier1/cvar.h +++ b/r5dev/tier1/cvar.h @@ -103,8 +103,9 @@ extern ConVar* sq_showvmoutput; extern ConVar* sq_showvmwarning; //------------------------------------------------------------------------- // NETCHANNEL | -extern ConVar* net_encryptpacket; -extern ConVar* net_userandomkey; +extern ConVar* net_tracePayload; +extern ConVar* net_encryptionEnable; +extern ConVar* net_useRandomKey; extern ConVar* net_usesocketsforloopback; extern ConVar* r5net_matchmaking_hostname; extern ConVar* r5net_show_debug; diff --git a/r5dev/vstdlib/completion.cpp b/r5dev/vstdlib/completion.cpp index 81039b50..62cc8dda 100644 --- a/r5dev/vstdlib/completion.cpp +++ b/r5dev/vstdlib/completion.cpp @@ -12,7 +12,6 @@ #include "engine/cl_rcon.h" #endif // !DEDICATED #include "engine/net.h" -#include "engine/net_chan.h" #include "engine/sys_utils.h" #include "engine/baseclient.h" #include "rtech/rtech_game.h" @@ -540,59 +539,6 @@ void _RTech_Decompress_f_CompletionFunc(const CCommand& args) outBlock.close(); } -/* -===================== -_NET_TraceNetChan_f_CompletionFunc - - Logs all data transmitted and received - over the UDP socket to a file on the disk. - File: ''. -===================== -*/ -void _NET_TraceNetChan_f_CompletionFunc(const CCommand& args) -{ - static bool bTraceNetChannel = false; - if (!bTraceNetChannel) - { - net_usesocketsforloopback->SetValue(1); - DevMsg(eDLL_T::ENGINE, "\n"); - DevMsg(eDLL_T::ENGINE, "+--------------------------------------------------------+\n"); - DevMsg(eDLL_T::ENGINE, "|>>>>>>>>>>>>>| NETCHANNEL TRACE ACTIVATED |<<<<<<<<<<<<<|\n"); - DevMsg(eDLL_T::ENGINE, "+--------------------------------------------------------+\n"); - DevMsg(eDLL_T::ENGINE, "\n"); - - // Begin the detour transaction to hook the the process. - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - - NET_Trace_Attach(); - // Commit the transaction. - if (DetourTransactionCommit() != NO_ERROR) - { - // Failed to hook into the process, terminate. - TerminateProcess(GetCurrentProcess(), 0xBAD0C0DE); - } - } - else - { - DevMsg(eDLL_T::ENGINE, "\n"); - DevMsg(eDLL_T::ENGINE, "+--------------------------------------------------------+\n"); - DevMsg(eDLL_T::ENGINE, "|>>>>>>>>>>>>| NETCHANNEL TRACE DEACTIVATED |<<<<<<<<<<<<|\n"); - DevMsg(eDLL_T::ENGINE, "+--------------------------------------------------------+\n"); - DevMsg(eDLL_T::ENGINE, "\n"); - - // Begin the detour transaction to hook the the process. - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - - NET_Trace_Detach(); - - // Commit the transaction. - DetourTransactionCommit(); - } - bTraceNetChannel = !bTraceNetChannel; -} - /* ===================== _VPK_Decompress_f_CompletionFunc diff --git a/r5dev/vstdlib/completion.h b/r5dev/vstdlib/completion.h index 46916e45..278d7774 100644 --- a/r5dev/vstdlib/completion.h +++ b/r5dev/vstdlib/completion.h @@ -26,7 +26,6 @@ void _RTech_StringToGUID_f_CompletionFunc(const CCommand& args); void _RTech_Decompress_f_CompletionFunc(const CCommand& args); void _VPK_Unpack_f_CompletionFunc(const CCommand& args); void _VPK_Mount_f_CompletionFunc(const CCommand& args); -void _NET_TraceNetChan_f_CompletionFunc(const CCommand& args); void _NET_SetKey_f_CompletionFunc(const CCommand& args); void _NET_GenerateKey_f_CompletionFunc(const CCommand& args); #ifndef DEDICATED