#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
typedef int (__stdcall *PMESSAGEBOX)(HWND ,LPCTSTR ,LPCTSTR ,UINT);
typedef struct _CODE_ARGS_
{
PMESSAGEBOX pMessageBox;
HWND hWnd;
LPCTSTR lpText;
LPCTSTR lpCaption;
UINT uType;
}CODE_ARGS;
DWORD Inject_Fun(CODE_ARGS *pCodeArgs)
{
pCodeArgs->pMessageBox(pCodeArgs->hWnd,pCodeArgs->lpText,pCodeArgs->lpCaption,1);
return 0;
}
void Inject_Fun_End(void)
{
}
int main(int argc, char* argv[])
{
DWORD PID = 0;
puts("Input Target Process ID:\n");
scanf("%u",&PID);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,PID);
DWORD dw = GetLastError();
void *pRemoteAddr = VirtualAllocEx(hProcess,NULL,1024,MEM_COMMIT,PAGE_EXECUTE_READWRITE);//开辟空间
CODE_ARGS Code_Args = {0};
Code_Args.pMessageBox = (PMESSAGEBOX)GetProcAddress(LoadLibrary("User32.dll"),"MessageBoxA");
Code_Args.hWnd = NULL;
Code_Args.uType = 1;
char TextArr[] = "Hello Boys I Come Here!";
char CaptionArr[] = "Code Inject!";
DWORD uType = 1;
void *pRemoteArgAddr = NULL;
void *pRemoteProc = NULL;
DWORD dwOffset = 0,dwWriteByte = 0;
//写入结构体两个字符串
Code_Args.lpText = (char *)pRemoteAddr;
WriteProcessMemory(hProcess,(void *)((DWORD)pRemoteAddr + dwOffset),TextArr,strlen(TextArr)+1,&dwWriteByte);
dwOffset += dwWriteByte;
Code_Args.lpCaption = (char *)((DWORD)pRemoteAddr + dwOffset);
WriteProcessMemory(hProcess,(void *)((DWORD)pRemoteAddr + dwOffset),CaptionArr,strlen(CaptionArr)+1,&dwWriteByte);
dwOffset += dwWriteByte;
//写入结构体
pRemoteArgAddr = (BYTE *)pRemoteAddr + dwOffset;
WriteProcessMemory(hProcess,pRemoteArgAddr,&Code_Args,sizeof(Code_Args),&dwWriteByte);
dwOffset += dwWriteByte;
//写入函数机器码 写入长度在release下好用,debug版本需要修复
pRemoteProc = (BYTE *)pRemoteAddr + dwOffset;
WriteProcessMemory(hProcess,pRemoteProc,(void *)Inject_Fun,(DWORD)Inject_Fun_End - (DWORD)Inject_Fun,&dwWriteByte);
DWORD TID = 0;
HANDLE hRemotethread = CreateRemoteThread(hProcess,NULL,0,(LPTHREAD_START_ROUTINE)pRemoteProc,pRemoteArgAddr,0,&TID);
WaitForSingleObject(hRemotethread,INFINITE);
CloseHandle(hRemotethread);
VirtualFreeEx(hProcess,pRemoteAddr,0,MEM_RELEASE);
CloseHandle(hProcess);
return 0;
}
代码注入
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling。字面上理解Me...
- 本分享的想法源于看了这篇分享由于在对Unity项目后期进行lua热更新方案实施, 我也不想造成源代码的修改, 故在...