Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying Existing .NET Assemblies

Is there a way to modify existing .NET assemblies without resorting to 3rd party tools? I know that PostSharp makes this possible but I find it incredibly wasteful that the developler of PostSharp basically had to rewrite the functionality of the whole System.Reflection namespace in order to make existing assemblies modifiable.

System.Reflection.Emit only allows the creation of new, dynamic assemblies. However, all the builder classes used here inherit from the basic reflection classes (e.g. TypeBuilder inherits from System.Type). Unfortunately, there doesn't seem to be a way to coerce an existing, dynamically loaded type into a type builder. At least, there's no official, supported way.

So what about unsupported? Does anyone know of backdoors that allow to load existing assemblies or types into such builder classes?

Mind, I am not searching for ways to modify the current assembly (this may even be an unreasonable request) but just to modify existing assemblies loaded from disc. I fear there's no such thing but I'd like to ask anyway.

In the worst case, one would have to resort to ildasm.exe to disassemble the code and then to ilasm.exe for reassembly but there's no toolchain (read: IL reader) contained in .NET to work with IL data (or is there?).

/EDIT:

I've got no specific use case. I'm just interested in a general-purpose solution because patching existing assemblies is a quite common task. Take obfuscators for example, or profilers, or AOP libraries (yes, the latter can be implemented differently). As I've said, it seems incredibly wasteful to be forced to rewrite large parts of the already existing infrastructure in System.Reflection.


@Wedge:

You're right. However, there's no specific use case here. I've modified the original question to reflect this. My interest was sparked by another question where the asker wanted to know how he could inject the instructions pop and ret at the end of every method in order to keep Lutz Roeder's Reflector from reengineering the (VB or C#) source code.

Now, this scenario can be realized with a number of tools, e.g. PostSharp mentioned above and the Reflexil plugin for Reflector, that, in turn, uses the Cecil library.

All in all, I'm just not satisfied with the .NET framework.

@Joel:

Yes, I'm aware of this limitation. Thanks anyway for pointing it out, since it's important.

@marxidad:

This seems like the only feasible approach. However, this would mean that you'd still have to recreate the complete assembly using the builder classes, right? I.e. you'd have to walk over the whole assembly manually.

Hmm, I'll look into that.

like image 674
Konrad Rudolph Avatar asked Sep 23 '08 20:09

Konrad Rudolph


People also ask

Where are .NET assemblies stored?

An assembly can be found mostly in either of the following places: GAC - C:\Windows\Assembly\GAC (Microsoft provided and by third-party in some cases) Installation folder (most of the third-party controls)

What are the different types of .NET assemblies?

In the Microsoft . NET framework, an assembly is a partially compiled code library for use in deployment, versioning and security. There are two types: process assemblies (EXE) and library assemblies (DLL). A process assembly represents a process which will use classes defined in library assemblies. .

What does a .NET assembly contain?

NET assemblies contain the definition of types, versioning information for the type, meta-data, and manifest. The designers of . NET have worked a lot on the component (assembly) resolution. An assembly can be a single file or it may consist of the multiple files.

What is assembly in .NET with example?

An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (. dll) files, and are the building blocks of . NET applications.


1 Answers

Mono.Cecil also allows you to remove the strong name from a given assembly and save it back as an unsigned assembly. Once you remove the strong name from the assembly, you can just modify the IL of your target method and use the assembly as you would any other assembly. Here's the link for removing the strong name with Cecil:

http://groups.google.com/group/mono-cecil/browse_thread/thread/3cc4ac0038c99380/b8ee62b03b56715d?lnk=gst&q=strong+named#b8ee62b03b56715d

Once you've removed the strong name, you can pretty much do whatever you want with the assembly. Enjoy!

like image 100
plaureano Avatar answered Nov 05 '22 15:11

plaureano