Fix warning flags.
13 #define IA32_MOV_REG_IMM 0xB8 // encoding is +r <imm32>
16 extern void Msg( const char *, ... );
19 * Checks if a call to a fpic thunk has just been written into dest.
20 * If found replaces it with a direct mov that sets the required register to the value of pc.
22 * @param dest Destination buffer where a call opcode + addr (5 bytes) has just been written.
23 * @param pc The program counter value that needs to be set (usually the next address from the source).
26 void check_thunks(unsigned char *dest, unsigned char *pc)
31 /* Step write address back 4 to the start of the function address */
32 unsigned char *writeaddr = dest - 4;
33 unsigned char *calloffset = *(unsigned char **)writeaddr;
34 unsigned char *calladdr = (unsigned char *)(dest + (unsigned int)calloffset);
36 /* Lookup name of function being called */
37 if ((*calladdr == 0x8B) && (*(calladdr+2) == 0x24) && (*(calladdr+3) == 0xC3))
40 char movByte = IA32_MOV_REG_IMM;
42 /* Calculate the correct mov opcode */
43 switch (*(calladdr+1))
67 Msg("Unknown thunk: %c\n", *(calladdr+1));
72 /* Move our write address back one to where the call opcode was */
80 /* Write the value - The provided program counter value */
81 *(void **)writeaddr = (void *)pc;
89 //if dest is NULL, returns minimum number of bytes needed to be copied
90 //if dest is not NULL, it will copy the bytes to dest as well as fix CALLs and JMPs
91 //http://www.devmaster.net/forums/showthread.php?t=2311
92 int copy_bytes(unsigned char *func, unsigned char* dest, int required_len) {
95 while(bytecount < required_len && *func != 0xCC)
97 // prefixes F0h, F2h, F3h, 66h, 67h, D8h-DFh, 2Eh, 36h, 3Eh, 26h, 64h and 65h
101 unsigned char opcode = 0x90;
102 unsigned char modRM = 0xFF;
103 while(*func == 0xF0 ||
106 (*func & 0xFC) == 0x64 ||
107 (*func & 0xF8) == 0xD8 ||
108 (*func & 0x7E) == 0x62)
114 else if((*func & 0xF8) == 0xD8)
132 // two-byte opcode byte
145 if (dest) *dest++ = opcode;
152 if((opcode & 0xC0) != 0xC0)
159 if((opcode & 0xC4) == 0x00 ||
160 ((opcode & 0xF4) == 0x60 && ((opcode & 0x0A) == 0x02 || (opcode & 0x09) == 0x09)) ||
161 (opcode & 0xF0) == 0x80 ||
162 ((opcode & 0xF8) == 0xC0 && (opcode & 0x0E) != 0x02) ||
163 (opcode & 0xFC) == 0xD0 ||
164 (opcode & 0xF6) == 0xF6)
167 if (dest) *dest++ = modRM;
173 if(((opcode & 0xF0) == 0x00 && (opcode & 0x0F) >= 0x04 && (opcode & 0x0D) != 0x0D) ||
174 (opcode & 0xF0) == 0x30 ||
176 (opcode & 0xF0) == 0x80 ||
177 ((opcode & 0xF0) == 0xA0 && (opcode & 0x07) <= 0x02) ||
178 (opcode & 0xF8) == 0xC8)
185 if (dest) *dest++ = modRM;
191 if((modRM & 0x07) == 0x04 &&
192 (modRM & 0xC0) != 0xC0)
195 *dest++ = *func++; //SIB
201 // mod R/M displacement
203 // Dword displacement, no base
204 if((modRM & 0xC5) == 0x05) {
206 *(unsigned int*)dest = *(unsigned int*)func;
214 if((modRM & 0xC0) == 0x40) {
222 // Dword displacement
223 if((modRM & 0xC0) == 0x80) {
225 *(unsigned int*)dest = *(unsigned int*)func;
235 // Can't have immediate operand
239 if((opcode & 0xC7) == 0x04 ||
240 (opcode & 0xFE) == 0x6A || // PUSH/POP/IMUL
241 (opcode & 0xF0) == 0x70 || // Jcc
244 (opcode & 0xFD) == 0xA0 || // MOV
245 opcode == 0xA8 || // TEST
246 (opcode & 0xF8) == 0xB0 || // MOV
247 (opcode & 0xFE) == 0xC0 || // RCL
248 opcode == 0xC6 || // MOV
249 opcode == 0xCD || // INT
250 (opcode & 0xFE) == 0xD4 || // AAD/AAM
251 (opcode & 0xF8) == 0xE0 || // LOOP/JCXZ
253 (opcode == 0xF6 && (modRM & 0x30) == 0x00)) // TEST
261 else if((opcode & 0xF7) == 0xC2) // RET
264 *(unsigned short*)dest = *(unsigned short*)func;
270 else if((opcode & 0xFC) == 0x80 ||
271 (opcode & 0xC7) == 0x05 ||
272 (opcode & 0xF8) == 0xB8 ||
273 (opcode & 0xFE) == 0xE8 || // CALL/Jcc
274 (opcode & 0xFE) == 0x68 ||
275 (opcode & 0xFC) == 0xA0 ||
276 (opcode & 0xEE) == 0xA8 ||
278 (opcode == 0xF7 && (modRM & 0x30) == 0x00))
281 //Fix CALL/JMP offset
282 if ((opcode & 0xFE) == 0xE8) {
283 if (operandSize == 4)
285 *(long*)dest = ((func + *(long*)func) - dest);
287 //pRED* edit. func is the current address of the call address, +4 is the next instruction, so the value of $pc
288 check_thunks(dest+4, func+4);
291 *(short*)dest = ((func + *(short*)func) - dest);
294 if (operandSize == 4)
295 *(unsigned long*)dest = *(unsigned long*)func;
297 *(unsigned short*)dest = *(unsigned short*)func;
302 bytecount += operandSize;
308 if(opcode == 0xBA || // BT
309 opcode == 0x0F || // 3DNow!
310 (opcode & 0xFC) == 0x70 || // PSLLW
311 (opcode & 0xF7) == 0xA4 || // SHLD
322 else if((opcode & 0xF0) == 0x80) // Jcc -i
325 if (operandSize == 4)
326 *(unsigned long*)dest = *(unsigned long*)func;
328 *(unsigned short*)dest = *(unsigned short*)func;
333 bytecount += operandSize;
341 //insert a specific JMP instruction at the given location
342 void inject_jmp(void* src, void* dest) {
343 *(unsigned char*)src = OP_JMP;
344 *(long*)((unsigned char*)src+1) = (long)((unsigned char*)dest - ((unsigned char*)src + OP_JMP_SIZE));
347 //fill a given block with NOPs
348 void fill_nop(void* src, unsigned int len) {
349 unsigned char* src2 = (unsigned char*)src;
356 void* eval_jump(void* src) {
357 unsigned char* addr = (unsigned char*)src;
362 if (addr[0] == OP_PREFIX && addr[1] == OP_JMP_SEG) {
364 addr = *(unsigned char**)addr;
365 //TODO: if addr points into the IAT
366 return *(void**)addr;
370 else if (addr[0] == OP_JMP_BYTE) {
371 addr = &addr[OP_JMP_BYTE_SIZE] + *(char*)&addr[1];
372 //mangled 32bit jump?
373 if (addr[0] == OP_JMP) {
374 addr = addr + *(int*)&addr[1];
380 else if (addr[0] == OP_JMP) {
381 addr = &addr[OP_JMP_SIZE] + *(int*)&addr[1];
388 from ms detours package
389 static bool detour_is_imported(PBYTE pbCode, PBYTE pbAddress)
391 MEMORY_BASIC_INFORMATION mbi;
392 VirtualQuery((PVOID)pbCode, &mbi, sizeof(mbi));
394 PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)mbi.AllocationBase;
395 if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE) {
399 PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((PBYTE)pDosHeader +
400 pDosHeader->e_lfanew);
401 if (pNtHeader->Signature != IMAGE_NT_SIGNATURE) {
405 if (pbAddress >= ((PBYTE)pDosHeader +
406 pNtHeader->OptionalHeader
407 .DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress) &&
408 pbAddress < ((PBYTE)pDosHeader +
409 pNtHeader->OptionalHeader
410 .DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress +
411 pNtHeader->OptionalHeader
412 .DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size)) {
417 __except(EXCEPTION_EXECUTE_HANDLER) {