-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrcon.cpp
348 lines (282 loc) · 8.9 KB
/
rcon.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "rcon.h"
#include <CDetour/detours.h>
// tier1 supremecy
#include <bitbuf.h>
#include <netadr.h>
#include <utllinkedlist.h>
#include <convar.h>
// Not sure how/where to best put this in amb file. #yolo
#ifdef _MSC_VER
#pragma comment(lib, "ws2_32")
#endif
// I thought that these had made it upstream. I was wrong.
#ifndef DETOUR_MEMBER_MCALL_CALLBACK
#define DETOUR_MEMBER_MCALL_CALLBACK(name, classptr) \
((name##Class *)classptr->*(&name##Class::name))
#endif
#ifndef DETOUR_MEMBER_MCALL_ORIGINAL
#define DETOUR_MEMBER_MCALL_ORIGINAL(name, classptr) \
((name##Class *)classptr->*(&name##Class::name##_Actual))
#endif
typedef unsigned int listenerId_t;
struct listener_t
{
listenerId_t id;
bool authed;
bool hasAddr;
netadr_t addr;
};
class CServerRemoteAccess;
#define SERVERDATA_EXECCOMMAND 2
#define SERVERDATA_AUTH 3
static CDetour *detWriteReq;
static CDetour *detCheckPass;
static CDetour *detIsPass;
static CDetour *detSocketClosed;
static CDetour *detLogCommand;
static int iRemoteListenersOffs;
static CServerRemoteAccess *g_pServer;
static bool g_bInRConCommand = false;
static listenerId_t iLastListener;
ConVar smrcon_rejectbanned( "smrcon_rejectbanned", "1", 0 );
static inline listener_t GetListenerFromId(listenerId_t id)
{
CUtlLinkedList<listener_t, listenerId_t> *m_Listeners = (CUtlLinkedList<listener_t, listenerId_t> *)((intptr_t)g_pServer + iRemoteListenersOffs);
return m_Listeners->Element(id);
}
static bool IsAddressBanned(const netadr_s &address)
{
#if SOURCE_ENGINE == SE_CSGO
ns_address address2(address);
#endif
static ICallWrapper *pWrapper = NULL;
if (!pWrapper)
{
void *addr;
if (!g_pGameConf->GetMemSig("Filter_ShouldDiscard", &addr) || !addr)
{
return false;
}
PassInfo pass[2];
pass[0].flags = PASSFLAG_BYREF;
#if SOURCE_ENGINE == SE_CSGO
pass[0].size = sizeof(ns_address *);
#else
pass[0].size = sizeof(netadr_s *);
#endif
pass[0].type = PassType_Basic;
pass[1].flags = PASSFLAG_BYVAL;
pass[1].size = sizeof(bool);
pass[1].type = PassType_Basic;
pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &pass[1], pass, 1);
}
unsigned char vstk[sizeof(netadr_s *)];
unsigned char *vptr = vstk;
#if SOURCE_ENGINE == SE_CSGO
*(const ns_address **)vptr = &address2;
#else
*(const netadr_s **)vptr = &address;
#endif
bool ret;
pWrapper->Execute(vstk, &ret);
return ret;
}
DETOUR_DECL_MEMBER2(LogCommand, void, listenerId_t, id, const char *, data)
{
// in case this isn't already init'd
g_pServer = (CServerRemoteAccess *)(this);
if( g_fwdOnRConLog->GetFunctionCount() == 0 )
{
return DETOUR_MEMBER_CALL(LogCommand)(id, data);
}
listener_t listener = GetListenerFromId(id);
cell_t res = Pl_Continue;
g_fwdOnRConLog->PushCell(id);
g_fwdOnRConLog->PushString((listener.hasAddr) ? listener.addr.ToString(true) : "");
g_fwdOnRConLog->PushString(data);
g_fwdOnRConLog->Execute(&res);
if (res == Pl_Continue)
{
return DETOUR_MEMBER_CALL(LogCommand)(id, data);
}
return;
}
DETOUR_DECL_MEMBER4(WriteDataRequest, void, void *, pRCon, listenerId_t, id, const void *, pData, int, size)
{
// in case this isn't already init'd
g_pServer = (CServerRemoteAccess *)(this);
listener_t listener = GetListenerFromId(id);
// Sending commands doesn't mean that they're authed
if (!listener.authed)
{
// let the engine decide their fate
return DETOUR_MEMBER_CALL(WriteDataRequest)(pRCon, id, pData, size);
}
if (size < (int)((sizeof(int)*2) + sizeof(char)))
{
// we need to be able to read at least two ints and a string from this
return DETOUR_MEMBER_CALL(WriteDataRequest)(pRCon, id, pData, size);
}
bf_read buffer(pData, size);
/*int reqId = */ buffer.ReadLong();
int type = buffer.ReadLong();
if (type != SERVERDATA_EXECCOMMAND)
{
// Auth will go through our auth handler already and we don't care about anything else
return DETOUR_MEMBER_CALL(WriteDataRequest)(pRCon, id, pData, size);
}
char command[512];
if (!buffer.ReadString(command, sizeof(command)-1))
{
return DETOUR_MEMBER_CALL(WriteDataRequest)(pRCon, id, pData, size);
}
command[sizeof(command)-1] = 0;
// Just because they're authed doesn't mean we'll let them do anything. Pass the info to sp
if( g_fwdOnRConCommand->GetFunctionCount() == 0 )
{
return DETOUR_MEMBER_CALL(WriteDataRequest)(pRCon, id, pData, size);
}
cell_t allow = 1;
cell_t res = Pl_Continue;
g_fwdOnRConCommand->PushCell(id);
g_fwdOnRConCommand->PushString((listener.hasAddr) ? listener.addr.ToString(true) : "");
g_fwdOnRConCommand->PushString(command);
g_fwdOnRConCommand->PushCellByRef(&allow);
g_fwdOnRConCommand->Execute(&res);
if (res == Pl_Continue || allow != 0)
{
g_bInRConCommand = true;
DETOUR_MEMBER_CALL(WriteDataRequest)(pRCon, id, pData, size);
g_bInRConCommand = false;
}
else
{
// we have to trigger logging on our own if we're not calling WriteDataRequest
char loginfo[512];
g_pSM->Format(loginfo, sizeof(loginfo), "command \"%s\" (rejected)", command);
DETOUR_MEMBER_MCALL_CALLBACK(LogCommand, this)(id, loginfo);
}
}
DETOUR_DECL_MEMBER4(CheckPassword, void, void *, pRCon, listenerId_t, id, int, reqId, const char *, password)
{
// in case this isn't already init'd
g_pServer = (CServerRemoteAccess *)(this);
iLastListener = id;
// IsPassword gets called inside of here
return DETOUR_MEMBER_CALL(CheckPassword)(pRCon, id, reqId, password);
}
DETOUR_DECL_MEMBER1(IsPassword, bool, const char *, password)
{
listener_t listener = GetListenerFromId(iLastListener);
if (smrcon_rejectbanned.GetBool() && IsAddressBanned(listener.addr))
{
return false;
}
bool origRet = DETOUR_MEMBER_CALL(IsPassword)(password);
if( g_fwdOnRConAuth->GetFunctionCount() == 0 )
{
return origRet;
}
cell_t bSuccess = origRet ? 1 : 0;
cell_t res = Pl_Continue;
g_fwdOnRConAuth->PushCell(iLastListener);
g_fwdOnRConAuth->PushString((listener.hasAddr) ? listener.addr.ToString(true) : "");
g_fwdOnRConAuth->PushString(password);
g_fwdOnRConAuth->PushCellByRef(&bSuccess);
g_fwdOnRConAuth->Execute(&res);
if (res > Pl_Continue)
{
if (bSuccess == 0)
return false;
else
return true;
}
return (bSuccess != 0);
}
/*
* RCon socket funcs from CRConServer vtable
* 0 CRConServer::ShouldAcceptSocket(int,netadr_s const&)
* 1 CRConServer::OnSocketAccepted(int,netadr_s const&,void **)
* 2 CRConServer::OnSocketClosed(int,netadr_s const&,void *)
*/
DETOUR_DECL_MEMBER3(OnSocketClosed, void, int, unk, const netadr_s&, addr, void *, pSocketData)
{
/*
* From OnSocketAccepted, 2nd in vtable
* Win:
* result = sub_1016E4B0(1, a2);
* *(_DWORD *)(v3 + 8) = result;
* Lin:
* *(_DWORD *)(v4 + 8) = CServerRemoteAccess::GetNextListenerID((int)g_ServerRemoteAccess, 1, a3);
*
* 8 is our magic number for the listener id
*/
listenerId_t id = *(listenerId_t *)((intptr_t)pSocketData + 8);
g_fwdOnRConDisconnect->PushCell(id);
g_fwdOnRConDisconnect->Execute(NULL);
return DETOUR_MEMBER_CALL(OnSocketClosed)(unk, addr, pSocketData);
}
bool InitRConDetours()
{
if (!g_pGameConf->GetOffset("RemoteListeners", &iRemoteListenersOffs))
{
g_pSM->LogError(myself, "Couldn't find RemoteListeners offset in game conf");
return false;
}
detWriteReq = DETOUR_CREATE_MEMBER(WriteDataRequest, "WriteDataRequest");
if (detWriteReq == NULL)
{
g_pSM->LogError(myself, "Error setting up WriteDataRequest detour");
return false;
}
detCheckPass = DETOUR_CREATE_MEMBER(CheckPassword, "CheckPassword");
if (detCheckPass == NULL)
{
g_pSM->LogError(myself, "Error setting up CheckPassword detour");
return false;
}
detIsPass = DETOUR_CREATE_MEMBER(IsPassword, "IsPassword");
if (detIsPass == NULL)
{
g_pSM->LogError(myself, "Error setting up IsPassword detour");
return false;
}
detSocketClosed = DETOUR_CREATE_MEMBER(OnSocketClosed, "OnSocketClosed");
if (detSocketClosed == NULL)
{
g_pSM->LogError(myself, "Error setting up OnSocketClosed detour");
return false;
}
detLogCommand = DETOUR_CREATE_MEMBER(LogCommand, "LogCommand");
if (detLogCommand == NULL)
{
g_pSM->LogError(myself, "Error setting up LogCommand detour");
return false;
}
detWriteReq->EnableDetour();
detCheckPass->EnableDetour();
detIsPass->EnableDetour();
detSocketClosed->EnableDetour();
detLogCommand->EnableDetour();
return true;
}
#define KILL_DET(det) \
det->DisableDetour(); \
det = NULL;
void RemoveRConDetours()
{
KILL_DET(detWriteReq);
KILL_DET(detCheckPass);
KILL_DET(detIsPass);
KILL_DET(detSocketClosed);
KILL_DET(detLogCommand);
}
cell_t IsCmdFromRCon(IPluginContext *pContext, const cell_t *params)
{
return (g_bInRConCommand) ? 1 : 0;
}
sp_nativeinfo_t g_Natives[] =
{
{"SMRCon_IsCmdFromRCon", IsCmdFromRCon},
{NULL, NULL},
};