This repository has been archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
lib.cpp
178 lines (155 loc) · 4.01 KB
/
lib.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
#include <Windows.h>
#include "MinHook.h"
size_t read_uint8888_le_addr = 0;
size_t read_uint88_le_addr = 0;
size_t v_disk_file_in_stream_read_addr = 0;
typedef size_t(__thiscall* v_disk_file_in_stream_read_t)(void*, void*, size_t);
v_disk_file_in_stream_read_t builtin_v_disk_file_in_stream_read = 0;
__declspec(naked) void read_uint16_le_hook()
{
{__asm {
push ecx
mov ecx, [edi + 0x20]
mov edx, [edi + 4]
push ebx
mov ebx, [esp + 8 + 4]
push ebp
push esi
push 2
lea eax, [esp + 0x14 - 2]
push eax
push ebx
push ecx
xor esi, esi
call edx
add esp, 0x10
cmp eax, 2
jnz error
movzx esi, word ptr ss:[esp + 0x10 - 2]
mov ecx, [esp + 0x10 + 8]
xor eax, eax
mov[ecx], esi
end:
pop esi
pop ebp
pop ebx
pop ecx
retn
error:
mov eax, 0xFFFFFFFF
mov[ecx], 0
jmp end
}}
}
__declspec(naked) void read_uint32_le_hook()
{
{__asm {
push ecx
mov ecx, [esi + 0x20]
mov edx, [esi + 4]
push ebx
mov ebx, [esp + 8 + 4]
push ebp
push edi
push 4
lea eax, [esp + 0x14 - 4]
push eax
push ebx
push ecx
xor edi, edi
call edx
add esp, 0x10
cmp eax, 4
jnz error
mov edi, [esp + 0x10 - 4]
mov ecx, [esp + 0x10 + 8]
xor eax, eax
mov[ecx], edi
end:
pop edi
pop ebp
pop ebx
pop ecx
retn
error:
mov eax, 0xFFFFFFFF
mov ecx, [esp + 0x10 + 8]
mov[ecx], 0
jmp end
}}
}
size_t __fastcall v_disk_file_in_stream_read_cache(void* _this, void* unused, void* buffer, size_t size)
{
static char cache[0xFF] = {};
static size_t cursor = NULL;
static size_t cache_size = NULL;
if (size != 2 && size != 4)
{
return builtin_v_disk_file_in_stream_read(_this, buffer, size);
}
if (cursor < cache_size) {
memcpy(buffer, (char*)(cache + cursor), size);
cursor += size;
if (cursor >= cache_size) {
cache_size = NULL;
cursor = NULL;
}
return size;
}
size_t result = builtin_v_disk_file_in_stream_read(_this, buffer, size);
if (size == 4)
{
switch (*(unsigned __int32*)buffer)
{
// Local file header
case 0x04034B50:
case 0x51561E05:
cache_size = 26;
break;
// Central directory file header
case 0x02014B50:
case 0x57541E05:
cache_size = 42;
break;
// EOCD
case 0x06054B50:
case 0x53501E05:
cache_size = 18;
break;
default:
return result;
}
builtin_v_disk_file_in_stream_read(_this, cache, cache_size);
cursor = 0;
}
return result;
}
void start()
{
size_t base_addr = (size_t)GetModuleHandleA("Base.dll");
read_uint8888_le_addr = base_addr + 0x0002FFB0;
read_uint88_le_addr = base_addr + 0x0002FF10;
v_disk_file_in_stream_read_addr = base_addr + 0x0006A000;
MH_Initialize();
// Patch 1: 바이너리 읽기 성능 개선
MH_CreateHook((LPVOID)read_uint8888_le_addr, &read_uint32_le_hook, NULL);
MH_CreateHook((LPVOID)read_uint88_le_addr, &read_uint16_le_hook, NULL);
MH_EnableHook((LPVOID)read_uint8888_le_addr);
MH_EnableHook((LPVOID)read_uint88_le_addr);
// Patch 2: zip 헤더 캐시
MH_CreateHook((LPVOID)v_disk_file_in_stream_read_addr, &v_disk_file_in_stream_read_cache, (LPVOID*)&builtin_v_disk_file_in_stream_read);
MH_EnableHook((LPVOID)v_disk_file_in_stream_read_addr);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReversed)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
start();
break;
case DLL_PROCESS_DETACH:
// MH_Uninitialize();
break;
}
return TRUE;
}