asm/asm.c
author Ruben Gonzalez <drifter01620@gmail.com>
Wed Jul 02 14:07:04 2014 -0400 (2014-07-02)
changeset 186 072ae02a1bc6
parent 184 7ddf645a50d5
permissions -rw-r--r--
Fix warning flags.
     1 #include "asm.h"
     2 
     3 #ifndef WIN32
     4 #define _GNU_SOURCE
     5 #include <dlfcn.h>
     6 #include <string.h>
     7 
     8 #define REG_EAX			0
     9 #define REG_ECX			1
    10 #define REG_EDX			2
    11 #define REG_EBX			3
    12 
    13 #define IA32_MOV_REG_IMM		0xB8	// encoding is +r <imm32>
    14 #endif
    15 
    16 extern void Msg( const char *, ... );
    17 
    18 /**
    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.
    21 *
    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).
    24 * @noreturn
    25 */
    26 void check_thunks(unsigned char *dest, unsigned char *pc)
    27 {
    28 #if defined WIN32
    29 	return;
    30 #else
    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);
    35 
    36 	/* Lookup name of function being called */
    37 	if ((*calladdr == 0x8B) && (*(calladdr+2) == 0x24) && (*(calladdr+3) == 0xC3))
    38 	{
    39 		//a thunk maybe?
    40 		char movByte = IA32_MOV_REG_IMM;
    41 
    42 		/* Calculate the correct mov opcode */
    43 		switch (*(calladdr+1))
    44 		{
    45 		case 0x04:
    46 			{
    47 				movByte += REG_EAX;
    48 				break;
    49 			}
    50 		case 0x1C:
    51 			{
    52 				movByte += REG_EBX;
    53 				break;
    54 			}
    55 		case 0x0C:
    56 			{
    57 				movByte += REG_ECX;
    58 				break;
    59 			}
    60 		case 0x14:
    61 			{
    62 				movByte += REG_EDX;
    63 				break;
    64 			}
    65 		default:
    66 			{
    67 				Msg("Unknown thunk: %c\n", *(calladdr+1));
    68 				break;
    69 			}
    70 		}
    71 
    72 		/* Move our write address back one to where the call opcode was */
    73 		writeaddr--;
    74 
    75 
    76 		/* Write our mov */
    77 		*writeaddr = movByte;
    78 		writeaddr++;
    79 
    80 		/* Write the value - The provided program counter value */
    81 		*(void **)writeaddr = (void *)pc;
    82 		writeaddr += 4;
    83 	}
    84 
    85 	return;
    86 #endif
    87 }
    88 
    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) {
    93 	int bytecount = 0;
    94 
    95 	while(bytecount < required_len && *func != 0xCC)
    96 	{
    97 		// prefixes F0h, F2h, F3h, 66h, 67h, D8h-DFh, 2Eh, 36h, 3Eh, 26h, 64h and 65h
    98 		int operandSize = 4;
    99 		int FPU = 0;
   100 		int twoByte = 0;
   101 		unsigned char opcode = 0x90;
   102 		unsigned char modRM = 0xFF;
   103 		while(*func == 0xF0 ||
   104 			  *func == 0xF2 ||
   105 			  *func == 0xF3 ||
   106 			 (*func & 0xFC) == 0x64 ||
   107 			 (*func & 0xF8) == 0xD8 ||
   108 			 (*func & 0x7E) == 0x62)
   109 		{
   110 			if(*func == 0x66)
   111 			{
   112 				operandSize = 2;
   113 			}
   114 			else if((*func & 0xF8) == 0xD8)
   115 			{
   116 				FPU = *func;
   117 				if (dest)
   118 						*dest++ = *func++;
   119 				else
   120 						func++;
   121 				bytecount++;
   122 				break;
   123 			}
   124 
   125 			if (dest)
   126 				*dest++ = *func++;
   127 			else
   128 				func++;
   129 			bytecount++;
   130 		}
   131 
   132 		// two-byte opcode byte
   133 		if(*func == 0x0F)
   134 		{
   135 			twoByte = 1;
   136 			if (dest)
   137 				*dest++ = *func++;
   138 			else
   139 				func++;
   140 			bytecount++;
   141 		}
   142 
   143 		// opcode byte
   144 		opcode = *func++;
   145 		if (dest) *dest++ = opcode;
   146 		bytecount++;
   147 
   148 		// mod R/M byte
   149 		modRM = 0xFF;
   150 		if(FPU)
   151 		{
   152 			if((opcode & 0xC0) != 0xC0)
   153 			{
   154 				modRM = opcode;
   155 			}
   156 		}
   157 		else if(!twoByte)
   158 		{
   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)
   165 			{
   166 				modRM = *func++;
   167 				if (dest) *dest++ = modRM;
   168 				bytecount++;
   169 			}
   170 		}
   171 		else
   172 		{
   173 			if(((opcode & 0xF0) == 0x00 && (opcode & 0x0F) >= 0x04 && (opcode & 0x0D) != 0x0D) ||
   174 			   (opcode & 0xF0) == 0x30 ||
   175 			   opcode == 0x77 ||
   176 			   (opcode & 0xF0) == 0x80 ||
   177 			   ((opcode & 0xF0) == 0xA0 && (opcode & 0x07) <= 0x02) ||
   178 			   (opcode & 0xF8) == 0xC8)
   179 			{
   180 				// No mod R/M byte
   181 			}
   182 			else
   183 			{
   184 				modRM = *func++;
   185 				if (dest) *dest++ = modRM;
   186 				bytecount++;
   187 			}
   188 		}
   189 
   190 		// SIB
   191 		if((modRM & 0x07) == 0x04 &&
   192 		   (modRM & 0xC0) != 0xC0)
   193 		{
   194 			if (dest)
   195 				*dest++ = *func++;   //SIB
   196 			else
   197 				func++;
   198 			bytecount++;
   199 		}
   200 
   201 		// mod R/M displacement
   202 
   203 		// Dword displacement, no base
   204 	if((modRM & 0xC5) == 0x05) {
   205 		if (dest) {
   206 			*(unsigned int*)dest = *(unsigned int*)func;
   207 			dest += 4;
   208 		}
   209 		func += 4;
   210 		bytecount += 4;
   211 	}
   212 
   213 		// Byte displacement
   214 	if((modRM & 0xC0) == 0x40) {
   215 		if (dest)
   216 			*dest++ = *func++;
   217 		else
   218 			func++;
   219 		bytecount++;
   220 	}
   221 
   222 		// Dword displacement
   223 	if((modRM & 0xC0) == 0x80) {
   224 		if (dest) {
   225 			*(unsigned int*)dest = *(unsigned int*)func;
   226 			dest += 4;
   227 		}
   228 		func += 4;
   229 		bytecount += 4;
   230 	}
   231 
   232 		// immediate
   233 		if(FPU)
   234 		{
   235 			// Can't have immediate operand
   236 		}
   237 		else if(!twoByte)
   238 		{
   239 			if((opcode & 0xC7) == 0x04 ||
   240 			   (opcode & 0xFE) == 0x6A ||   // PUSH/POP/IMUL
   241 			   (opcode & 0xF0) == 0x70 ||   // Jcc
   242 			   opcode == 0x80 ||
   243 			   opcode == 0x83 ||
   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
   252 			   opcode == 0xEB ||
   253 			   (opcode == 0xF6 && (modRM & 0x30) == 0x00))   // TEST
   254 			{
   255 				if (dest)
   256 					*dest++ = *func++;
   257 				else
   258 					func++;
   259 				bytecount++;
   260 			}
   261 		else if((opcode & 0xF7) == 0xC2) // RET
   262 			{
   263 				if (dest) {
   264 					*(unsigned short*)dest = *(unsigned short*)func;
   265 					dest += 2;
   266 				}
   267 				func += 2;
   268 				bytecount += 2;
   269 			}
   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 ||
   277 					opcode == 0xC7 ||
   278 					(opcode == 0xF7 && (modRM & 0x30) == 0x00))
   279 			{
   280 				if (dest) {
   281 					//Fix CALL/JMP offset
   282 					if ((opcode & 0xFE) == 0xE8) {
   283 						if (operandSize == 4)
   284 						{
   285 							*(long*)dest = ((func + *(long*)func) - dest);
   286 
   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);
   289 						}
   290 						else
   291 							*(short*)dest = ((func + *(short*)func) - dest);
   292 
   293 					} else {
   294 						if (operandSize == 4)
   295 							*(unsigned long*)dest = *(unsigned long*)func;
   296 						else
   297 							*(unsigned short*)dest = *(unsigned short*)func;
   298 					}
   299 					dest += operandSize;
   300 				}
   301 				func += operandSize;
   302 				bytecount += operandSize;
   303 
   304 			}
   305 		}
   306 		else
   307 		{
   308 			if(opcode == 0xBA ||			// BT
   309 			   opcode == 0x0F ||			// 3DNow!
   310 			   (opcode & 0xFC) == 0x70 ||   // PSLLW
   311 			   (opcode & 0xF7) == 0xA4 ||   // SHLD
   312 			   opcode == 0xC2 ||
   313 			   opcode == 0xC4 ||
   314 			   opcode == 0xC5 ||
   315 			   opcode == 0xC6)
   316 			{
   317 				if (dest)
   318 					*dest++ = *func++;
   319 				else
   320 					func++;
   321 			}
   322 			else if((opcode & 0xF0) == 0x80) // Jcc -i
   323 			{
   324 				if (dest) {
   325 					if (operandSize == 4)
   326 						*(unsigned long*)dest = *(unsigned long*)func;
   327 					else
   328 						*(unsigned short*)dest = *(unsigned short*)func;
   329 
   330 					dest += operandSize;
   331 				}
   332 				func += operandSize;
   333 				bytecount += operandSize;
   334 			}
   335 		}
   336 	}
   337 
   338 	return bytecount;
   339 }
   340 
   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));
   345 }
   346 
   347 //fill a given block with NOPs
   348 void fill_nop(void* src, unsigned int len) {
   349 	unsigned char* src2 = (unsigned char*)src;
   350 	while (len) {
   351 		*src2++ = OP_NOP;
   352 		--len;
   353 	}
   354 }
   355 
   356 void* eval_jump(void* src) {
   357 	unsigned char* addr = (unsigned char*)src;
   358 
   359 	if (!addr) return 0;
   360 
   361 	//import table jump
   362 	if (addr[0] == OP_PREFIX && addr[1] == OP_JMP_SEG) {
   363 		addr += 2;
   364 		addr = *(unsigned char**)addr;
   365 		//TODO: if addr points into the IAT
   366 		return *(void**)addr;
   367 	}
   368 
   369 	//8bit offset
   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];
   375 		}
   376 		return addr;
   377 	}
   378 	/*
   379 	//32bit offset
   380 	else if (addr[0] == OP_JMP) {
   381 		addr = &addr[OP_JMP_SIZE] + *(int*)&addr[1];
   382 	}
   383 	*/
   384 
   385 	return addr;
   386 }
   387 /*
   388 from ms detours package
   389 static bool detour_is_imported(PBYTE pbCode, PBYTE pbAddress)
   390 {
   391 	MEMORY_BASIC_INFORMATION mbi;
   392 	VirtualQuery((PVOID)pbCode, &mbi, sizeof(mbi));
   393 	__try {
   394 		PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)mbi.AllocationBase;
   395 		if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE) {
   396 			return false;
   397 		}
   398 
   399 		PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((PBYTE)pDosHeader +
   400 														  pDosHeader->e_lfanew);
   401 		if (pNtHeader->Signature != IMAGE_NT_SIGNATURE) {
   402 			return false;
   403 		}
   404 
   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)) {
   413 			return true;
   414 		}
   415 		return false;
   416 	}
   417 	__except(EXCEPTION_EXECUTE_HANDLER) {
   418 		return false;
   419 	}
   420 }
   421 */