[C #] Use dnSpy to decompile and modify the Target Program (EXE or DLL) and compile and run the program,
This article is an original article and the source code is the original code. For example, repost/copy the original code, please clearly mark the original name, author, and URL on the webpage/code. Thank you!
The tool used in this article is:
https://github.com/cnxy/dnSpy/archive/v4.0.0.zip
Or dnSpy official:
https://github.com/0xd4d/dnSpy/releases/download/v4.0.0/dnSpy.zip
The software interface is as follows:
The software 64-Bit Memory and 32-Bit Memory, respectively for dnspy.exe and dnSpy-x86.exe, debugging, please pay attention to the corresponding software version.
Enter the topic:
1. First, write a Hello World console running program, as shown in:
The Code is as follows:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ChangeString{ class Program { static void Main(string[] args) { string str = "Hello World!"; Console.WriteLine(str); Console.ReadKey(); } }}
2. Compile and run, as shown in:
Here, the str variable in the program is "Hello World !", Now, you need to use the dnSpy tool to change the internal str variable and debug it online.
Iii. decompile the program using dnSpy:
①Use the dnspytool to decompile the just-generated changestring.exe, as shown in:
② Right-click the menu in the Main function and select "Edit Method (C #)" from the context menu, as shown in:
③ Change the content of the str variable to "Just For Test" and click the "compile" button, as shown in:
④ Click "Save all", retain the default value on the displayed page, and click "OK", as shown in:
⑤ After saving, click the "Start" button, retain the default value in the pop-up interface, click the "OK" button, and then automatically start the application, as shown in:
The program runs successfully!
Note:
Note the following during compilation; otherwise, compilation is not allowed:
① Other DLL modules must be included;
② If there are other characters that cannot be identified after decompilation, manually modify OK before compiling.
In addition, this program is not only effective for EXE, but also for DLL. Please test it by yourself. Thank you!