Editing resources in existing .net assemblies.

Technorati Tags:

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:

  1. *.resources - The binary resource files.
  2. assembly_name.res - The resources manifest.
  3. 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:

  1. Open the .resources file you want to change and rename the original resource item.
  2. Hit Ctrl-F to insert file and select the modified version from you file system.
  3. 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.snk assembly_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.

Published in: on September 24, 2007 at 20:42 Comments (1)

See that line of code up there? I need it here as well.

It happens all the time, you’re writing a new code segment and see the line you’re about to write (or close enough version of it) a few lines above your cursor.

If you’re anything like me – you have your line numbers always displayed so you can tell which line it is that you’re after; you hit Ctrl·G, type in the line number, select the text once you get there and go back to the line you were editing using Ctrl·-, Ctrl·G or the arrow keys, depending on your mood; now you just need to paste the code back. A lot of time wasted here.

If you’re not so much like me – you’ll use the mouse and spend a little less time (in this specific case).

So, why spend time at all? Let’s automate baby!

Take this macro and paste it in your Macro explorer in VS2003 (might also work in VS2005 but I haven’t tested it just yet):

CopyLine macro for Visual Studio 2003 (and later?)
Sub CopyLine()
source = DTE.ActiveDocument.Selection.TopPoint.Line
line = InputBox("Line number to copy here:")
If (line <> "") Then
DTE.ActiveDocument.Selection.GotoLine(line)
DTE.ActiveDocument.Selection.EndOfLine(True)
DTE.ActiveDocument.Selection.Copy()
DTE.ActiveDocument.Selection.GotoLine(source)
DTE.ActiveDocument.Selection.Paste()
End If
End Sub

I assigned Ctrl·Alt·Shift·C to this macro so my line copying is just 2-4 clicks away (if your line numbers reach 4 digits or more, you have more serious problems…)

You can copy this to another macro and replace the Copy( ) command with Cut( ) so you’ll also have a line moving macro. :-)

Published in: on August 22, 2007 at 2:33 Leave a Comment