一、ILSpy
这是一个很好用的.net反编译工具,可以把c#的dll或者exe的代码直接读出来。
比如这里有个小demo,做了两个Form。
Form1的代码非常简单:
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "password")
{
this.Hide();
Form2 f = new Form2();
f.Show();
}
}
即:当别人输入的字符为password时,才能进入Form2。
如果我们拿到已发布的程序,并且不知道密码,就可以打开ILSpy,把crack_test.exe这个文件直接拖进去。
二、修改
看到了代码逻辑,那么进去就不是难事。现在又有新的问题:每次进去都好麻烦,我想要修改程序逻辑,直接跳过验证密码的那段逻辑。这时候该怎么做呢?
三个步骤:
1、把il代码dump出来
使用ildasm.exe工具打开我们需要修改的exe或者dll
点击File->dump
选择ok
这样就能把当前exe或者dll的资源和il代码搞出来了。
其中1.il是我在保存时自定义的名称。
2.修改il代码
打开1.il文件
搜索button1_Click这个函数名,定位到代码:
.method private hidebysig instance void
button1_Click(object sender,
class [mscorlib]System.EventArgs e) cil managed
{
// Code size 49 (0x31)
.maxstack 2
.locals init ([0] bool V_0,
[1] class crack_test.Form2 f)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldfld class [System.Windows.Forms]System.Windows.Forms.TextBox crack_test.Form1::textBox1
IL_0007: callvirt instance string [System.Windows.Forms]System.Windows.Forms.Control::get_Text()
IL_000c: ldstr "password"
IL_0011: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_0016: stloc.0
IL_0017: ldloc.0
IL_0018: brfalse.s IL_0030
IL_001a: nop
IL_001b: ldarg.0
IL_001c: call instance void [System.Windows.Forms]System.Windows.Forms.Control::Hide()
IL_0021: nop
IL_0022: newobj instance void crack_test.Form2::.ctor()
IL_0027: stloc.1
IL_0028: ldloc.1
IL_0029: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::Show()
IL_002e: nop
IL_002f: nop
IL_0030: ret
} // end of method Form1::button1_Click
注意看这里,当判断为false时,跳转到最后一行。我们把它修改成继续往下执行:
这是修改后的:
至此,保存文件。
3、重新编译il代码
打开cmd,切换到ilasm.exe所在的目录(根据自己机器环境来)。我的是这样的:
输入命令:
ilasm /exe C:\crack_test\crack_test\bin\Debug\crack\1.il /resource=C:\crack_test\crack_test\bin\Debug\crack\1.res
像这样:
其中/exe参数表示生成目标是exe,如果我们要生成dll,那么这里就填写dll。后面两个参数分别是.il文件和.res文件的位置。
执行后得到如下结果:
此时1.il同级目录下多了一个1.exe文件。
执行1.exe,发现不需要输入password即可打开Form2。
注:
1、文中用的工具和工程将分享到如下链接。
2、程序在关闭Form2后不会退出,懒得改了,手动结束进程即可。
3、该方法仅适用于未做混淆的.net程序。
附件下载
http://huhinghao123.eicp.net//n/81ba75dc6b94c606aa45def7e581a31b