--- Daodan/src/Daodan_Patch.c 2014/03/16 20:06:52 983 +++ Daodan/src/Daodan_Patch.c 2014/08/25 23:21:09 1008 @@ -1,5 +1,5 @@ #include "Daodan_Patch.h" -#include "Daodan_Utility.h" +#include "Patches/Utility.h" #include #include @@ -41,28 +41,11 @@ bool DDrPatch_MakeCall(void* from, void* void* DDrPatch_MakeDetour(void* from, void* to) { int len = 0; + /* - DISASM MyDisasm; - int i = 0; - DDrStartupMessage(""); - DDrStartupMessage(""); - - memset (&MyDisasm, 0, sizeof(DISASM)); - MyDisasm.EIP = (UIntPtr) from; - i = 0; - DDrStartupMessage("Orig before @ 0x%06x", from); - while (i<10){ - len = Disasm(&MyDisasm); - if (len != UNKNOWN_OPCODE) { - DDrStartupMessage("%s, Opcode: 0x%x, len: %d, branch: %d, to: 0x%06x", MyDisasm.CompleteInstr, MyDisasm.Instruction.Opcode, len, MyDisasm.Instruction.BranchType, MyDisasm.Instruction.AddrValue); - DDrStartupMessage(" Cat: 0x%04x, prefix count: %d", MyDisasm.Instruction.Category & 0xffff, MyDisasm.Prefix.Number ); - MyDisasm.EIP += (UIntPtr)len; - i++; - } - }; - DDrStartupMessage(""); + STARTUPMESSAGE("Orig before", 0); + DDrPatch_PrintDisasm(from, 10, 0); */ - DISASM disasm; memset(&disasm, 0, sizeof(DISASM)); disasm.EIP = (UIntPtr) from; @@ -77,7 +60,7 @@ void* DDrPatch_MakeDetour(void* from, vo if (len != UNKNOWN_OPCODE) { if ((disasm.Instruction.Category & 0xffff) == CONTROL_TRANSFER) { if (disasm.Prefix.Number > 0) { - DDrStartupMessage("Daodan: Detour: Branch in trampoline area from address 0x%08x with prefixes", from); + STARTUPMESSAGE("Detour: Branch in trampoline area from address 0x%08x with prefixes", from); return (void*)-1; } branches++; @@ -144,7 +127,7 @@ void* DDrPatch_MakeDetour(void* from, vo } break; default: - DDrStartupMessage("Daodan: Detour: Unknown branch in trampoline area from address 0x%08x", from); + STARTUPMESSAGE("Detour: Unknown branch in trampoline area from address 0x%08x", from); return (void*)-1; } } else { @@ -154,65 +137,37 @@ void* DDrPatch_MakeDetour(void* from, vo disasm.EIP += (UIntPtr)len; } else { - DDrStartupMessage("Daodan: Detour: Unknown opcode in trampoline area from address 0x%08x", from); + STARTUPMESSAGE("Detour: Unknown opcode in trampoline area from address 0x%08x", from); return (void*)-1; } } if (branches > 1) { - DDrStartupMessage("Daodan: Detour: Too many branches in trampoline'd code from address 0x%08x: %d", from, branches); + STARTUPMESSAGE("Detour: Too many branches in trampoline'd code from address 0x%08x: %d", from, branches); return (void*)-1; } DDrPatch_MakeJump(&trampoline[pos], (void*)disasm.EIP); DDrPatch_NOOP(from, (void*)disasm.EIP - from); + + DWORD oldp; + if (!VirtualProtect(trampoline, 40, PAGE_EXECUTE_READWRITE, &oldp)) { + STARTUPMESSAGE("Detour: Could not mark page for trampoline as executable: from address 0x%08x", from); + return (void*)-1; + } DDrPatch_MakeJump(from, to); + /* - memset (&MyDisasm, 0, sizeof(DISASM)); - MyDisasm.EIP = (UIntPtr) trampoline; - i = 0; - DDrStartupMessage("Trampoline @ 0x%06x", trampoline); - while (i<10){ - len = Disasm(&MyDisasm); - if (len != UNKNOWN_OPCODE) { - DDrStartupMessage(MyDisasm.CompleteInstr); - MyDisasm.EIP += (UIntPtr)len; - i++; - } - }; - DDrStartupMessage(""); - - memset (&MyDisasm, 0, sizeof(DISASM)); - MyDisasm.EIP = disasm.EIP; - i = 0; - DDrStartupMessage("Orig after @ 0x%06x", disasm.EIP); - while (i<7){ - len = Disasm(&MyDisasm); - if (len != UNKNOWN_OPCODE) { - DDrStartupMessage(MyDisasm.CompleteInstr); - MyDisasm.EIP += (UIntPtr)len; - i++; - } - }; - DDrStartupMessage(""); - - memset (&MyDisasm, 0, sizeof(DISASM)); - MyDisasm.EIP = (UIntPtr) from; - i = 0; - DDrStartupMessage("Orig start after @ 0x%06x", from); - while (i<3){ - len = Disasm(&MyDisasm); - if (len != UNKNOWN_OPCODE) { - DDrStartupMessage(MyDisasm.CompleteInstr); - MyDisasm.EIP += (UIntPtr)len; - i++; - } - }; - DDrStartupMessage(""); - DDrStartupMessage(""); - DDrStartupMessage(""); - */ + STARTUPMESSAGE("Trampoline", 0); + DDrPatch_PrintDisasm(trampoline, 10, 6); + + STARTUPMESSAGE("Orig after", 0); + DDrPatch_PrintDisasm(disasm.EIP, 7, 0); + + STARTUPMESSAGE("Orig start after", 0); + DDrPatch_PrintDisasm(from, 3, 6); +*/ return trampoline; } @@ -272,30 +227,71 @@ bool DDrPatch_Int16(short* dest, unsigne return false; } -bool DDrPatch__strdup(int* dest, const char* value) +bool DDrPatch_NOOP(char* dest, unsigned int length) { DWORD oldp; - if (VirtualProtect(dest, 4, PAGE_EXECUTE_READWRITE, &oldp)) + if (VirtualProtect(dest, length, PAGE_EXECUTE_READWRITE, &oldp)) { - *dest = (int)_strdup(value); - VirtualProtect(dest, 4, oldp, &oldp); + memset(dest, 0x90, length); + VirtualProtect(dest, length, oldp, &oldp); return true; } else return false; } -bool DDrPatch_NOOP(char* dest, unsigned int length) +void* DDrPatch_ExecutableASM(char* from, char* nextInst, const unsigned char* code, int length) { + char* newCode = malloc(length+5); + if (!DDrPatch_NOOP(newCode, length+5)) + return (void*)-1; + + memcpy(newCode, code, length); + if (!DDrPatch_MakeJump(&newCode[length], nextInst)) + return (void*)-1; + DWORD oldp; - - if (VirtualProtect(dest, length, PAGE_EXECUTE_READWRITE, &oldp)) - { - memset(dest, 0x90, length); - VirtualProtect(dest, length, oldp, &oldp); - return true; + if (!VirtualProtect(newCode, length+5, PAGE_EXECUTE_READWRITE, &oldp)) { + STARTUPMESSAGE("ExecASM: Could not mark page for new code as executable: from address 0x%08x", from); + return (void*)-1; } - else - return false; + + if (!DDrPatch_MakeJump(from, newCode)) + return (void*)-1; + + return newCode; } + +void DDrPatch_PrintDisasm(void* addr, int instLimit, int sizeLimit) +{ + DISASM MyDisasm; + int len = 0; + int size = 0; + int i = 0; + + memset(&MyDisasm, 0, sizeof(DISASM)); + + MyDisasm.EIP = (UIntPtr) addr; + + STARTUPMESSAGE("", 0); + STARTUPMESSAGE("Disassembly @ 0x%06x", addr); + + if (sizeLimit <= 0) + sizeLimit = 20 * instLimit; + + while ((i < instLimit) && (size < sizeLimit)) { + len = Disasm(&MyDisasm); + if (len != UNKNOWN_OPCODE) { + size += len; + STARTUPMESSAGE(" %s, Opcode: 0x%x, len: %d, branch: %d, to: 0x%06x", MyDisasm.CompleteInstr, MyDisasm.Instruction.Opcode, len, MyDisasm.Instruction.BranchType, MyDisasm.Instruction.AddrValue); + STARTUPMESSAGE(" Cat: 0x%04x, prefix count: %d", MyDisasm.Instruction.Category & 0xffff, MyDisasm.Prefix.Number ); + + MyDisasm.EIP += (UIntPtr)len; + i++; + } + }; + + STARTUPMESSAGE("", 0); +} +