问题
There are many reasons to want to \'convert\' a .bat
to .exe
- to hide/obfuscate implementation, passwords, path to resources , to create a service from batch file ... and mainly to make your work to look more complicated and important than it really is.
There are also many reasons to not want to use third party tools.
So what if you want to \'convert\' a batch file to .exe
without external software?
(convert is in quotes because I don\'t think there\'s really way to compile a batch file to executable. There are too many abusive twisty techniques and bugs used extensively and all the tools that I know in fact create a temporary .bat
file and then call it )
回答1:
One very obvious approach is to use IEXPRESS - the ancient built-in tool that creates self-extracting packages and is capable to execute post extraction commands. So here's IEXPRESS sed-directive/.bat file that creates a self-extracting .exe with packed .bat. It accepts two arguments - the .bat file you want to convert and the target executable:
;@echo off
; rem https://github.com/npocmaka/batch.scripts/edit/master/hybrids/iexpress/bat2exeIEXP.bat
;if "%~2" equ "" (
; echo usage: %~nx0 batFile.bat target.Exe
;)
;set "target.exe=%__cd__%%~2"
;set "batch_file=%~f1"
;set "bat_name=%~nx1"
;set "bat_dir=%~dp1"
;copy /y "%~f0" "%temp%\2exe.sed" >nul
;(echo()>>"%temp%\2exe.sed"
;(echo(AppLaunched=cmd.exe /c "%bat_name%")>>"%temp%\2exe.sed"
;(echo(TargetName=%target.exe%)>>"%temp%\2exe.sed"
;(echo(FILE0="%bat_name%")>>"%temp%\2exe.sed"
;(echo([SourceFiles])>>"%temp%\2exe.sed"
;(echo(SourceFiles0=%bat_dir%)>>"%temp%\2exe.sed"
;(echo([SourceFiles0])>>"%temp%\2exe.sed"
;(echo(%%FILE0%%=)>>"%temp%\2exe.sed"
;iexpress /n /q /m %temp%\2exe.sed
;del /q /f "%temp%\2exe.sed"
;exit /b 0
[Version]
Class=IEXPRESS
SEDVersion=3
[Options]
PackagePurpose=InstallApp
ShowInstallProgramWindow=0
HideExtractAnimation=1
UseLongFileName=1
InsideCompressed=0
CAB_FixedSize=0
CAB_ResvCodeSigning=0
RebootMode=N
InstallPrompt=%InstallPrompt%
DisplayLicense=%DisplayLicense%
FinishMessage=%FinishMessage%
TargetName=%TargetName%
FriendlyName=%FriendlyName%
AppLaunched=%AppLaunched%
PostInstallCmd=%PostInstallCmd%
AdminQuietInstCmd=%AdminQuietInstCmd%
UserQuietInstCmd=%UserQuietInstCmd%
SourceFiles=SourceFiles
[Strings]
InstallPrompt=
DisplayLicense=
FinishMessage=
FriendlyName=-
PostInstallCmd=<None>
AdminQuietInstCmd=
UserQuietInstCmd=
example:
bat2exeIEXP.bat myBatFile.bat MyExecutable.exe
This should work practically on every Windows machine out there but has one major limitation - you cannot pass arguments to the created .exe file
So one other possible approach is to look at the .NET compilers (again should be available on almost every win machine).I've choose Jscript.net .
This is a hybrid jscript.net
/.bat
script that will read the .batch file content.Will create another jscript.net with the .bat file content and after the compilation will create a new bat file int the temp folder and will call it.And will accept command line arguments.(explained might look complex but in fact it's simple):
@if (@X)==(@Y) @end /* JScript comment
@echo off
setlocal
del %~n0.exe /q /s >nul 2>nul
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
set "jsc=%%v"
)
if not exist "%~n0.exe" (
"%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
)
%~n0.exe "%jsc%" %*
del /q /f %~n0.exe 1>nul 2>nul
endlocal & exit /b %errorlevel%
*/
//https://github.com/npocmaka/batch.scripts/blob/master/hybrids/.net/bat2exe.bat
import System;
import System;
import System.IO;
import System.Diagnostics;
var arguments:String[] = Environment.GetCommandLineArgs();
if (arguments.length<3){
Console.WriteLine("Path to cmd\bat file not given");
Environment.Exit(1);
}
var binName=Path.GetFileName(arguments[2])+".exe";
if(arguments.length>3){
binName=Path.GetFileName(arguments[3]);
}
var batchContent:byte[]= File.ReadAllBytes(arguments[2]);
var compilerLoc=arguments[1];
var content="["
for (var i=0;i<batchContent.length-1;i++){
content=content+batchContent[i]+","
}
content=content+batchContent[batchContent.length-1]+"]";
var temp=Path.GetTempPath();
var dt=(new Date()).getTime();
var tempJS=temp+"\\2exe"+dt+".js";
var toCompile="\r\n\
import System;\r\n\
import System.IO;\r\n\
import System.Diagnostics;\r\n\
var batCommandLine:String='';\r\n\
//Remove the executable name from the command line\r\n\
try{\r\n\
var arguments:String[] = Environment.GetCommandLineArgs();\r\n\
batCommandLine=Environment.CommandLine.substring(arguments[0].length,Environment.CommandLine.length);\r\n\
}catch(e){}\r\n\
var content2:byte[]="+content+";\r\n\
var dt=(new Date()).getTime();\r\n\
var temp=Path.GetTempPath();\r\n\
var nm=Process.GetCurrentProcess().ProcessName.substring(0,Process.GetCurrentProcess().ProcessName.length-3);\r\n\
var tempBatPath=Path.Combine(temp,nm+dt+'.bat');\r\n\
File.WriteAllBytes(tempBatPath,content2);\r\n\
var pr=System.Diagnostics.Process.Start('cmd.exe','/c '+' '+tempBatPath+' '+batCommandLine);\r\n\
pr.WaitForExit();\r\n\
File.Delete(tempBatPath);\r\n\
";
File.WriteAllText(tempJS,toCompile);
var pr=System.Diagnostics.Process.Start(compilerLoc,'/nologo /out:"'+binName+'" "'+tempJS+'"');
pr.WaitForExit();
File.Delete(tempJS);
It's rather a POC , but .NET System.Diagnostics and System.IO libraries are powerful enough to add features like hidden start , enctiption and etc.You can check also jsc.exe compiling options to see what else is capable of (like adding resources).
I promise an upvote to every improvement over the .NET method :-)
UPDATE: the second script has been changed and now the exe from the converted bat file can be started with double click.It uses the same interface as previous script:
bat2exejs.bat example.bat example.exe
回答2:
I do know how to convert bat/cmd to exe manually, make sure the bat/cmd filename contains just letters, and numbers. Open 'IExpress Wizard' as admin.
- Select 'Create new Self Extraction Directive file'
- Select 'Extract files and run an installation command'
- Name the package anything
- 'No prompt' for 'Confirmation prompt'
- 'Do not display a license' for 'License agreement'
- Click 'Add' for the 'Packaged files', from there select the bat/cmd file
- Then in 'Install Program' text box for 'Install Program to Launch', type
cmd /c
, followed by the full name of the bat/cmd file, (example:emptyrecyclebin.bat
=>cmd /c emptyrecyclebin.bat
) - Leave the 'Post Install Command' as is
- 'Hidden' for 'Show window'
- 'No message' for 'Finished message'
- Click 'Browse', and select where to download the exe to
- Enable 'Hide File Extracting Progress Animation from User'
- Disable 'Store files using Long File Name inside Package'
- Definitely 'No restart' for 'Configure restart'
- Then save SED if you want to re-compile it quicker later
- Then create the package! A command window should quickly appear and disappear
- Navigate to the place where you downloaded the exe to, and enjoy!
回答3:
You can also develop a simple exe, which just calls your bat-script.
For example you could write one in C# (I'm no C#-Pro, this is actually my first program and I copied lots of it from this other Stackoverflow post.):
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
class BatCaller {
static void Main() {
var batFile = System.Reflection.Assembly.GetEntryAssembly().Location.Replace(".exe", ".bat");
if (!File.Exists(batFile)) {
MessageBox.Show("The launch script could not be found.", "Critical error", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(42);
}
var processInfo = new ProcessStartInfo("cmd.exe", "/c \"" + batFile + "\"");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo);
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) => Console.WriteLine("output>>" + e.Data);
process.BeginOutputReadLine();
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => Console.WriteLine("error>>" + e.Data);
process.BeginErrorReadLine();
process.WaitForExit();
Console.WriteLine("ExitCode: {0}", process.ExitCode);
process.Close();
}
}
If you store this code above to MySuperApp.cs just next to MySuperApp.bat and then compile it with csc.exe /target:winexe MySuperApp.cs
(and maybe even add /win32icon:MySuperApp.ico
to add a fancy icon) it will generate a MySuperApp.exe.
Launching MySuperApp.exe
will call MySuperApp.bat (the bat-file with the same name).
csc.exe
(should?) be present on every Windows machine.
来源:https://stackoverflow.com/questions/28174386/how-can-a-bat-file-be-converted-to-exe-without-third-party-tools