DLL 注入不断失败,出现不一致的错误

DLL Injection Keeps Failing with Inconsistent Errors

本文关键字:不一致 错误 注入 失败 DLL      更新时间:2023-10-16

我正在尝试在C# WPF应用程序中编写DLL注入器。我以前在Windows窗体中编写过DLL注入器,但是我想从头开始编写一个新程序。我之前用许多其他语言(使用传统的VirtualAllocEx/WriteProcessMemory/CreateRemoteThread方法(编写过DLL注入器,但是我遇到了一些特殊的问题。我的DLL注入器说它成功了,但我的测试程序显示没有任何变化。我用Marshal.GetLastWin32Error()做了一些测试,结果得到了ERROR_FILE_NOT_FOUND,结果是VirtualAllocExWriteProcessMemoryCreateRemoteThread。然后,我在C++中创建了一个注入器来测试C#是否是问题所在。当我测试C++ DLL注入器时,每个方法都抛出了ERROR_NO_MORE_FILES错误。最后,我下载了旧的DLL注入器,发现它不再起作用。

我根本不知道问题是什么。

C# 注入器代码:

IntPtr handle = OpenProcess(ProcessAccessFlags.All, false, SelectedProcess.Id);
Console.WriteLine("OpenProcess: " + new Win32Exception(Marshal.GetLastWin32Error()).Message); // This does not fail.
foreach (string files in DLLFiles.Items) // Get the files from the ListView control
{
// Allocate memory
IntPtr address = VirtualAllocEx(handle, IntPtr.Zero, (uint)(files.Length + 1), AllocationType.Commit, MemoryProtection.ExecuteReadWrite);
Console.WriteLine("VirtualAllocEx: " + new Win32Exception(Marshal.GetLastWin32Error()).Message);
// Write in memory
if (address != IntPtr.Zero)
{
bool success = WriteProcessMemory(handle, address, Encoding.ASCII.GetBytes(files), files.Length + 1, out IntPtr lpNumberOfBytesWritten);
Console.WriteLine("WriteProcessMemory: " + new Win32Exception(Marshal.GetLastWin32Error()).Message);
if (success)
{
IntPtr module = LoadLibrary("kernel32.dll"); // Get the module
Console.WriteLine("LoadLibrary: " + new Win32Exception(Marshal.GetLastWin32Error()).Message);
IntPtr LoadLib = GetProcAddress(module, "LoadLibraryA"); // Get the address of the function
Console.WriteLine("GetProcAddress: " + new Win32Exception(Marshal.GetLastWin32Error()).Message);
// Create a remote thread in the target process
IntPtr thread_handle = CreateRemoteThread(handle, IntPtr.Zero, 0, LoadLib, address, 0, out IntPtr lpThreadId);
Console.WriteLine("CreateRemoteThread: " + new Win32Exception(Marshal.GetLastWin32Error()).Message);
if (thread_handle == IntPtr.Zero)
{
Console.Write("[");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(files);
Console.ResetColor();
Console.WriteLine("] Injection Failed: " + new Win32Exception(Marshal.GetLastWin32Error()).Message);
}
else
{
Console.Write("[");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(files);
Console.ResetColor();
Console.WriteLine("] Injected Successfully.");
}
}
}
}

运行后,控制台说注入成功,但错误输出仍然显示:

系统找不到指定的文件。

C++喷油器代码:

#include <windows.h>
#include <iostream>
#include <string>
int main() {
DWORD pid = 25860; // Hardcoded for testing purposes.
std::string str;
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); // Get the process handle
std::cout << handle << std::endl;
std::cout << "Error: " << GetLastError() << std::endl;
LPVOID addr = VirtualAllocEx(handle, NULL, sizeof("C:\Users\BenHerebefore\source\repos\InjectionTest\InjectionTest\InjectionTest.dll") + 1, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
std::cout << addr << std::endl;
std::cout << "Error: " << GetLastError() << std::endl;
BOOL success = WriteProcessMemory(handle, addr, "C:\Users\BenHerebefore\source\repos\InjectionTest\InjectionTest\InjectionTest.dll", sizeof("C:\Users\BenHerebefore\source\repos\InjectionTest\InjectionTest\InjectionTest.dll") + 1, NULL);
std::cout << success << std::endl;
std::cout << "Error: " << GetLastError() << std::endl;
HMODULE module = LoadLibrary("kernel32");
std::cout << module << std::endl;
std::cout << "Error: " << GetLastError() << std::endl;
FARPROC proc = GetProcAddress(module, "LoadLibraryA");
std::cout << proc << std::endl;
std::cout << "Error: " << GetLastError() << std::endl;
HANDLE test = CreateRemoteThread(handle, NULL, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(LoadLibrary("kernel32"), "LoadLibrary"), addr, 0, NULL);
std::cout << test << std::endl;
std::cout << "Error: " << GetLastError() << std::endl;
std::getline(std::cin, str);
}   

C++喷油器在每行后显示Error: 18CreateRemoteThread的结果返回0并且消息Error: 5。我尝试以管理员身份运行该程序,但它仍然不起作用。

我完全不知道问题是什么。

根据妮娜的回答,我编译错误。谢谢,妮娜!

您的注入器/dll 是否与您注入的进程的体系结构相同?例如,如果要注入到 64 位进程中,则应将 dll 和注入器编译为 x64。喜欢wsie如果是x86或WOW64,则应相应地进行编译。