当前位置: 移动技术网 > IT编程>开发语言>c# > .net反编译原理

.net反编译原理

2019年12月04日  | 移动技术网IT编程  | 我要评论
本篇文章涉及到使用ILDasm和ILasm进行反编译和编译。 ...

目录



目录

nlog日志框架使用探究-1
nlog日志框架使用探究-2
科学使用log4view2

前言

本来没有想写反编译相关的文章,但是写着写着就扯到反编译破解了。更何况上一篇《科学使用log4view2》文章还被管理员移出了首页。

20191203150818.png

花了几个小时写的文章被移除首页的感受你们了解吗?终于凭借我的三寸不烂之舌终于打动管理大大恢复到了首页。

20191203154406.png

为了更加合规合法,因此有了该篇文章。

上一篇文章我们使用dnspy可以非常方便的反编译甚至可以直接修改il并生成新的程序集。本篇文章我们就来讲讲在dnspy出现之前,我们是如何修改程序集的。

ildasm

在没有dnspy这等强大的反编译工具之前,若需要修改程序集,一般都需要使用微软官方提供的ildasm反编译,ilasm编译il。

本篇文章的重点不是谈论il的语法,il如何阅读,想要了解这些,网上一搜一大把。推荐一篇吧:。

il拆解器是il汇编器(ilasm.exe)的配套工具。ildasm.exe采用包含中间语言(il)代码的可移植可执行(pe)文件,并创建适合作为ilasm.exe 输入的文本文件。

这是官方的解释,说白了就是它可以将.net程序集反编译为il语言文件,同时该文件可以被ilasm读取并处理(编译)。

ildasm的路径在c:\program files (x86)\microsoft sdks\windows\vxxa\bin下可以找到。

上一篇文章主要因为涉及到破解被移除了首页,虽然我死不承认,一直说我是在科学使用。

20191203154729.png

但是为了不惹麻烦,这次我学聪明了。我只会科学使用别人的软件,但是我会破解自己的软件。

为此我特意编写了一个非常牛逼的产品我称之为niubilityproduct。
它的功能是当输入参数为niubility时,它会展示这个产品有多牛逼。
20191203154033.png

一般人没办法看到它有多牛逼,为此我们需要破解它。

static void main(string[] args)
{
    string key;
    if (args.length == 1)
    {
        key = args[0];
    }
    else
    {
        key = console.readline();
    }

    if (key == "niubility")
    {
        console.writeline("this is a niubility product");
    }
    else
    {
        console.writeline("bye !");
    }
    console.readkey();
}

和dnspy一样,我们可以将dll或exe文件直接拖入到ildsam。
20191203155014.png

界面比较清爽简洁。双击main方法就能显示出il语言

20191203155133.png

但是ildsam不能编辑il,前面说了它可以将程序集反编译为il文件。在菜单栏中选择文件然后点击转储,将其保存包含il的il后缀的文件。

20191203155634.png
他会生成一个il后缀的文件以及一个res后缀的文件

  • il文件就是包含il语言的文件
  • res文件包含了托管资源的元数据的资源名称。

20191203155734.png

il文件可以使用文本打开

.class private auto ansi beforefieldinit niubilityproduct.program
       extends [mscorlib]system.object
{
.method private hidebysig static void  main(string[] args) cil managed
  {
    .entrypoint
    // 代码大小       77 (0x4d)
    .maxstack  2
    .locals init ([0] string key,
             [1] bool cs$4$0000)
    ...

    il_0016:  nop
    il_0017:  call       string [mscorlib]system.console::readline()
    il_001c:  stloc.0
    il_001d:  nop
    il_001e:  ldloc.0
    il_001f:  ldstr      "niubility"
    il_0024:  call       bool [mscorlib]system.string::op_equality(string,
                                                                   string)
    il_0029:  ldc.i4.0
    il_002a:  ceq
    il_002c:  stloc.1
    il_002d:  ldloc.1
    il_002e:  brtrue.s   il_003f

    il_0030:  nop
    il_0031:  ldstr      "this is a niubility product"
    il_0036:  call       void [mscorlib]system.console::writeline(string)
    il_003b:  nop
    il_003c:  nop
    il_003d:  br.s       il_004c

    il_003f:  nop
    il_0040:  ldstr      "bye !"
    il_0045:  call       void [mscorlib]system.console::writeline(string)
    il_004a:  nop
    il_004b:  nop
    il_004c:  ret
  } // end of method program::main

我们可以不管输入什么值, 无需判断直接调用if里面的指令,调用完后会跳转到il_004c

    il_0017:  call       string [mscorlib]system.console::readline()
    il_001c:  stloc.0
    il_001d:  nop
    il_001e:  nop
    il_001f:  nop
    il_0024:  nop
    il_0029:  nop
    il_002a:  nop
    il_002c:  nop
    il_002d:  nop
    il_002e:  nop
    il_0031:  ldstr      "this is a niubility product"
    il_0036:  call       void [mscorlib]system.console::writeline(string)
    il_003b:  nop
    il_003c:  nop
    il_003d:  br.s       il_004c

修改完成我们需要将il重新进行编译

ilasm

ilasm在win10下可以直接搜索到。或者在.net 4.0及以上在c:\windows\microsoft.net\framework64\v4.0.30319目录可以找到,.net 3.5在c:\windows\microsoft.net\framework64\v2.0.50727目录可以找到。
通过命令ilasm /exe /output=输出文件完整路径 /resource=资源路径 il文件路径

f:\study\demo\niubilityproduct>ilasm /exe /output=niubilityproduct.exe /resource=niubilityproduct.res niubilityproduct.il

microsoft (r) .net framework il assembler.  version 4.8.3752.0
copyright (c) microsoft corporation.  all rights reserved.
assembling 'niubilityproduct.il'  to exe --> 'niubilityproduct.exe'
source file is utf-8

assembled method niubilityproduct.program::main
assembled method niubilityproduct.program::.ctor
creating pe file

emitting classes:
class 1:        niubilityproduct.program

emitting fields and methods:
global
class 1 methods: 2;

emitting events and properties:
global
class 1
writing pe file
operation completed successfully

f:\study\demo\niubilityproduct>

现在调用随便输入什么都能看到这个产品的牛逼之处

20191203175400.png

20191203154040.png

结语

本篇文章涉及到使用ildasm和ilasm进行反编译和编译。掌握了本篇的技能之后你会发现...还是dnspy好用

推荐文献

  1. 读懂il代码就这么简单 (一)
  2. 初识ildasm.exe——il反编译的实用工具
  3. c# il dasm 使用

20191127212134.png
微信扫一扫二维码关注订阅号杰哥技术分享
出处:https://www.cnblogs.com/jack-blog/p/11978817.html
作者:杰哥很忙
本文使用「cc by 4.0」创作共享协议。欢迎转载,请在明显位置给出出处及链接。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网