printf (which is I think part of the POSIX standard) should make it relatively easy to produce a binary stub.
For MS-DOS, things are not that easy since not all characters can be printed to a file. The following post lists some possibilities (using debug, which is not present on all systems, producing a VBScript, using echo (can't handle all characters), set /p (/p option not present on older systems) or other built-in commands). I think the best solution would be to use echo to create a very small binary stub (a .COM and/or .EXE) which is capable of sending to STDOUT or to a file the rest of the binary data, based on a (e.g.) base64 output. The extraction would then proceed in three stages:
extract the short stub, which is hand-crafted to only contain characters that can be printed (hoping that NULL or other problematic characters will never be needed in any header or opcode, which might be difficult on ARM versions of Windows, but those should usually have other tools available like VBS, PowerShell or other).
run the short .COM or .EXE stub, passing it some base64 (or other base) data, that it writes to a second .COM or .EXE file
run the second .COM or .EXE file, which reads back from the original os.bat to produce the final result.exe (and delete all intermediate files subsequently)
There might be a solution using a fixed header that can be printed by MS-DOS echo, followed by binary data that matches the entire os.bat file. The extraction would then consist in printing a tiny header, and using copy header.txt+os.bat result.exe to concatenate them.
printf
(which is I think part of the POSIX standard) should make it relatively easy to produce a binary stub.For MS-DOS, things are not that easy since not all characters can be printed to a file. The following post lists some possibilities (using
debug
, which is not present on all systems, producing a VBScript, usingecho
(can't handle all characters),set /p
(/p
option not present on older systems) or other built-in commands). I think the best solution would be to useecho
to create a very small binary stub (a.COM
and/or.EXE
) which is capable of sending to STDOUT or to a file the rest of the binary data, based on a (e.g.) base64 output. The extraction would then proceed in three stages:.COM
or.EXE
stub, passing it some base64 (or other base) data, that it writes to a second.COM
or.EXE
file.COM
or.EXE
file, which reads back from the originalos.bat
to produce the finalresult.exe
(and delete all intermediate files subsequently)https://www.experts-exchange.com/questions/26372121/DOS-Batch-file-to-write-Binary.html
There might be a solution using a fixed header that can be printed by MS-DOS
echo
, followed by binary data that matches the entireos.bat
file. The extraction would then consist in printing a tiny header, and usingcopy header.txt+os.bat result.exe
to concatenate them.