zoukankan      html  css  js  c++  java
  • 给C#程序加壳(C# 调用嵌入资源的EXE文件方法)

    本来想把一个EXE程序变成我的窗口的一部分,试了很久没有成功,也不能说没有收获,这是一个对c#的EXE程序加壳的程序。

    代码复制如下:

    1. 我们有一个test.exe的WinForm程序,这是我们要加壳的目标程序。
    2. 新建一个WinForm工程,删除Form1,然后新建一个类。如下。
    3. 将test.exe 拷贝到该工程目录,作为嵌入式资源using System;
    using System.Windows.Forms;
    using System.Resources;
    using System.Reflection;
    using System.IO;
    namespace test
    {
        static class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                Stream stream = Assembly. GetExecutingAssembly_r(). GetManifestResourceStream_r("test.Code.exe");
                byte[] bs = new byte[stream.Length];
                stream.Read(bs, 0, (int)stream.Length);
                Assembly asm = Assembly.Load(bs);
                MethodInfo info = asm.EntryPoint;
                ParameterInfo[] parameters = info. GetParameters_r();
                if ((parameters != null) && (parameters.Length > 0))
                    info.Invoke(null, (object[])args);
                else
                    info.Invoke(null, null);
            }
        }
    }

    这是一个错漏百出的程序,参见参考文献4,以至于我弄了很久没有没有结果。

    最后弄出来了,总结一下:

    1.要加壳的一定是c#程序,我用其他类型程序试过,不可行。

    2.必须是嵌入式资源,有关这个解释可以参考文献1和2.

    3.我在执行“Stream stream = Assembly. GetExecutingAssembly_r(). GetManifestResourceStream_r("test.Code.exe");”的时候总是得到为null,在参考了文献3以后可以正确获取了值。

    最后正确的代码应该是这样的:

    using System;
    using System.Windows.Forms;
    using System.Resources;
    using System.Reflection;
    using System.IO;
    
    namespace MyNamespace
    {
        public class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                String projectName = Assembly.GetExecutingAssembly().GetName().Name.ToString();
                Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(projectName + ".test.exe");
                byte[] bs = new byte[stream.Length];
                stream.Read(bs, 0, (int)stream.Length);
                Assembly asm = Assembly.Load(bs);
    
                MethodInfo info = asm.EntryPoint;
                ParameterInfo[] parameters = info.GetParameters();
                if ((parameters != null) && (parameters.Length > 0))
                    info.Invoke(null, (object[])args);
                else
                    info.Invoke(null, null);
    
            }
        }
    }

    参考文献:

    1.http://zch448.blog.163.com/blog/static/73706910201231902712864/

    2.http://www.cnblogs.com/zhangjun1130/archive/2011/04/11/2012566.html

    3.http://www.cnblogs.com/lonelyDog/archive/2012/02/16/2354407.html

    4.http://www.cnblogs.com/huangcong/archive/2010/03/23/1693293.html

  • 相关阅读:
    python内置模块collections介绍
    Python的set集合详解
    不同 Python 数据类型的搜寻
    Python 分支、循环、条件与枚举
    ssrf爆破mysql
    php反序列化
    thinkphp历史漏洞
    Thinkphp 缓存RCE
    绕WAF文章收集
    mssql手工盲注
  • 原文地址:https://www.cnblogs.com/xlw1219/p/2795069.html
Copyright © 2011-2022 走看看