Update: The original developer noticed my IL level hacks and made them official in the next Monochrome version (and current private beta), with a configurable and hidden blacklist. Thanks to Piotr Pawlowski (foobar2000/ex official Winamp plugin dev) for this!
The following tutorial shows the very basics of .NET hacking.
This time, a practical real world application is used: modifying IL to self-censor content that is said and received on IRC. This was done by me to help eradicate possible sources of drama in my life (thus saving myself from an early death). Plus it gives people a possible relief from my ranting.
This tutorial will go through the basics of editing a .NET executable on a IL level using code decompilation/recompilation.
First of all, acquire dnSpy. It is a excellent debugger for .NET with extra capabilities for recompilation of CLR code.
The target used is Monochrome : I am well aware its GPL, but its good for a tutorial such as this, plus where is the fun in just recompiling the app?
Secondly, load up Monochrome in dnSpy:
You can see the various namespaces in use by Monochrome, thankfully its not protected. Various .NET based protections will scramble code making it harder to debug such applications, just like Denuvo and VMProtect with native C/C++ apps.
Next will take some time, to find some code that relates to what we want: basically we want to just limit what we can say and what we can receive. I did some looking around and funnily enough there is a C# class called “InputBox” in the code…
Suspiciously, there is some methods in there that look, well, suspicious :). sendinput() looks like a dead giveaway for something, so lets find out….
Press “Start” and click “OK” to start the debugging process.
To debug a class method, simply double click on the method. You can select breakpoints by double clicking the code line in the decompile.
To check if this method is responsible, I just typed into monochrome’s inputbox “test test test”. Instantly, the breakpoint is hit and….
We found our line. Now lets try editing the code of Monochrome so that it filters out what we type according to a filter list. Right click the code listing in dnSpy and click “Edit Class (C#)”
Here we see a decompiled listing of the code in the InputBox class. From here we can do edits to the source code.
Here I added a line so we can use the regular expression namespace and classes in .NET, which could be useful for text filtering.
In the sendInput method, I did some extra code which will be appended. The code basically looks for any mention of keywords in a list that are in the entered text. If they exist, they are rejected, otherwise, the sentences are rejected. Then click “Compile” to recompile the code. You may need to clean up any code errors the recompiler picks up, which are listed in the log.
To test our code injection, we save the module by going to File -> “Save Module”.
Then we just run the debugging process again, with the patched executable to test.
As you can see, the new code is in the executable, now time to test…..
No matter how hard I try, I cannot enter anything with the word “byuu” in it. It is not registered in the log, and thus, never sent.
Next is finding the code responsible for recieving entries in the channel view, ironically there is a class called “ChannelView” with the “addline” entries.
Which seems to fit the format of the text in the main log box shown earlier. Now is just a matter of adding code to filter lines based on content.
To make the job easier, just right click the code and click “Edit Method (C#)”. This will allow editing of just the code relevant for that particular function.
Make your alterations and then click “Compile”. Don’t forget to save the module again to save your patched code changes.
One thing I wasn’t sure of, is whether dnSpy allows realtime code editing and then rerunning of recompiled code. Feel free to correct me if it can, which will make imo, a great .NET assembly editor even better.