-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathramdownloader.c
83 lines (71 loc) · 2.53 KB
/
ramdownloader.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <errno.h>
#include <string.h>
#include <sys/sysinfo.h>
void modify_totalram(pid_t child_pid, unsigned long addr) {
struct sysinfo info;
// Read the sysinfo struct from child's memory
errno = 0;
long data;
for (unsigned long i = 0; i < sizeof(info); i += sizeof(data)) {
data = ptrace(PTRACE_PEEKDATA, child_pid, addr + i, NULL);
if (data == -1 && errno != 0) {
perror("PTRACE_PEEKDATA failed");
return;
}
memcpy((char *)&info + i, &data, sizeof(data));
}
printf("Original totalram: %lu\n", info.totalram);
printf("Downloading more RAM...\n");
// Modify the totalram value
info.totalram = info.totalram * 20; // Ram multiplier
// Write the modified sysinfo struct back to child's memory
for (unsigned long i = 0; i < sizeof(info); i += sizeof(data)) {
memcpy(&data, (char *)&info + i, sizeof(data));
if (ptrace(PTRACE_POKEDATA, child_pid, addr + i, data) == -1) {
perror("PTRACE_POKEDATA failed");
return;
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <program> [args...]\n", argv[0]);
exit(EXIT_FAILURE);
}
pid_t child_pid = fork();
if (child_pid == 0) {
// Child process
ptrace(PTRACE_TRACEME, 0, NULL, NULL); // Allow the parent to trace
raise(SIGSTOP); // Stop until the parent is ready to trace
execvp(argv[1], argv + 1);
perror("execvp"); // execvp only returns on error
exit(EXIT_FAILURE);
} else {
// Parent process
int status;
waitpid(child_pid, &status, WUNTRACED); // Wait for the child to stop
struct user_regs_struct regs;
while (1) {
ptrace(PTRACE_SYSCALL, child_pid, NULL, NULL);
waitpid(child_pid, &status, 0);
if (WIFEXITED(status)) break; // Exit loop if child has exited
ptrace(PTRACE_GETREGS, child_pid, NULL, ®s);
if (regs.orig_rax == SYS_sysinfo) {
// Check if this is the entry or exit of the syscall
if (regs.rax == 0) { // Assuming syscall successful, modify on exit
modify_totalram(child_pid, regs.rdi); // rdi holds the pointer to the sysinfo struct
}
}
}
}
return 0;
}