创建进程时注入DLL


#include "stdafx.h"
#include <Windows.h>

// 函数声明
typedef BOOL (WINAPI* Proc_CreateProcessW)(LPCWSTR lpApplicationName,
LPWSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCWSTR lpCurrentDirectory,
LPSTARTUPINFOW lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation);

typedef HMODULE (WINAPI* Func_LoadLibraryW)(LPCWSTR lpLibFileName);

BYTE* mov_eax_xx(BYTE* lpCurAddres, DWORD eax)
{
*lpCurAddres = 0xB8;
*(DWORD*)(lpCurAddres + 1) = eax;
return lpCurAddres + 5;
}

BYTE* mov_ebx_xx(BYTE* lpCurAddres, DWORD ebx)
{
*lpCurAddres = 0xBB;
*(DWORD*)(lpCurAddres + 1) = ebx;
return lpCurAddres + 5;
}

BYTE* mov_ecx_xx(BYTE* lpCurAddres, DWORD ecx)
{
*lpCurAddres = 0xB9;
*(DWORD*)(lpCurAddres + 1) = ecx;
return lpCurAddres + 5;
}

BYTE* mov_edx_xx(BYTE* lpCurAddres, DWORD edx)
{
*lpCurAddres = 0xBA;
*(DWORD*)(lpCurAddres + 1) = edx;
return lpCurAddres + 5;
}

BYTE* mov_esi_xx(BYTE* lpCurAddres, DWORD esi)
{
*lpCurAddres = 0xBE;
*(DWORD*)(lpCurAddres + 1) = esi;
return lpCurAddres + 5;
}

BYTE* mov_edi_xx(BYTE* lpCurAddres, DWORD edi)
{
*lpCurAddres = 0xBF;
*(DWORD*)(lpCurAddres + 1) = edi;
return lpCurAddres + 5;
}

BYTE* mov_ebp_xx(BYTE* lpCurAddres, DWORD ebp)
{
*lpCurAddres = 0xBD;
*(DWORD*)(lpCurAddres + 1) = ebp;
return lpCurAddres + 5;
}

BYTE* mov_esp_xx(BYTE* lpCurAddres, DWORD esp)
{
*lpCurAddres = 0xBC;
*(DWORD*)(lpCurAddres + 1) = esp;
return lpCurAddres + 5;
}

BYTE* mov_eip_xx(BYTE* lpCurAddres, DWORD eip, DWORD newEip)
{
if ( !newEip )
{
newEip = (DWORD)lpCurAddres;
}

*lpCurAddres = 0xE9;
*(DWORD*)(lpCurAddres + 1) = eip - (newEip + 5);
return lpCurAddres + 5;
}

BYTE* push_xx(BYTE* lpCurAddres, DWORD dwAdress)
{

*lpCurAddres = 0x68;
*(DWORD*)(lpCurAddres + 1) = dwAdress;

return lpCurAddres + 5;
}

BYTE* Call_xx(BYTE* lpCurAddres, DWORD eip, DWORD newEip)
{
if ( !newEip )
{
newEip = (DWORD)lpCurAddres;
}

*lpCurAddres = 0xE8;
*(DWORD*)(lpCurAddres + 1) = eip - (newEip + 5);
return lpCurAddres + 5;
}

BOOL SuspendTidAndInjectCode(HANDLE hProcess, HANDLE hThread, DWORD dwFuncAdress, const BYTE * lpShellCode, size_t uCodeSize)
{
SIZE_T NumberOfBytesWritten = 0;
BYTE ShellCodeBuf[0x480];
CONTEXT Context;
DWORD flOldProtect = 0;
LPBYTE lpCurESPAddress = NULL;
LPBYTE lpCurBufAdress = NULL;
BOOL bResult = FALSE;

// 挂载起线程
SuspendThread(hThread);

memset(&Context,0,sizeof(Context));
Context.ContextFlags = CONTEXT_FULL;

if ( GetThreadContext(hThread, &Context))
{
// 在对方线程中开辟一个 0x480 大小的局部空
lpCurESPAddress = (LPBYTE)((Context.Esp - 0x480) & 0xFFFFFFE0);

// 获取指针 用指针来操作
lpCurBufAdress = &ShellCodeBuf[0];

if (lpShellCode)
{
memcpy(ShellCodeBuf + 128, lpShellCode, uCodeSize);
lpCurBufAdress = push_xx(lpCurBufAdress, (DWORD)lpCurESPAddress + 128); // push
lpCurBufAdress = Call_xx(lpCurBufAdress, dwFuncAdress, (DWORD)lpCurESPAddress + (DWORD)lpCurBufAdress - (DWORD)&ShellCodeBuf); //Call
}

lpCurBufAdress = mov_eax_xx(lpCurBufAdress, Context.Eax);
lpCurBufAdress = mov_ebx_xx(lpCurBufAdress, Context.Ebx);
lpCurBufAdress = mov_ecx_xx(lpCurBufAdress, Context.Ecx);
lpCurBufAdress = mov_edx_xx(lpCurBufAdress, Context.Edx);
lpCurBufAdress = mov_esi_xx(lpCurBufAdress, Context.Esi);
lpCurBufAdress = mov_edi_xx(lpCurBufAdress, Context.Edi);
lpCurBufAdress = mov_ebp_xx(lpCurBufAdress, Context.Ebp);
lpCurBufAdress = mov_esp_xx(lpCurBufAdress, Context.Esp);
lpCurBufAdress = mov_eip_xx(lpCurBufAdress, Context.Eip, (DWORD)lpCurESPAddress + (DWORD)lpCurBufAdress - (DWORD)&ShellCodeBuf);
Context.Esp = (DWORD)(lpCurESPAddress - 4);
Context.Eip = (DWORD)lpCurESPAddress;

if ( VirtualProtectEx(hProcess, lpCurESPAddress, 0x480, PAGE_EXECUTE_READWRITE, &flOldProtect)
&& WriteProcessMemory(hProcess, lpCurESPAddress, &ShellCodeBuf, 0x480, &NumberOfBytesWritten)
&& FlushInstructionCache(hProcess, lpCurESPAddress, 0x480)
&& SetThreadContext(hThread, &Context) )
{
bResult = TRUE;
}

}

// 回复线程
ResumeThread(hThread);

return TRUE;
}

DWORD GetFuncAdress()
{
return (DWORD)GetProcAddress(GetModuleHandleA("Kernel32"), "LoadLibraryW");
}

BOOL WINAPI CreateProcessWithDllW( LPCWSTR lpApplicationName,
LPWSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCWSTR lpCurrentDirectory,
LPSTARTUPINFOW lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation,
LPWSTR lpDllFullPath,
Proc_CreateProcessW FuncAdress
)
{
BOOL bResult = FALSE;
size_t uCodeSize = 0;
DWORD dwCreaFlags;
PROCESS_INFORMATION pi;
ZeroMemory( &pi, sizeof(pi) );

if (FuncAdress == NULL)
{
FuncAdress = CreateProcessW;
}

// 设置创建就挂起进程
dwCreaFlags = dwCreationFlags | CREATE_SUSPENDED;
if (CreateProcessW(lpApplicationName,
lpCommandLine,
lpProcessAttributes,
lpThreadAttributes,
bInheritHandles,
dwCreaFlags,
lpEnvironment,
lpCurrentDirectory,
lpStartupInfo,
&pi
))
{
if ( lpDllFullPath )
uCodeSize = 2 * wcslen(lpDllFullPath) + 2;
else
uCodeSize = 0;

// 得到LoadLibraryW 的地址
DWORD dwLoadDllProc = GetFuncAdress();

// 挂起线程 写入Shellcode
if (SuspendTidAndInjectCode(pi.hProcess, pi.hThread, dwLoadDllProc, (BYTE*)lpDllFullPath, uCodeSize))
{
if ( lpProcessInformation )
memcpy(lpProcessInformation, &pi, sizeof(PROCESS_INFORMATION));

if ( !(dwCreationFlags & CREATE_SUSPENDED) )
ResumeThread(pi.hThread);

bResult = TRUE;
}
}

return bResult;
}

int _tmain(int argc, _TCHAR* argv[])
{
WCHAR wszPath[] = L"D:\\TestCreateProcessWithDll.exe";
WCHAR wszDll[] = L"D:\\SampleDLL.dll";

STARTUPINFOW si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
CreateProcessWithDllW(NULL, wszPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi, wszDll, NULL);

return 0;
}

创建进程时注入DLL,码迷,mamicode.com

时间: 2024-12-21 22:34:07

创建进程时注入DLL的相关文章

一起talk C栗子吧(第一百三十三回:C语言实例--创建进程时的内存细节)

各位看官们,大家好,上一回中咱们说的是从内存角度看进程和线程的例子,这一回咱们说的例子是:创建进程时的内存细节.闲话休提,言归正转.让我们一起talk C栗子吧! 看官们,我们都知道使用fork函数可以创建一个新的进程,今天我们一起说一下使用该函数创建进程时内存的一些细节问题.我们介绍的的重点还是内存布局中的四个分区. 1.在父进程中使用fork时,会创建一个新的进程,我们叫它子进程,子进程有自己的内存空间: 2.子进程的内存空间中没有代码区,子进程和父进程共享代码区: 3.子进程的内存空间中拥

2.添加键盘钩子。向进程中注入dll

学习笔记 1.首先要建立mfc的动态链接库.在def文件中放入要导出的函数名. 2.添加函数如下 //安装钩子 //HHOOK SetWindowsHookEx( // int idHook,//钩子的类型WH_KEYBOARD键盘钩子 // HOOKPROC lpfn,//钩子的回调函数,钩子类型不同回调函数不同 // HINSTANCE hwnd,//dll动态链接库的句柄 // DWORD dwthreadid//游戏主进程的id 是GetWindowThreadProcessId的返回值

注入 - 远线程注入DLL到目标进程

// // 函数 -- void InjectDLL(LPCTSTR) // // 功能 -- 注入DLL到指定窗口 // // @param -- 要注入的DLL全路径 // // 返回值 -- 无 // void InjectDLL(LPCTSTR lpDLLFullPath) { DWORD dwPID = 0; // 目标进程PID HANDLE hGameProcess = NULL; // 目标进程句柄 LPDWORD lpdwAddr = NULL; // 远程申请的内存空间地址

一起talk C栗子吧(第一百三十四回:C语言实例--创建线程时的内存细节)

各位看官们,大家好,上一回中咱们说的是"创建进程时的内存细节"的例子,这一回咱们说的例子是:创建线程时的内存细节.闲话休提,言归正转.让我们一起talk C栗子吧! 看官们,我们在前面章回中介绍过创建线程的例子,创建线程时使用的函数是pthread_create.今天我们一起说一下使用该函数创建线程时内存的一些细节问题.我们介绍的的重点还是内存布局中的四个分区.这点和前一回中介绍创建进程时内存细节的思路相同. 1.在主线程中使用pthread_create函数可以创建一个新线程,我们叫

C#/.NET 中启动进程时所使用的 UseShellExecute 设置为 true 和 false 分别代表什么意思?

原文:C#/.NET 中启动进程时所使用的 UseShellExecute 设置为 true 和 false 分别代表什么意思? 在 .NET 中创建进程时,可以传入 ProcessStartInfo 类的一个新实例.在此类型中,有一个 UseShellExecute 属性. 本文介绍 UseShellExecute 属性的作用,设为 true 和 false 时,分别有哪些进程启动行为上的差异. 本文内容 本质差异 效果差异 如何选择 本质差异 Process.Start 本质上是启动一个新的

远程创建线程注入DLL

HANDLE WINAPI CreateRemoteThread(__in HANDLE hProcess,__in LPSECURITY_ATTRIBUTES lpThreadAttributes,__in SIZE_T dwStackSize,__in LPTHREAD_START_ROUTINE lpStartAddress,__in LPVOID lpParameter,__in DWORD dwCreationFlags,__out LPDWORD lpThreadId);hProce

如何创建DLL,以及注入DLL

为了防止忘记,特记下 DLL的创建,在VS2017中选择dll的创建 // dllmain.cpp : Defines the entry point for the DLL application. #include "stdafx.h" #include <Windows.h> #include <stdio.h> HMODULE thisModule; HHOOK hook; LRESULT CALLBACK LaunchListener(int nCod

windows目标进程注入dll

在别的程序注入dll 步骤: 1,获取目标进程ID,CreateToolhelp32Snapshot()函数; 2,获取目标进程句柄,OpenProcess()函数; 3,目标进程要一块内存,VirtualAllocEx()函数,不是VirtualAlloc()函数: 4,往要来的目标内存写入要注入的dll文件名,WriteProcessMemory; 5,拿到kernel32模块句柄,GetModuleHandle()函数; 6,拿到kernel32模块里LoadLibraryA()函数地址,

SetThreadContext注入DLL

注入DLL的方式有很多 R3就有远程线程CreateRemoteThread.SetWindowsHookEx.QueueUserApc.SetThreadContext R0可以参考sudami大神的思路 关于本文32位下参考 http://bbs.pediy.com/showthread.php?t=181174&highlight=setthreadContext+dll+%E6%B3%A8%E5%85%A5 言归正传 大体思路就是 我们先打开目标进程,枚举目标线程采用的是系统快照的方式,