This repository was archived by the owner on Dec 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathIAntiRevoke.cpp
393 lines (320 loc) · 12 KB
/
IAntiRevoke.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "IAntiRevoke.h"
#include <unordered_map>
#include <MinHook.h>
#include <spdlog/spdlog.h>
#include "IRuntime.h"
#include "Utils.h"
IAntiRevoke& IAntiRevoke::GetInstance()
{
static IAntiRevoke i;
return i;
}
void IAntiRevoke::InitMarker()
{
/*
Italian
GetId : it
GetPluralId : it
GetName : Italian
GetNativeName : Italiano
English
GetId : en
GetPluralId : en
GetName : English
GetNativeName : English
Simplified Chinese
GetId : classic-zh-cn
GetPluralId : zh
GetName : Chinese (Simplified, @zh_CN)
GetNativeName : [bad string]
Traditional Chinese
GetId : zhhant-hk
GetPluralId : zh
GetName : Chinese (Traditional, Hong Kong)
GetNativeName : [bad string]
Japanese
GetId : ja-raw
GetPluralId : ja
GetName : Japanese
GetNativeName : [bad string]
Korean
GetId : ko
GetPluralId : ko
GetName : Korean
GetNativeName : [bad string]
So we use PluralId and Name.
*/
std::unordered_map<std::wstring, std::vector<MarkDataT>> MultiLangMarks =
{
{
L"it",
{
{ L"Italian", L"eliminato ", 10 * 6 }
}
},
{
L"en",
{
{ L"English", L"deleted ", 8 * 6 }
}
},
{
L"zh",
{
{ L"Simplified", L"已删除 ", 7 * 6 },
{ L"Traditional", L"已刪除 ", 7 * 6 },
{ L"Cantonese", L"刪咗 ", 5 * 6 } // Thanks @Rongronggg9, #29
}
},
{
L"ja",
{
{ L"Japanese", L"削除された ", 11 * 6 }
}
},
{
L"ko",
{
{ L"Korean", L"삭제 ", 5 * 6 }
}
}
// For more languages or corrections, please submit on the GitHub Issue Tracker.
};
// Set default lang
//
_MarkData = MultiLangMarks[L"en"].at(0);
Safe::TryExcept(
[&]()
{
LanguageInstance *pLangInstance = IRuntime::GetInstance().GetData().Address.pLangInstance;
std::wstring CurrentPluralId = pLangInstance->GetPluralId()->GetText();
std::wstring CurrentName = pLangInstance->GetName()->GetText();
// Fix for irregularly named language packages
//
if (CurrentPluralId == L"yue" && CurrentName == L"Cantonese") {
CurrentPluralId = L"zh";
}
// Find language
//
auto Iterator = MultiLangMarks.find(CurrentPluralId);
if (Iterator == MultiLangMarks.end()) {
spdlog::warn("An unadded language. PluralId: \"{}\", Name: \"{}\"", Convert::UnicodeToAnsi(CurrentPluralId), Convert::UnicodeToAnsi(CurrentName));
return;
}
const std::vector<MarkDataT> &Sublanguages = Iterator->second;
// Set default sublanguage
//
_MarkData = Sublanguages.at(0);
// If has multiple sublanguages
//
if (Sublanguages.size() > 1)
{
for (const MarkDataT &Language : Sublanguages)
{
if (CurrentName.find(Language.LangName) != std::wstring::npos)
{
// Found sublanguage, set it
//
_MarkData = Language;
break;
}
}
}
}, [&](ULONG ExceptionCode)
{
spdlog::warn("Function: [" __FUNCTION__ "] An exception was caught. Code: {:#x}", ExceptionCode);
}
);
}
void IAntiRevoke::SetupHooks()
{
MH_STATUS Status = MH_Initialize();
if (Status != MH_OK) {
spdlog::critical("[IAntiRevoke] MH_Initialize() failed. Status: {}", MH_StatusToString(Status));
return;
}
if (!HookFreeFunction()) {
spdlog::critical("[IAntiRevoke] HookFreeFunction() failed.");
return;
}
if (!HookRevokeFunction()) {
spdlog::critical("[IAntiRevoke] HookRevokeFunction() failed.");
return;
}
}
void IAntiRevoke::ProcessBlockedMessages()
{
while (true)
{
Sleep(1000);
std::lock_guard<std::mutex> Lock(_Mutex);
for (HistoryMessage *pMessage : _BlockedMessages)
{
Safe::TryExcept(
[&]()
{
QtString *pTimeText = nullptr;
HistoryMessageEdited *pEdited = pMessage->GetEdited();
HistoryMessageSigned *pSigned = pMessage->GetSigned();
// Signed msg take precedence over Edited msg, and TG uses the Signed text when both exist.
//
if (pSigned != nullptr) {
// Signed msg
pTimeText = pSigned->GetTimeText();
}
else if (pEdited != nullptr) {
// Edited msg
pTimeText = pEdited->GetTimeText();
}
else {
// Normal msg
pTimeText = pMessage->GetTimeText();
}
// vvvvvvvvvvvvvvvvvvvv TODO: This is a workaround, try to hook HistoryMessage's destructor to improve.
if (pTimeText == nullptr ||
pTimeText->IsEmpty() || // This message content hasn't been cached by Telegram.
pTimeText->Find(_MarkData.Content) != std::wstring::npos /* This message is marked. */)
{
return;
}
// Mark "deleted"
//
std::wstring MarkedTime;
if (pSigned != nullptr)
{
// Signed msg text: "<author>, <time>" ("xxx, 10:20")
//
std::wstring OriginalString = pTimeText->GetText();
size_t Pos = OriginalString.rfind(L", ");
if (Pos == std::wstring::npos) {
return;
}
MarkedTime = OriginalString.substr(0, Pos + 2) + _MarkData.Content + OriginalString.substr(Pos + 2);
}
else {
MarkedTime = _MarkData.Content + pTimeText->GetText();
}
pTimeText->Replace(MarkedTime.c_str());
// Modify width
//
HistoryViewElement *pMainView = pMessage->GetMainView();
if (pMainView == nullptr) {
return;
}
pMainView->SetWidth(pMainView->GetWidth() + _MarkData.Width);
pMessage->SetTimeWidth(pMessage->GetTimeWidth() + _MarkData.Width);
Media *pMainViewMedia = pMainView->GetMedia();
if (pMainViewMedia != nullptr) {
pMainViewMedia->SetWidth(pMainViewMedia->GetWidth() + _MarkData.Width);
}
HistoryMessageReply *pReply = pMessage->GetReply();
if (pReply != nullptr) {
pReply->MaxReplyWidth() += _MarkData.Width;
}
}, [&](ULONG ExceptionCode)
{
spdlog::warn("Function: [" __FUNCTION__ "] An exception was caught. Code: {:#x}, Address: {}", ExceptionCode, (void*)pMessage);
}
);
}
}
}
void IAntiRevoke::CallFree(void* Block)
{
_FnOriginalFree(Block);
}
bool IAntiRevoke::HookFreeFunction()
{
auto FnFree = IRuntime::GetInstance().GetData().Function.Free;
if (MH_CreateHook(FnFree, &IAntiRevoke::Callback_DetourFree, (void**)&_FnOriginalFree) != MH_OK) {
return false;
}
return MH_EnableHook(FnFree) == MH_OK;
}
bool IAntiRevoke::HookRevokeFunction()
{
void* HookAddress = IRuntime::GetInstance().GetData().Address.FnDestroyMessageCaller;
void* TargetAddress = Utils::GetFunctionAddress(&History::OnDestroyMessage);
// Save the original function
//
_FnOriginalDestroyMessage = (FnDestroyMessageT)((uintptr_t)HookAddress + 5 + *(int32_t*)((uintptr_t)HookAddress + 1));
#if defined PLATFORM_X86
std::vector<uint8_t> Shellcode = Memory::MakeCall(HookAddress, TargetAddress);
return Memory::ForceOperate(
HookAddress,
Shellcode.size(),
[&]() {
memcpy(HookAddress, Shellcode.data(), Shellcode.size());
}
);
#elif defined PLATFORM_X64
std::vector<uint8_t> Shellcode = {
0xFF, 0xFF, 0xFF, // mov rcx,rbx ; Original placeholders
0xFF, 0x15, 0x02, 0x00, 0x00, 0x00, 0xEB, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // call History::OnDestroyMessage ; Detour
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // cmp byte ptr [rbx+00000228],00 { 0 } ; Original
0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // jmp XXXXXXXXXXXXXXXX ; Jump back
};
auto HookBegin = (uint8_t*)HookAddress - 3;
auto JumpBack = HookBegin + 15;
std::memcpy(Shellcode.data(), HookBegin, 3);
std::memcpy(Shellcode.data() + 11, &TargetAddress, sizeof(TargetAddress));
std::memcpy(Shellcode.data() + 19, HookBegin + 8, 7);
std::memcpy(Shellcode.data() + 32, &JumpBack, sizeof(JumpBack));
std::vector<uint8_t> Jumper = {
0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // jmp XXXXXXXXXXXXXXXX
};
auto Allocated = VirtualAlloc(nullptr, 0x1000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (Allocated == nullptr) {
spdlog::warn("VirtualAlloc failed. LastError: {}", ::GetLastError());
return false;
}
std::memcpy(Jumper.data() + 6, &Allocated, sizeof(Allocated));
std::memcpy(Allocated, Shellcode.data(), Shellcode.size());
return Memory::ForceOperate(HookBegin, Jumper.size(),
[&]() {
std::memcpy(HookBegin, Jumper.data(), Jumper.size());
}
);
#else
# error "Unimplemented."
#endif
}
void IAntiRevoke::OnFree(void *Block)
{
std::unique_lock<std::mutex> Lock(_Mutex);
// When we delete a msg by ourselves, Telegram will free this memory block.
// So, we need to earse this msg from the vector.
//
_BlockedMessages.erase((HistoryMessage*)Block);
Lock.unlock();
CallFree(Block);
}
void IAntiRevoke::OnDestroyMessage(History *pHistory, HistoryMessage* pMessage)
{
Safe::TryExcept(
[&]()
{
// TODO: Allow revoking BOT messages in non-private chats.
//
if (!pMessage->IsMessage()) {
return;
}
QtString *pTimeText = pMessage->GetTimeText();
if (!pTimeText->IsValidTime()) {
spdlog::warn("A bad TimeText. Address: {}", (void*)pMessage);
return;
}
spdlog::debug("Caught a deleted meesage. Address: {}", (void*)pMessage);
std::lock_guard<std::mutex> Lock(_Mutex);
_BlockedMessages.insert(pMessage);
},
[&](ULONG ExceptionCode)
{
spdlog::warn("Function: [" __FUNCTION__ "] An exception was caught. Code: {:#x}", ExceptionCode);
}
);
}
void __cdecl IAntiRevoke::Callback_DetourFree(void *Block)
{
IAntiRevoke::GetInstance().OnFree(Block);
}