Monday, May 30, 2011

Batch Tutorial

Beginners Guide to BATCH

So this certainly won't be as long as my C# or Python guides, but they will be pretty fun!

Batch is a very simple programming language, its also defines many hackers.

In this lesson series we'll cover making some simple applications, and demonstrating some ways that batch files could be used.

Lesson 1: Hello World
Let's create a hello world application. We're going to begin by opening Notepad. We'll then add in the following code:

@echo off
echo Hello World!
We are using the echo command/keyword. There are echo on/off commands. We're using the @echo off command.

We then tell it to echo out (print/write out) "Hello World!"

We then pause the application.

Now you need to save that into a batch file. To do so, click File > Save As. Type in:
Also, change the File Type drop-down menu to "All Files".

Then press save, and you should have your bat/batch file.

Open up where you saved the file to, and then run your file.

You should get a result the same as the black window in the picture below:

Now we'll give the echo on part a try.

Create another notepad/batch file or just modify the one you currently have.

Change the first line from @echo off to @echo on, so you have:
@echo on
echo "Hello World"
Now save that, and give it a try.

You'll notice some additional text lines. These lines begin with your file's location, and end with the command you input, then on the line beneath it, you'll see the result of the command.

For mine, since my account name is Jacob and I saved it to my desktop I got:

This is actually a good "debugging" tool, we can see where our file is, where the command is, what the command is, and what the result is.

So we can conclude with these results:
If we use "@echo on" we get to see the directory/location of our file and the command.
If we use "@echo off" we simply display the results of our command, we simply see the text we put in.

We use the echo command to echo out text. This prints/writes out a line of text to the screen. We use the pause command to pause the program/application. That way it waits/pauses for user input. In our hello world program, we're simply doing this to make it so the program waits long enough for us to read "Hello World!"

Let's switch @echo on back to @echo off and add in another set of code:

echo I am testing pause
Just add that in right after the first pause. This will help to demonstrate how pauses work. Simply run your program, you'll see that you get queried to press a key once, after the "Hello World!" has been displayed, and again after the "I am testing pause" has been displayed. After you pass the final pause we run out of code, so the application quits and closes.

Lesson 2: Clearing the screen
We do this by using the clear screen command, or rather the cls command.

Let's implement this into our hello world program, alternatively you can create another program.

Let's change our hello world program to have the cls command within:

@echo off
echo Hello World!
echo I am testing pause
You'll notice that your screen clears after pressing a key to go through the first pause. Then the "I am testing pause" text appears.

Lesson 3: The REM Command
REM stands for remark. A remark is similar to a comment, if you've ever programmed before. These remarks are not used by the program, they simply help to describe the program to the programmer.

They're very useful.

Let's add in, right beneath our @echo off line, a remark command:

REM This is a hello world program, where we test cls, pause, and echo commands.
So that one line of code will not get executed when we run the program. It is simply there to let us know what's going on in the code. Of course, in a simple hello world program we may not need this, but in larger programs, its crucial if we ever want to fully understand the code again.

Lesson 4: The start command
Now, first off we're going to do a start command. Create a picture in the same directory folder as your batch file. I'll call mine "example.jpg" but yours could be anything. It could be any type of file, and named anything.

We'll create a new batch file, inside of this blank batch file, add in the following:

@echo off
echo Hello Friend
echo Let me show you a picture
start example.jpg
In this example we display "Hello Friend", and then "Let me show you a picture". Once the user clicks enter to the last bit of text, the picture opens.

Lesson 5: The goto, set, and if commands
These commands really make up the fundamentals of any program.

Now, let's create a new batch file just for this.

Insert the following code:
@echo off
echo If you would like to see math, type 1 and then press enter
echo If you would like to see a picture, type 2 and then press enter
echo If you would simply like to continue with the program, type 3 and then press enter
set /p option=
if '%option%'=='1' goto :addition
if '%option%'=='2' start example.jpg
if '%option%'=='3' goto :continue

echo 1 + 1 = 2

echo I see you chose to continue.
echo Well, I guess I should say good bye, go ahead and press any key.
So, let's examine exactly what happens in that code. We start with stuff we know, echo out some text. We then use the set command, this is because we're setting some parameters to take in, we're calling these parameters "option", which will be the value of the user input.

We then use if statements to determine if something is true.

So we start by checking if the user entered 1, if so, we jump to the addition section. If the user entered 2, we use our start command to open up a picture, and if they typed three, we use some text to wrap up the program and then close by going to the continue section.

We define a section that we can go to by using the :name format.
When we say "goto :addition" it jumps to the addition section, which is the area beneath :addition. In there we simply echo out 1 + 1 = 2 and then pause.

In the :continue section we display some text and pause.

Wait a moment! Did you run it yet? If you jump to the addition section we actually run into the continue section. This is because the program is executed sequentially. We go right from the math section into the continue section. That is a very important concept with batch files.

You don't truly call methods, like you might in object oriented programming. The goto command simply jumps to that line of code, then runs down from there.

Lesson 6: Using other batch files
Create two new batch files. For example purposes, I'll call mine "call.bat" and "open.bat". You'll understand why I chose those names in a moment.

Save these two batch files in the same directory/folder. In the first batch file, the one I called "call.bat" type in:
@echo off
echo I am going to call another batch file's code when you press enter
Now, in the other batch file type in:

@echo off
echo This is from the other batch file
Go ahead and save the two files, then run the first one (call.bat). You'll see that when you press enter to the first line of text, you get the text from the other batch file. This is because we used the call command to call the code from one batch file, into the other.

We also use the exit command, this is the proper way to exit a batch file, rather than just letting the code run out. The exit command simply closes/exits the program.

Lesson 7: "Virus" programs
The reason everyone wants to create batch files: viruses. We can create fake viruses simply using the "echo" command to scare people with some official sounding text, or we could go a bit more advanced.

Let's start by launching notepad using a batch file.
Type in:
@echo off
echo I am opening notepad minimized
start /MIN notepad
echo I am opening notepad maximized
start /MAX notepad
We can use the start command to start/open other programs. We can use the /MIN and /MAX to open programs minimized or maximized.

Now, let's examine implementing colors.

Type in the following:
@echo off
REM testing colors
color 2
echo Testing
color 41
echo Still Testing
Colors are given using the color command, we use the background, then text color.
For example, when we say "color 41" we're really saying, "background color is 4 (red) and text is 1 (blue)". 

Here are some colors for you:
9=light blue
a=light green
b=light aqua
c=light red
d=light purple
e=light yellow
f=bright white

Alternatively, you can find these by going into command prompt (cmd) and typing in "color help" without the quotation marks.

Now we'll test out the title, type in, to any of the batch files we've created so far:

title This is the Title
The title is displayed in the top region of the program opposite of where the minimize, maximize, and close buttons are.

Another good "virus" thing you can do is shutdown computers.

We can use the following code to restart:
@echo off
START C:\Windows\RUNDLL.EXE user.exe,exitwindowsexec
And the following to shutdown:
@echo off
Shut down the computer

C:\Windows\RUNDLL32.EXE user,exitwindows
If we wanted to delete files, we could use the delete command, which looks like "del".
For example, if I created a text file on my desktop called "example.txt" I could delete it using the following code:
@echo off
del example.txt
Now, what if we wanted to delete a whole folder? Well, we can do this using the deltree command, the del command is like deleting a branch from a tree, the deltree command is like taking a chainsaw to the thing and deleting the whole darn tree.

So, if we wanted to delete a folder, we'd do this using the format:
deltree *FOLDER NAME*
Of course you would replace *FOLDER NAME* with the name of the folder you are intent on deleting. 

We can use the format command to format a whole drive. That means we could delete everything on a drive. For example, if we used the following code:

format C:
And we ran that, we would format our C-drive. This would delete everything on your C drive, and unless you have a backup on an external hard drive, you're going to lose all the data on your computer.

So remember:
del - deletes files
deltree - deletes folders
format - deletes everything

Now, part of the point of a virus isn't just to be destructive and delete stuff, sometimes the most effective virus creates things. Just like how cancer works, by reproducing the cancerous cells again and again, we need our virus to work that way.

We could do this using the copy command. Let's say we want to copy something called "first" to the Windows folder.

copy first C:\Windows
This copies the Windows folder. The copy command tells it to copy, the first specifies the name of the folder we want to copy, the C:\Windows is the destination of our copy. Do that enough times, and you've got yourself a virus.

Say though, we may want to repeat something, again and again. We do this by using a loop, a loop runs code again and again until a condition is met, and this could be useful in creating a virus.

Take a look at this code:
@echo off
echo "hi"
goto Loop
Go ahead and run it, as long as you have a decent computer, no harm will be done. It just displays the word "hi", well.... forever. That's how loops work, a simple goto command, nothing you're not familiar with already.

If we wanted to make it conditional, so it continues executing until a condition is met, we would put the goto command in an if statement.

So how do you put everything we've covered into a virus? Easy, just put pieces you like together.

Maybe you want some text leading up to your virus, maybe you want them colored, maybe you want them to delete or copy. Maybe you only want it to happen if a user enters something specific, or maybe you want it to happen immediately.

Go ahead, create your virus! But remember, use it at your own risk. It could damage your computer. I am not responsible for your actions or actions taken against you. I do not support illegal hacking.

1 comment:

  1. If you need your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you need to watch this video
    right away...

    (VIDEO) Text Your Ex Back?