Skip to content

Commit 3273bfb

Browse files
committed
Fix elf64DynMode.
Since Ubuntu 22.04 Address Space Layout Randomization is active, so it's needed to be taken into account when you want to hook dynamic functions.
1 parent 0b0a8ca commit 3273bfb

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

cTools/libs/binDynMod/elfDynMod/elf64DynMod.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ void *elf64Hook(const Elf64File *elf64, const char *func, const void *hand)
5555
}
5656

5757
uint64_t relpltAmount = relplt->sh_size / sizeof(Elf64Rel);
58+
Elf64Sym *hook_sym = elf64GetSymByName(elf64, "elf64Hook");
59+
uint64_t func_original_addr = elf64GetSSymAddr(hook_sym);
60+
uint64_t func_addr_diff = (uint64_t)&elf64Hook - func_original_addr;
5861

5962
/***
6063
* r_info - This member gives both the symbol table index,
@@ -70,9 +73,11 @@ void *elf64Hook(const Elf64File *elf64, const char *func, const void *hand)
7073
void *relAddr = NULL;
7174
uint64_t i = 0;
7275
for (i = 0; i < relpltAmount; ++i)
73-
if (ELF64_R_SYM(elf64->relaplt[i].r_info) == symbolIndex){
74-
relAddr = (void*) *(uint64_t*) elf64->relaplt[i].r_offset;
75-
*(uint64_t*) (elf64->relaplt[i].r_offset) = (uint64_t) hand;
76+
if (ELF64_R_SYM(elf64->relaplt[i].r_info) == symbolIndex) {
77+
uint64_t offset = elf64->relaplt[i].r_offset;
78+
uint64_t* addr = (uint64_t*)(func_addr_diff + offset);
79+
relAddr = (void*) *addr;
80+
*addr = (uint64_t) hand;
7681

7782
return relAddr;
7883
}

0 commit comments

Comments
 (0)