--- Daodan/src/Daodan_Patch.c 2013/03/04 15:52:19 689 +++ Daodan/src/Daodan_Patch.c 2014/03/16 20:06:52 983 @@ -1,4 +1,7 @@ #include "Daodan_Patch.h" +#include "Daodan_Utility.h" +#include + #include #include #include @@ -35,6 +38,184 @@ bool DDrPatch_MakeCall(void* from, void* return false; } +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(""); +*/ + + DISASM disasm; + memset(&disasm, 0, sizeof(DISASM)); + disasm.EIP = (UIntPtr) from; + + char* trampoline = malloc(40); + DDrPatch_NOOP(trampoline, 40); + int pos = 0; + int branches = 0; + + while (((void*)disasm.EIP - from) < 5) { + len = Disasm(&disasm); + 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); + return (void*)-1; + } + branches++; + int target = disasm.Instruction.AddrValue; + bool targetInTrampoline = ((void*)disasm.Instruction.AddrValue - from) < 5; + switch (disasm.Instruction.BranchType) { + case JmpType: + case CallType: + if (targetInTrampoline) { + int offset = disasm.Instruction.AddrValue - disasm.EIP; + if (disasm.Instruction.BranchType == JmpType) + DDrPatch_MakeJump(&trampoline[pos], &trampoline[pos]+offset); + else + DDrPatch_MakeCall(&trampoline[pos], &trampoline[pos]+offset); + } else { + if (disasm.Instruction.BranchType == JmpType) + DDrPatch_MakeJump(&trampoline[pos], (void*)target); + else + DDrPatch_MakeCall(&trampoline[pos], (void*)target); + } + pos += 5; + break; + case RetType: + case JECXZ: + memcpy(&trampoline[pos], (void*)disasm.EIP, len); + pos += len; + break; + // Opcode +1 + case JO: + case JC: + case JE: + case JNA: + case JS: + case JP: + case JL: + case JNG: + if (targetInTrampoline) { + memcpy(&trampoline[pos], (void*)disasm.EIP, len); + pos += len; + } else { + trampoline[pos++] = disasm.Instruction.Opcode + 1; + trampoline[pos++] = 5; + DDrPatch_MakeJump(&trampoline[pos], (void*)target); + pos += 5; + } + break; + // Opcode -1 + case JNO: + case JNC: + case JNE: + case JA: + case JNS: + case JNP: + case JNL: + case JG: + if (targetInTrampoline) { + memcpy(&trampoline[pos], (void*)disasm.EIP, len); + pos += len; + } else { + trampoline[pos++] = disasm.Instruction.Opcode - 1; + trampoline[pos++] = 5; + DDrPatch_MakeJump(&trampoline[pos], (void*)target); + pos += 5; + } + break; + default: + DDrStartupMessage("Daodan: Detour: Unknown branch in trampoline area from address 0x%08x", from); + return (void*)-1; + } + } else { + memcpy(&trampoline[pos], (void*)disasm.EIP, len); + pos += len; + } + disasm.EIP += (UIntPtr)len; + } + else { + DDrStartupMessage("Daodan: 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); + return (void*)-1; + } + + + DDrPatch_MakeJump(&trampoline[pos], (void*)disasm.EIP); + DDrPatch_NOOP(from, (void*)disasm.EIP - from); + 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(""); + */ + return trampoline; +} + bool DDrPatch_String(char* dest, const unsigned char* string, int length) { DWORD oldp;