/
Main.cpp
157 lines (130 loc) · 4.35 KB
/
Main.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
#include <Windows.h>
#include <cstdio>
typedef NTSTATUS(__stdcall* ZwRaiseException_t)(PEXCEPTION_RECORD ExceptionRecord, PCONTEXT ThreadContext, BOOLEAN HandleException);
ZwRaiseException_t raiseExceptionFunc;
void PrintSingleCharacter(char c)
{
DWORD written;
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), &c, 1, &written, NULL);
}
void PrintMessage(const char* fmt, ...)
{
char buf[4096];
va_list args;
va_start(args, fmt);
vsnprintf_s(buf, 4096, fmt, args);
char* bufptr = buf;
while (*bufptr)
{
if (*bufptr == '~')
{
++bufptr;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), *bufptr);
}
else
{
PrintSingleCharacter(*bufptr);
}
++bufptr;
}
PrintSingleCharacter('\n');
va_end(args);
}
__declspec(naked) void FinishThread()
{
__asm {
push 0 // STATUS_SUCCESS
push -2 // ZwCurrentThread()
call TerminateThread
}
}
unsigned char codeBuf[52428800];
unsigned char leagueBuf[52428800];
DWORD_PTR baseTextAddress = 0;
DWORD WINAPI GetProtectedMemory(PVOID Address)
{
CONTEXT ctx;
EXCEPTION_RECORD exr;
MEMORY_BASIC_INFORMATION mbi;
memset(&mbi, 0, sizeof(mbi));
VirtualQuery(Address, &mbi, sizeof(mbi));
if (mbi.Protect == PAGE_NOACCESS)
{
RtlCaptureContext(&ctx);
memset(&exr, 0, sizeof(EXCEPTION_RECORD));
ctx.Eip = (DWORD)FinishThread;
exr.ExceptionAddress = (PVOID)Address;
exr.NumberParameters = 2;
exr.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
exr.ExceptionInformation[1] = (ULONG_PTR)Address;
raiseExceptionFunc(&exr, &ctx, 1);
}
TerminateThread(GetCurrentThread(), 0);
return 0;
}
void EnsureMemoryIsDecrypted(PVOID Address)
{
HANDLE hThread = CreateThread(NULL, 0, GetProtectedMemory, Address, NULL, 0);
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
}
DWORD WINAPI DoStuff(LPVOID lpParameter)
{
AllocConsole();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
DWORD textPageCount = 0;
PIMAGE_DOS_HEADER imageDosHeader = (PIMAGE_DOS_HEADER)GetModuleHandleA(NULL);
PIMAGE_NT_HEADERS imageNtHeaders = (PIMAGE_NT_HEADERS)((DWORD_PTR)imageDosHeader + imageDosHeader->e_lfanew);
DWORD sectionCount = imageNtHeaders->FileHeader.NumberOfSections;
PIMAGE_SECTION_HEADER imageSection = (PIMAGE_SECTION_HEADER)((DWORD_PTR)&imageNtHeaders->OptionalHeader + imageNtHeaders->FileHeader.SizeOfOptionalHeader);
for (DWORD i = 0; i < sectionCount; ++i)
{
if (strcmp((const char*)(imageSection + i)->Name, ".text") == 0)
{
baseTextAddress = (DWORD_PTR)imageDosHeader + (imageSection + i)->PointerToRawData;
textPageCount = (imageSection + i)->SizeOfRawData / 0x1000;
break;
}
}
for (DWORD i = 0; i < textPageCount; ++i)
{
LPVOID mem = (LPVOID)(baseTextAddress + i * 0x1000);
EnsureMemoryIsDecrypted(mem);
memcpy((unsigned char*)codeBuf + ((DWORD_PTR)mem - baseTextAddress), mem, 0x1000);
if ((i + 1) % 200 == 0 || i + 1 == textPageCount)
PrintMessage("Sections decrypted: ~%c%d/%d~%c", 11, i + 1, textPageCount, 15);
}
DWORD fileWritten;
HANDLE hFile = CreateFileA("League of Legends.exe", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL);
DWORD leagueExeSize = GetFileSize(hFile, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD read;
ReadFile(hFile, leagueBuf, leagueExeSize, &read, NULL);
CloseHandle(hFile);
hFile = CreateFileA("League of Legends_decrypted.exe", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
memcpy(leagueBuf + (baseTextAddress - (DWORD_PTR)imageDosHeader), codeBuf, textPageCount * 0x1000);
WriteFile(hFile, leagueBuf, leagueExeSize, &fileWritten, NULL);
PrintMessage("===============================================================================");
PrintMessage("Base address: ~%c0x%08x~%c", 11, imageDosHeader, 15);
PrintMessage("~%cAll sections dumped~%c: decrypted .exe in:", 10, 15);
char fileNameBuf[512];
memset(fileNameBuf, 0, sizeof(fileNameBuf));
GetFinalPathNameByHandleA(hFile, fileNameBuf, 512, 0);
PrintMessage("~%c%s~%c", 11, fileNameBuf, 15);
CloseHandle(hFile);
}
}
return 0;
}
BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
raiseExceptionFunc = (ZwRaiseException_t)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtRaiseException");
CloseHandle(CreateThread(NULL, 0, DoStuff, NULL, 0, NULL));
}
return TRUE;
}