-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall_mmap.c
48 lines (46 loc) · 1021 Bytes
/
call_mmap.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
41
42
43
44
45
46
47
48
#include <sys/mman.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ptrace_injector.h"
#define SYS_MMAP 9
void *call_mmap(int pid, void *dst)
{
struct user_regs_struct regs;
if (ptrace(PTRACE_GETREGS, pid, ®s, ®s) < 0)
{
perror("ptrace getregs");
exit(1);
}
// 0f 05 = syscall
copy_into_mem(pid, 2, dst, "\x0f\x05");
regs.rax = SYS_MMAP;
regs.rdi = 0;
regs.rsi = 4096;
regs.rdx = PROT_READ | PROT_EXEC;
regs.r10 = MAP_PRIVATE | MAP_ANONYMOUS;
regs.r8 = -1;
regs.r9 = 0;
regs.rip = (long) dst;
if (ptrace(PTRACE_SETREGS, pid, ®s, ®s) < 0)
{
perror("ptrace setregs");
exit(1);
}
if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) < 0)
{
perror("ptrace singlestep");
exit(1);
}
waitpid(pid, 0, 0);
if (ptrace(PTRACE_GETREGS, pid, ®s, ®s) < 0)
{
perror("ptrace getregs");
exit(1);
}
return ((void *) regs.rax);
}