Xor bat文件中的字符串?

时间:2014-08-30 12:20:54

标签: batch-file

简而言之,我想给我的朋友一个bat文件,它执行以下操作(未经测试)

echo Your mother is so fat
pause
echo the recursive function computing her mass causes a stack overflow

我可能会复制/粘贴他的蝙蝠文件,所以我不希望破坏线。我该如何隐藏文字?我想我可以将字符串存储在一个变量中,然后在回显它之前我应该​​将每个字母与32进行异或。但是我不知道如何取一个字符串,每个字母都要异或,而不是回显它来显示这个笑话。我怎么能隐藏文字?我也可以BASE64对它进行编码/解码,但IDK如果我只使用一个bat文件怎么做呢

4 个答案:

答案 0 :(得分:10)

1)这是使用mshta作为命令行工具隐藏文本的一种方法(在这种情况下使用ascii代码):

@echo off
mshta vbscript:execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(Chr(89) & Chr(111)& Chr(117) & Chr(114) & Chr(32) & Chr(109) & Chr(97) & Chr(109) & Chr(97) & Chr(32) ):Close")|more

2)您可以使用CERTUTIL对base64 / hex文件进行编码/解码,但它需要一个可以静默删除的临时文件(more info):

echo 796f7572206d616d6120697320736f20666174>"%temp%\fat.hex"
certutil -decodehex "%temp%\fat.hex" "%temp%\fat.txt" >nul 2>&1
type "%temp%\fat.txt" 
del /q /f "%temp%\fat.txt" 

3)Dbenham's hex print function

@echo off
setlocal

::Define a Linefeed variable
set LF=^


::above 2 blank lines are critical - do not remove.


::Create a string variable with encoded TABs
call :hexprint "0x790x6f0x750x720x200x6d0x610x6d0x610x200x690x730x200x730x6f0x200x660x610x74" var
echo  %var%

exit /b

:hexPrint  string  [rtnVar]
  for /f eol^=^%LF%%LF%^ delims^= %%A in (
    'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(%~1"'
  ) do if "%~2" neq "" (set %~2=%%A) else echo(%%A
exit /b

4)carlos' genCar function使用MAKECAB

@echo off
setlocal


break>fat.txt 
 for %%# in (121 111 117 114 32 109 97 109 97 32 105 115 32 115 111 32 102 97 116) do (
    call :genChar %%#
    type %%#.chr>>fat.txt
    del /q /f %%#.chr >nul 2>&1
 )

type fat.txt 
del /q /f fat.txt 

goto :eof

  :genChar
  setlocal
  set "USAGE=echo:Usage: Supply an integer 0-255& goto :EOF"
  if "%~1" equ "" %USAGE%
  set /a "val=%~1" 2>nul
  if "%~1" neq "%val%" %USAGE%
  if %~1 lss 0    %USAGE%
  if %~1 gtr 255  %USAGE%

  set tempfile=%~1.tmp
  set "options=/d compress=off /d reserveperdatablocksize=26"
  if %~1 neq 26  (type nul >"%tempfile%"
  makecab %options% /d reserveperfoldersize=%~1 "%tempfile%" %~1.chr >nul
  type %~1.chr | (
  (for /l %%N in (1 1 38) do pause)>nul&findstr "^">"%tempfile%")
  >nul copy /y "%tempfile%" /a %~1.chr /b
  del "%tempfile%"
  ) else (copy /y nul + nul /a 26.chr /a >nul)
  endlocal

对于更加神秘的脚本,您可以组合下摆。只有MSHTA和MAKECAB解决方案才能在每台Windows机器上运行。我认为FORFILES和CERTUTIL是默认的Vista及以上版本。可以创建更多示例...

答案 1 :(得分:7)

这是我心中的一个主题,因为我在my implementation of the classic Colossal Cave Adventure game as a Windows batch file.

中做了类似的事情

在游戏脚本中,我有选择地加密显示文本,变量名称和注释。解码加密文本的代码直接嵌入到同一个脚本中!我正常编写游戏的源代码,并使用大括号来表示要加密的部分。游戏中的一个功能是能够生成自己的加密形式!

我使用了一个简单的对称旋转密码,所以它实际上比加密更混淆。但这就是游戏和你的情况所需要的一切。

我已经提取了例程的简化版本并在下面提供。

第一个脚本是一个独立脚本,它有选择地加密源文件中的文本并将结果写入stdout。只需将输出重定向到新文件即可获得该文件的加密版本。

<强> selectiveROT13.bat

@echo off
:selectiveROT13  InFile
::
::  Selectively applies the simple "rotate alphabet 13 places" cipher
::  to the contents of file InFile. Only text between curly braces
::  is affected. The affected content can span multiple lines.
::
::  Writes the results to stdout.
::  Percent completion is continuously written to stderr.
::
setlocal enableDelayedExpansion
set "upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "lower=abcdefghijklmnopqrstuvwxyz"
for /l %%A in (0 1 25) do (
  set /a "B=(%%A+13)%%26"
  for /f %%B in ("!B!") do (
    set "upper!upper:~%%A,1!=!upper:~%%B,1!"
    set "lower!lower:~%%A,1!=!lower:~%%B,1!"
  )
)
setlocal disableDelayedExpansion
>&2 cls
set "active="
for /f %%N in ('type %1^|find /c /v ""') do set /a "lnCnt=%%N, pct=-1"
for /f "skip=2 tokens=1,* delims=[]" %%a in ('find /v /n "" %1') do (
  set "ln=%%b"
  setlocal enableDelayedExpansion
  set "str=A!ln!"
  set "len=0"
  for /L %%A in (12,-1,0) do (
    set /a "len|=1<<%%A"
    for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
  )
  set /a len-=1
  set rtn=
  for /l %%n in (0,1,!len!) do (
    set "c=!ln:~%%n,1!"
    if "!c!" equ "{" set "active=1"
    if "!c!" equ "}" set "active="
    if defined active if defined upper!c! for /f %%c in ("!c!") do (
      if "!upper:%%c=%%c!" equ "!upper!" (
        set "c=!upper%%c!"
      ) else (
        set "c=!lower%%c!"
      )
    )
    set "rtn=!rtn!!c!"
  )
  echo(!rtn!
  for %%A in ("!active!") do (
    endlocal
    set "active=%%~A"
  )
)
exit /b 0

以下是您的笑话程序,其中包含用于解码加密文本的简化版代码。我的原始代码使用字符串变量,但此版本适用于字符串文字。源脚本是正常编写的,没有加密。大括号表示要加密的代码。除了你的笑话,我还提供了文档和示例来演示一些功能。

<强> joke_src.bat

@echo off
setlocal enableDelayedExpansion
call :init

:: Disable delayed expansion to protect ! within string literals
setlocal disableDelayedExpansion

:: Curly braces are used to denote text that should be encrypted.
:: Encryption can span multiple lines
:: {
:::Line1
:::Line2
:::Line3
:: }

:: I defined a simple SHOW macro that expands to CALL :SHOW
:: Use the %show% macro to display encrypted text.
:: The braces can be hidden by using the undefined %{% & %}% variables
%show% %{%"Quote literals ("") must be doubled ("""") in the source"%}%

:: Here I use a FOR loop to show all encrypted lines within this script
:: that begin with :::
echo(
for /f "delims=: tokens=*" %%A in ('findstr /b ":::" "%~f0"') do %show% "%%A"

echo(
echo And now it is time for a little joke.
echo(
echo Your mother is so fat...
pause
%show% %{%"the recursive function computing her mass causes a stack overflow!"%}%
exit /b


:show  Str
::{
::  Applies the simple "rotate alphabet 13 places" cipher to string Str
::  and writes the result to stdout. Consecutive quotes ("") are converted
::  into a single quote (").
::}
  setlocal disableDelayedExpansion
  set "str=%~1"
  setlocal enableDelayedExpansion
  set "str=!str:""="!^"
  if defined {obfuscated} (
    set "len=0"
    set "str2=.!str!"
    for /L %%A in (12,-1,0) do (
      set /a "len|=1<<%%A"
      for %%B in (!len!) do if "!str2:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
    set /a len-=1
    set rtn=
    for /l %%n in (0,1,!len!) do (
      set "c=!str:~%%n,1!"
      if defined {upper}!c! for /f %%c in ("!c!") do (
        if "!{upper}:%%c=%%c!" equ "!{upper}!" (
          set "c=!{upper}%%c!"
        ) else (
          set "c=!{lower}%%c!"
        )
      )
      set "rtn=!rtn!!c!"
    )
  ) else set "rtn=!str!"
  echo(!rtn!
exit /b 0


:init
  set "}="
  set "{="}
  set "{upper}=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  set "{lower}=abcdefghijklmnopqrstuvwxyz"
  for /l %%A in (0 1 25) do (
    set /a "B=(%%A+13)%%26"
    for /f %%B in ("!B!") do (
      set "{upper}!{upper}:~%%A,1!=!{upper}:~%%B,1!"
      set "{lower}!{lower}:~%%A,1!=!{lower}:~%%B,1!"
    )
  )
  set "{obfuscated}="
  set "{obfuscationTest}={A}"
  if "!{obfuscationTest}:A=!" equ "!{obfuscationTest}!" set {obfuscated}=1
  set "show=call :show"
exit /b

以下命令将生成脚本的加密版本:

selectiveROT13 joke_src.bat >joke.bat

以下是加密表格。这是你发给你朋友的东西。 (没有额外的文档和课程示例)

<强> joke.bat

@echo off
setlocal enableDelayedExpansion
call :init

:: Disable delayed expansion to protect ! within string literals
setlocal disableDelayedExpansion

:: Curly braces are used to denote text that should be encrypted.
:: Encryption can span multiple lines
:: {
:::Yvar1
:::Yvar2
:::Yvar3
:: }

:: I defined a simple SHOW macro that expands to CALL :SHOW
:: Use the %show% macro to display encrypted text.
:: The braces can be hidden by using the undefined %{% & %}% variables
%show% %{%"Dhbgr yvgrenyf ("") zhfg or qbhoyrq ("""") va gur fbhepr"%}%

:: Here I use a FOR loop to show all encrypted lines within this script
:: that begin with :::
echo(
for /f "delims=: tokens=*" %%A in ('findstr /b ":::" "%~f0"') do %show% "%%A"

echo(
echo And now it is time for a little joke.
echo(
echo Your mother is so fat...
pause
%show% %{%"gur erphefvir shapgvba pbzchgvat ure znff pnhfrf n fgnpx biresybj!"%}%
exit /b


:show  Str
::{
::  Nccyvrf gur fvzcyr "ebgngr nycunorg 13 cynprf" pvcure gb fgevat Fge
::  naq jevgrf gur erfhyg gb fgqbhg. Pbafrphgvir dhbgrf ("") ner pbairegrq
::  vagb n fvatyr dhbgr (").
::}
  setlocal disableDelayedExpansion
  set "str=%~1"
  setlocal enableDelayedExpansion
  set "str=!str:""="!^"
  if defined {boshfpngrq} (
    set "len=0"
    set "str2=.!str!"
    for /L %%A in (12,-1,0) do (
      set /a "len|=1<<%%A"
      for %%B in (!len!) do if "!str2:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
    set /a len-=1
    set rtn=
    for /l %%n in (0,1,!len!) do (
      set "c=!str:~%%n,1!"
      if defined {hccre}!c! for /f %%c in ("!c!") do (
        if "!{hccre}:%%c=%%c!" equ "!{hccre}!" (
          set "c=!{hccre}%%c!"
        ) else (
          set "c=!{ybjre}%%c!"
        )
      )
      set "rtn=!rtn!!c!"
    )
  ) else set "rtn=!str!"
  echo(!rtn!
exit /b 0


:init
  set "}="
  set "{="}
  set "{hccre}=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  set "{ybjre}=abcdefghijklmnopqrstuvwxyz"
  for /l %%A in (0 1 25) do (
    set /a "B=(%%A+13)%%26"
    for /f %%B in ("!B!") do (
      set "{hccre}!{hccre}:~%%A,1!=!{hccre}:~%%B,1!"
      set "{ybjre}!{ybjre}:~%%A,1!=!{ybjre}:~%%B,1!"
    )
  )
  set "{boshfpngrq}="
  set "{boshfpngvbaGrfg}={N}"
  if "!{boshfpngvbaGrfg}:A=!" equ "!{boshfpngvbaGrfg}!" set {boshfpngrq}=1
  set "show=call :show"
exit /b

这个系统的美妙之处在于joke.bat和joke_src.bat都会产生完全相同的输出:

Quote literals (") must be doubled ("") in the source

Line1
Line2
Line3

And now it is time for a little joke.

Your mother is so fat...
Press any key to continue . . .
the recursive function computing her mass causes a stack overflow!

另一个不错的功能是selectiveROT13.bat可以应用于joke.bat以重新生成原始的未加密源。

答案 2 :(得分:5)

此代码创建base64编码文件:

@echo off
set "var=the recursive function computing her mass causes a stack overflow"
>file.tmp echo %var%
certutil -f -encode file.tmp file.tmp2 >nul 
echo file.tmp2|find /v "-----" >file.txt
del file.tmp?
pause

你可以像这样使用文件(在编码文件的每一行的开头添加echo):

@echo off
cls
echo Your mother is so fat
pause
(
echo dGhlIHJlY3Vyc2l2ZSBmdW5jdGlvbiBjb21wdXRpbmcgaGVyIG1hc3MgY2F1c2Vz
echo IGEgc3RhY2sgb3ZlcmZsb3cNCg==
)>file.tmp
certutil -f -decode file.tmp file.txt >nul
timeout /t 2 /nobreak >nul
type file.txt
timeout /t 5 /nobreak >nul

答案 3 :(得分:5)

我认为您可以使用以下字符串替换脚本在纯BAT中替换其他字符而不使用任何临时文件。

  

%STR:old_char = new_char%

例如,我已经定义了一些编码和解码功能。代码附在此处,它将打印您想要的内容。

@echo off

set str1=Y urke ohtrkmsks kfao
set str2=ohtkrtcursmvtkfuncom nkc epuomngkhtrkeasskcaustskaksoacik vtrfl w

call :decode "%str1%"
call :decode "%str2%"
pause
goto :eof

:decode
set "str=%~1"
set str=%str: =#%
set str=%str:k= %
set str=%str:i=k%
set str=%str:m=i%
set str=%str:e=m%
set str=%str:t=e%
set str=%str:o=t%
set str=%str:#=o%
echo %str%
goto :eof

我还附上了下面的编码脚本。

@echo off

set str1=Your mother is so fat
set str2=the recursive function computing her mass causes a stack overflow

call :encode "%str1%"
call :encode "%str2%"
pause
goto :eof

:encode
set "str=%~1"
set str=%str:o=#%
set str=%str:t=o%
set str=%str:e=t%
set str=%str:m=e%
set str=%str:i=m%
set str=%str:k=i%
set str=%str: =k%
set str=%str:#= %
echo %str%
goto :eof