我想将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的返回值保存到变量中,我曾经看过它是否正常工作,它看起来很好但是它崩溃了目标进程,日志没有记录任何与之相关的错误。 托管程序可以注入非托管进程吗?我是否输错了变量类型? shellcode是否被错误地翻译成字节数组?请让我知道,谢谢。
编辑:它在CreateRemoteThread
崩溃答案 0 :(得分:1)
CreateRemoteThread
在另一个进程中创建一个本机线程,它接收的起始地址必须指向有效的机器代码,否则线程将使进程崩溃。
您描述的场景不同,您希望指示另一个进程的Python解释器执行某些代码。这可以做到,但它有所不同,而且相当困难。
将本机库注入到执行两项操作的其他进程中:
使用IPC将您要执行的Python代码发送到另一个进程,然后您注入的库中的代码使用Python解释器执行该代码。
您可以找到如何将DLL注入另一个进程in this Codeproject article的示例。
答案 1 :(得分:0)
看起来你正试图从.net运行一些任意的Python代码。现在,您正在尝试调用Python解释器来实际执行此操作。
这样做的缺点是: -
现在解决这个问题的方法是直接在.net中调用python程序。现在我的生活中从未这样做过,我一生中从未见过蟒蛇(除了嘶嘶声之外)。看看http://msdn.microsoft.com/en-us/library/ee461504.aspx,遗憾的是它们看起来像是将python存储在一个文件中并在那里调用它。但是我相信你可以调用存储为字符串的代码。
使用python的DLR实现的主要缺点是你依靠第三方来获取Python - > CLR翻译正确。但我认为IronPython是一个微软赞助的开源项目。
有关详细信息,请参阅:http://ironpython.codeplex.com/