Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I implement a kill switch into a .bat fork bomb?

Tags:

batch-file

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.

like image 572
Nzall Avatar asked Aug 24 '15 23:08

Nzall


1 Answers

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
like image 173
hammockdude Avatar answered Sep 30 '22 04:09

hammockdude