I have seen in this forum some different methods to attach binary data to the script and to extract/decode it.
Some of them use hexadecimal dump and other base64 encoding. But base64 encoding is a bit more space efficient than a hex dump.
I've seen base64 decoding using certutil.
Here the decoding is made using cscript, which I think is present even in XP. Can anyone confirm this point?
Code: Select all
@echo off
:searchAndDecode
SetLocal EnableDelayedExpansion & set "_FILES_=" & set "fil=" & set "ini="
set "_vbs_file_=%TEMP%\%~n0.vbs"
::search for encoded data
for /F "usebackq tokens=1-3 delims=:" %%1 in (`findstr /B /N ":base64Encoded:" "%~f0"`) do (
if "%%3" EQU "!fil!" (set "_FILES_=!_FILES_!!fil!:!ini!:%%1,") else (set "fil=%%3" & set "ini=%%1")
)
set "_FILES_=%_FILES_:~0,-1%"
rem create a vbscript to do the decoding
for %%# in (%_FILES_%) do (
for /F "tokens=1-3 delims=:" %%1 in ("%%#") do (
(
echo/set outStream=CreateObject("ADODB.Stream"^)
echo/outStream.Type=1
echo/outStream.Open
echo/set oFS=CreateObject("Scripting.FileSystemObject"^)
echo/set inStream=oFS.OpenTextFile("%~f0",1,0,0^)
echo/set oXML=CreateObject("MSXml2.DOMDocument"^)
echo/set oItem=oXML.createElement("tmp"^)
echo/oItem.DataType="bin.base64"
echo/for i=1 to %%2 step 1
echo/ inStream.readline
echo/next
echo/do while i^<%%3
echo/ oItem.text=inStream.readline
echo/ decodedBytes=oItem.NodeTypedValue
echo/ outStream.Write decodedBytes
echo/ i=i+1
echo/loop
echo/outStream.SaveToFile "%TEMP%\%%1",2
echo/inStream.close
echo/outStream.close
echo/set oItem=nothing
echo/set oXML=nothing
echo/set outStream=nothing
echo/set inStream=nothing
echo/set oFS=nothing
)>"%_vbs_file_%"
Cscript.exe /B /E:vbs "%_vbs_file_%" >NUL
start notepad "%TEMP%\%%1"
)
)
del /F /Q "%_vbs_file_%" 2>NUL
EndLocal
exit/B 0
:base64Encoded:loremIpsum.txt:
TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXB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