向进程中注入Python代码

2024-09-23 06:36:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把Python代码注入到一个进程中,当它注入时,它似乎会使我的进程崩溃。我没有在我自己的程序中得到任何错误,但目标进程停止工作。被调用的非托管api没有给我任何错误,并且似乎已经正确地执行了它们的执行。在

[DllImport("kernel32")]
    public static extern IntPtr CreateRemoteThread(IntPtr hProcess,IntPtr lpThreadAttributes,uint dwStackSize, IntPtr lpStartAddress,IntPtr lpParameter,uint dwCreationFlags, out uint lpThreadId);
    [Flags]
    enum ProcessAccessFlags : uint
    {
        All = 0x001F0FFF,
        Terminate = 0x00000001,
        CreateThread = 0x00000002,
        VMOperation = 0x00000008,
        VMRead = 0x00000010,
        VMWrite = 0x00000020,
        DupHandle = 0x00000040,
        SetInformation = 0x00000200,
        QueryInformation = 0x00000400,
        Synchronize = 0x00100000
    }
    [DllImport("kernel32.dll")]
    static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
    [Flags]
    public enum AllocationType
    {
        Commit = 0x1000,
        Reserve = 0x2000,
        Decommit = 0x4000,
        Release = 0x8000,
        Reset = 0x80000,
        Physical = 0x400000,
        TopDown = 0x100000,
        WriteWatch = 0x200000,
        LargePages = 0x20000000,
        VIRTUAL_MEM = (0x1000 | 0x2000)
    }
    [Flags]
    public enum MemoryProtection
    {
        Execute = 0x10,
        ExecuteRead = 0x20,
        ExecuteReadWrite = 0x40,
        ExecuteWriteCopy = 0x80,
        NoAccess = 0x01,
        ReadOnly = 0x02,
        ReadWrite = 0x04,
        WriteCopy = 0x08,
        GuardModifierflag = 0x100,
        NoCacheModifierflag = 0x200,
        WriteCombineModifierflag = 0x400,
        PAGE_EXECUTE_READWRITE = 0x00000040
    }
    [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
    static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out UIntPtr lpNumberOfBytesWritten);
    [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
    static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint dwFreeType);
    [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
    internal static extern Int32 WaitForSingleObject( IntPtr handle,Int32 milliseconds);
    [DllImport("kernel32.dll")]
    public static extern Int32 CloseHandle(IntPtr hObject);
    private void InjectCode(string shellcode = "print('Hello, World!')")
    {
        foreach (Process proc in Process.GetProcesses())
        {
            if (proc.ProcessName == "Toontown")
            {
                int shellcode_length = shellcode.Length;
                IntPtr h_process = OpenProcess(ProcessAccessFlags.All, false, (int)proc.Id);
                IntPtr shellcode_address = (IntPtr)VirtualAllocEx(h_process, (IntPtr)0, (uint)shellcode_length, AllocationType.VIRTUAL_MEM, MemoryProtection.PAGE_EXECUTE_READWRITE);
                byte[] bytes = new byte[shellcode.Length * sizeof(char)];
                Buffer.BlockCopy(shellcode.ToCharArray(), 0, bytes, 0, bytes.Length);

                UIntPtr bytesout;
                uint t_id;

                bool Written = WriteProcessMemory(h_process, shellcode_address, bytes, (uint)shellcode_length, out  bytesout);
                IntPtr hThread = (IntPtr)CreateRemoteThread(h_process, (IntPtr)null, 0, (IntPtr)shellcode_length, (IntPtr)shellcode_address, 0, out t_id);
                int Result = WaitForSingleObject(hThread, 10 * 1000);
                if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFFF)
                {
                    if (hThread != null)
                    {
                        CloseHandle(hThread);
                    }
                }
                Thread.Sleep(1000);
                VirtualFreeEx(h_process, shellcode_address, (UIntPtr)0, 0x8000);
                if (hThread != null)
                {
                    CloseHandle(hThread);
                }
            }
        }
    }

如您所见,我已经将非托管API的返回值保存到变量中,这些变量用于查看它是否正常工作,它似乎运行良好,但它会导致目标进程崩溃,日志中没有记录任何与之相关的错误。 托管程序能否注入非托管进程?我投错变量类型了吗?外壳代码是否被错误地转换成字节数组?请告诉我,谢谢。在

编辑:它在CreateRemoteThread崩溃


Tags: true进程错误externstaticpublicprocessdll
2条回答

CreateRemoteThread在另一个进程中创建本机线程,它接收到的起始地址必须指向有效的计算机代码,否则该线程将使进程崩溃。在

您描述的场景不同,您需要指示另一个进程的Python解释器执行一些代码。这是可以做到的,但这是不同的,难度要大得多。在

将本机库注入到另一个进程中,该进程执行两项操作:

  • 设置Python解释器
  • 设置一些进程间通信方式(IPC)

使用IPC将要执行的Python代码发送到另一个进程,注入的库中的代码然后使用Python解释器执行该代码。在

您可以找到一个如何将DLL注入另一个进程in this Codeproject article的示例。在

看起来您正试图从.net运行一些任意的Python代码。现在您正试图调用Python解释器来实际执行此操作。在

这样做的坏处是:-在

  • 你发现这很复杂。在
  • 进程间通信会使它变得更加困难,因为您要跨边界移动字节
  • 然后,您需要将从每一方获取的信息解析为有意义的信息(可能使用某种XML)
  • 总算有了上面的一切

现在解决这个问题的一种方法是直接在.net中调用python程序。现在我从来没有这样做过,我也从来没有在我的生活中见过Python(可能除了那种嘶嘶作响的类型)。看一看http://msdn.microsoft.com/en-us/library/ee461504.aspx,不幸的是他们把python存储在一个文件中并在那里调用它。不过,我确信您可以调用存储为字符串的代码。在

使用python的DLR实现的主要缺点是依赖第三方来正确地进行python->;CLR转换。但我认为IronPython是微软赞助的开源项目。在

有关详细信息,请参阅:http://ironpython.codeplex.com/

相关问题 更多 >