tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
SirJosh3917
Posts: 36
Joined: 02 May 2016 18:59

tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#1 Post by SirJosh3917 » 02 Oct 2016 19:31

Hi guys, here's a quick batch file from my project I'm working on ( BatFramework github.com/SirJosh3917/BatFramework - Don't expect it to be good yet :P )

tobat.bat is a batch file. It's part of the BatFramework project I'm making, but it's as easy as leaving it on your desktop.
Just drag and drop whatever file you want to turn into a batch file on top of it.


It uses certutil to convert the file into base64, then the batch file takes the certificate and modifies it to make it into perfection.
Of course you can do this in command line, but dragging and dropping is slightly easier.

tobat.bat is included as tobat.zip as an attatchment.
Attachments
tobat.zip
tobat.bat is included inside.
(916 Bytes) Downloaded 451 times

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#2 Post by ShadowThief » 02 Oct 2016 20:52

You might want to explain what it actually does (converts a file to base64 and stores it in a separate batch file for later deconversion) instead of whatever "takes the certificate and modifies it to make it into perfection" is supposed to mean.

SirJosh3917
Posts: 36
Joined: 02 May 2016 18:59

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#3 Post by SirJosh3917 » 02 Oct 2016 20:56

ShadowThief wrote:You might want to explain what it actually does (converts a file to base64 and stores it in a separate batch file for later deconversion) instead of whatever "takes the certificate and modifies it to make it into perfection" is supposed to mean.


Isn't that what I said...?

SirJosh3917 wrote:It uses certutil to convert the file into base64, then the batch file takes the certificate and modifies it to make it into perfection.


Tool it uses: CHECK - Certutil
Proccess : CHECK - Converts it into base64, modifies certificate created by certutil into a batch file.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#4 Post by ShadowThief » 02 Oct 2016 23:45

"Modifies it to make it into perfection" is extremely vague and doesn't make any sense. You mention converting the file to base64, but you don't "turn the file into a batch file," you simply store the encoded file in a batch wrapper.

Also, certutil has a maximum input file size of 72727 KB, or just over 71 MB. You may want to mention this somewhere.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#5 Post by aGerman » 03 Oct 2016 04:11

I use a similar code.

encode_b64.bat

Code: Select all

@echo off
for /f %%i in ("certutil.exe") do if not exist "%%~$path:i" (
  echo CertUtil.exe not found.
  pause
  exit /b
)
certutil.exe -f -encode "%~1" "temp.b64~" || (pause&exit /b)

>"decode_%~n1_b64.bat" (
  echo(@echo off
  echo(for /f %%%%i in ("certutil.exe"^) do if not exist "%%%%~$path:i" (
  echo(  echo CertUtil.exe not found.
  echo(  pause
  echo(  exit /b
  echo(^)
  echo(^>"temp.b64~" (
  for /f "usebackq delims=" %%i in ("temp.b64~") do echo(echo(%%i
  echo(^)
  echo(certutil.exe -f -decode "temp.b64~" "%~nx1"
  echo(del "temp.b64~"
  echo(pause
)
del "temp.b64~"

I leave -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- because it doesn't matter ...
Steffen

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#6 Post by Squashman » 03 Oct 2016 06:19

What is this used for? I am not understanding the purpose.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#7 Post by aGerman » 03 Oct 2016 07:50

Base64 encoded data consists of 64 different printable ASCII characters only. That way you are able to represent binary data as text.
Pro: You can use it wherever only printable ASCII characters are valid (e.g. inside of code tags in a forum post).
Con: 2 bytes of the original data become 3 bytes base64 code.

Steffen

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

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#8 Post by Aacini » 03 Oct 2016 08:10

The purpose of this program is convert a binary file (like an .exe, or a .jpg image, or whatever you want) into ASCII characters that can be posted in a text-only forum (like this one), so the text can be copied by any user and the original binary file be recovered.

Several methods had been used for this purpose, including a couple programs written by myself. The last version of my method uses 223 ASCII printable characters to encode the binary data, so the resulting text file may be even smaller than the original binary file in certain cases. For example, the cmd.exe file of my Windows 8.1 Spanish version that have an original size of 404,992 bytes is encoded into a "Set223" file of just 376,168 bytes (the 93% of the original size).

Antonio

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#9 Post by Squashman » 03 Oct 2016 08:40

Now I understand. I actually just started doing this last week. I wanted to embed an executable into my batch file for portability purposes.

I took the my executable which was 61,952 bytes and encoded it with certutil and this made it into a file of 85,242 bytes.

Thought that was a little to big so I used makecab to compress it first. That made it 24,154 bytes and then I ran it through certutil which made it 33,766 bytes.

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

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#10 Post by Aacini » 03 Oct 2016 08:56

@Squashman: I invite you to use my BinToBat223.bat program to encode your .exe file and post the result. After the MakeFiles.bat is created, open it with Notepad, extract the encoded text that appear between "<resource id=..." and "</resource>" lines at end of the file, and save it in a separate file. The size of this file is the size of the "Set223" encoded text.

You may also do this on both the .exe and the .cab files.

Antonio

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#11 Post by Squashman » 03 Oct 2016 09:51

Aacini wrote:@Squashman: I invite you to use my BinToBat223.bat program to encode your .exe file and post the result. After the MakeFiles.bat is created, open it with Notepad, extract the encoded text that appear between "<resource id=..." and "</resource>" lines at end of the file, and save it in a separate file. The size of this file is the size of the "Set223" encoded text.

You may also do this on both the .exe and the .cab files.

Antonio

Seem to be having problems.

Code: Select all

I:\>bin2bat223.bat unix2dos_Original.exe
Note that the encoding of each file will start *after* the corresponding FC
command had ended; this may take awhile depending on file size...

Encoding unix2dos_Original.exeFC: cannot open ZERO.TMP - No such file or folder

 done

File MakeFiles.bat created

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#12 Post by ShadowThief » 03 Oct 2016 10:13

Using a for loop takes forever on large files. Thankfully, certutil only processes the parts of files that are between -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- so you can just append the base64 data to the end of your reversal script with type instead of needing to go line by line.

Code: Select all

::------------------------------------------------------------------------------
:: Converts a file to base64 using a heredoc instead of a for loop for speed.
:: The script allows for a file with a maximum size of 72726 KB.
::
:: Arguments: %1 - The file to convert
::------------------------------------------------------------------------------
@echo off
setlocal enabledelayedexpansion
cls

:: Determine if a parameter was passed in and quit if one wasn't
if "%~nx1"=="" (
   echo No file specified. Exiting.
   exit /b
)
if %~z1 GTR 74472684 (
   echo Maximum file size of 74472684 bytes exceeded. Exiting.
   exit /b
)

:: Create a new output file based on the file name of the input file,
:: converting spaces to underscores if necessary
set base64_output_file=%~n1.b64
set base64_output_file=%base64_output_file: =_%
if exist %base64_output_file% del /q %base64_output_file%

:: Convert the file to base64
set /p "=Converting file..." <nul
certutil -encode "%~nx1" %base64_output_file% >nul
echo DONE

:: Generate reversal script
set /p "=Generating reversal script..." <nul
set batch_output_file=%base64_output_file%.bat
>%base64_output_file%.bat (
   echo(@echo off
   echo(certutil -decode "%%~0" Fonts.rar
   echo(exit /b
   echo/
)
type %base64_output_file% >>%base64_output_file%.bat
del %base64_output_file%
echo DONE

echo Process complete.


The largest file that works with certutil (74472684 bytes) takes 6.5 seconds to process. For comparison, the code in the first post takes the same file 5 hours to complete and creates a reversal script that is 13 MB larger than mine.
Last edited by ShadowThief on 03 Oct 2016 16:34, edited 2 times in total.

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

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#13 Post by Aacini » 03 Oct 2016 11:12

Squashman wrote:Seem to be having problems.

Code: Select all

I:\>bin2bat223.bat unix2dos_Original.exe
Note that the encoding of each file will start *after* the corresponding FC
command had ended; this may take awhile depending on file size...

Encoding unix2dos_Original.exeFC: cannot open ZERO.TMP - No such file or folder

 done

File MakeFiles.bat created


I don't understand what happen. The ZERO.TMP file is created by this line:

Code: Select all

fsutil file createnew zero.tmp "%~Z1" > NUL

I know that FSUTIL does not require admin privileges in order to create a file this way (I just tested it again), so I don't understand what could be the error here...

Could you test if this command correctly create a 1000 bytes size file?

Code: Select all

fsutil file createnew zero.tmp "1000"

If so, perhaps you may remove the "> NUL" part from the line above in order to see any message from fsutil...

Antonio

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#14 Post by Squashman » 03 Oct 2016 12:13

Aacini wrote:I know that FSUTIL does not require admin privileges in order to create a file this way (I just tested it again), so I don't understand what could be the error here...

Could you test if this command correctly create a 1000 bytes size file?

Code: Select all

fsutil file createnew zero.tmp "1000"

If so, perhaps you may remove the "> NUL" part from the line above in order to see any message from fsutil...

Antonio

Code: Select all

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

H:\>fsutil /?
The FSUTIL utility requires that you have administrative privileges.

H:\>fsutil file createnew zero.tmp "1000"
The FSUTIL utility requires that you have administrative privileges.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: tobat.bat (Part of the BatFramework) - Drag and drop a file and turn it into batch

#15 Post by ShadowThief » 03 Oct 2016 12:16

Weird.
Image

Post Reply