5

I want to show a friend how big the impact of a fork bomb can be on performance, but without needing to restart the computer afterwards.

Assuming the following fork bomb:

%0|%0

Is there a way to add a kill switch to this which, with one button press, will stop all running copies of this file, stop any new from being created and hopefully save the machine? I'm not really familiar with the command prompt syntax, so I'm not sure.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Nzall
  • 3,439
  • 5
  • 29
  • 59
  • surprisingly, I haven't found an answer to this yet, although I might have been looking in the wrong places. – Nzall Aug 24 '15 at 23:20
  • Well, it's a batch script, so I imagine you can just press CTRL+C as fast as you can (naturally, I'm not going to test this on my own machine, but CTRL+C stops batch scripts in other cases). – SomethingDark Aug 24 '15 at 23:22
  • @SomethingDark I think that, depending on how deep the bomb has been forking, how fast your CPU is and how fast you are at pressing the buttons, that might not work. If it forks every Nth of a second, you need to be spamming Ctrl+C at least N+1 times a second. you'll probably need an autoclicker for that. – Nzall Aug 24 '15 at 23:23
  • Well it was just speculation. You can always write a second script that runs `taskkill /F /IM cmd.exe /T`, which will kill all existing instances of cmd.exe. – SomethingDark Aug 24 '15 at 23:26
  • I just ran it, and it attacked my Win 8 computer a bit, and I couldn't CTRL-C, or close window by clicking the cross. I could still do things and had PowerShell ISE open, and used `taskkill /f /im cmd.exe` and "right click command prompt -> close all windows", and closing windows. At some point it stopped, I don't know which one stopped it. I let it run for maybe 5 seconds before trying, and it ran another maybe 15 seconds before stopping. – TessellatingHeckler Aug 25 '15 at 00:42
  • You could put a check for a file, e.g. `if stop.txt exists, exit, else (%0|%0)` then if you create the file, that should stop new command prompts appearing. That relies on you still being able to do things, though. – TessellatingHeckler Aug 25 '15 at 00:44

3 Answers3

3

Two ideas:

a) limit the depth your "bomb" is going to fork:

@echo off
set args=%*
if "%args%" EQU "" (set args=0) else set /a args=%args%+1
if %args% LSS 8 start /min thisfile.bat

(this will produce 2^9 -1 command windows, but only your main window is open.)

b) kill the cmd.exe process in the main batch file

@echo off
SET args=%*
:repeat
start /min thisfile.bat.bat some_arg
if "%args%" NEQ "" goto repeat
pause
taskkill /im cmd.exe

pressing any key in the initial batch file will instamntly kill all cmd windows currently open.

These solutions were tested with some restrictions, so if they work in a "hot" forkbomb, please let me know.

hope this helps.

EDIT:

c)implement a time switch in the original bat: (still stops even if you can't get past pause)

@echo off
set args=%*
:repeat
start /min thisfile.bat some_arg
if "%args%" NEQ "" goto repeat
timeout /T 10
taskkill /im cmd.exe

or, using the smaller "bomb":

@echo off
set args=%*
if "%args%" NEQ "" (%0 some_arg|%0 some_arg) else start /min thisfile.bat some_arg
timeout /T 10
taskkill /im cmd.exe
1

If you're out to just show your friend fork bombs why not go for a silent but deadly approach? The fork bomb is in vbs, the cleanup is in batch. Do note, the vbs fork bomb does nothing turning your pc off/on wont fix, it just floods your session proccess's.

The fork bomb:

Do until true = false
  CreateObject("Wscript.Shell").Run Wscript.ScriptName
Loop

Source

Cleanup:

title=dontkillme
FOR /F "tokens=2 delims= " %%A IN ('TASKLIST /FI ^"WINDOWTITLE eq
dontkillme^" /NH') DO SET tid=%%A
echo %tid%
taskkill /F /IM cmd.exe /FI ^"PID ne %tid%^"

Source

Bloodied
  • 1,004
  • 7
  • 20
1

If it runs in an accessible directory, you could try

IF EXIST kill.txt (exit /b) ELSE (%0|%0)

and make a file called kill.txt in the directory to stop the bomb.

sudoBash
  • 31
  • 1
  • 5