使用远程线程来注入DLL

DLL注入技术要求我们目标进程中的一个线程调用LoadLibrary来载入我们想要的DLL

(1)用OpenProcess函数打开目标进程
(2)用VirtualAllocEx函数在远程进程的地址空间中分派一块内存
(3)用WriteProcessMemory函数把DLL的路径名复制到第一步分配的内存中
(4)用GetProcAddress函数来得到LoadLibraryW函数(在Kernel32.dll中)的实际地址
(5)用CreateRemoteThread函数在远程进程中创建一个线程,让新线程调用LoadLibraryW函数并在参数中传入第1步分配的内存地址。这时,DLL已经被注入到远程进程的地址空间中,DLL的DllMain函数会收到DLL_PROCESS_ATTACH通知并且可以执行我们想要执行的代码。当DllMain返回的时候,远程线程会从LoadLibraryW调用返回到BaseThreadStart函数。BaseThreadStart函数然后调用ExitThread,使远程线程终止。

补充:
(1)OpenProcess函数的作用是打开一个已存在的进程对象,并返回进程的句柄
(2)VirtualAllocEx函数的作用是在指定进程的虚拟空间申请内存,执行成功返回分配内存的首地址,不成功返回NULL
(3)WriteProcessMemory函数的作用是在某一进程的内存区域写入数据
(4)GetProcAddress函数的作用是获取DLL导出函数的地址,使用返回的函数指针调用DLL函数
(5)CreateRemoteThread函数的作用是创建一个在其它进程地址空间中运行的线程

示例代码:

BOOL WINAPI InjectLibW(DWORD dwProcessId, PCWSTR pszLibFile) {

    HANDLE hProcess = NULL, hThread = NULL;
PWSTR pszLibFileRemote = NULL; // Get a handle for the target process.
hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | // Required by Alpha
PROCESS_CREATE_THREAD | // For CreateRemoteThread
PROCESS_VM_OPERATION | // For VirtualAllocEx/VirtualFreeEx
PROCESS_VM_WRITE, // For WriteProcessMemory
FALSE, dwProcessId);
if (hProcess == NULL) return false; // Calculate the number of bytes needed for the DLL's pathname
int cch = + lstrlenW(pszLibFile);
int cb = cch * sizeof(wchar_t); // Allocate space in the remote process for the pathname
pszLibFileRemote = (PWSTR)VirtualAllocEx(hProcess, NULL, cb, MEM_COMMIT, PAGE_READWRITE);
if (pszLibFileRemote == NULL) return false; // Copy the DLL's pathname to the remote process' address space
if (!WriteProcessMemory(hProcess, pszLibFileRemote, (PVOID) pszLibFile, cb, NULL))
return false; // Get the real address of LoadLibraryW in Kernel32.dll
PTHREAD_START_ROUTINE pfnThreadRtn = (PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "LoadLibraryW");
if (pfnThreadRtn == NULL) return false; // Create a remote thread that calls LoadLibraryW(DLLPathname)
hThread = CreateRemoteThread(hProcess, NULL, , pfnThreadRtn, pszLibFileRemote, , NULL);
if (hThread == NULL) return false; // Wait for the remote thread to terminate
WaitForSingleObject(hThread, INFINITE); // Free the remote memory that contained the DLL's pathname
if (pszLibFileRemote != NULL)
VirtualFreeEx(hProcess, pszLibFileRemote, , MEM_RELEASE); if (hThread != NULL)
CloseHandle(hThread); if (hProcess != NULL)
CloseHandle(hProcess); return true;
}

源自《Windows核心编程(第5版)》

使用远程线程来注入DLL的更多相关文章

  1. 《windows核心编程系列》十九谈谈使用远程线程来注入DLL。

    windows内的各个进程有各自的地址空间.它们相互独立互不干扰保证了系统的安全性.但是windows也为调试器或是其他工具设计了一些函数,这些函数可以让一个进程对另一个进程进行操作.虽然他们是为调试 ...

  2. 实战DELPHI:远程线程插入(DLL注入)

    http://www.jx19.com/xxzl/Delphi/2010/04/17/ShiZhanDELPHI_YuanChengXianChengChaRu_DLLZhuRu/ 远程注入DLL方法 ...

  3. Dll注入:X86/X64 远程线程CreateRemoteThread 注入

    远线程注入原理是利用Windows 系统中CreateRemoteThread()这个API,其中第4个参数是准备运行的线程,我们可以将LoadLibrary()填入其中,这样就可以执行远程进程中的L ...

  4. windows:shellcode 远程线程hook/注入(一)

    https://www.cnblogs.com/theseventhson/p/13199381.html 上次分享了通过APC注入方式,让目标线程运行shellcode.这么做有个前提条件:目标线程 ...

  5. windows:shellcode 远程线程hook/注入(三)

    今天介绍第三种远程执行shellcode的思路:函数回调: 1.所谓回调,简单理解: windows出厂时,内部有很多事务的处理无法固化(无法100%预料外部会遇到哪些情况),只能留下一堆的接口,让开 ...

  6. windows:shellcode 远程线程hook/注入(二)

    https://www.cnblogs.com/theseventhson/p/13218651.html   上次分享了基本的远程注入方法,遗留了一个问题:shellcode执行完后怎么回到线程su ...

  7. windows:shellcode 远程线程hook/注入(五)

    前面几篇文章介绍了通过APC注入.进程注入.windows窗口处理函数回调.kernercallback回调执行shellcode,今天继续介绍通过heap Spray(翻译成中文叫堆喷射)执行she ...

  8. windows:shellcode 远程线程hook/注入(四)

    https://www.cnblogs.com/theseventhson/p/13236421.html  这里介绍了利用回调函数执行shellcode的基本原理:这里介绍另外一种利用回调执行she ...

  9. ReflectiveLoader分析(远程线程注入 PE修正)

    从github上下载了ReflectiverLoader认真学习了一下 在代码中得到一些心得和自己的想法,都按步骤写到了代码中,现在分享给大家,如有错,望大家指正 其中需要注入的dll和解析, 内存R ...

随机推荐

  1. 【selenium+Python WebDriver API】之复选框顺序正选和顺序反选

    from selenium import webdriver from selenium.webdriver.common.by import By import os,time driver = w ...

  2. Maven 编译

    pom.xml 添加插件 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins< ...

  3. Learning an Optimal Policy: Model-free Methods

    http://www.mit.edu/~9.54/fall14/slides/Reinforcement%20Learning%202-Model%20Free.pdf [基于所有.单个样本]

  4. VirtualBox创建VM结果ProcessorType是空的

    用WMI来查询CPU的频率,一直没问题: "Select MaxClockSpeed From Win32_Processor Where ProcessorType = 3" 结 ...

  5. Spring/Java error: namespace element 'annotation-config' … on JDK 1.5 and higher

    Extract the jar file: mkdir spring cd spring jar xvf ../spring.jar Check the Spring version in META- ...

  6. CF148D. Bag of mice(概率DP)

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  7. 关于Spring注解 @Service @Component @Controller @Repository 用法

    @Component 相当于实例化类的对象,其他三个注解可以理解为@Component的子注解或细化. 在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spr ...

  8. GstAppSink简介

    Description Appsink is a sink plugin that supports many different methods for making the application ...

  9. pandas,apply并行计算的一个demo

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-10-11 17:55:26 # @Author : Sheldon (thi ...

  10. Java for LeetCode 080 Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...