batch-fileself-reference

Very basic batch self-replicating code: how dangerous?


After reading a bit too much about von Neumann self-replicating machines, I decided to tinker a bit with batch, trying to make a simple file that would make a copy of itself upon running. I ended up with this:

set self=%~n0
REM get own filename
TYPE %self%.bat > %self%E.bat

The file is saved as WEE.bat and each run of the latest iteration makes a copy with an additional E.

It is very simple to add a simple line to automaticaly run the latest iteration, and at this point I actually have no idea what happens: Does something keep my computer to try to overflow its own drive? with the "improvement", how different would this piece of code be from an actual malware (besides the obvious spreading through network thing)?


Solution

  • Does something keep my computer to try to overflow its own drive? with the "improvement", how different would this piece of code be from an actual malware (besides the obvious spreading through network thing)?

    No. If you instruct your computer to do something useless or detrimental it will do so. The only limiting factor here is that with echo enabled, every line of execution must be spewed to the console window, so it's not going to be terribly fast. You'll have a chance to kill it with Ctrl-C, but its going to create thousands of files until the file name is too long to invoke, at which point it will fail. Most hard drives have orders of magnitude more space on them than your script could possibly fill-up.

    If you want to experiment, see timeout /?. Add a timeout of five or ten seconds for each run of the script and you can see the number of files increasing in whatever directory you run the batch file in.

    EDIT:

    It appears the limiting factor here is the file path/name length at 270 characters.

    >type D:\TMP\Joseph\testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.cmd  1>D:\TMP\Joseph\testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.cmd
    The filename, directory name, or volume label syntax is incorrect.
    
    >D:\TMP\Joseph\testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.cmd
    The parameter is incorrect.