-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshiva_target.c
42 lines (37 loc) · 1.13 KB
/
shiva_target.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* This source file contains code that tweak, alter, read and write
* to the memory space of the executable target program.
*/
#include "shiva.h"
/*
* This function modifies the "live" dynamic segment in memory. It will modify
* the first dynamic tag found of type 'tag' and change it to 'value'.
*/
bool
shiva_target_dynamic_set(struct shiva_ctx *ctx, uint64_t tag, uint64_t value)
{
int i;
uint64_t phdr_vaddr = ctx->ulexec.base_vaddr + elf_phoff(&ctx->elfobj);
Elf64_Phdr *phdr = (Elf64_Phdr *)phdr_vaddr;
Elf64_Dyn *dyn = NULL;
for (i = 0; i < elf_segment_count(&ctx->elfobj); i++) {
shiva_debug("%d == PT_DYNAMIC(2)\n", phdr[i].p_type);
if (phdr[i].p_type != PT_DYNAMIC)
continue;
dyn = (Elf64_Dyn *)((uint64_t)(phdr[i].p_vaddr + ctx->ulexec.base_vaddr));
break;
}
if (dyn == NULL) {
fprintf(stderr, "shiva_target_dynamic_tag() failed, dyn == NULL\n");
return false;
}
printf("Looking for tag: %d\n", tag);
for (i = 0; dyn[i].d_tag != DT_NULL; i++) {
if (dyn[i].d_tag == tag) {
shiva_debug("Set dynamic tag %d: %#lx\n", dyn[i].d_tag, value);
dyn[i].d_un.d_val = value;
return true;
}
}
return false;
}