DLL 注入不断失败,出现不一致的错误
DLL Injection Keeps Failing with Inconsistent Errors
我正在尝试在C# WPF应用程序中编写DLL注入器。我以前在Windows窗体中编写过DLL注入器,但是我想从头开始编写一个新程序。我之前用许多其他语言(使用传统的VirtualAllocEx
/WriteProcessMemory
/CreateRemoteThread
方法(编写过DLL注入器,但是我遇到了一些特殊的问题。我的DLL注入器说它成功了,但我的测试程序显示没有任何变化。我用Marshal.GetLastWin32Error()
做了一些测试,结果得到了ERROR_FILE_NOT_FOUND,结果是VirtualAllocEx
,WriteProcessMemory
和CreateRemoteThread
。然后,我在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: 18
。CreateRemoteThread
的结果返回0
并且消息Error: 5
。我尝试以管理员身份运行该程序,但它仍然不起作用。
我完全不知道问题是什么。
根据妮娜的回答,我编译错误。谢谢,妮娜!
您的注入器/dll 是否与您注入的进程的体系结构相同?例如,如果要注入到 64 位进程中,则应将 dll 和注入器编译为 x64。喜欢wsie如果是x86或WOW64,则应相应地进行编译。
相关文章:
- 大于65535的C++数组[size]引发不一致的溢出
- 在 C++(和 C)中进行类型转换时明显不一致
- 我在c ++矩阵计算器程序中找不到错误
- 填充上编译器生成的复制构造函数之间的不一致
- 犰狳的 print() 方法和 cout 在从 Rcpp 调用时顺序不一致
- CreateDIBSection为同一图像返回不一致的位图位值
- 在 Qml 中从 QSqlTableModel 中删除单行时视图不一致
- 模板参数推导不一致
- 声明中不一致的no是否违反ODR?
- DLL 注入不断失败,出现不一致的错误
- 元数据操作失败LNK2022错误 (8013118D):重复类型中的布局信息不一致 (选择设备参数):(0x020002
- C++的 istream::eof() 的不一致是规范中的错误还是实现中的错误?
- C++ 对指针的引用:不一致的段错误
- 形成两组的并集似乎给出了错误且不一致的答案
- OutputDebugString导致不一致的错误
- 字符串流错误不一致
- c++字符串操作给我奇怪的/不一致的/错误的结果
- 如何纠正不一致的未解决外部错误
- 运行时出现不一致的分段错误
- 收到错误:将我的 c++ 代码从 Linux 移植到 android 时'asm'中的操作数约束不一致