-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
51 lines (35 loc) · 1.19 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h> /* for fstat */
#include <fcntl.h> /* for open */
#include <unistd.h> /* for read */
#include "memexecve.h"
#define printf_exit(format, ...) do { fprintf(stderr, format, ##__VA_ARGS__); exit(1);} while(0);
int main(int argc, char** argv, char** envp)
{
int fd;
struct stat st;
char* filename;
char* loaded_target;
if(argc < 2)
{
printf_exit("USAGE: ./memexecve /path/to/file.elf\n");
}
filename = argv[1];
if( (fd = open(filename, O_RDONLY)) < 0)
printf_exit("open \"%s\": %s\n", filename, strerror(errno) );
if( fstat(fd, &st) < 0)
printf_exit("fstat %d: %s\n", fd, strerror(errno));
loaded_target = malloc(st.st_size);
read(fd, loaded_target, st.st_size);
/* Employ dirty hacks to ensure our target
* does not dislike the first argv argument -
* make it a full path */
argv[1] = realpath(argv[1], NULL);
/*
* Execute from memory and make sure the first argv argument
* is not included */
exec_elf(loaded_target, st.st_size, &argv[1], envp);
}