Reply to comment

Encryption using batch scripts (.bat)

Batch encryption:

  • Sample text string: encryption
  • Script output: eyEor75jc+IgCn#x15*e+pfLL6n_X#JaOmoU8LA@IB6ix*#24OgCatfL-Xe@K#xCp_A_8Hb5-rCg

Sometimes projects require strange solutions. In this case, encryption and decryption of simple text through batch script. The algorithm used is very weak and rather inflated but it gets the job done. This approach should not be used to secure sensitive information in automated scripts etc.

In this case, the script was used to obfuscate certain paths that could make future exploitation easier than what it had to be. The algorithm for both encryption and decryption can easily be mimicked in other programming languages, allowing the use of other scripting languages to play nice with the batch script version.

The script does suffer from bugs. Spaces in the input text terminate the process. I tried to escape clear text symbols that may inject incorrect formatting into the script but it can get flaky.

In general, this script is a proof-of-concept and should be revised before using in any production environment.

Decrypt

@echo off
@SetLocal EnableDelayedExpansion
@title ExponentiallyHorrible Encryption - Decrypt
rem By NickC
rem SkinnyBarriers Group
cls

for /f %%a in (%1) do (
call :decrypt %%a>>decrypted.txt
)
exit

:decrypt
set flip=
set decrypted=
set hash=%1
echo %1>alg.tmp
for %%a in (alg.tmp) do (
set /a length=%%~za
)
set /a length -=2
del alg.tmp
set count=
for /l %%a in (0,1,!length!-1) do (
for /l %%b in (0,1,%%a+1) do (
set /a count+=1
)
for /l %%b in (!count!,1,!count!) do (
set char=!hash:~%%b,1!
if not '!char!'=='' (
set decrypted=!decrypted!!hash:~%%b,1!
)
set /a count+=1
)
)
echo !decrypted!>alg.tmp
for %%a in (alg.tmp) do (
set /a length=%%~za
)
set /a length -=2
del alg.tmp
set /a split=!length!/2
for /l %%a in (!split!, 1, !split!) do (
set decrypted=!decrypted:~%%a!!decrypted:~0,%%a!
)
for /l %%a in (!length!, -1, 0) do (
set flip=!flip!!decrypted:~%%a,1!
)
set decrypted=!flip!
echo !decrypted!
goto :eof

Encrypt

@echo off
@SetLocal EnableDelayedExpansion
@title ExponentiallyHorrible Encryption - Encrypt
rem By NickC
rem SkinnyBarriers Group
cls

for /f %%a in (%~s1) do (
call :encrypt %%a>>encrypted.txt
)
exit

:encrypt
set flip=
set salt=
set hash=
set pass=%1
set encrypted=
set upperabc=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set lowerabc=abcdefghijklmnopqrstuvwxyz
set numbers=0123456789
set symbols=-*%$#@!~_+
for /l %%z in (1,1,100) do (
set /a rand1=!random!%%25
set /a rand2=!random!%%25
set /a rand3=!random!%%9
set /a rand4=!random!%%10
for /l %%b in (!rand1!,1,!rand1!+1) do set salt=!salt!!upperabc:~%%b,1!
for /l %%b in (!rand2!,1,!rand2!+1) do set salt=!salt!!lowerabc:~%%b,1!
for /l %%b in (!rand3!,1,!rand3!+1) do set salt=!salt!!numbers:~%%b,1!
for /l %%b in (!rand4!,1,!rand4!+1) do set salt=!salt!!symbols:~%%b,1!
)
echo !pass!>alg.tmp
for %%a in (alg.tmp) do (
set /a length=%%~za
)
set /a length -=2
del alg.tmp
for /l %%a in (!length!, -1, 0) do (
set flip=!flip!!pass:~%%a,1!
)
set pass=!flip!
set /a split=!length!/2
for /l %%a in (!split!, 1, !split!) do (
set pass=!pass:~%%a!!pass:~0,%%a!
)
for /l %%a in (0,1,!length!-1) do (
set quicksalt=
for /l %%b in (0,1,%%a+1) do (
set /a rand1=!random!%%100
for /l %%c in (!rand1!,1,!rand1!+1) do set quicksalt=!quicksalt!!salt:~%%c,1!
)
set hash=!hash!!quicksalt!!pass:~%%a,1!
)
set encrypted=!hash!
echo !encrypted!
goto :eof

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options