.Net平台程序hack。

.Net/Java平台比传统Win32程序更容易Hack,因为它们都编译成了中间语言。今天小试一把.Net平台exe程序hack。

步骤:

1.了解ILASM和ILDASM.

这两个程序分别是将IL文件重新打包成exe和将用.Net平台开发出的exe程序翻译成IL语言的工具,可在下面的文件夹中找到:%systemRoot%\Microsoft.NET\Framework\版本号

(1)编写一个小的exe:

using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpHacker
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Console.WriteLine(
"Hello world!");
            Console.ReadLine();
        }
    }
}

(2).将exe程序反编译成IL文件

ildasm的使用方法:

C:\ildasm>ildasm /?
用法: ildasm [options] <file_name> [options]

用于输出重定向的选项:
  /OUT=<file name>    直接输出到文件而不是 GUI。
  /TEXT               直接输出到控制台窗口而不是 GUI。

  /HTML               以 HTML 格式输出(仅在与 /OUT 选项一起使用时有效)。
  /RTF                以多格式文本格式输出(仅在与 /TEXT 选项一起使用时有效)。
用于 GUI 或文件/控制台输出的选项(仅限于 EXE 和 DLL 文件):
  /BYTES              将实际字节(十六进制)显示为指令注释。
  /RAWEH              以原始形式显示异常处理子句。
  /TOKENS             显示类和成员的元数据标记。
  /SOURCE             将原始源行显示为注释。
  /LINENUM            包括对原始源行的引用。
  /VISIBILITY=<vis>[+<vis>...]    仅反汇编具有指定
          可见性的项。(<vis> = PUB | PRI | FAM | ASM | FAA | FOA | PSC)
  /PUBONLY            仅反汇编公共项(与 /VIS=PUB 相同)。
  /QUOTEALLNAMES      将所有名称用单引号括起来。
  /NOCA               取消输出自定义属性。
  /CAVERBAL           以文字形式输出 CA blob (默认情况下为二进制形式)。
  /NOBAR              取消反汇编进度栏窗口弹出。

下列选项只对文件/控制台输出有效:
用于 EXE 和 DLL 文件的选项:
  /UTF8               对输出使用 UTF-8 编码(默认为 ANSI)。
  /UNICODE            对输出使用 UNICODE 编码。
  /NOIL               取消 IL 汇编程序代码输出。
  /FORWARD            使用前向类声明。
  /TYPELIST           输出完整的类型列表(以在往返过程中保留类型排序)。
  /HEADERS            将文件头信息包括在输出中。
  /ITEM=<class>[::<method>[(<sig>)]  只反汇编指定的项

  /STATS              包括图像的统计信息。
  /CLASSLIST          包括在模块中定义的类的列表。
  /ALL                /HEADER、/BYTES、/STATS、/CLASSLIST、/TOKENS 的组合

用于 EXE、DLL、OBJ 和 LIB 文件的选项:
  /METADATA[=<specifier>] 显示元数据,其中 <specifier> 为:
          MDHEADER    显示元数据头信息和大小。
          HEX         显示以十六进制表示的更多内容以及字。
          CSV         显示记录计数和堆大小。
          UNREX       显示无法解析的外部对象。
          SCHEMA      显示元数据头和架构信息。
          RAW         显示原始元数据表。
          HEAPS       显示原始堆。
          VALIDATE    验证元数据的一致性。
仅用于 LIB 文件的选项:
  /OBJECTFILE=<obj_file_name> 显示库中单个对象文件的元数据

选项的关键字为“-”或“/”,通过前三个字符识别选项

示例: ildasm /tok /byt myfile.exe /out=myfile.il

将EXE反编译成IL代码:
C:\ildasm>ildasm c:\ildasm\CSharpHacker\CSharpHacker.exe /OUT=CSharpHacker.il

C:\ildasm>

(3)打开.IL文件

文件如下:

//  Microsoft (R) .NET Framework IL Disassembler.  Version 2.0.50727.42
//
  Copyright (c) Microsoft Corporation.  All rights reserved.

 

// Metadata version: v2.0.50727
.assembly extern mscorlib
{
  
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 2:0:0:0
}
.assembly CSharpHacker
{
  
.custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 )             // ...1.0.0.0..
  .custom instance void [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 65 38 62 37 33 31 32 31 2D 36 35 32 33   // ..$e8b73121-6523
                                                                                                  2D 34 32 37 34 2D 61 36 34 39 2D 31 64 64 66 32   // -4274-a649-1ddf2
                                                                                                  39 65 66 32 66 30 35 00 00 )                      // 9ef2f05..
  .custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 )
  
.custom instance void [mscorlib]System.Reflection.AssemblyTrademarkAttribute::.ctor(string) = ( 01 00 00 00 00 )
  
.custom instance void [mscorlib]System.Reflection.AssemblyCopyrightAttribute::.ctor(string) = ( 01 00 19 E7 89 88 E6 9D 83 E6 89 80 E6 9C 89 20   // ...............
                                                                                                  28 43 29 20 48 5A 58 20 32 30 30 39 00 00 )       // (C) HZX 2009..
  .custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 0C 43 53 68 61 72 70 48 61 63 6B 65 72 00   // ...CSharpHacker.
                                                                                                00 )
  
.custom instance void [mscorlib]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 03 48 5A 58 00 00 )                         // ...HZX..
  .custom instance void [mscorlib]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 00 00 00 )
  
.custom instance void [mscorlib]System.Reflection.AssemblyDescriptionAttribute::.ctor(string) = ( 01 00 00 00 00 )
  
.custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 0C 43 53 68 61 72 70 48 61 63 6B 65 72 00   // ...CSharpHacker.
                                                                                              00 )

  
// --- 下列自定义属性会自动添加,不要取消注释 -------
  //  .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 )

  
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
  
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                             63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
  .hash algorithm 0x00008004
  
.ver 1:0:0:0
}
.module CSharpHacker.exe
// MVID: {A2C94774-A2FC-4F9E-B11D-41892A6C46F6}
.imagebase 0x00400000
.file alignment 0x00001000
.stackreserve 
0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
//
 Image base: 0x02FD0000


// =============== CLASS MEMBERS DECLARATION ===================

.class private auto ansi beforefieldinit CSharpHacker.Program
       
extends [mscorlib]System.Object
{
  
.method private hidebysig static void  Main(string[] args) cil managed
  {
    
.entrypoint
    
// 代码大小       19 (0x13)
    .maxstack  8
    
IL_0000:  nop
    
IL_0001:  ldstr      "Hello world!"
    
IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    
IL_000b:  nop
    
IL_000c:  call       string [mscorlib]System.Console::ReadLine()
    
IL_0011:  pop
    
IL_0012:  ret
  } 
// end of method Program::Main

  
.method public hidebysig specialname rtspecialname
          
instance void  .ctor() cil managed
  {
    
// 代码大小       7 (0x7)
    .maxstack  8
    
IL_0000:  ldarg.0
    
IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    
IL_0006:  ret
  } 
// end of method Program::.ctor

// end of class CSharpHacker.Program


// =============================================================

// *********** 反汇编完成 ***********************
//
 警告: 创建了 Win32 资源文件 CSharpHacker.res

(4)找到Hello world,修改之


 .method private hidebysig static void  Main(string[] args) cil managed
  {
    
.entrypoint
    
// 代码大小       19 (0x13)
    .maxstack  8
    
IL_0000:  nop
    
IL_0001:  ldstr      "Hello world!And hacked by sixi!"
    
IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    
IL_000b:  nop
    
IL_000c:  call       string [mscorlib]System.Console::ReadLine()
    
IL_0011:  pop
    
IL_0012:  ret
  } 
// end of method Program::Main

(5)重新打包:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>ilasm /?

Microsoft (R) .NET Framework IL Assembler.  Version 2.0.50727.42
Copyright (c) Microsoft Corporation.  All rights reserved.

Usage: ilasm [Options] <sourcefile> [Options]

Options:
/NOLOGO         Don't type the logo
/QUIET          Don't report assembly progress
/NOAUTOINHERIT  Disable inheriting from System.Object by default
/DLL            Compile to .dll
/EXE            Compile to .exe (default)
/PDB            Create the PDB file without enabling debug info tracking
/DEBUG          Disable JIT optimization, create PDB file, use sequence points f
rom PDB
/DEBUG=IMPL     Disable JIT optimization, create PDB file, use implicit sequence
 points
/DEBUG=OPT      Enable JIT optimization, create PDB file, use implicit sequence
points
/OPTIMIZE       Optimize long instructions to short
/FOLD           Fold the identical method bodies into one
/CLOCK          Measure and report compilation times
/RESOURCE=<res_file>    Link the specified resource file (*.res)
                        into resulting .exe or .dll
/OUTPUT=<targetfile>    Compile to file with specified name
                        (user must provide extension, if any)
/KEY=<keyfile>      Compile with strong signature
                        (<keyfile> contains private key)
/KEY=@<keysource>   Compile with strong signature
                        (<keysource> is the private key source name)
/INCLUDE=<path>     Set path to search for #include'd files
/SUBSYSTEM=<int>    Set Subsystem value in the NT Optional header
/FLAGS=<int>        Set CLR ImageFlags value in the CLR header
/ALIGNMENT=<int>    Set FileAlignment value in the NT Optional header
/BASE=<int>     Set ImageBase value in the NT Optional header (max 2GB for 32-bi
t images)
/STACK=<int>    Set SizeOfStackReserve value in the NT Optional header
/MDV=<version_string>   Set Metadata version string
/MSV=<int>.<int>   Set Metadata stream version (<major>.<minor>)
/PE64           Create a 64bit image (PE32+)
/NOCORSTUB      Suppress generation of CORExeMain stub
/STRIPRELOC     Indicate that no base relocations are needed
/ITANIUM        Target processor: Intel Itanium
/X64            Target processor: 64bit AMD processor
/ENC=<file>     Create Edit-and-Continue deltas from specified source file

Key may be '-' or '/'
Options are recognized by first 3 characters
Default source file extension is .il

Target defaults:
/PE64      => /PE64 /ITANIUM
/ITANIUM   => /PE64 /ITANIUM
/X64       => /PE64 /X64


C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>ilasm /exe c:\ildasm\CSharpHacker.
il /output=c:\ildasm\cSharpHacker\CSharpHacker2.exe

Microsoft (R) .NET Framework IL Assembler.  Version 2.0.50727.42
Copyright (c) Microsoft Corporation.  All rights reserved.
Assembling 'c:\ildasm\CSharpHacker.il'  to EXE --> 'c:\ildasm\cSharpHacker\CShar
pHacker2.exe'
Source file is ANSI

Assembled method CSharpHacker.Program::Main
Assembled method CSharpHacker.Program::.ctor
Creating PE file

Emitting classes:
Class 1:        CSharpHacker.Program

Emitting fields and methods:
Global
Class 1 Methods: 2;

Emitting events and properties:
Global
Class 1
Writing PE file
Operation completed successfully

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>

(6)整个工作结束。

.Net平台的exe文件hack比Win32软件反汇编后jump要容易的多。很多.Net平台的第三方插件会提供免费版,按照此思路。。。

备注:此文章仅用于个人技术研究,由此造成的法律问题,本人概不负责。

////////////////////////////////
////////Sixi. Let it be.../////
//////////////////////////////

原文地址:https://www.cnblogs.com/sixiweb/p/1985811.html