- Timestamp:
- Aug 23, 2012, 1:21:31 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
zzuf/trunk/src/libzzuf/sys.c
r4837 r4838 49 49 #if defined HAVE_WINDOWS_H 50 50 static void insert_funcs(void); 51 52 /* TODO: get rid of this later */53 HINSTANCE (WINAPI *LoadLibraryA_orig)(LPCSTR);54 HINSTANCE WINAPI LoadLibraryA_new(LPCSTR path)55 {56 return LoadLibraryA_orig(path);57 }58 59 BOOL (WINAPI *AllocConsole_orig)(void);60 BOOL WINAPI AllocConsole_new(void)61 {62 return AllocConsole_orig();63 }64 65 BOOL (WINAPI *AttachConsole_orig)(DWORD);66 BOOL WINAPI AttachConsole_new(DWORD d)67 {68 return AttachConsole_orig(d);69 }70 51 #endif 71 52 … … 130 111 switch (opcd) 131 112 { 132 case 0x68: return (insn_size + 4); /* PUSH Iv */ 133 case 0x6a: return (insn_size + 1); /* PUSH Ib */ 134 case 0x90: return insn_size; /* NOP */ 135 default: break; 113 case 0x68: 114 return (insn_size + 4); /* PUSH Iv */ 115 case 0x6a: 116 return (insn_size + 1); /* PUSH Ib */ 117 case 0x90: 118 return insn_size; /* NOP */ 119 case 0xb8: 120 case 0xb9: 121 case 0xba: 122 case 0xbb: 123 case 0xbc: 124 case 0xbd: 125 case 0xbe: 126 case 0xbf: 127 return insn_size + 5; /* MOV immediate */ 128 default: 129 break; 136 130 } 137 131 138 132 /* PUSH/POP rv */ 139 if ((opcd & 0xf0) == 0x50) return insn_size; 133 if ((opcd & 0xf0) == 0x50) 134 return insn_size; 140 135 141 136 /* MNEM E?, G? or G?, E? */ … … 159 154 break; 160 155 161 default: break; 156 default: 157 fprintf(stderr, "unknown opcode %02x\n", opcd); 158 break; 162 159 } 163 160 … … 172 169 { 173 170 int insn_size = zz_lde(code); 174 if (insn_size == 0) return -1; 171 if (insn_size == 0) 172 return -1; 175 173 patch_size += insn_size; 176 174 } … … 206 204 207 205 trampoline = malloc(patch_size + reloc_size + 13); /* Worst case */ 208 if (trampoline == NULL) return -1; 206 if (trampoline == NULL) 207 return -1; 209 208 memset(trampoline, 0xcc, patch_size + 13); 210 209 … … 212 211 { 213 212 int insn_size = zz_lde(code + code_offset); 214 if (insn_size == 0) return -1; 213 if (insn_size == 0) 214 return -1; 215 215 216 216 /* mov rax, [rip + ...] is the signature for stack cookie */ … … 335 335 /* if we can't get enough byte, we quit */ 336 336 if ((patch_size = compute_patch_size(old_api, required_size)) == -1) 337 { 338 fprintf(stderr, "cannot compute patch size\n"); 337 339 return -1; 338 339 if (make_trampoline(old_api, patch_size, &trampoline, &trampoline_size) < 0) goto _out; 340 } 341 342 if (make_trampoline(old_api, patch_size, &trampoline, &trampoline_size) < 0) 343 { 344 fprintf(stderr, "cannot make trampoline\n"); 345 goto _out; 346 } 340 347 341 348 /* We must make the trampoline executable, this line is required because of DEP */ 342 349 /* NOTE: We _must_ set the write protection, otherwise the heap allocator will crash ! */ 343 if (!VirtualProtect(trampoline, trampoline_size, PAGE_EXECUTE_READWRITE, &old_prot)) goto _out; 350 if (!VirtualProtect(trampoline, trampoline_size, PAGE_EXECUTE_READWRITE, &old_prot)) 351 { 352 fprintf(stderr, "cannot make the trampoline writable\n"); 353 goto _out; 354 } 344 355 345 356 /* We patch the targeted API, so we must set it as writable */ 346 if (!VirtualProtect(old_api, patch_size, PAGE_EXECUTE_READWRITE, &old_prot)) goto _out; 357 if (!VirtualProtect(old_api, patch_size, PAGE_EXECUTE_READWRITE, &old_prot)) 358 { 359 fprintf(stderr, "cannot make old API writable\n"); 360 goto _out; 361 } 347 362 memcpy(old_api, jmp_prolog, patch_size); 348 363 VirtualProtect(old_api, patch_size, old_prot, &old_prot); /* we don't care if this functon fails */
Note: See TracChangeset
for help on using the changeset viewer.