Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Bash command :(){ :|:& };: will spawn processes to kernel death. Can you explain the syntax?

Tags:

linux

bash

I looked at this page and can't understand how this works.

This command "exponentially spawns subprocesses until your box locks up".

But why? What I understand less are the colons.

user@host$ :(){ :|:& };:

like image 706
silviot Avatar asked Feb 05 '09 13:02

silviot


People also ask

What is the bash command?

Description. bash is a sh-compatible command language interpreter that executes commands read from the standard input or from a file. bash also incorporates useful features from the Korn and C shells (ksh and csh).

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script.

What is $1 and $2 in bash?

$1 is the first argument (filename1) $2 is the second argument (dir1)

Is bash command line?

Bash is a command-line and scripting language for most Unix/Linux-based operating systems. The generated output is in the form of characters.


1 Answers

:(){ :|:& };: 

..defines a function named :, which spawns itself (twice, one pipes into the other), and backgrounds itself.

With line breaks:

:() {     :|:& }; : 

Renaming the : function to forkbomb:

forkbomb() {     forkbomb | forkbomb & }; forkbomb 

You can prevent such attacks by using ulimit to limit the number of processes-per-user:

$ ulimit -u 50 $ :(){ :|:& };: -bash: fork: Resource temporarily unavailable $ 

More permanently, you can use /etc/security/limits.conf (on Debian and others, at least), for example:

* hard nproc 50 

Of course that means you can only run 50 processes, you may want to increase this depending on what the machine is doing!

like image 78
dbr Avatar answered Sep 20 '22 18:09

dbr