Understanding Bash fork() bomb ~ :(){ :|:& };:
Q. Can you explain following bash code or bash fork() bomb?
:(){ :|:& };:
A. This is a bash function. It gets called recursively (recursive function). This is most horrible code for any Unix / Linux box. It is often used by sys admin to test user processes limitations (Linux process limits can be configured via /etc/security/limits.conf and PAM).
Once a successful fork bomb has been activated in a system it may not be possible to resume normal operation without rebooting, as the only solution to a fork bomb is to destroy all instances of it.
WARNING! These examples may crash your computer if executed.Understanding :(){ :|:& };: fork() bomb code
:() - It is a function name. It accepts no arguments at all. Generally, bash function is defined as follows:
foo(){
arg1=$1
echo ''
#do_something on $arg argument
}
fork() bomb is defined as follows:
:(){ :|:& };:
:|: - Next it call itself using programming technique called recursion and pipes the output to another call of the function ':'. The worst part is function get called two times to bomb your system.
& - Puts the function call in the background so child cannot die at all and start eating system resources.
; - Terminate the function definition
: - Call (run) the function aka set the fork() bomb.
Here is more human readable code:
bomb() {
bomb | bomb &
}; bomb
Properly configured Linux / UNIX box should not go down when fork() bomb sets off.
Related: How to: Prevent a fork bomb by limiting user process under Linux.
Update: Check out comment # 5 for more fork bomb examples under Perl, Windows XP and C.
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Other Helpful FAQs:
- How to use bash shell
- FreeBSD Install BASH Shell
- UNIX: Remove a file with a name starting with - character
- BASH Shell: How To Redirect stderr To stdout ( redirect stderr to a File )
- Why and How To Customize a UNIX / Linux User Account?
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: :(){ :|:& };:, bash fork bomb, fork bomb



November 26th, 2007 at 9:53 am
i tested this on a fresh install of CentOS and it totaly crashed
How about a nice “howto” for protecting against this attacks?
November 27th, 2007 at 12:01 am
I second the vote for a nice howto.
Cheers
November 27th, 2007 at 5:02 pm
Thirded!
How to prevent a “fork bomb”.
November 28th, 2007 at 1:19 am
Yes How to must be started, for the benefits of the innocent users, as the viruses are spread by entities whose brains are configured differently.
November 28th, 2007 at 9:09 am
Perl exmaple:
Python example:
import os while(1): os.fork()Windows XP / Vista bat file example:
UNIX style for Windows:
C program example:
#include int main() { while(1) fork(); }Plz note that the fork bomb is a form of denial of service, so don’t run on production or unauthorized system.
November 28th, 2007 at 11:42 am
An other one is that when you have set a quota for your mailboxes and crontab is generating mail over and over eventually the quota will exceed, after a while the mailq will fill up with 1000’s of mail and the system will crash.
November 28th, 2007 at 11:52 am
Hi,
i run it on my testing server and it start consuming my server process.
so one thing is clear from this form bomb that it starting issuing new processes rather than threads.
how we prevent our sites from such fork attacks
Thanks in advance
November 28th, 2007 at 12:56 pm
kunal,
Read http://www.cyberciti.biz/tips/linux-limiting-user-process.html
November 28th, 2007 at 2:27 pm
I tried this on a virtual instance of SLAX, and it totally killed it.
December 5th, 2007 at 4:21 pm
perl inline…
perl -e “fork while fork” &
January 30th, 2008 at 4:15 am
Sleek code
I tried on FC4. Took only 1 minute as root.
In WinXP, I waited till 7 minutes. Displayed some errors and all, but was responding. Tried MS Word, Task Manager, nothing could be executed. The machine was useless untill reset.
March 31st, 2008 at 6:06 pm
This works very quickly on all windows:
:bomb
%0 | %0
goto bomb