Monday, June 1, 2015

windows - Batch file to extract exe from itself using more, not working



I have been experimenting with making a batch-file installer program. by embedding the file in the batch file and using a more +... line to extract it.



Here is my code:



@echo off
set self=%~df0
>%userprofile%\desktop\file.exe more +8 "%self%"

echo done!
pause
exit
REM beginning of exe file...

c
sd
€ ~ * 0 ~ * € *’s6 (e
t
€ s.

(/
€ * (f
*^( og
,
(9 oh
*0 K ~ ->~ (i
(j
~ -( þ7 sk
ol
€ Þ(m

Ü~ *


etc...


The problem:



I am attempting to extract a GUI exe, however, once it is extracted, when I try to run it, Windows treats it like it is a 16-bit program... Why does this happen? And how can I fix it?












screenshot


Answer



As pointed out in the comments, stuffing an executable program inside a text file will butcher the binary file. Instead, you can store an encoded version and then decode it. To accomplish that easily, you can use PowerShell. Start with this batch file:




@echo off
powershell -command "[IO.File]::WriteAllBytes('extracted.exe', [Convert]::FromBase64String((gc '%0')[-1]))"
extracted
del extracted.exe
exit

REM Base64-encoded program will be inserted below


Add an extra blank line at the end.




The interesting part is the PowerShell invocation. That command reads in the contents of the batch file, takes the very last line, decodes it from Base64, and writes those bytes to a file called extracted.exe. Then the batch file just runs that EXE, and once that's done it cleans up by deleting the temporary executable.



To make the last line have the necessary information, fire up PowerShell, cd into the directory with the batch file, then run this (with actual file names and paths put in):



[Convert]::ToBase64String([IO.File]::ReadAllBytes('C:\fullPathTo\file.exe')) | Out-File 'batchFile.bat' -Append -Encoding ASCII


Once that's done, your batch file will be able to extract that program and run it.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...