简体   繁体   English

DLL注入记事本

[英]DLL Injection into notepad

I want to make a message box appear in notepad , so I found a simple dll injection example. 我想在记事本中显示一个消息框,因此找到了一个简单的dll注入示例。 The injector itself is not mine and seems to work fine (gets the process's id , creates a remote thread , gets absolute path of the dll file). 注入器本身不是我的,并且似乎可以正常工作(获取进程的id,创建一个远程线程,获取dll文件的绝对路径)。 The problem, I think, is in the implementation of the dll . 我认为问题在于dll的实现。 The projects compile without any warnings, but the expected outcome isn't achieved. 这些项目在没有任何警告的情况下进行编译,但是没有达到预期的结果。 Can you take a look and help me understand the problem? 您可以看看并帮助我了解问题吗? (I have put the release version of the dll in the injector project folder) (我已将dll的发行版本放在注射器项目文件夹中)

dllmain.cpp: 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: dll.h:

#ifndef _DLL_H_
#define _DLL_H_

# define DLLEXPORT __declspec (dllexport)

# define DLLIMPORT __declspec (dllimport)

DLLEXPORT void mess(void);
#endif 

and the injection.cpp for reference, it contains a function which finds the wanted process id, a function which creates the remote thread and a main: 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;
}

Thanks. 谢谢。

Be carefull on x64 x86 binaries 小心x64 x86二进制文件

On windows 7 / 8 / 10 notepad.exe is a 64 bits process, so you need to compile your DLL & injector in x64 在Windows 7/8/10上,notepad.exe是64位进程,因此您需要在x64中编译DLL和注射器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM