利用CreateRemoteThread 实现远程代码注入的例子!!

#include "stdafx.h"


#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
void CheckError ( int, int, char *); //出错处理函数
PDWORD pdwThreadId; 
HANDLE hRemoteThread, hRemoteProcess;
DWORD fdwCreate, dwStackSize, dwRemoteProcessId;

PWSTR pszLibFileRemote=NULL;

//需要管理员权限启动 或者自动提权就好!!!

void main(int argc,char **argv)
{
//SendMessage(FindWindow(0,0),WM_SYSCOMMAND,SC_MONITORPOWER,2);//关闭  
//::Sleep(5000);  
//SendMessage(FindWindow(0,0),WM_SYSCOMMAND,SC_MONITORPOWER,-1);//打开  
//return;
int iReturnCode;
char lpDllFullPathName[MAX_PATH];
WCHAR pszLibFileName[MAX_PATH]={0};
dwRemoteProcessId = 6348; 
strcpy(lpDllFullPathName, "D:\\jdtest\\zhuru\\Debug\\dllTest.dll");
//将DLL文件全路径的ANSI码转换成UNICODE码
iReturnCode = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS,
lpDllFullPathName, strlen(lpDllFullPathName),
pszLibFileName, MAX_PATH);
CheckError(iReturnCode, 0, "MultByteToWideChar");
//打开远程进程
hRemoteProcess = OpenProcess(PROCESS_CREATE_THREAD | //允许创建线程 
PROCESS_VM_OPERATION | //允许VM操作
PROCESS_VM_WRITE, //允许VM写
FALSE, dwRemoteProcessId ); 
CheckError( (int) hRemoteProcess, NULL, "Remote Process not Exist or Access Denied!");
//计算DLL路径名需要的内存空间
int cb = (1 + lstrlenW(pszLibFileName)) * sizeof(WCHAR);
pszLibFileRemote = (PWSTR) VirtualAllocEx( hRemoteProcess, NULL, cb, MEM_COMMIT, PAGE_READWRITE);
CheckError((int)pszLibFileRemote, NULL, "VirtualAllocEx");
//将DLL的路径名复制到远程进程的内存空间
iReturnCode = WriteProcessMemory(hRemoteProcess, pszLibFileRemote, (PVOID) pszLibFileName, cb, NULL);
CheckError(iReturnCode, false, "WriteProcessMemory");
//计算LoadLibraryW的入口地址 
PTHREAD_START_ROUTINE pfnStartAddr = (PTHREAD_START_ROUTINE)
GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "LoadLibraryW");
CheckError((int)pfnStartAddr, NULL, "GetProcAddress");
//启动远程线程,通过远程线程调用用户的DLL文件 
hRemoteThread = CreateRemoteThread( hRemoteProcess, NULL, 0, pfnStartAddr, pszLibFileRemote, 0, NULL);
CheckError((int)hRemoteThread, NULL, "Create Remote Thread");
getchar();
//等待远程线程退出
WaitForSingleObject(hRemoteThread, INFINITE);
//清场处理
if (pszLibFileRemote != NULL)
{
VirtualFreeEx(hRemoteProcess, pszLibFileRemote, 0, MEM_RELEASE);
}
if (hRemoteThread != NULL) 
{
CloseHandle(hRemoteThread );
}
if (hRemoteProcess!= NULL) 
{
CloseHandle(hRemoteProcess);
}
}
//错误处理函数CheckError()
void CheckError(int iReturnCode, int iErrorCode, char *pErrorMsg)
{
if(iReturnCode==iErrorCode)
{
printf("%s Error:%d\n\n", pErrorMsg, GetLastError());
//清场处理
if (pszLibFileRemote != NULL)
{
VirtualFreeEx(hRemoteProcess, pszLibFileRemote, 0, MEM_RELEASE);
}
if (hRemoteThread != NULL) 
{
CloseHandle(hRemoteThread );
}
if (hRemoteProcess!= NULL)
{
CloseHandle(hRemoteProcess);
}
exit(0);
}
}




/*
#include <windows.h>
#include <TlHelp32.h>
#include <iostream>
#include <time.h>


// 提升进程访问权限
bool enableDebugPriv()
{
HANDLE  hToken;
LUID    sedebugnameValue;
TOKEN_PRIVILEGES tkp;
if  ( !OpenProcessToken(  GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)
)
{
return false;
}
if( !LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue) )
{
CloseHandle(hToken);
return false;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if( !AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL) )
{
CloseHandle(hToken);
return false;
}
return true;
}


// 根据进程名称得到进程ID,如果有多个运行实例的话,返回第一个枚举到的进程的ID
DWORD processNameToId(LPCTSTR lpszProcessName)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);
if( !Process32First(hSnapshot, &pe) )
{
MessageBox( NULL,
"The frist entry of the process list has not been copyied to the buffer",
"Notice",
MB_ICONINFORMATION | MB_OK
);
return 0;
}
while( Process32Next(hSnapshot, &pe) )
{
if( !strcmp(lpszProcessName, pe.szExeFile) )
{
return pe.th32ProcessID;
}
}
return 0;
}


int main(int argc, char* argv[])
{
// 定义线程体的大小
const DWORD dwThreadSize = 5 * 1024;
DWORD dwWriteBytes;
// 提升进程访问权限
enableDebugPriv();
// 等待输入进程名称,注意大小写匹配
std::cout << "Please input the name of target process !" << std::endl;
char szExeName[MAX_PATH] = { 0 };
std::cin >> szExeName;
DWORD dwProcessId = processNameToId(szExeName);
if( dwProcessId == 0 )
{
MessageBox( NULL,
"The target process have not been found !",
"Notice",
MB_ICONINFORMATION | MB_OK
);
return -1;
}


// 根据进程ID得到进程句柄
HANDLE hTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
if( !hTargetProcess )
{
MessageBox( NULL,
"Open target process failed !",
"Notice",
MB_ICONINFORMATION | MB_OK
);
return 0;
}


// 在宿主进程中为线程体开辟一块存储区域
// 在这里需要注意MEM_COMMIT内存非配类型以及PAGE_EXECUTE_READWRITE内存保护类型
// 其具体含义请参考MSDN中关于VirtualAllocEx函数的说明。
void* pRemoteThread = VirtualAllocEx(   hTargetProcess,
0,
dwThreadSize,
MEM_COMMIT , PAGE_EXECUTE_READWRITE);
if( !pRemoteThread )
{
MessageBox( NULL,
"Alloc memory in target process failed !",
"notice",
MB_ICONINFORMATION | MB_OK
);
return 0;
}
// 设置需要注入的DLL名称
char szDll[256];
memset(szDll, 0, 256);
strcpy(szDll, "D:\\jdtest\\zhuru\\Debug\\dllTest.dll");
// 拷贝注入DLL内容到宿主空间
if( !WriteProcessMemory(    hTargetProcess,
pRemoteThread,
(LPVOID)szDll,
dwThreadSize,
0) )
{
MessageBox( NULL,
"Write data to target process failed !",
"Notice",
MB_ICONINFORMATION | MB_OK
);
return 0;
}


LPVOID pFunc = LoadLibraryA;
//在宿主进程中创建线程
HANDLE hRemoteThread = CreateRemoteThread(  hTargetProcess,
NULL,
0,
(LPTHREAD_START_ROUTINE)pFunc,
pRemoteThread,
0,
&dwWriteBytes);
if( !hRemoteThread )
{
MessageBox(    NULL,
"Create remote thread failed !",
"Notice",
MB_ICONINFORMATION | MB_OK
);
return 0;
}
// 等待LoadLibraryA加载完毕
WaitForSingleObject(hRemoteThread, INFINITE );
VirtualFreeEx(hTargetProcess, pRemoteThread, dwThreadSize, MEM_COMMIT);
CloseHandle( hRemoteThread );
CloseHandle( hTargetProcess );
return 0;
}*/




资源 https://download.csdn.net/download/jangdong/10521681

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一段远程线程注入代码例子: ``` #include <Windows.h> #include <TlHelp32.h> DWORD GetProcessIdByName(const wchar_t* processName) { DWORD processId = 0; HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (snapshot != INVALID_HANDLE_VALUE) { PROCESSENTRY32 processEntry = { 0 }; processEntry.dwSize = sizeof(PROCESSENTRY32); if (Process32First(snapshot, &processEntry)) { do { if (_wcsicmp(processEntry.szExeFile, processName) == 0) { processId = processEntry.th32ProcessID; break; } } while (Process32Next(snapshot, &processEntry)); } CloseHandle(snapshot); } return processId; } int main() { const wchar_t* targetProcessName = L"notepad.exe"; const wchar_t* dllPath = L"C:\\path\\to\\mydll.dll"; DWORD targetProcessId = GetProcessIdByName(targetProcessName); if (targetProcessId == 0) { printf("Target process not found.\n"); return 1; } HANDLE targetProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, targetProcessId); if (targetProcess == NULL) { printf("Failed to open target process.\n"); return 1; } LPVOID dllPathAddress = VirtualAllocEx(targetProcess, NULL, wcslen(dllPath) * sizeof(wchar_t), MEM_COMMIT, PAGE_READWRITE); if (dllPathAddress == NULL) { printf("Failed to allocate memory in target process.\n"); CloseHandle(targetProcess); return 1; } if (!WriteProcessMemory(targetProcess, dllPathAddress, dllPath, wcslen(dllPath) * sizeof(wchar_t), NULL)) { printf("Failed to write to target process memory.\n"); VirtualFreeEx(targetProcess, dllPathAddress, 0, MEM_RELEASE); CloseHandle(targetProcess); return 1; } HMODULE kernel32Module = GetModuleHandle(L"kernel32.dll"); if (kernel32Module == NULL) { printf("Failed to get kernel32 module handle.\n"); VirtualFreeEx(targetProcess, dllPathAddress, 0, MEM_RELEASE); CloseHandle(targetProcess); return 1; } LPVOID loadLibraryAddress = GetProcAddress(kernel32Module, "LoadLibraryW"); if (loadLibraryAddress == NULL) { printf("Failed to get LoadLibraryW address.\n"); VirtualFreeEx(targetProcess, dllPathAddress, 0, MEM_RELEASE); CloseHandle(targetProcess); return 1; } HANDLE remoteThread = CreateRemoteThread(targetProcess, NULL, 0, (LPTHREAD_START_ROUTINE)loadLibraryAddress, dllPathAddress, 0, NULL); if (remoteThread == NULL) { printf("Failed to create remote thread.\n"); VirtualFreeEx(targetProcess, dllPathAddress, 0, MEM_RELEASE); CloseHandle(targetProcess); return 1; } WaitForSingleObject(remoteThread, INFINITE); VirtualFreeEx(targetProcess, dllPathAddress, 0, MEM_RELEASE); CloseHandle(remoteThread); CloseHandle(targetProcess); printf("Injection succeeded.\n"); return 0; } ``` 请注意,这只是一个示例代码,实际使用时需要根据具体情况进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值