-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
424 lines (339 loc) · 11.7 KB
/
main.c
File metadata and controls
424 lines (339 loc) · 11.7 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <dirent.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#define IOVEC_ENTRY(x) {x ? (char *)x : 0, x ? strlen(x) + 1 : 0}
#define IOVEC_SIZE(x) (sizeof(x) / sizeof(struct iovec))
#define PAYLOAD_NAME "backpork.elf"
typedef struct notify_request {
char unused[45];
char message[3075];
} notify_request_t;
typedef struct app_info {
uint32_t app_id;
uint64_t unknown1;
char title_id[14];
char unknown2[0x3c];
} app_info_t;
extern int sceKernelSendNotificationRequest(int, notify_request_t*, size_t, int);
extern int sceKernelGetAppInfo(pid_t pid, app_info_t *info);
static void notify(const char* fmt, ...) {
notify_request_t req = {};
va_list args;
va_start(args, fmt);
vsnprintf(req.message, sizeof(req.message) - 1, fmt, args);
va_end(args);
sceKernelSendNotificationRequest(0, &req, sizeof(req), 0);
}
// from john-tornblom
static pid_t find_pid(const char *name) {
int mib[4] = {1, 14, 8, 0};
pid_t mypid = getpid();
pid_t pid = -1;
size_t buf_size;
uint8_t *buf;
if (sysctl(mib, 4, 0, &buf_size, 0, 0)) {
return -1;
}
if (!(buf = malloc(buf_size))) {
return -1;
}
if (sysctl(mib, 4, buf, &buf_size, 0, 0)) {
free(buf);
return -1;
}
for (uint8_t *ptr = buf; ptr < (buf + buf_size);) {
int ki_structsize = *(int *)ptr;
pid_t ki_pid = *(pid_t *)&ptr[72];
char *ki_tdname = (char *)&ptr[447];
ptr += ki_structsize;
if (!strcmp(name, ki_tdname) && ki_pid != mypid) {
pid = ki_pid;
}
}
free(buf);
return pid;
}
static int cleanup_directory(const char *path) {
DIR *d = opendir(path);
if (!d) {
return -1;
}
int result = 0;
struct dirent *entry;
while ((entry = readdir(d)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
char full_path[PATH_MAX];
int path_len = snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
if (path_len < 0 || path_len >= sizeof(full_path)) {
result = -1;
break;
}
struct stat st;
if (stat(full_path, &st) != 0) {
result = -1;
break;
}
if (S_ISDIR(st.st_mode)) {
if (cleanup_directory(full_path) != 0) {
result = -1;
break;
}
}
// Skip files (don't delete them)
}
closedir(d);
if (result == 0) {
result = rmdir(path);
}
return result;
}
static int mount2(const char *src, const char *dst, const char *type) {
struct iovec iov[] = {
IOVEC_ENTRY("fstype"),
IOVEC_ENTRY(type),
IOVEC_ENTRY("from"),
IOVEC_ENTRY(src),
IOVEC_ENTRY("fspath"),
IOVEC_ENTRY(dst),
};
return nmount(iov, IOVEC_SIZE(iov), 0);
}
static char *mount_fakelibs(const char *sandbox_id, const char *cwd, pid_t pid, char *random_folder) {
char fake_path[PATH_MAX + 1];
snprintf(fake_path, sizeof(fake_path), "%s/fakelib", cwd);
struct stat st;
if (stat(fake_path, &st) != 0) {
printf("[WARNING] stat on %s failed (errno: %d, %s)\n", fake_path, errno, strerror(errno));
return NULL;
}
char *fake_mount_path = (char *)malloc(PATH_MAX + 1);
if (!fake_mount_path) {
return NULL;
}
snprintf(fake_mount_path, PATH_MAX + 1, "/mnt/sandbox/%s/%s/common/lib", sandbox_id, random_folder);
int res = mount2(fake_path, fake_mount_path, "unionfs");
if (res != 0) {
printf("[WARNING] mount_unionfs failed: %d (errno: %d, %s)\n", res, errno, strerror(errno));
unmount(fake_mount_path, MNT_FORCE);
free(fake_mount_path);
return NULL;
}
printf("[INFO] Mounted fakelibs from %s to %s\n", fake_path, fake_mount_path);
return fake_mount_path;
}
static void wait_for_pid_exit(pid_t pid) {
int kq = kqueue();
if (kq == -1) {
perror("kqueue");
return;
}
struct kevent kev;
EV_SET(&kev, pid, EVFILT_PROC, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_EXIT, 0, NULL);
int ret = kevent(kq, &kev, 1, NULL, 0, NULL);
if (ret == -1) {
printf("[WARNING] kevent registration failed for pid %d: %s\n", pid, strerror(errno));
close(kq);
sleep(3);
return;
}
printf("[INFO] Waiting for pid %d to exit...\n", pid);
while (1) {
struct kevent event;
int nev = kevent(kq, NULL, 0, &event, 1, NULL);
if (nev < 0) {
printf("[WARNING] kevent wait failed: %s\n", strerror(errno));
close(kq);
return;
}
if (nev > 0 && event.fflags & NOTE_EXIT) {
printf("[INFO] Process %d exited\n", pid);
break;
}
}
close(kq);
}
static int find_highest_sandbox_number(const char* title_id) {
char base_path[PATH_MAX];
int highest = -1;
for (int i = 0; i < 1000; i++) {
snprintf(base_path, sizeof(base_path), "/mnt/sandbox/%s_%03d", title_id, i);
struct stat st;
if (stat(base_path, &st) == 0 && S_ISDIR(st.st_mode)) {
highest = i;
} else {
break;
}
}
return highest;
}
static char* find_random_folder(const char* title_id, int sandbox_num) {
char base_path[PATH_MAX];
snprintf(base_path, sizeof(base_path), "/mnt/sandbox/%s_%03d", title_id, sandbox_num);
DIR* dir = opendir(base_path);
if (!dir) {
printf("[WARNING] Failed to open directory: %s\n", base_path);
return NULL;
}
struct dirent* entry;
while ((entry = readdir(dir))) {
if (entry->d_name[0] == '.') {
continue;
}
char full_path[PATH_MAX];
snprintf(full_path, sizeof(full_path), "%s/%s/common/lib", base_path, entry->d_name);
struct stat st;
if (stat(full_path, &st) == 0 && S_ISDIR(st.st_mode)) {
closedir(dir);
printf("[DEBUG] Found random folder: %s in sandbox %03d\n", entry->d_name, sandbox_num);
return strdup(entry->d_name);
}
}
closedir(dir);
printf("[WARNING] No random folder found in %s\n", base_path);
return NULL;
}
static void cleanup_game(pid_t pid, const char *sandbox_id, char *fake_mount_path) {
// Wait for sandbox to be cleaned up by the system
char sandbox_app0[PATH_MAX];
snprintf(sandbox_app0, sizeof(sandbox_app0), "/mnt/sandbox/%s/app0", sandbox_id);
printf("[INFO] Waiting for sandbox cleanup: %s\n", sandbox_app0);
int wait_count = 0;
struct stat sandbox_st;
while (stat(sandbox_app0, &sandbox_st) == 0 && wait_count < 30) {
sleep(1);
wait_count++;
if (wait_count % 5 == 0) {
printf("[DEBUG] Still waiting for sandbox cleanup... (%d seconds)\n", wait_count);
}
}
if (stat(sandbox_app0, &sandbox_st) == 0) {
printf("[WARNING] Sandbox still exists after 30 seconds, proceeding anyway\n");
} else {
printf("[INFO] Sandbox cleaned up after %d seconds\n", wait_count);
}
// Unmount the fakelibs
printf("[INFO] Unmounting %s\n", fake_mount_path);
unmount(fake_mount_path, 0);
// Remove the entire sandbox directory
char sandbox_dir[PATH_MAX];
snprintf(sandbox_dir, sizeof(sandbox_dir), "/mnt/sandbox/%s", sandbox_id);
printf("[INFO] Removing directory %s\n", sandbox_dir);
if (cleanup_directory(sandbox_dir) == 0) {
notify("Directory removed successfully.");
printf("[INFO] Directory removed successfully\n");
} else {
printf("[WARNING] Failed to remove directory: %s\n", strerror(errno));
}
free(fake_mount_path);
printf("[INFO] Cleanup finished.\n");
}
static void patch_game(pid_t child_pid, const char *title_id) {
// Find highest sandbox number
int sandbox_num = find_highest_sandbox_number(title_id);
if (sandbox_num == -1) {
printf("[WARNING] No sandbox found for %s\n", title_id);
return;
}
// Build sandbox_id
char sandbox_id[14];
snprintf(sandbox_id, sizeof(sandbox_id), "%s_%03d", title_id, sandbox_num);
// Check if fakelib exists
char fakelib_src_path[PATH_MAX];
snprintf(fakelib_src_path, sizeof(fakelib_src_path), "/mnt/sandbox/%s/app0/fakelib", sandbox_id);
struct stat st;
if (stat(fakelib_src_path, &st) != 0) {
return;
}
// Find random folder
char* random_folder = find_random_folder(title_id, sandbox_num);
if (!random_folder) {
printf("[WARNING] Failed to find random folder for %s\n", title_id);
return;
}
notify("Detected game %s. Patching...", title_id);
printf("[INFO] Detected game %s (pid %d) in sandbox %s. Patching...\n", title_id, child_pid, sandbox_id);
char src_path[PATH_MAX];
snprintf(src_path, sizeof(src_path), "/mnt/sandbox/%s/app0", sandbox_id);
char* fake_mount_path = mount_fakelibs(sandbox_id, src_path, child_pid, random_folder);
if (fake_mount_path) {
notify("Patch successful. Waiting for game to exit...");
printf("[INFO] Patch successful. Waiting for game to exit...\n");
wait_for_pid_exit(child_pid);
cleanup_game(child_pid, sandbox_id, fake_mount_path);
}
free(random_folder);
}
int main() {
syscall(SYS_thr_set_name, -1, PAYLOAD_NAME);
int pid;
while ((pid = find_pid(PAYLOAD_NAME)) > 0) {
if (kill(pid, SIGKILL)) {
return -1;
}
printf("[INFO] Killed old instance\n");
sleep(1);
}
pid_t syscore_pid = find_pid("SceSysCore.elf");
if (syscore_pid == -1) {
printf("[WARNING] Failed to find SceSysCore.elf pid\n");
return -1;
}
int kq = kqueue();
if (kq == -1) {
perror("kqueue");
return -1;
}
struct kevent kev;
EV_SET(&kev, syscore_pid, EVFILT_PROC, EV_ADD | EV_ENABLE | EV_CLEAR,
NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, NULL);
int ret = kevent(kq, &kev, 1, NULL, 0, NULL);
if (ret == -1) {
perror("kevent");
close(kq);
return -1;
}
notify("Welcome To BackPork 0.1 By BestPig 🐷");
printf("[INFO] Monitoring SceSysCore.elf (pid %d) for game launches...\n", syscore_pid);
pid_t child_pid = -1;
while (1) {
struct kevent event;
int nev = kevent(kq, NULL, 0, &event, 1, NULL);
if (nev < 0) {
perror("kevent");
continue;
}
if (nev == 0) continue;
if (event.fflags & NOTE_CHILD) {
child_pid = event.ident;
}
if (event.fflags & NOTE_EXEC && child_pid != -1 && event.ident == child_pid) {
app_info_t appinfo = {0};
if (sceKernelGetAppInfo(child_pid, &appinfo) != 0) {
child_pid = -1;
continue;
}
char title_id[10] = {0};
memcpy(title_id, appinfo.title_id, 9);
// Check if it's a PPSA/CUSA game
if (strncmp(title_id, "PPSA", 4) != 0 && strncmp(title_id, "CUSA", 4) != 0) {
child_pid = -1;
continue;
}
patch_game(child_pid, title_id);
child_pid = -1;
}
}
close(kq);
return 0;
}