Introduction to Batch File Viruses
In this lesson, we will explore batch file viruses and learn about various techniques used in creating small and simple batch file scripts. Even if you have no background in programming, you will find it very easy to follow along and learn.
What Are Batch Files?
A batch file is a text file containing a series of commands to be executed by the command interpreter. Batch files have the extension .bat
(or .cmd
). They can be easily created using any text editor such as Notepad.
Basic Commands and Examples
Let’s start with some basic commands.
echo
Command
The echo
command is used to print out a message. Type the following in a text editor and save it as something.bat
.
echo Hello World
When you run this batch file, a command window will pop up and then close immediately. To fix this and see the output, we can add the pause
command.
echo Hello World pause
The pause
command pauses the execution of the batch file until a user presses a key. You should see the following output:
C:\>echo Hello World Hello World Press any key to continue . . .
@echo off
Command
To clean up the output and hide the command itself, we use @echo off
.
@echo off echo Hello World pause
This will output:
Hello World Press any key to continue . . .
Adding Dynamic Behavior
Let’s add a variable to make the script more dynamic.
@echo off set a=Bernie echo Hello %a% pause
The set
command is used to define variables. The variable a
holds the value “Bernie”. The output will be:
Hello Bernie Press any key to continue . . .
Modifiers in Commands
Some commands come with modifiers that alter their behavior slightly.
Performing Arithmetic Operations
@echo off set /a a=5 set /a b=10 set /a c=%a% + %b% echo %c% pause
The /a
modifier allows arithmetic operations. This script sets variables a
and b
to 5 and 10 respectively, adds them, and prints the result.
Getting User Input
@echo off set /p a=Enter your name: echo Hello %a% pause
The /p
modifier prompts the user to input a value.
Enter your name: Eve Hello Eve Press any key to continue . . .
Examples of Useful Batch Files
Pinger Batch Script 1.0
This script pings an IP address or URL continuously.
@echo off title Pinger Batch Script 1.0 set /p target=Enter IP address or URL: ping %target% -t pause
The title
command sets the window title. To stop the ping command, press CTRL+C
.
Shutdown Timer
This batch file schedules a shutdown after a specified number of minutes.
@echo off title Shutdown Timer set /p mins=Enter number of minutes to wait until shutdown: set /a secs=%mins%*60 shutdown -s -t %secs% pause
This script asks the user for a number of minutes, converts it to seconds, and schedules a shutdown.
Creating a Simple Batch File Virus
Here’s a simple example of a harmless batch file “virus” that creates an infinite loop of opening command prompts. Note: This is for educational purposes only. Do not use maliciously.
@echo off :start start goto start
When you run this batch file, it will keep opening new command prompt windows until your system resources are exhausted. To stop it, use CTRL+C
or close all command prompts manually.
Conclusion
Batch files are powerful tools that can automate repetitive tasks, create useful utilities, or even demonstrate basic programming concepts. By understanding these fundamental commands and techniques, you can start creating your own batch scripts. Remember to always use batch files responsibly.