使用 .NET 反射自克隆可执行文件

发布于 2024-12-07 19:10:00 字数 1511 浏览 3 评论 0原文

我正在尝试在 C# .NET 中进行某种实验。我想创建一个克隆自身的程序(最终作为变异克隆)。最后我想让它生出一个“儿子”,还需要有克隆自己的能力等等!

我想做这样的事情:

static void Main(string[] args)
        {

            string Son_Name = "son";
            string Full_Son_Name = Son_Name + ".exe";
            AssemblyName aName = new AssemblyName(Son_Name);
            AssemblyBuilder aBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);
            ModuleBuilder mBuilder = aBuilder.DefineDynamicModule("Module", Full_Son_Name, false);
            TypeBuilder tBuilder = mBuilder.DefineType("Program", TypeAttributes.Public);
            MethodBuilder methodBuilder = tBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static);
            ILGenerator ilGenerator = methodBuilder.GetILGenerator();
            MethodInfo m_ReadLine = typeof(System.Console).GetMethod("ReadLine", BindingFlags.Public | BindingFlags.Static);

            Clone_My_Main_method(methodbuilder);//I need something to do THAT 
            aBuilder.SetEntryPoint(methodBuilder);
            tBuilder.CreateType();
            aBuilder.Save(Full_Son_Name);

因此,通过这段代码,我成功地在运行时制作了一个动态程序集,并将其保存为“son.exe”或执行它。现在我需要“构建”该程序集,读取我的 main 并将其复制到子程序集中。

我开始使用 ilgenerator.emit,但我进入了无限的“编程”循环。我想制作parent.exe,用ildasm反汇编它,翻译ilgenerator.emit操作码中的所有CIL代码,并使用ilgenerator.emit发出所有这些代码。简单地说,我认为这是一项乏味的工作,而且不能……工作。

事实上,我想在运行时读取我自己的主要方法,然后用一些技巧将其放入动态程序集中。

有人有什么想法吗? 谢谢 :)

I'm trying a sort of experiment in C# .NET. I'd like to create a program that clones itself (eventually as a mutated clone). Finally, I want it to produce a "son" that also needs to have the ability to clone himself and so on!

I want to make something like this:

static void Main(string[] args)
        {

            string Son_Name = "son";
            string Full_Son_Name = Son_Name + ".exe";
            AssemblyName aName = new AssemblyName(Son_Name);
            AssemblyBuilder aBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);
            ModuleBuilder mBuilder = aBuilder.DefineDynamicModule("Module", Full_Son_Name, false);
            TypeBuilder tBuilder = mBuilder.DefineType("Program", TypeAttributes.Public);
            MethodBuilder methodBuilder = tBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static);
            ILGenerator ilGenerator = methodBuilder.GetILGenerator();
            MethodInfo m_ReadLine = typeof(System.Console).GetMethod("ReadLine", BindingFlags.Public | BindingFlags.Static);

            Clone_My_Main_method(methodbuilder);//I need something to do THAT 
            aBuilder.SetEntryPoint(methodBuilder);
            tBuilder.CreateType();
            aBuilder.Save(Full_Son_Name);

So, with this code I succeded in making a dynamic assembly at runtime, and save it as "son.exe" or execute it. Now I need to "build" that assembly, reading my main and copying it into the son assembly.

I started using ilgenerator.emit, but I'm going into an infinite "programming" loop. I thought to make the parent.exe, disassemble it with ildasm, translate all the CIL code in ilgenerator.emit opcodes and emit all of that with ilgenerator.emit. Simply, I think this is a tedious work and cannot...work.

In fact, I'd like to READ at runtime my own main method, and then put it onto the dynamic assembly with some trick.

Anyone has any idea?
Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生一梦 2024-12-14 19:10:00

您应该使用 Mono Cecil,它可以读取和修改 .Net 程序集。

You should use Mono Cecil, which can read and modify .Net assemblies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文