-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexecelf.c
46 lines (40 loc) · 976 Bytes
/
execelf.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
#define _GNU_SOURCE
#include "execelf.h"
/*
execelf
Date: 10/27/21
Author: 0x1CA3
*/
int file_check(char *filename) {
FILE * executable = fopen(filename, "r");
if (executable == NULL) {
fprintf(stderr, "Error: File '%s' was not found!\n", filename);
return 1;
}
fclose(executable);
}
static void file_execution(size_t size, char *elf, char **f_file, char **envp) {
int des = memfd_create("targ_file", FD_CLOEXEC);
write(des, elf, size);
sprintf(e_elf, "/proc/self/fd/%u", des);
execve(e_elf, f_file, envp);
}
int main(int argc, char **argv, char **envp) {
int des;
char *elf;
if (argc != 2) {
fprintf(stderr, "Usage: %s <elf_file>\n", argv[0]);
exit(EXIT_FAILURE);
} else {
if (file_check(argv[1]) == 1) {
exit(EXIT_FAILURE);
} else {
des = open(argv[1], O_RDONLY);
fstat(des, &l_stat);
elf = malloc(l_stat.st_size);
read(des, elf, l_stat.st_size);
file_execution(l_stat.st_size, elf, &argv[1], envp);
}
}
EXIT_SUCCESS;
}