Modify. NET program or DLL several ways

Source: Internet
Author: User
Tags mscorlib reflector
Http://hi.baidu.com/expertsearch/blog/item/3763489498ffe9027bf48092.html

The purpose of this article is to answer a friend about the changes to the assembly of the message, are relatively simple to modify the way, does not involve shelling and so on.

1: Using ILAsm and ILDASM

We first set up the following test procedures:
Namespace Test
{
Class Program
{
static void Main (string[] args)
{
string s = "Hello world!";
Console.WriteLine (s);
Console.readkey ();
}
}
}

Get Test.exe after compiling
Open the command-line tool provided by Visual Studio, which has already set up the relevant environment variables by default, and enter the following command:
ILDASM test.exe/out=test.il
Will get two files: test.il and test.res, open test.il with Notepad, and you will see the following IL code:
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
We'll change the first line to:
Il_0000:ldstr "Hello world! --has been Modified "
Of course, you can add some functions to the call and delete the function we do not want, if not familiar with the IL, you can first write in VS to the code, decompile to IL, and then directly copy over. After the modification, click Save and run the following command at the command line:
Ilasm/out=test2.exe test.il
If there are no errors in the syntax, you will see the following prompts
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
Proof has been modified successfully, we can directly run Test2.exe, the Discovery program has been modified by our output:
Hello world! --Has been modified.

2. Use the Reflector plug-in Reflexil

Directly with the IL modified more trouble, careless is very easy to make mistakes, fortunately we have JB Evain prepared reflexil.
Reflexil is based on Mono.cecil and is a powerful assembly editor.
When you are finished, open reflector--> View--> add-ins--> Add--> Select Reflexil.dll,
Then you can open it directly with reflector tools.

Start formal changes, with reflector open test.exe, open reflexil, select Main function, you can find the IL code shown in the bottom, you can right-click Edit,delete,create, and so on, yes, you also found the Replace all With code, this can be directly modified with C # code. Other features to try to understand the other, click on the class, you can also modify the class access rights, such as private change to public. We select Line No. 0, select Edit directly, and change the text after operand to: Hello world! --Modified by Reflexil. Click Update and select Test.exe Module,save as Test3.exe in reflector. Execute Test3.exe, and see that our changes are in effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.