3

I want to be able to simply run a Windows batch file and have it create a .exe executable.

I know that you can create files with the following script in batch:

@echo off
echo This will be in a text file! >> test.txt
echo And this will be the second line! >> test.txt

So I was wondering, would be possible to copy the source code of an EXE file and add an "echo" in front of every line, and a " >> test.exe" and the end of every line?

I tried to open the 39KB .exe in Notepad ++, but it was just a bunch of black NUL characters.

So I'm pretty much asking if there's a way to embed files inside batch files? Even an audio file maybe?

Shane Smiskol
  • 952
  • 1
  • 11
  • 38
  • See: http://stackoverflow.com/questions/27383152/storing-large-file-within-batch-file/27384211#27384211 – Aacini Jun 15 '15 at 04:05

1 Answers1

3

here's how it can be done with certutil utility. In this case it is demonstrated with hard-coded path to whoami.exe which is a builtin command in windows.

First a script that creates the .bat with the embedded .exe :

@echo off
certutil -f -encode  %windir%\system32\whoami.exe whoami.b64
echo @echo off >container.bat
echo certutil -decode "%%~f0" whoami.exe>>container.bat
echo call whoami.exe>>container.bat
echo exit /b %%errorlevel%%>>container.bat
type whoami.b64>>container.bat
del whoami.b64

For your exe you'll need to change the path to the exe.Then the container.bat should look like:

@echo off 
certutil -decode "%~f0" whoami.exe
call whoami.exe
exit /b %errorlevel%
-----BEGIN CERTIFICATE-----
...
..base64 encoded content...
...
-----END CERTIFICATE----

certutil can encode/decode base64 and hex here base64 is more convenient because it can be embedded into batch thanks to enclosements (BEGIN CERTIFICATE and END CERTIFICATE) and base64 encoded strings are shorter than hex ones.

npocmaka
  • 55,367
  • 18
  • 148
  • 187