gpt4 book ai didi

c++ - DLL注入(inject)记事本

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:14:44 26 4
gpt4 key购买 nike

我想在记事本中显示一个消息框,所以我找到了一个简单的dll 注入(inject)示例。注入(inject)器本身不是我的并且似乎工作正常(获取进程的 id ,创建一个远程线程,获取 dll 文件的绝对路径)。我认为问题在于 dll 的实现。项目在没有任何警告的情况下编译,但没有达到预期的结果。你能看一下并帮助我理解问题吗? (我已经把发布版本的dll放到了injector项目文件夹中)

dllmain.cpp:

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

#include "dll.h"
#include <windows.h>

#include <stdio.h>
#include <stdlib.h>

DLLEXPORT void mess() {
MessageBoxA(NULL, "HELLO THERE", "From Notepad", NULL);
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH: mess(); break;
case DLL_THREAD_ATTACH: mess(); break;
case DLL_THREAD_DETACH: mess(); break;
case DLL_PROCESS_DETACH: mess(); break;
}
return TRUE;
}

dll.h:

#ifndef _DLL_H_
#define _DLL_H_

# define DLLEXPORT __declspec (dllexport)

# define DLLIMPORT __declspec (dllimport)

DLLEXPORT void mess(void);
#endif

和供引用的injection.cpp,它包含一个查找所需进程ID的函数,一个创建远程线程的函数和一个main:

#include "stdafx.h"
#include <windows.h>
#include <tlhelp32.h>
#include <shlwapi.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define WIN32_LEAN_AND_MEAN
#define CREATE_THREAD_ACCESS (PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ)
DWORD GetProcessId(IN PCHAR szExeName)

{
DWORD dwRet = 0;
DWORD dwCount = 0;

HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (hSnapshot != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe = { 0 };
pe.dwSize = sizeof(PROCESSENTRY32);

BOOL bRet = Process32First(hSnapshot, &pe);

while (bRet)
{
if (!strcmp( szExeName, pe.szExeFile))
{
dwCount++;
dwRet = pe.th32ProcessID;
}
bRet = Process32Next(hSnapshot, &pe);
}

if (dwCount > 1)
dwRet = 0xFFFFFFFF;

CloseHandle(hSnapshot);
}

return dwRet;
}

BOOL CreateRemoteThreadInject(DWORD ID, const char * dll)
{
HANDLE Process;

LPVOID Memory;

LPVOID LoadLibrary;

if (!ID) return false;

Process = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, ID);

LoadLibrary = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

Memory = (LPVOID)VirtualAllocEx(Process, NULL, strlen(dll) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

WriteProcessMemory(Process, (LPVOID)Memory, dll, strlen(dll) + 1, NULL);

CreateRemoteThread(Process, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibrary, (LPVOID)Memory, NULL, NULL);

CloseHandle(Process);

VirtualFreeEx(Process, (LPVOID)Memory, 0, MEM_RELEASE);

return true;
}

int main()
{
char dll[MAX_PATH] ;

GetFullPathName("testdll.dll", MAX_PATH, dll, NULL);

DWORD ID = GetProcessId("notepad.exe");

if (!CreateRemoteThreadInject(ID, dll)) cout<<"failure";

else cout << "success";

return 0;
}

谢谢。

最佳答案

小心 x64 x86 二进制文件

在 Windows 7/8/10 上,notepad.exe 是一个 64 位进程,因此您需要在 x64 中编译 DLL 和注入(inject)器

关于c++ - DLL注入(inject)记事本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45614032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com