forked from Scrap-Mods/Steam-Bans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdllmain.cpp
249 lines (210 loc) · 8.59 KB
/
dllmain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <filesystem>
#include <fstream>
#include <vector>
#include <map>
#define CON_LOG_PREFIX "[SteamBans] "
#include "Console.h"
//Ignore error 4996
#pragma comment(lib, "steam_api64.lib")
#pragma warning(disable: 4996)
#include <steam/steam_api.h>
struct g_ctx
{
std::atomic_bool running;
HANDLE hUnloadEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // Signal to unload
HANDLE hUnloadEventAck = CreateEvent(NULL, TRUE, FALSE, NULL);
HMODULE hModule;
void* vftable_SteamNetConnectionStatusChanged;
std::vector<unsigned long long> banned_steamids = {};
} Context;
FARPROC GetProcAddressEx(const HMODULE hModule, const std::string lpProcName)
{
const FARPROC pfnProc = GetProcAddress(hModule, lpProcName.c_str());
const auto lpProcNameW = std::wstring(lpProcName.begin(), lpProcName.end());
if (!pfnProc)
{
const auto msg = std::format(TEXT("Could not find export {}!"), lpProcNameW);
MessageBox(NULL, msg.c_str(), TEXT("Error"), MB_OK | MB_ICONERROR);
FreeLibraryAndExitThread(Context.hModule, NULL);
return nullptr;
}
return pfnProc;
}
void onSteamNetConnectionStatusChanged(std::uintptr_t self, SteamNetConnectionStatusChangedCallback_t* pParam);
decltype(&onSteamNetConnectionStatusChanged) oSteamNetConnectionStatusChanged;
bool IsBannedSteamID(const std::uint64_t steamid)
{
return std::find(Context.banned_steamids.begin(), Context.banned_steamids.end(), steamid) != Context.banned_steamids.end();
}
void onSteamNetConnectionStatusChanged(std::uintptr_t self, SteamNetConnectionStatusChangedCallback_t* pParam)
{
const std::uint64_t connectionSteamID = pParam->m_info.m_identityRemote.GetSteamID64();
if (IsBannedSteamID(connectionSteamID))
{
if (pParam->m_info.m_eState == k_ESteamNetworkingConnectionState_Connecting)
{
DebugOutL(std::format(TEXT("Banned SteamID tried to join: {}"), connectionSteamID));
SteamNetworkingSockets()->CloseConnection(pParam->m_hConn, 0, "You are banned!", false);
}
return;
}
return oSteamNetConnectionStatusChanged(self, pParam);
}
void UpdateBanList()
{
// Make or open steamid ban list in the same directory as the process
std::filesystem::path path = std::filesystem::current_path() / "steam_bans.txt";
// Create the file if it doesn't exist
if (!std::filesystem::exists(path))
{
std::ofstream file(path);
file << "# Put your banned steamids here\n";
file << "# Each steamid should be on a new line\n";
file << "# For example:\n";
file << "76561199531536640 # someone's crash bot\n";
file << "76561199665208613 # someone's crash bot #2\n";
file.close();
}
Context.banned_steamids.clear();
DebugOutL("Fetching banned users from steam_bans.txt...");
// Each steamid should be on a new line
std::ifstream file(path);
std::string line;
while (std::getline(file, line))
{
if (line.size() < 17)
continue;
// Ensure 17 numerical digits
if (!std::all_of(line.begin(), line.begin() + 17, ::isdigit))
continue;
// Convert the string to a steamid
const std::uint64_t steamid64 = std::stoull(line);
DebugOutL("Added SteamID to banned list: ", steamid64);
Context.banned_steamids.push_back(steamid64);
}
file.close();
}
void SetupChangeNotifications()
{
HANDLE hNotification = FindFirstChangeNotification(
std::filesystem::current_path().c_str(),
FALSE,
FILE_NOTIFY_CHANGE_LAST_WRITE
);
if (hNotification == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, TEXT("Could not setup change notifications!"), TEXT("Error"), MB_OK | MB_ICONERROR);
FreeLibraryAndExitThread(Context.hModule, NULL);
return;
}
std::filesystem::file_time_type last_write_time_old = std::filesystem::last_write_time(std::filesystem::current_path() / "steam_bans.txt");
HANDLE handles[2] = { hNotification, Context.hUnloadEvent };
while (Context.running)
{
auto last_result = WaitForMultipleObjects(2, handles, FALSE, 100);
if (last_result == WAIT_OBJECT_0)
{
// Check if steamid ban list has been updated
std::filesystem::file_time_type last_write_time = std::filesystem::last_write_time(std::filesystem::current_path() / "steam_bans.txt");
if (last_write_time != last_write_time_old)
{
UpdateBanList();
last_write_time_old = last_write_time;
}
}
else if (last_result == WAIT_OBJECT_0 + 1)
{
break;
}
}
FindCloseChangeNotification(hNotification);
SetEvent(Context.hUnloadEventAck);
FreeLibraryAndExitThread(Context.hModule, NULL);
}
std::uint64_t FollowJMP(std::uintptr_t address)
{
const std::int32_t offset = *reinterpret_cast<std::int32_t*>(address + 1);
return address + 5 + offset;
}
using fGetCallbackMap = std::map<std::uint32_t, void*>* (__cdecl*)();
void Main(HMODULE hModule)
{
Context.running = true;
std::this_thread::sleep_for(std::chrono::seconds(1));
Console::Attach();
UpdateBanList();
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)SetupChangeNotifications, NULL, 0, 0);
// Check if steam is running
HMODULE hSteam = GetModuleHandle(TEXT("steam_api64.dll"));
// Wait until steam is running
while (!hSteam && Context.running)
{
hSteam = GetModuleHandle(TEXT("steam_api64.dll"));
DebugOutL("Waiting for steam_api64.dll...");
std::this_thread::sleep_for(std::chrono::seconds(1));
}
FARPROC pUnregisterCallResult = GetProcAddressEx(hSteam, "SteamAPI_UnregisterCallResult");
std::uint64_t func = FollowJMP((std::uintptr_t)pUnregisterCallResult);
func = FollowJMP(func + 0x19);
fGetCallbackMap GetCallbackMap = (fGetCallbackMap)func;
std::map<std::uint32_t, void*>* callbacks = GetCallbackMap();
auto it = callbacks->find(SteamNetConnectionStatusChangedCallback_t::k_iCallback);
while (it == callbacks->end() && Context.running)
{
it = callbacks->find(SteamNetConnectionStatusChangedCallback_t::k_iCallback);
DebugOutL("Waiting for SteamNetConnectionStatusChangedCallback_t...");
std::this_thread::sleep_for(std::chrono::seconds(1));
}
SteamNetConnectionStatusChangedCallback_t* callback = (SteamNetConnectionStatusChangedCallback_t*)it->second;
if (callback)
{
void** vftable = *(void***)callback;
const int vftable_index = 1;
Context.vftable_SteamNetConnectionStatusChanged = &vftable[vftable_index];
DWORD oldProtect;
VirtualProtect(&vftable[vftable_index], sizeof(void*), PAGE_EXECUTE_READWRITE, &oldProtect);
oSteamNetConnectionStatusChanged = (decltype(oSteamNetConnectionStatusChanged))vftable[vftable_index];
vftable[vftable_index] = (void*)onSteamNetConnectionStatusChanged;
VirtualProtect(&vftable[vftable_index], sizeof(void*), oldProtect, &oldProtect);
}
else
{
DebugOutL(TEXT("Could not find SteamNetConnectionStatusChangedCallback_t!"));
FreeLibraryAndExitThread(hModule, NULL);
return;
}
DebugOutL("Loaded successfully!");
DebugOutL(std::format(TEXT("Put your banned steamids in {}"), (std::filesystem::current_path() / "steam_bans.txt").c_str()));
DebugOutL("Each steamid should be on a new line, and the file should be in the same directory as the process!");
}
BOOL APIENTRY DllMain
(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls(hModule);
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Main, hModule, 0, 0);
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
{
Context.running = false;
DWORD exitCode = 0;
GetExitCodeProcess(GetCurrentProcess(), &exitCode);
if (exitCode == STILL_ACTIVE)
SignalObjectAndWait(Context.hUnloadEvent, Context.hUnloadEventAck, INFINITE, FALSE);
if (Context.vftable_SteamNetConnectionStatusChanged && oSteamNetConnectionStatusChanged)
{
DWORD oldProtect;
VirtualProtect(Context.vftable_SteamNetConnectionStatusChanged, sizeof(void*), PAGE_EXECUTE_READWRITE, &oldProtect);
*(void**)Context.vftable_SteamNetConnectionStatusChanged = oSteamNetConnectionStatusChanged;
VirtualProtect(Context.vftable_SteamNetConnectionStatusChanged, sizeof(void*), oldProtect, &oldProtect);
}
DebugOutL("Unloaded!");
}
return TRUE;
}