cmder cexec.bat分析学习

0x00 前言

cexec.bat分析学习是在init.bat的基础上进行调用的。
其他相关分析文章
cmder Cmder.bat分析学习
DIY自己的cmder.exe
cmder init.bat分析学习

0x01 正文

1.1~10行

@echo off
setlocal

if "%~1" equ "" goto :wrongSyntax

if not defined CMDER_USER_FLAGS (
  :: in case nothing was passed to %CMDER_USER_FLAGS%
  set "CMDER_USER_FLAGS= "
)

这里的setlocal其实相当于开了一个函数。

if “%~1” equ “” goto :wrongSyntax

%~1相当于是在获取第一个参数,并且过滤掉"

这里判断如果没有参数传递则跳转到wrongSyntax

这里继续来看下wrongSyntax部分

:wrongSyntax
echo The syntax of the command is incorrect.
echo.
echo use /? for help
echo.
endlocal
exit /b

这里就是输出一些内容,然后就是关闭函数。

继续以上分析

if not defined CMDER_USER_FLAGS (
  :: in case nothing was passed to %CMDER_USER_FLAGS%
  set "CMDER_USER_FLAGS= "
)

判断变量是否存在,如果不存在则进行初始化

2. 10~12行

set "feNot=false"
goto :parseArgument

这里设置feNot变量的值为false

然后跳转到parseArgument

然后接着来看parseArgument

2.1 parseArgument

:parseArgument
set "currenArgu=%~1"
if /i "%currenArgu%" equ "/setPath" (
  :: set %flag_exists% shortcut
  endlocal
  set "ccall=call %~dp0cexec.cmd"
  set "cexec=%~dp0cexec.cmd"
) else if /i "%currenArgu%" == "/?" (
  goto :help
) else if /i "%currenArgu%" equ "/help" (
  goto :help
) else if /i "%currenArgu%" equ "/h" (
  goto :help
) else if /i "%currenArgu%" equ "NOT" (
  set "feNot=true"
  goto :doShift
) else (
  if "%~1" equ "" goto :wrongSyntax
  if "%~2" equ "" goto :wrongSyntax
  set "feFlagName=%~1"
  set "feCommand=%~2"
  if not "%~3" equ "" (
    set "feParam=%~3"
  )
  goto :detect
)

set “currenArgu=%~1”
将通过命令行传过来的第一个参数复制currenArgu

if /i “%currenArgu%” equ “/setPath” (
这里的if /i的意思是不区分大小
整体意思就是判断命令行输入的内容是不是/setPath
如果是的话则
endlocal
关闭临时变量空间并且设置
获取当前路径下的cexec.cmd
ccall=call %~dp0cexec.cmd
cexec=%~dp0cexec.cmd

接着向下分析
else if /i “%currenArgu%” == “/?”
如果输入的内容为/?
则跳转到goto :help
help这里就是打印了一些帮助内容
内容如下:
在这里插入图片描述
包括/help /h,这些输入的内容都会触发help界面

接着向下看
else if /i “%currenArgu%” equ “NOT”
当输入的内容为NOT时

set “feNot=true”
设置feNot为true

goto :doShift
跳转到doShift

:doShift
  shift

这里的shift需要进行解释,并且写一个小的脚本进行演示。

shift [/n] 代表将命令行输入的参数向左偏移
脚本链接

@echo off
echo   %1 %2 %3
shift /1
echo   %1 %2 %3

在这里插入图片描述
如果以上都不是的话,那么执行

  if "%~1" equ "" goto :wrongSyntax
  if "%~2" equ "" goto :wrongSyntax
  set "feFlagName=%~1"
  set "feCommand=%~2"
  if not "%~3" equ "" (
    set "feParam=%~3"
  )
  goto :detect

如果前两个参数都为空,则会跳转到wrongSyntax

如果不为空则
feFlagName=第一个参数
feCommand=第二个参数

if not “%~3” equ “”
如果第三个参数不是空的话,则
feParam=第三个参数

随后跳转到detect

2.2 detect

set "feFlagName=%feFlagName% "
:: echo.
:: echo %CMDER_USER_FLAGS%
:: echo %feNOT%
:: echo %feFlagName%
:: echo %feCommand%
:: echo %feParam%
:: echo.
echo %CMDER_USER_FLAGS% | %WINDIR%\System32\find /i "%feFlagName%">nul
if "%ERRORLEVEL%" == "0" (
  if "%feNOT%" == "false" (
    endlocal && call %feCommand% %feParam%
    exit /b 0
  )
) else (
  if "%feNOT%" == "true" (
    endlocal && call %feCommand% %feParam%
    exit /b 0
  )
)
endlocal
exit /b 1

set "feFlagName=%feFlagName% "

设置feFlagName=%feFlagName%

剩下的意思就是说如果ERRORLEVEL是0的话并且feNOT为false则call %feCommand% %feParam%,实际上感觉,这两个是一样的,等之后其他分析结束之后可能要进行一个串联才能知道这里是什么了。

发布了441 篇原创文章 · 获赞 124 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/qq_36869808/article/details/103837196