| 1 |
|
#include "Daodan_Patch.h" |
| 2 |
< |
#include "Daodan_Utility.h" |
| 2 |
> |
#include "Patches/Utility.h" |
| 3 |
|
#include <beaengine/BeaEngine.h> |
| 4 |
|
|
| 5 |
|
#include <windows.h> |
| 41 |
|
void* DDrPatch_MakeDetour(void* from, void* to) |
| 42 |
|
{ |
| 43 |
|
int len = 0; |
| 44 |
+ |
|
| 45 |
|
/* |
| 46 |
< |
DISASM MyDisasm; |
| 47 |
< |
int i = 0; |
| 47 |
< |
DDrStartupMessage(""); |
| 48 |
< |
DDrStartupMessage(""); |
| 49 |
< |
|
| 50 |
< |
memset (&MyDisasm, 0, sizeof(DISASM)); |
| 51 |
< |
MyDisasm.EIP = (UIntPtr) from; |
| 52 |
< |
i = 0; |
| 53 |
< |
DDrStartupMessage("Orig before @ 0x%06x", from); |
| 54 |
< |
while (i<10){ |
| 55 |
< |
len = Disasm(&MyDisasm); |
| 56 |
< |
if (len != UNKNOWN_OPCODE) { |
| 57 |
< |
DDrStartupMessage("%s, Opcode: 0x%x, len: %d, branch: %d, to: 0x%06x", MyDisasm.CompleteInstr, MyDisasm.Instruction.Opcode, len, MyDisasm.Instruction.BranchType, MyDisasm.Instruction.AddrValue); |
| 58 |
< |
DDrStartupMessage(" Cat: 0x%04x, prefix count: %d", MyDisasm.Instruction.Category & 0xffff, MyDisasm.Prefix.Number ); |
| 59 |
< |
MyDisasm.EIP += (UIntPtr)len; |
| 60 |
< |
i++; |
| 61 |
< |
} |
| 62 |
< |
}; |
| 63 |
< |
DDrStartupMessage(""); |
| 46 |
> |
STARTUPMESSAGE("Orig before", 0); |
| 47 |
> |
DDrPatch_PrintDisasm(from, 10, 0); |
| 48 |
|
*/ |
| 65 |
– |
|
| 49 |
|
DISASM disasm; |
| 50 |
|
memset(&disasm, 0, sizeof(DISASM)); |
| 51 |
|
disasm.EIP = (UIntPtr) from; |
| 60 |
|
if (len != UNKNOWN_OPCODE) { |
| 61 |
|
if ((disasm.Instruction.Category & 0xffff) == CONTROL_TRANSFER) { |
| 62 |
|
if (disasm.Prefix.Number > 0) { |
| 63 |
< |
DDrStartupMessage("Daodan: Detour: Branch in trampoline area from address 0x%08x with prefixes", from); |
| 63 |
> |
STARTUPMESSAGE("Detour: Branch in trampoline area from address 0x%08x with prefixes", from); |
| 64 |
|
return (void*)-1; |
| 65 |
|
} |
| 66 |
|
branches++; |
| 67 |
|
int target = disasm.Instruction.AddrValue; |
| 68 |
< |
bool targetInTrampoline = ((void*)disasm.Instruction.AddrValue - from) < 5; |
| 68 |
> |
bool targetInTrampoline = ((void*)((int)disasm.Instruction.AddrValue) - from) < 5; |
| 69 |
|
switch (disasm.Instruction.BranchType) { |
| 70 |
|
case JmpType: |
| 71 |
|
case CallType: |
| 127 |
|
} |
| 128 |
|
break; |
| 129 |
|
default: |
| 130 |
< |
DDrStartupMessage("Daodan: Detour: Unknown branch in trampoline area from address 0x%08x", from); |
| 130 |
> |
STARTUPMESSAGE("Detour: Unknown branch in trampoline area from address 0x%08x", from); |
| 131 |
|
return (void*)-1; |
| 132 |
|
} |
| 133 |
|
} else { |
| 137 |
|
disasm.EIP += (UIntPtr)len; |
| 138 |
|
} |
| 139 |
|
else { |
| 140 |
< |
DDrStartupMessage("Daodan: Detour: Unknown opcode in trampoline area from address 0x%08x", from); |
| 140 |
> |
STARTUPMESSAGE("Detour: Unknown opcode in trampoline area from address 0x%08x", from); |
| 141 |
|
return (void*)-1; |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
if (branches > 1) { |
| 146 |
< |
DDrStartupMessage("Daodan: Detour: Too many branches in trampoline'd code from address 0x%08x: %d", from, branches); |
| 146 |
> |
STARTUPMESSAGE("Detour: Too many branches in trampoline'd code from address 0x%08x: %d", from, branches); |
| 147 |
|
return (void*)-1; |
| 148 |
|
} |
| 149 |
|
|
| 153 |
|
|
| 154 |
|
DWORD oldp; |
| 155 |
|
if (!VirtualProtect(trampoline, 40, PAGE_EXECUTE_READWRITE, &oldp)) { |
| 156 |
< |
DDrStartupMessage("Daodan: Detour: Could not mark page for trampoline as executable: from address 0x%08x", from); |
| 156 |
> |
STARTUPMESSAGE("Detour: Could not mark page for trampoline as executable: from address 0x%08x", from); |
| 157 |
|
return (void*)-1; |
| 158 |
|
} |
| 159 |
|
DDrPatch_MakeJump(from, to); |
| 160 |
+ |
|
| 161 |
|
/* |
| 162 |
< |
memset (&MyDisasm, 0, sizeof(DISASM)); |
| 163 |
< |
MyDisasm.EIP = (UIntPtr) trampoline; |
| 164 |
< |
i = 0; |
| 165 |
< |
DDrStartupMessage("Trampoline @ 0x%06x", trampoline); |
| 166 |
< |
while (i<10){ |
| 183 |
< |
len = Disasm(&MyDisasm); |
| 184 |
< |
if (len != UNKNOWN_OPCODE) { |
| 185 |
< |
DDrStartupMessage(MyDisasm.CompleteInstr); |
| 186 |
< |
MyDisasm.EIP += (UIntPtr)len; |
| 187 |
< |
i++; |
| 188 |
< |
} |
| 189 |
< |
}; |
| 190 |
< |
DDrStartupMessage(""); |
| 191 |
< |
|
| 192 |
< |
memset (&MyDisasm, 0, sizeof(DISASM)); |
| 193 |
< |
MyDisasm.EIP = disasm.EIP; |
| 194 |
< |
i = 0; |
| 195 |
< |
DDrStartupMessage("Orig after @ 0x%06x", disasm.EIP); |
| 196 |
< |
while (i<7){ |
| 197 |
< |
len = Disasm(&MyDisasm); |
| 198 |
< |
if (len != UNKNOWN_OPCODE) { |
| 199 |
< |
DDrStartupMessage(MyDisasm.CompleteInstr); |
| 200 |
< |
MyDisasm.EIP += (UIntPtr)len; |
| 201 |
< |
i++; |
| 202 |
< |
} |
| 203 |
< |
}; |
| 204 |
< |
DDrStartupMessage(""); |
| 205 |
< |
|
| 206 |
< |
memset (&MyDisasm, 0, sizeof(DISASM)); |
| 207 |
< |
MyDisasm.EIP = (UIntPtr) from; |
| 208 |
< |
i = 0; |
| 209 |
< |
DDrStartupMessage("Orig start after @ 0x%06x", from); |
| 210 |
< |
while (i<3){ |
| 211 |
< |
len = Disasm(&MyDisasm); |
| 212 |
< |
if (len != UNKNOWN_OPCODE) { |
| 213 |
< |
DDrStartupMessage(MyDisasm.CompleteInstr); |
| 214 |
< |
MyDisasm.EIP += (UIntPtr)len; |
| 215 |
< |
i++; |
| 216 |
< |
} |
| 217 |
< |
}; |
| 218 |
< |
DDrStartupMessage(""); |
| 219 |
< |
DDrStartupMessage(""); |
| 220 |
< |
DDrStartupMessage(""); |
| 221 |
< |
*/ |
| 162 |
> |
STARTUPMESSAGE("Trampoline", 0); |
| 163 |
> |
DDrPatch_PrintDisasm(trampoline, 10, 6); |
| 164 |
> |
|
| 165 |
> |
STARTUPMESSAGE("Orig after", 0); |
| 166 |
> |
DDrPatch_PrintDisasm(disasm.EIP, 7, 0); |
| 167 |
|
|
| 168 |
+ |
STARTUPMESSAGE("Orig start after", 0); |
| 169 |
+ |
DDrPatch_PrintDisasm(from, 3, 6); |
| 170 |
+ |
*/ |
| 171 |
|
return trampoline; |
| 172 |
|
} |
| 173 |
|
|
| 227 |
|
return false; |
| 228 |
|
} |
| 229 |
|
|
| 230 |
< |
bool DDrPatch__strdup(int* dest, const char* value) |
| 230 |
> |
bool DDrPatch_NOOP(char* dest, unsigned int length) |
| 231 |
|
{ |
| 232 |
|
DWORD oldp; |
| 233 |
|
|
| 234 |
< |
if (VirtualProtect(dest, 4, PAGE_EXECUTE_READWRITE, &oldp)) |
| 234 |
> |
if (VirtualProtect(dest, length, PAGE_EXECUTE_READWRITE, &oldp)) |
| 235 |
|
{ |
| 236 |
< |
*dest = (int)_strdup(value); |
| 237 |
< |
VirtualProtect(dest, 4, oldp, &oldp); |
| 236 |
> |
memset(dest, 0x90, length); |
| 237 |
> |
VirtualProtect(dest, length, oldp, &oldp); |
| 238 |
|
return true; |
| 239 |
|
} |
| 240 |
|
else |
| 241 |
|
return false; |
| 242 |
|
} |
| 243 |
|
|
| 244 |
< |
bool DDrPatch_NOOP(char* dest, unsigned int length) |
| 244 |
> |
void* DDrPatch_ExecutableASM(char* from, char* nextInst, const unsigned char* code, int length) |
| 245 |
|
{ |
| 246 |
+ |
char* newCode = malloc(length+5); |
| 247 |
+ |
if (!DDrPatch_NOOP(newCode, length+5)) |
| 248 |
+ |
return (void*)-1; |
| 249 |
+ |
|
| 250 |
+ |
memcpy(newCode, code, length); |
| 251 |
+ |
if (!DDrPatch_MakeJump(&newCode[length], nextInst)) |
| 252 |
+ |
return (void*)-1; |
| 253 |
+ |
|
| 254 |
|
DWORD oldp; |
| 255 |
< |
|
| 256 |
< |
if (VirtualProtect(dest, length, PAGE_EXECUTE_READWRITE, &oldp)) |
| 257 |
< |
{ |
| 302 |
< |
memset(dest, 0x90, length); |
| 303 |
< |
VirtualProtect(dest, length, oldp, &oldp); |
| 304 |
< |
return true; |
| 255 |
> |
if (!VirtualProtect(newCode, length+5, PAGE_EXECUTE_READWRITE, &oldp)) { |
| 256 |
> |
STARTUPMESSAGE("ExecASM: Could not mark page for new code as executable: from address 0x%08x", from); |
| 257 |
> |
return (void*)-1; |
| 258 |
|
} |
| 259 |
< |
else |
| 260 |
< |
return false; |
| 259 |
> |
|
| 260 |
> |
if (!DDrPatch_MakeJump(from, newCode)) |
| 261 |
> |
return (void*)-1; |
| 262 |
> |
|
| 263 |
> |
return newCode; |
| 264 |
|
} |
| 265 |
+ |
|
| 266 |
+ |
void DDrPatch_PrintDisasm(void* addr, int instLimit, int sizeLimit) |
| 267 |
+ |
{ |
| 268 |
+ |
DISASM MyDisasm; |
| 269 |
+ |
int len = 0; |
| 270 |
+ |
int size = 0; |
| 271 |
+ |
int i = 0; |
| 272 |
+ |
|
| 273 |
+ |
memset(&MyDisasm, 0, sizeof(DISASM)); |
| 274 |
+ |
|
| 275 |
+ |
MyDisasm.EIP = (UIntPtr) addr; |
| 276 |
+ |
|
| 277 |
+ |
STARTUPMESSAGE("", 0); |
| 278 |
+ |
STARTUPMESSAGE("Disassembly @ 0x%06x", addr); |
| 279 |
+ |
|
| 280 |
+ |
if (sizeLimit <= 0) |
| 281 |
+ |
sizeLimit = 20 * instLimit; |
| 282 |
+ |
|
| 283 |
+ |
while ((i < instLimit) && (size < sizeLimit)) { |
| 284 |
+ |
len = Disasm(&MyDisasm); |
| 285 |
+ |
if (len != UNKNOWN_OPCODE) { |
| 286 |
+ |
size += len; |
| 287 |
+ |
STARTUPMESSAGE(" %s, Opcode: 0x%x, len: %d, branch: %d, to: 0x%06x", MyDisasm.CompleteInstr, MyDisasm.Instruction.Opcode, len, MyDisasm.Instruction.BranchType, MyDisasm.Instruction.AddrValue); |
| 288 |
+ |
STARTUPMESSAGE(" Cat: 0x%04x, prefix count: %d", MyDisasm.Instruction.Category & 0xffff, MyDisasm.Prefix.Number ); |
| 289 |
+ |
|
| 290 |
+ |
MyDisasm.EIP += (UIntPtr)len; |
| 291 |
+ |
i++; |
| 292 |
+ |
} |
| 293 |
+ |
}; |
| 294 |
+ |
|
| 295 |
+ |
STARTUPMESSAGE("", 0); |
| 296 |
+ |
} |
| 297 |
+ |
|