16 December, 2011

Using ILMerge and Eazfuscator.NET

I ran into some new requirements on my last project.
I've made an autoupdate system which will be used for an upcoming project, and i wanted to merge all the dll's into the autoupdate executable.
Just to minimize trouble and communication when downloading the autoupdater.
I also wanted to obfuscate my executable.

Merge Assemblies:

Now first i stumbled upon ILMerge, which is used to merge assemblies.
This is a tool release by Microsoft so it should be safe to use, you can read more about it here:

It's pretty simple to use:

ILMerge.exe /out:MyMergedExe.exe MyOriginalExe.exe Myfirst.dll MySecond.dll
First, all my projects use framework version 4, so we need to add some parameters so that ILMerge will be able
to merge a framework version 4.0 project.

ILMerge.exe /targetplatform:v4,"C:\Windows\Microsoft.NET\Framework\v4.0.30319" /out:MyMergedExe.exe MyOriginalExe.exe Myfirst.dll MySecond.dll
In the ILMerge folder (C:\Program Files (x86)\Microsoft\ILMerge\), create a file named ILMerge.exe.config:
<?xml version ="1.0"?>
 <startup useLegacyV2RuntimeActivationPolicy="true">
  <requiredRuntime safemode="true" imageVersion="v4.0.30319" version="v4.0.30319"/>
Great, now everything runs and works with .NET Framework v4.0.

Then i wanted a more dynamic way to include the DLL's without editing the ILMerge command line every time.
Luckily there is a parameter named /wildcards

ILMerge.exe /wildcards /targetplatform:v4,"C:\Windows\Microsoft.NET\Framework\v4.0.30319" /out:MyMergedExe.exe MyOriginalExe.exe *.dll
And to get some more information about the merge process, add the /log parameter.

That's basically everything you need to know about merging assemblies into a single executable.

Obfuscate Executable:

So let's see how we can obfuscate the executable.
There is a lot of obfuscators out there.
I tried 3-4 variants, and found a lot of buggy obfuscators out there. Some crashed, some did not work, some costs a lot of $.
Anyway i ended up with the free EazFuscator.NET. It works, and has a nice commandline interface.
You can read more about it here:

Eazfuscator.NET.exe -s -o MyObfuscatedExe.exe MyMergedExe.exe

I also added -s to get some statistics about the merge process.
You dont need to use the GUI application that comes with Eazfuscator.NET.
We will use the commandline interface only.

Automate, using PostBuild event:

Now, let's automate all this information into a complete build:

In your project, add this to the postbuild event:

if $(ConfigurationName) == Release $(ProjectDir)\Build\PostBuild.cmd "$(TargetDir)", "$(TargetName)"

Create a folder in your project and name it Build.
Inside the build folder, create a file named Postbuild.cmd with this content:

REM Add this to the project Postbuild event: 
REM if $(ConfigurationName) == Release $(ProjectDir)\Build\PostBuild.cmd "$(TargetDir)", "$(TargetName)"
ECHO --------------------------------------------------------------------------------------------------------------------------
ECHO Executing PostBuild.cmd with parameters: TargetDir=%1, TargetName=%2
CD %1
ECHO Merging Assemblies Into Executable: %2
IF NOT EXIST %1MergedRelease mkdir %1MergedRelease
"C:\Program Files (x86)\Microsoft\ILMerge\ILMerge.exe" /wildcards /log /targetplatform:v4,"C:\Windows\Microsoft.NET\Framework\v4.0.30319" /out:MergedRelease/%2.exe %2.exe *.dll
ECHO Obfuscating Executable: %2
IF NOT EXIST %1MergedRelease\ObfuscatedRelease mkdir %1MergedRelease\ObfuscatedRelease
"C:\Program Files (x86)\Eazfuscator.NET\Eazfuscator.NET.exe" -s -o MergedRelease/ObfuscatedRelease/%2.exe MergedRelease/%2.exe
ECHO --------------------------------------------------------------------------------------------------------------------------

Now when building a release build:
The normal exe with separate dll's will be created under

Under this folder there will be a subfolder named MergedRelease:

In this folder our merged exe with the pdb file will be created.

Under this folder there will be a subfolder named ObfuscatedRelease:

In this folder our merged and obfuscated exe will be created.

Now thats all, a merged and obfuscated exe everytime you do a release build.

No comments:

Post a Comment