Base64 decoding of a String batch file - batch-file

I want to decode a base64 encoded text and use it to login to a website. The base64 encoded text is the username and password. I hav found tools like Base64.exe and b64.exe but they take input as a file whereas in my case input is a string. plz help me out. All this shud be done in a batch file only and also am reading the encoded text from a config.txt file.

You can use the CERTUTIL command without installing external software (though it will need temp files):
#echo off
del /q /f "%temp%\b64" >nul 2>nul
del /q /f "%temp%\decoded" >nul 2>nul
set "base64string=YmFzZTY0c3RyaW5n"
echo -----BEGIN CERTIFICATE----->"%temp%\b64"
<nul set /p=%base64string% >>"%temp%\b64"
echo -----END CERTIFICATE----->>"%temp%\b64"
certutil /decode "%temp%\b64" "%temp%\decoded" >nul 2>nul
for /f "useback tokens=* delims=" %%# in ("%temp%\decoded") do set "decoded=%%#"
echo %decoded%
del /q /f "%temp%\b64" >nul 2>nul
del /q /f "%temp%\decoded" >nul 2>nul
You can also use powershell which will be slower despite there will be no temp files:
set "base64string=YmFzZTY0c3RyaW5n"
for /f "tokens=* delims=" %%# in ('powershell [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("""%base64string%"""^)^)') do set "decoded=%%#"
echo %decoded%
You can try also with atob.bat :
call atob.bat YmFzZTY0c3RyaW5n decoded
echo %decoded%
Or with base64.bat
for /f "tokens=* delims=" %%# in ('base64.bat -decode YmFzZTY0c3RyaW5n') do set "decoded=%%#"
echo %decoded%

I found the solution. I am extracting the base64 encoded text from "config.txt" to a new file say "tmp.txt". now I run the cmd "b64.exe -d tmp.txt tmp2.txt".I get the decoded text in the new "tmp2.txt" file. I delete the "tmp.txt" and "tmp2.txt" once i read the username and password.
Thank you

You can use the macro style invented by #jeb, #dbenham and #Ed Dyreen:
#echo off
====SETLOCAL DisableDelayedExpansion EnableExtensions
set ^"LF=^
%===EXPANDS TO NOTHING===%
"
REM \n is an escaped LF + caret
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
REM Initalize macro
set ^"$b64d=FOR %%$ in (%%$ MainMacro) do if "%%$" == "MainMacro" (%\n%
^>nul %__APPDIR__%CERTUTIL.EXE -f -decodehex args.tmp proc.tmp 1%\n%
type proc.tmp%\n%
del args.tmp proc.tmp%\n%
) ELSE ^>args.tmp echo("
echo BEFORE :: %time%
%$b64d%dGhpcwppcwpzaWNrIQo=
echo AFTER :: %time%
Output:
BEFORE :: 21:13:31.94
this
is
sick!
AFTER :: 21:13:32.02

Related

Write variable in quotes to output file

(for %%f in (%zipfiles%) do (
<nul set /p ="%%f",
certutil -hashfile "%%f" SHA256|find /v ":" || echo empty file
))> "C:\Location\Report\ListOfFiles.csv"
the "%%f" does not work, neither %%"f" nor "/p =%%f",
What I want is the filename in quotes:
"Filename 001.zip",68b17a9d0d98dd64f3c6c5b29e5cd304a6397d21f24e3087723ccad9f6f77c58
#ECHO OFF
SETLOCAL EnableExtensions
set "zipfiles=*.zip"
(for %%f in (%zipfiles%) do (
<nul set /p =""%%f","
certutil -hashfile "%%f" SHA256|find /v ":" || echo empty file
))>".\ListOfFiles.csv"
Here in <nul set /p =""%%f",":
outer double quotes used to escape the comma as well as inner double quotes so that
inner double quotes and the comma are copied to the output stream.
#ECHO OFF
SETLOCAL
set "zipfiles=file*"
(for %%f in (%zipfiles%) do (
if %%~zf==0 (echo "%%~ff",empty file
) else for /f "delims=" %%a in ('certutil -hashfile "%%f" SHA256^|find /v ":"') do echo "%%~ff",%%a
))> "u:\ListOfFiles.csv"
GOTO :EOF
Simple enough - no tricks. I tried with files named filename* on my machine. Simply use %%~zf for zero-length files to produce that name and otherwise select only the non-:-containing lines for certutil output to build the other lines; ^ used to escape the pipe so the certutil output is filtered.
destination filename also changed to suit my system.

Editing a particular key value pair in a ini file in batch script

I have a coniguration file related to an application with key value pair as usual.
one of the variable in [General] tab is xvariable=0.I want this to change like xvariable=1 ie new value to 1.I dont want to use external exes or other programs.kindly help
Sorry for late reply and forgive me as i am newbie to batch scripting as well stackoverflow my initial ini file look like this
[General]
DisableAutomaticallyReconnection=0
TLUSERDIR=C:\\Users\\Balu\\AppData\\Roaming\\Schlumberger\\Techlog
memoryManagment=-1
[ModuleManager]
lastUsedProfile=TechlogCoreModule_1_Default
[TNotificationService]
Info_balloon=1
Warning_balloon=1
Error_balloon=1
Command_balloon=1
InfoMacro_balloon=1
Expected output ini contains only one change in [General] tab i.e.
[General]
DisableAutomaticallyReconnection=1 (new value)
other lines of ini will remain as it is.
I have tried three methods(as new to batch scripting)
Script 1
# echo off
for /d %%F in (c:\users\*) do (
::echo %%F
set filename=%%F\AppData\Roaming\Schlumberger\Techlog 2> nul
cd %%F\AppData\Roaming\Schlumberger\Techlog 2> nul
where=$(PWD)
:: echo !where!
copy Techlog.ini techlog.orig 2> nul
type Techlog.ini|find /i /v "general"|find /i /v
"DisableAutomaticallyReconnection" > techlog1 2> nul
echo [General] > techlog2 2> nul
echo DisableAutomaticallyReconnection=1 >> techlog2 2> nul
del /q Techlog.ini 2> nul
type techlog2 techlog1 > Techlog.ini 2> nul
del /q techlog1 2> nul
del /q techlog2 2> nul
)
Script 2
#echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%i in
(C:\Users\Balu\AppData\Roaming\Schlumberger\Techlog\Techlog.ini) do (
set input=%%i
:: echo !input!
set input=!input:DisableAutomaticallyReconnection=1!
echo !input!
echo !input! >>
C:\Users\Balu\AppData\Roaming\Schlumberger\Techlog\Techlog.ini.chg
)
ren C:\Users\Balu\AppData\Roaming\Schlumberger\Techlog\Techlog.ini Techlog.ini.old
ren C:\Users\Balu\AppData\Roaming\Schlumberger\Techlog\Techlog.ini.chg Techlog.ini
Script 3
#echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%i in (DataFile.ini) do (
set input=%%i
set input=!input:DisableAutomaticallyReconnection=test=1!
echo !input! >> DataFile.ini.chg
)
ren DataFile.ini DataFile.ini.old
ren DataFile.ini.chg DataFile.ini
Thanks in advance.
Here's a single example script, which you will need to edit, (on line 2), to define your source INI file.
#SetLocal DisableDelayedExpansion
#Set "IFile=C:\SomePathTo\YourFile.ini"
#Set "ISect=General"
#Set "ISKey=xvariable"
#If Not Exist "%IFile%" GoTo :EOF
#Set "SName=[NULL]"
#For /F "Tokens=1*Delims=]" %%# In ('Type "%IFile%" 2^>NUL^|"%__AppDir__%find.exe" /V /N ""^&Break^>"%IFile%"')Do #(
Echo("%%$"|"%__AppDir__%findstr.exe" "^\"\[.*\]\"$">NUL&&Set "SName=%%$"
SetLocal EnableDelayedExpansion
If /I "[%ISect%]"=="!SName!" (If /I Not "[%ISect%]"=="%%$" (
Echo("%%$"|"%__AppDir__%findstr.exe" /IRC:"^\"[;\ \ ]*%ISKey%[\ \ ]*=[\ \ 01]*\"$">NUL&&Echo(%ISKey%=1||Echo(%%$
)Else Echo(%%$)Else Echo(%%$
EndLocal)>>"%IFile%"
#Pause
As you've provided very little information and have shown no evidence that you've even attempted this task yourself, I shall not be providing further support beyond this. Although it's worth me mentioning that in the following, "^\"[;\ \ ]*%ISKey%[\ \ ]*=[\ \ 01]*\"$" is actually: "^\"[;\SPACE\TAB]*%ISKey%[\SPACE\TAB]*=[\SPACE\TAB01]*\"$".

Convert batch script into base64 / non-readable format

Years ago I saw some batch scripts that contain nothing relevant at a fist look, but with a lot of content. The content was base64 encoded, and when you run the scripts, they are just running the commands that are encoded there.
Something like
SET mypath=%~dp0
echo "%mypath%
will look similar with
some_base_64_decoding_function(
U0VUIG15cGF0aD0lfmRwMA0KIGVjaG8gIiVteXBhdGgl
)
I don't really remember how those file were, and I can't find anything relevant on the Internet, except this article: Convert PowerShell script into non-readable format
but this only refeer to PowerShell scripts, and I need it for .bat scripts.
Anyone to give me a hint?
This is a sample batch file for encoding files in Base 64 with Certutil utility.
How to use it?
Just save this code on your notepad or on notepad++ or on any text editor as :
Certutil_B64_Encoding_Files.bat and drag and drop any file over it to be encoded
#echo off
Title Encoding files with CERTUTIL utility by Hackoo 2017
color 0A & Mode 83,3
If "%~1"=="" (
color 0C & Mode 80,3
echo(
echo You must drag and drop a file over this batch script to be encoded !
Timeout /T 5 /nobreak>nul & exit /b
)
#for /f %%i in ("certutil.exe") do if not exist "%%~$path:i" (
echo CertUtil.exe not found.
pause
exit /b
)
set "TempFile=%Temp%\Temp_b64
set "OutputFile=%~nx1_encoded%~x0"
If exist "%OutputFile%" Del "%OutputFile%" >nul 2>&1
echo(
echo Please wait a while ... Encoding "%~nx1" is in progress ...
certutil.exe -f -encode "%~1" "%TempFile%" >nul 2>&1
(
echo #echo off
echo CERTUTIL -f -decode "%%~f0" "%%Temp%%\%~nx1" ^>nul 2^>^&1
echo Start "%~n1" "%%Temp%%\%~nx1"
echo Exit
)>> "%OutputFile%"
copy "%OutputFile%" /b + "%TempFile%" /b >nul 2>&1
If exist "%TempFile%" Del "%TempFile%" >nul 2>&1
Timeout /T 2 /NoBreak>nul & exit
Encoded HTA Example: CommandLine.hta_encoded.bat
This is a result of an encoded output of a HTA file of mine named as CommandLine.hta_encoded.bat
So, you should copy and paste this code as CommandLine.hta_encoded.bat and execute it by double click. And you will get something like this :
Encoded VBS Example : DJBuzzRadio.vbs_encoded.bat
#echo off
CERTUTIL -f -decode "%~f0" "%Temp%\DJBuzzRadio.vbs" >nul 2>&1
Start "DJBuzzRadio" "%Temp%\DJBuzzRadio.vbs"
Exit
-----BEGIN CERTIFICATE-----
UGxheSAiaHR0cDovL3d3dy5jaG9jcmFkaW9zLmNoL2RqYnV6enJhZGlvX3dpbmRv
d3MubXAzLmFzeCINClN1YiBQbGF5KFVSTCkNCiAgIERpbSBTb3VuZA0KICAgU2V0
IFNvdW5kID0gQ3JlYXRlT2JqZWN0KCJXTVBsYXllci5PQ1giKQ0KICAgU291bmQu
VVJMID0gVVJMDQogICBTb3VuZC5zZXR0aW5ncy52b2x1bWUgPSAxMDANCiAgIFNv
dW5kLkNvbnRyb2xzLnBsYXkNCiAgIGRvIHdoaWxlIFNvdW5kLmN1cnJlbnRtZWRp
YS5kdXJhdGlvbiA9IDANCiAgICAgICB3c2NyaXB0LnNsZWVwIDEwMA0KICAgbG9v
cA0KICAgd3NjcmlwdC5zbGVlcCAoaW50KFNvdW5kLmN1cnJlbnRtZWRpYS5kdXJh
dGlvbikrMSkqMTAwMA0KRW5kIFN1Yg0K
-----END CERTIFICATE-----
MACRO-b64decode.bat
#echo off
SETLOCAL DISABLEDELAYEDEXPANSION
::DEFINITIONS
( set LF=^
%= EMPTY =%
)
set ^"NL=^^^%LF%%LF%^%LF%%LF%^^"
set $MACRO.ForEntireLine=^^^^^^^"eol^^^^=^^^^^^^%LF%%LF%^%LF%%LF%^^^%LF%%LF%^%LF%%LF%^^^^ delims^^^^=^^^^^^^"
::MACRO
ENDLOCAL &^
set $MACRO.b64exec=FOR %%? in (args main) do if "%%?" == "main" (%NL%
FOR %%a in (!args!) do (%NL%
^>encoded.txt echo %%a%NL%
^>nul certutil -decodehex encoded.txt decoded.txt 1%NL%
FOR /F %$MACRO.ForEntireLine% %%x in (decoded.txt) do %%x%NL%
del /F /Q encoded.txt decoded.txt%NL%
)%NL%
) ELSE SETLOCAL ENABLEDELAYEDEXPANSION ^& set args=
To call the macro inside your batch file:
call MACRO-b64decode.bat
%$MACRO.b64exec% ZWNobyBoZWxsbywgd29ybGReIQ==
which will output:
hello, world!
Note: DELAYEDEXPANSION is enabled

Escape variable in Batch

I have a piece of code:
for /F "tokens=*" %%A in (myfile.txt) do (
echo echo %%A ^>^>myfile.txt>>myfile.bat
)
As some of you can see, this code reads myfile.txt, and creates myfile.bat, which when opened creates an exact copy of the original myfile.txt. The problem comes in when myfile.txt contains special characters, such as >. How can I escape the %%A variable?
any help is appreciated.
I have this idea : using Certutil command to encode your text file and generate it again like this batch script can do :
#echo off
set "MyTxtFile=myfile.txt"
for %%i in (%MyTxtFile%) do (
set "MyBatchFile=%%~ni.bat"
set "TempFile=%%~ni.B64"
Set "NewFile=%%~ni__%%~xi"
)
#for /f %%i in ("certutil.exe") do if not exist "%%~$path:i" (
echo CertUtil.exe not found.
pause
exit /b
)
Rem to encode your text file to a temporary file
Certutil -encode "%MyTxtFile%" "%TempFile%" >nul 2>&1
(
echo #echo off
echo Title Generate code of "%MyTxtFile%" to "%NewFile%"
echo CERTUTIL -f -decode "%%~f0" "%NewFile%" ^>nul 2^>^&1
echo Exit
)> "%MyBatchFile%"
#Copy "%MyBatchFile%" /b + "%TempFile%" /b >nul 2>&1
Del "%TempFile%" >nul 2>&1
Timeout /T 2 /NoBreak>nul & exit
EDIT
This code can generate what you want based on your last comment test >> Hello :
#echo off
Title Generate code of "myfile.txt" to "myfile__.txt"
CERTUTIL -f -decode "%~f0" "myfile__.txt" >nul 2>&1
Exit
-----BEGIN CERTIFICATE-----
dGVzdCA+PiBIZWxsbw0KDQo=
-----END CERTIFICATE-----
The following commented code snippet could help (see Variable Edit/Replace):
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
rem silently create empty output files - just for testing
>NUL COPY /Y NUL myfile46134196.bat
>NUL COPY /Y NUL myfile46134196ou.txt
for /F "tokens=*" %%A in (myfile46134196in.txt) do (
set "_aux=%%A"
rem replace every `Greater Than` symbol with a properly escaped one
echo echo !_aux:^>=^^^>! ^>^>myfile46134196ou.txt>>myfile46134196.bat
rem ^> properly escape `Greater Than` symbol - input string
rem ^> -output string
rem ^^ add a caret (Circumflex Accent) to output string
)
rem debugging outputs
echo ON
type myfile46134196.bat
#ECHO OFF
rem test: run currently created .bat file
call myfile46134196.bat
rem test: and show result
echo ON
type myfile46134196in.txt
type myfile46134196ou.txt
#ECHO OFF
Output:
==> .\so\46134196.bat
==> type myfile46134196.bat
echo test^>^>hello >>myfile46134196ou.txt
==> type myfile46134196in.txt
test>>hello
==> type myfile46134196ou.txt
test>>hello
==>
Further resources (required reading, incomplete):
(command reference) An A-Z Index of the Windows CMD command line
(helpful particularities) Windows CMD Shell Command Line Syntax
(%A etc. special page) Command Line arguments (Parameters)
(special page) EnableDelayedExpansion
(> etc. special page) Redirection
(^ caret) Escape Characters, Delimiters and Quotes

Issues with Batch File Leaving a Space After Editing Text File

I'm looking to find a solution to an issue I'm having trying to add an extension to the end of every line in a text file. I have a solution that works well, except it adds a space to the end after every line. Here's what I have:
#echo off
Set "_t1=PDF List.txt"
Set "_t3=.pdf"
PUSHD %_t0%
If EXIST tmp.txt del tmp.txt
For /F "usebackq delims=" %%A in ("%_t1%") do echo %%A%_t3% >>tmp.txt
del "%_t1%"
rename tmp.txt "%_t1%"
For %%A in (0 1 2) do Set _t%%A=
POPD
Remove the space from your echo command:
For /F "usebackq delims=" %%A in ("%_t1%") do echo %%A%_t3%>>tmp.txt

Resources