Friday, October 21, 2011
Editing resources in existing .net assemblies.
Preface
Editing binary resources when you have the source code is pretty easy. Just change the original file and maybe change some properties in the project to re-embed the modified resource. Piece of cake.
But what if you don’t have the source code? Or maybe you do but the build process is too lengthy/fragile/complicated/error-prone/etc. ? Another good reason is obfuscation with a changing/random map file, which means you can’t rebuild a single assembly and expect it to interact with the previously built assemblies.
Here’s how to modify a binary resource in 3 easy steps.
Step #1: Disassemble the assembly.
Use the following command to disassemble your .net exe or dll:
ildasm /out=assembly_name.il assembly_name.dll
(replace assembly_name.dll with your dll or exe name).
This will output plenty of data files. We are interested in these:
- *.resources - The binary resource files.
- assembly_name.res - The resources manifest.
- assembly_name.il - The actual MSIL code.
Step #2: Edit the resources file(s).
Prepare the modified/new resource, it doesn’t matter what name it is but the format should be the same as the one originally included in the assembly (e.g. replacing bitmap for bitmap, icon for icon).
Use a resource editor to edit the resource file. I use Resourcer for DotNet by Lutz Roeder, it’s freeware. By the way, the same author also wrote Reflector.NET, an awesome tool for .NET developers.
If you’re using the Resourcer, here are the steps required:
- Open the .resources file you want to change and rename the original resource item.
- Hit Ctrl-F to insert file and select the modified version from you file system.
- Rename the new item to have the same original name as the previous one (you can’t you copy/paste for that, for some reason it just copies the entire item instead of just the text).
Step #3: Reassemble the assembly.
Use the following command to disassemble your .net exe or dll:
Important: This will overwrite the original dll/exe !
ilasm /resource=assembly_name.res /dll /output=assembly_name.dll /key=signing_key.snkassembly_name.il
(replace assembly_name.dll with your dll or exe name and of course change the /dll to /exe if needed).
The signing key parameter is optional and is only needed if the original assembly was signed.
Of course, if it was signed by someone else to prevent tempering, you won’t be able to use the modified assembly unless you provide the original signing key, which I assume you won’t have. But that’s just how .NET works.