[Bash] Let's analyze the Fork bomb now

Introduction

** * Needless to say, please take your own responsibility as promised **

The famous Fork bomb. For Bash: :(){ :|: & };:

This principle didn't come to my mind, so I tried to analyze it ** after confirming the operation (!) **.

It is listed in order (or worse, sloppy), so I would appreciate it if you could just read it as a reading material (poem).

Main story

Modified to make it easier to read and dropped

Use bomb as the function name instead of: . bomb() { bomb | bomb & }; bomb

The drop made it ** splendidly explode and required a reboot **. First of all success? is!

Try running in the foreground

It's a hassle to restart each time, so let's put it in the foreground.

bomb() { bomb | bomb ; }; bomb

This is also exploding, but it seems that it can be recovered by Ctrl + C or closing the terminal. This foreground execution will continue for analysis.

Try to sleep

Similarly, it seems that it will take time to analyze, so I will wait for about 10 seconds.

bomb() { sleep 10 ; bomb | bomb ; }; bomb

If you check in another terminal, you can see that ** slowly heading for ruin **. (The following -bash and sleep 10 will increase over time)

user@user-VirtualBox:~$ ps -f -u user | grep pts/2
user     25428 25363  0 15:54 ?        00:00:00 sshd: user@pts/2
user     25429 25428  0 15:54 pts/2    00:00:00 -bash
user     26605 25429  0 15:56 pts/2    00:00:00 -bash
user     26606 25429  0 15:56 pts/2    00:00:00 -bash
user     26607 26606  0 15:56 pts/2    00:00:00 sleep 10
user     26608 26605  0 15:56 pts/2    00:00:00 sleep 10
user     26612 25777  0 15:56 pts/3    00:00:00 grep --color=auto pts/2

Prepare the log

Try to log to a file called foo. You can still read it with one liner.

bomb() { sleep 10 ; echo `date +%H:%M:%S.%3N`: Throw 2 bombs... >> foo ; bomb | bomb ; }; bomb

Log confirmation result in another terminal:

user@user-VirtualBox:~$ tail -f foo
16:15:14.374: Throw 2 bombs...
16:15:24.378: Throw 2 bombs...
16:15:24.379: Throw 2 bombs...
16:15:34.384: Throw 2 bombs...
16:15:34.385: Throw 2 bombs...
16:15:34.388: Throw 2 bombs...
16:15:34.389: Throw 2 bombs...

What a wonderful log (self-praise)!

  1. Drop two at 16:15:14
  2. Drop 2 + 2 = 4 at 16:15:24
  3. Drop 2 + 2 + 2 + 2 = 8 at 16:15:34

Therefore, it became clear that ** the number of bombs (processes) is increasing in the game ** times! It would be scary if I hadn't slept for 10 seconds! Lol

Try to make one

Everyone will wonder (isn't it?), What about one? is.

bomb() { sleep 10 ; echo `date +%H:%M:%S.%3N`: Throw a bomb... >> foo ; bomb ; }; bomb

The log is a natural result:

user@user-VirtualBox:~$ tail -f foo
16:28:09.661: Throw a bomb...
16:28:19.664: Throw a bomb...
16:28:29.668: Throw a bomb...

The confirmation result of the process is as follows (confirmed at appropriate intervals):

user@user-VirtualBox:~$ ps -f -u user | grep pts/2
user     25428 25363  0 15:54 ?        00:00:00 sshd: user@pts/2
user     25429 25428  0 15:54 pts/2    00:00:00 -bash
user     26743 25429  0 16:28 pts/2    00:00:00 sleep 10
user     26746 25777  0 16:28 pts/3    00:00:00 grep --color=auto pts/2
user@user-VirtualBox:~$ ps -f -u user | grep pts/2
user     25428 25363  0 15:54 ?        00:00:00 sshd: user@pts/2
user     25429 25428  0 15:54 pts/2    00:00:00 -bash
user     26748 25429  0 16:28 pts/2    00:00:00 sleep 10
user     26750 25777  0 16:28 pts/3    00:00:00 grep --color=auto pts/2
user@user-VirtualBox:~$ ps -f -u user | grep pts/2
user     25428 25363  0 15:54 ?        00:00:00 sshd: user@pts/2
user     25429 25428  0 15:54 pts/2    00:00:00 -bash
user     26754 25429  0 16:28 pts/2    00:00:00 sleep 10
user     26756 25777  0 16:28 pts/3    00:00:00 grep --color=auto pts/2
user@user-VirtualBox:~$

You can see that the original -bash process is alive all the time and the sleep 10 process is started separately (26743 2674826754).

In other words, the new process is running, but the previous process is finished each time, so it does not multiply.

To put it plainly, it's an infinite loop that simply repeats bomb **.

Try changing from a pipe to a semicolon

But what if the space between the two bombs is ; instead of |?

bomb() { sleep 10 ; echo `date +%H:%M:%S.%3N`: Throw 1st bomb... >> foo ; bomb ; echo `date +%H:%M:%S.%3N`: Throw 2nd bomb... >> foo ; bomb ; }; bomb

user@user-VirtualBox:~$ tail -f foo
16:42:05.384: Throw 1st bomb...
16:42:15.387: Throw 1st bomb...
16:42:25.390: Throw 1st bomb...

You haven't reached the 2nd bomb. This is because the 1st bomb is repeated all the time, and the substance is the same as the case of one bomb.

in conclusion

Impressions (Poem)

Seeing is believing. It was a straightforward method, but it became smarter again. Lol

Confirmation environment

--Ubuntu 18.04 LTS (with VM)

Recommended Posts

[Bash] Let's analyze the Fork bomb now
Let's analyze the questionnaire survey data [4th: Sentiment analysis]
Let's clear the ambiguity of the hyphen (-) of the su command now! !!
Let's analyze the emotions of Tweet using Chainer (2nd)
Let's analyze the sentiment of Tweet using Chainer (1st)
Analyze the illustration (Memo)