简单操作IL文件

IL文件修改入门篇

================================== 
Object: 
   掌握简单的IL文件处理知识 
   能够熟练运用ildasm,ilasm工具 
================================== 
1.编写简单的hello.cs

2.编译源代码
csc hello.cs

3.反编译hello.exe,命令如下: 
ildasm hello.exe /out=hello.il

4.打开hello.il文件,找到下面语句 
IL_0000:  ldstr      "Hello World!" 
修改为 
IL_0000:  ldstr      "Hello World! A Cracked Version." 
保存文件。

5.编译il文件 
ilasm /res:hello.res hello.il /out:hellocracked.exe 
--------------------------------------------------------

Microsoft (R) .NET Framework IL Assembler.  Version 1.1.4322.573 
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. 
Assembling 'hello.il' , no listing file, to EXE --> 'hellocracked.exe' 
Source file is ANSI

Assembled method HelloWorld::Main 
Assembled method HelloWorld::.ctor 
Creating PE file

Emitting members: 
Global 
Class 1 Methods: 2; 
Writing PE file 
Operation completed successfully

----------------------------------------------------------- 
成功编译。

5.运行hellocracked.exe,结果如下: 
Hello World! A Cracked Version.


OK。

IL文件修改提高篇 
================================== 
Object: 
   熟悉强名字签名之后的代码处理 
================================== 
1.修改hello.cs文件,加入强名字属性代码 
[assembly:AssemblyKeyFileAttribute("key.snk")] 
[assembly:AssemblyDelaySignAttribute(false)]

2.生成强名字对,这就是一个典型的RSA应用 
sn -k key.snk

3.编译hello.cs文件 
csc hello.cs

4.反编译hello.exe,命令如下: 
ildasm hello.exe /out=hello.il

5.打开hello.il文件,找到下面语句 
IL_0000:  ldstr      "Hello World!" 
修改为 
IL_0000:  ldstr      "Hello World! A Cracked Version." 
保存文件。

5.编译il文件 
ilasm /res:hello.res hello.il /out:hellocracked.exe 
--------------------------------------------------------

Microsoft (R) .NET Framework IL Assembler.  Version 1.1.4322.573 
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. 
Assembling 'hello.il' , no listing file, to EXE --> 'hellocracked.exe' 
Source file is ANSI

Assembled method HelloWorld::Main 
Assembled method HelloWorld::.ctor 
Creating PE file

Emitting members: 
Global 
Class 1 Methods: 2; 
Writing PE file 
Operation completed successfully

----------------------------------------------------------- 
成功编译。

5.运行hellocracked.exe,结果如下:

Unhandled Exception: System.IO.FileLoadException: Strong name validation failed 
for assembly 'hellocracked.exe'. 
File name: "hellocracked.exe"

出现错误,原因是因为签名的代码被修改了,这是在破解时通常会遇到的,下面来介绍如何纠正该错误。

[方法A] 
6.1.1、重新生成exe文件 
ilasm /res:hello.res hello.il /out:hellocracked_resign.exe

6.1.2、因为我们有RSA keypair,所以可以重新签名程序,但是在破解时,是不知道签名的RSA keypair的,而且根据RSA算法,破解的可能性几乎不可能的。 
sn -R hellocracked_resign.exe key.snk 
----------------------------------------------------------- 
Microsoft (R) .NET Framework 强名称实用工具版本 1.1.4322.573 
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

成功地对程序集“hellocracked_resign.exe”进行了重新签名 
----------------------------------------------------------- 
6.1.3、重新运行hellocracked_resign.exe,OK 
Hello World! A Cracked Version.

[方法B] 
6.2.1、删除IL文件中的如下内容,保存文件 
.publickey = (00 24 00 00 04 80 00 00 94 00 00 00 06 02 00 00   // .$.............. 
                00 24 00 00 52 53 41 31 00 04 00 00 01 00 01 00   // .$..RSA1........ 
                3B B2 D0 F9 DA 7E 55 B2 50 40 6B CF EB 20 F6 67   // ;....~U.P@k.. .g 
                E7 D6 AF 65 32 4F 6D 21 5D 91 53 0B 04 C7 E2 15   // ...e2Om!].S..... 
                F0 6A EE 38 F8 74 DB 22 34 F9 A1 B5 16 C1 04 66   // .j.8.t."4......f 
                B7 0B A8 36 49 9E 8A 71 E1 D1 26 AB A2 78 4E 3A   // ...6I..q..&..xN: 
                8B 71 8C 7F 4D 54 22 28 5F 1F 8D DE 6C 96 EC 22   // .q..MT"(_...l.." 
                34 8A 35 3F 95 0A F4 F4 7F B7 8C F5 5D F4 CB 54   // 4.5?........]..T 
                92 94 DD 5E D5 0D 20 12 7F B1 9B 15 7F 0E FB 2A   // ...^.. ........* 
                76 5F 45 3D 20 2C E2 6D FE 55 72 30 49 76 28 FE ) // v_E= ,.m.Ur0Iv(.

6.2.2 重新生成exe文件 
ilasm /res:hello.res hello.il /out:hellocracked_nosign.exe

6.2.3 重新运行hellocracked_nosign.exe,OK 
Hello World! A Cracked Version. 
因为删除了签名信息,所以代码仍然可以正常执行,就这是破解时通常所用的方法。

如果你能够看懂IL代码,基本上就可以做你想做的任何修改了。

原文地址:https://www.cnblogs.com/zhangchenliang/p/2656693.html