-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyPtrace.cpp
More file actions
58 lines (51 loc) · 1.25 KB
/
myPtrace.cpp
File metadata and controls
58 lines (51 loc) · 1.25 KB
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
49
50
51
52
53
54
55
56
57
58
#include "myPtrace.h"
// attach to pid
int attach(pid_t pid)
{
int status;
if(ptrace(PTRACE_ATTACH, pid, NULL, NULL))
{
perror("PTRACE_ATTACH");
return 1;
}
std::cout << "attached to " << pid << std::endl << "wait for child" << std::endl;
wait(&status);
std::cout << "Status: " << status << std::endl;
return 0;
}
// detach to pid
int detach(pid_t pid)
{
if(ptrace(PTRACE_DETACH, pid, NULL, NULL))
{
perror("PTRACE_DETACH");
return 1;
}
std::cout << "detached from " << pid << std::endl;
return 0;
}
// peek data
int peek(pid_t pid, unsigned long addr, long &buffer)
{
errno = 0;
if(((buffer = ptrace(PTRACE_PEEKDATA, pid, (void *)addr, NULL)) == -1) && errno)
{
perror("PTRACE_PEEKDATA");
if(ptrace(PTRACE_DETACH, pid, NULL, NULL))
perror("PTRACE_DETACH");
return 1;
}
return 0;
}
// poke data
int poke(pid_t pid, unsigned long addr, long buffer)
{
errno = 0;
if (ptrace(PTRACE_POKEDATA, pid, (void *)addr, buffer) == -1 && errno)
{
perror("PTRACE_POKE");
return 1;
}
std::cout << "successfully wrote " << buffer << " to " << std::hex << "0x" << addr << std::dec << std::endl;
return 0;
}