http://hi.baidu.com/expertsearch/blog/item/3763489498ffe9027bf48092.html
本文目的是回答一個朋友關于修改程式集的留言,都是比較簡單的修改方式,不涉及脫殼等。
1: 利用ILASM和ILDASM
我們先建立如下測試程式:
namespace Test
{
class Program
{
static void Main(string[] args)
{
string s = "hello world!";
Console.WriteLine(s);
Console.ReadKey();
}
}
}
編譯之後得到Test.exe
打開Visual studio提供的指令行工具,它已經預設的設定好了相關的環境變量,輸入如下指令:
ildasm test.exe /OUT=test.il
将得到兩個檔案:test.il和test.res,用記事本打開test.il,将看到如下IL代碼:
IL_0000: ldstr "hello world!"
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: call void [mscorlib]System.Console::WriteLine(string)
IL_000c: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
IL_0011: pop
IL_0012: ret
我們将第一行修改為:
IL_0000: ldstr "hello world! --has been modified"
當然也可以增加一些函數的調用和删除我們不想要的函數,如果對IL不熟悉,可以先在VS中寫好想要的代碼,反編譯為IL,然後直接拷貝過來。修改完後,點儲存,在指令行運作如下指令:
ilasm /OUT=test2.exe test.il
如果文法沒有錯誤,将看到如下提示
Source file is ANSI
Assembled method Test.Program::Ma
Assembled method Test.Program::.c
Creating PE file
Emitting classes:
Class 1: Test.Program
Emitting fields and methods:
Global
Class 1 Methods: 2;
Emitting events and properties:
Global
Class 1
Writing PE file
Operation completed successfully
證明已經修改成功,我們可以直接運作test2.exe,發現程式已經按我們修改的輸出了:
hello world! -- has been modified.
2.利用Reflector的插件Reflexil
直接用IL修改比較麻煩,不小心很容易出錯,幸好我們有Jb Evain編寫的Reflexil。
Reflexil基于Mono.Cecil,是一個強大的程式集編輯器。
下載下傳完後打開Reflector --> View --> Add-Ins --> Add --> 選擇Reflexil.dll,
以後就可以直接用Reflector的Tools打開了。
開始正式的修改,用Reflector打開test.exe,打開Reflexil,選擇Main函數,可以發現IL代碼顯示在下方了,可以點選右鍵Edit,Delete,Create等操作,沒錯,你還發現了Replace all with code,通過這個可以直接用C#代碼直接修改。其它的功能多試試就明白了,另外點選類的時候,還可以修改類的通路權限等,比如将private改成public。我們選中第0行,直接選擇Edit,将Operand後面的文本修改為:hello world! -- modified by reflexil.點選Update,在Reflector中選中Test.exe module,Save as test3.exe。執行test3.exe,就看到我們的修改生效了。