Obfuscate code.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Obfuscate code.

#16 Post by Aacini » 27 Dec 2021 22:33

ShadowThief wrote:
27 Dec 2021 17:08
Good stuff, Aacini. I've tried to use similar techniques in the past but I've found that pre-making the files makes it impossible to transfer the files while keeping the hidden content intact.
aGerman wrote:
27 Dec 2021 18:08
That's because it requires a feature available on only one file system. In other words, Antonio should have mentioned that even Install.BAT may fail depending on the drive vou're running it (like it happened to me in the first place :lol:).

Steffen
You both are right! The method used here just works on NTFS formatted disks. The person in charge of the application should copy the Install.BAT file on such a disk, run it once, and delete the Install.BAT file. Then the user can run the Program.BAT file in order to use the application. If the final user try to copy (duplicate) the Program.BAT file in other computer, the standard methods used to transfer files will not copy the hidden contents, so the application will not work. In other words: the user must call the application's developer in order to install the program in another computer...

Of course, this very simple protection scheme have several restrictions, but it is enough for most standard users as I said before.

Antonio

MauricioDeAbreu
Posts: 43
Joined: 12 Dec 2021 06:45

Re: Obfuscate code.

#17 Post by MauricioDeAbreu » 28 Dec 2021 16:06

Aacini, thanks for responding.

I am new to Batch, anyway, I will try my best to achieve it.

But, at the moment I cannot take the test, as soon as I can I will and I will tell you.

Thankful as always.

MauricioDeAbreu
Posts: 43
Joined: 12 Dec 2021 06:45

Re: Obfuscate code.

#18 Post by MauricioDeAbreu » 01 Jan 2022 13:44

Aacini wrote:
27 Dec 2021 15:15
There is a simple method to "hide" values, like a password, in a Batch file that have proved to be effective against most intermediate Batch file users. I posted here such a method as a challenge: try to broke the code and get the password, but reviewing the Program.BAT file only! You can NOT review the Installer.BAT file for now...

To start the challenge, run the Install.BAT file once so the Program.BAT file is created. After that, run the Program.BAT file.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Install.BAT: Create Program.BAT file that include a "hidden" password
rem https://www.dostips.com/forum/viewtopic.php?f=3&t=6185
rem Antonio Perez Ayala aka Aacini (https://apaacini.com)


rem Do NOT review the code for now!!









for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a" 
for %%i in (A,B,C) do echo Protection scheme part %%i > %%i 
> ".\C:passîð!BS!!BS!Word.txtÿ" echo Yes, You Did It 
echo This is not the password > passWord.txt 

(
echo @echo off
echo setlocal EnableDelayedExpansion
echo/
echo set /P "pass1=Enter password: "
echo set /P "pass2=" ^< ".\C:passîð!BS!!BS!Word.txtÿ"
echo if "^!pass1^!" equ "^!pass2^!" goto OK
echo echo Bad password
echo goto :EOF
echo/
echo :OK
echo echo You did it!
) > Program.bat

echo Program.bat file created
When you broke the password, please do NOT post what the protection scheme is! Just post that you did it, so other users keep interested in the challenge...

Although this scheme is used here to hide only a password, it can also be used to hide sections of Batch code. Further details about this point in a posterior post...

Antonio
Dear Aacini

First of all, I apologize for the delay in responding.

When I accepted the challenge, I commented that I am new to Batch because I do not have knowledge. So when I accepted the challenge I felt overwhelmed and with a feeling of not being able to solve it.

After a few days of pausing for the dates, I decided to look at the file and run it. I have to be honest, when I saw the files for the first time, I had no idea how the goal of hiding the key was achieved.

First I tried to find the key, and I have to admit that I had to cheat, inserting an additional line to know the value of the variable. But, it was of no use to me, since I did not understand where the key was coming from, or at least not with certainty.

After thinking and trying, thinking and trying, and a lot of thinking. The light bulb went on once again, so I did a Google search, and came up with the possible answer.

Now I just had to replicate the trick, and then visualize the key to the challenge.

Now that I have the key, I know how to see it, and I understand the trick of hiding it (I learned how to do it). It only remains for me to figure out how to apply this trick so that my code runs hidden.

In order to avoid losing interest in the challenge, I wonder if you could send me a link on how to run my code in a hidden way, I would greatly appreciate it.

Thankful as always to all of you for your teachings.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Obfuscate code.

#19 Post by Aacini » 01 Jan 2022 21:06

This is very good! You have experimented by yourself the problems that another user, whit a similar Batch-file experience than yours, would have if he/she wants to broke your protection scheme...

The program below creates Program2.bat file that uses the same trick to "hide" a section of Batch code:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Install2.BAT: Create Program2.BAT with a "hidden" code section
rem https://www.dostips.com/forum/viewtopic.php?f=3&t=6185
rem Antonio Perez Ayala aka Aacini (https://apaacini.com)








for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a" 
for %%i in (A,B,C) do echo Protection scheme part %%i > %%i 
(
echo @echo off
echo (
echo echo Hi, I am the hidden code
echo set /P "var=Enter a value: " ^< CON
echo echo The value is: ^^^!var^^^!
echo pause ^< CON
echo ^) ^> CON
echo exit /B
) > ".\C:passîð!BS!!BS!Word.bat"
echo @echo This is not the code > passWord.bat 

(
echo @echo off
echo setlocal EnableDelayedExpansion
echo/
echo echo Calling the hidden code:
echo cmd /Q /V ^< ".\C:passîð!BS!!BS!Word.bat" ^> NUL
echo echo Returned from hidden code
echo goto :EOF
) > Program2.bat

echo Program2.bat file created
The way to execute the "hidden" Batch code is this:

Code: Select all

cmd /Q /V < HiddenFile.ext > NUL
In this case, the code in such a file must follow certain rules that are somewhat different than standard Batch files. You may review such complete rules at this post.

Antonio

MauricioDeAbreu
Posts: 43
Joined: 12 Dec 2021 06:45

Re: Obfuscate code.

#20 Post by MauricioDeAbreu » 03 Jan 2022 09:33

Thank you very much for answering.

I will review it and comment on the results or doubts.

Post Reply