开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 15572|回复: 5
收起左侧

[C#求助] C#如何线程注入DLL到另一个进程

[复制链接]
发表于 2022-2-20 15:56:07 | 显示全部楼层 |阅读模式   重庆市重庆市
需要一个C#线程注入DLL的例子或帮助
发表于 2023-11-15 17:15:55 | 显示全部楼层   江西省南昌市
我也想知道
回复 支持 反对

使用道具 举报

签到天数: 1 天

发表于 2023-9-26 15:35:13 | 显示全部楼层   湖北省武汉市
感谢分享
回复 支持 反对

使用道具 举报

签到天数: 8 天

发表于 2023-9-12 12:15:23 | 显示全部楼层   台湾省台中市
public class Injector
        {
            const int PROCESS_ALL_ACCESS = 0x1F0FFF;
            const int PROCESS_CREATE_THREAD = 0x0002;
            const int PROCESS_QUERY_INFORMATION = 0x0400;
            const int PROCESS_VM_OPERATION = 0x0008;
            const int PROCESS_VM_WRITE = 0x0020;
            const int PROCESS_VM_READ = 0x0010;
            const uint MEM_COMMIT = 0x00001000;
            const uint MEM_RESERVE = 0x00002000;
            const uint PAGE_READWRITE = 4;

            [DllImport("kernel32.dll")]
            public static extern int OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

            [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
            public static extern int GetModuleHandle(string lpModuleName);

            [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
            static extern int GetProcAddress(int hModule, string procName);

            [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
            static extern int VirtualAllocEx(int hProcess, int lpAddress,
                int dwSize, uint flAllocationType, uint flProtect);

            [DllImport("kernel32.dll", SetLastError = true)]
            static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, string lpBuffer, int nSize, int lpNumberOfBytesWritten);

            [DllImport("kernel32.dll")]
            static extern int CreateRemoteThread(int hProcess,
            int lpThreadAttributes, uint dwStackSize, int lpStartAddress, int lpParameter, uint dwCreationFlags, int lpThreadId);

            [StructLayout(LayoutKind.Sequential)]
            public struct Luid
            {
                public UInt32 LowPart;
                public Int32 HighPart;
            }

            public const UInt32 SePrivilegeEnabledByDefault = 0x00000001;
            public const UInt32 SePrivilegeEnabled = 0x00000002;
            public const UInt32 SePrivilegeRemoved = 0x00000004;
            public const UInt32 SePrivilegeUsedForAccess = 0x80000000;

            [StructLayout(LayoutKind.Sequential)]
            public struct TokenPrivileges
            {
                public UInt32 PrivilegeCount;
                public Luid Luid;
                public UInt32 Attributes;
            }

            [StructLayout(LayoutKind.Sequential)]
            public struct LuidAndAttributes
            {
                public Luid Luid;
                public UInt32 Attributes;
            }

            [DllImport("advapi32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool OpenProcessToken(IntPtr processHandle,
                UInt32 desiredAccess, out IntPtr tokenHandle);

            [DllImport("kernel32.dll", SetLastError = true)]
            static extern IntPtr GetCurrentProcess();

            [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool LookupPrivilegeValue(string lpSystemName, string lpName,
                out Luid lpLuid);

            [DllImport("kernel32.dll", SetLastError = true)]
            static extern bool CloseHandle(IntPtr hHandle);

            [DllImport("advapi32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool AdjustTokenPrivileges(IntPtr tokenHandle,
               [MarshalAs(UnmanagedType.Bool)] bool disableAllPrivileges,
               ref TokenPrivileges newState,
               UInt32 zero,
               IntPtr null1,
               IntPtr null2);


            public static void Inject(int pid)
            {
                var dllPath = "C:\aa.dll";
                var p = Process.GetProcessById(pid);
                var hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
                int dllPathSize = dllPath.Length + 1;
                int lpBaseAddress = VirtualAllocEx(hProcess, 0, dllPathSize, MEM_COMMIT, PAGE_READWRITE);

                WriteProcessMemory(hProcess, lpBaseAddress, dllPath, dllPathSize, 0);
                int module = GetModuleHandle("Kernel32.dll");
                int LoadLibraryAddress = GetProcAddress(module, "LoadLibraryA");
                var rt = CreateRemoteThread(hProcess, 0, 0, LoadLibraryAddress, lpBaseAddress, 0, 0);
            }


            public static string GetAefHookPath()
            {
                var fn = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AefHook.bin");
                if (!File.Exists(fn))
                {
                    var bytes = AefHookCode.xSplitBySpace().Select(s => byte.Parse(s)).ToArray();
                    File.WriteAllBytes(fn, bytes);
                }
                return fn;
            }


            private static uint _tokenAdjustPrivileges = 0x0020;
            private static uint _tokenQuery = 0x0008;
            public const string SeDebugName = "SeDebugPrivilege";

            public static bool EnableDebugPrivilege()
            {
                IntPtr hToken;
                Luid luidSeDebugNameValue;
                TokenPrivileges tkpPrivileges;

                if (!OpenProcessToken(Process.GetCurrentProcess().Handle, _tokenAdjustPrivileges | _tokenQuery, out hToken))
                {
                    return false;
                }

                if (!LookupPrivilegeValue(null, SeDebugName, out luidSeDebugNameValue))
                {
                    return false;
                }

                tkpPrivileges.PrivilegeCount = 1;
                tkpPrivileges.Luid = luidSeDebugNameValue;
                tkpPrivileges.Attributes = SePrivilegeEnabled;

                if (!AdjustTokenPrivileges(hToken, false, ref tkpPrivileges, 0, IntPtr.Zero, IntPtr.Zero))
                {
                    return false;
                }

                CloseHandle(hToken);
                return true;
            }

        }
回复 支持 反对

使用道具 举报

发表于 2023-9-11 18:40:56 | 显示全部楼层   河南省驻马店市
同求+1!!!
回复 支持 反对

使用道具 举报

发表于 2022-3-16 13:16:43 | 显示全部楼层   广东省梅州市
同求!!!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表