Learning how to solve the most common issue with obfuscation, using the #1 obfuscator tool in the market.
- Run a simple desktop application (works fine :)),
- Obfuscate the application,
- Run the obfuscated application (throws an exception :(),
- Reverse-engineer the application (helps understanding the real cause of the runtime exception),
- Fix the issue (re-obfuscate the .exe and verify everything is working as expected).
Could be installed within Visual Studio as well,
- VS2022 -> Extensions -> Manage Extensions -> Online -> search for "Dotfuscator Community" -> install (restart Visual Studio).
-
Build/Run the application in release mode (or via CLI),
- Using Visual Studio
- Using CLI
dotnet build -c release
-
Obfuscation
-
Using Visual Studio
-
Open Dotfuscator
- VS2022 -> Tools -> PreEmptive Protection - Dotfuscator Community.
-
File -> Open -> browse to
obfuscation-room\dotfuscator-settings.xml
-> Click Open. -
Click Build
-
You should get something like this
-
-
Using CLI
-
Find
dotfuscator.exe
, (should be at[VS Install Dir]\Common7\IDE\Extensions\PreEmptiveSolutions\DotfuscatorCE
folder),- Or, open dotfuscator GUI, open Task Manager, search for
dotfuscatorUI.exe
, right click, open file location.
- Or, open dotfuscator GUI, open Task Manager, search for
-
Add the path of the folder to PATH environmental variables,
-
Restart the CLI (ex. powershell 7) and run the following command
dotfuscator .\obfuscation-room\dotfuscator-settings.xml
- You should get something like this
-
-
-
The obfuscated application will be generated at this location
obfuscation-room\obfuscated\obfuscation-demo.exe
. -
Run the application, click on any button, exception occurs :(
Let's decompile the obfuscated application using popular ILSpy.
I've shipped ILSpy binaries with this project (Locate it at .\obfuscation-room\ILSpy_binaries_8.2.0.7535-x64.zip
).
-
Extract the content of the .zip file then run
ILSpy.exe
, -
File -> Open -> navigate to
\obfuscation-demo\obfuscation-room\obfuscated\obfuscation-demo.exe
-> Open, -
You should get something like this
See, mainwindow.baml
, it is the obfuscated version of MainWindow.xaml
. Notice how the names of <RepeatButton/>
s are changed
LeftButton
changed toa
,RightButton
changed toc
,RepeatButton_OnClick
event handler is renamed toa
Classes a
and b
are the obfuscated versions of App
and MainWindow
classes.
Look at the obfuscated version of RepeatButton_OnClick
Got it?
Strings in C# are not renamed (LeftButton
and RightButton
), whereas changed in XAML (a
and c
). Therefore -at runtime- the code flows to throw new ArgumentOutOfRangeException();
block, hence the exception occurs..
- Open the
dotfuscator-settings.xml
in dotfuscator GUI ->Renaming
->Exclusions
Exclude the buttons from being renamed.
- Click Build
P.S. Compare the stats between the old build and the new one (after the fix),
- Before (Fields Renamed: 8 of 8 (100.00%))
[Build Output] Types Renamed: 7 of 7 (100.00%)
[Build Output] Methods Renamed: 5 of 13 (38.46%)
[Build Output] Fields Renamed: 8 of 8 (100.00%)
- After (Fields Renamed: 6 of 8 (75.00%))
Types Renamed: 7 of 7 (100.00%)
Methods Renamed: 5 of 13 (38.46%)
Fields Renamed: 6 of 8 (75.00%)
You can also compare the old mainwindow.baml
with the new one (see how buttons' names left untouched, LeftButton
and RightButton
).
- Run the obfuscated application, click on any button, all work.
DONE.