当前位置:网站首页>Remote code injection
Remote code injection
2022-06-12 13:56:00 【HyperCall】
TIPS
1.
When the writing process was hidden, I wrote dll Injection tool , This tool will write to the target process dll name , And then use it loadlirary load dll. How to make that program call loadlirary What about ? It is used. CreateRemoteThread This function , Creating remote threads , Why should Microsoft provide such a function ? In fact, it is not specifically provided by Microsoft to inject into us API, This API The actual function of is to create remote threads , The thread prototype created is DWORD WINAPI ThreadProc(LPVOID lparam), There is only one parameter , And the parameter is a pointer to any content , and loadlirary The prototype of the function is HMODULE WINAPI LoadLibrary( LPCTSTR lpFileName), It can be seen that the two of them are very close , All are 1 Parameters , That parameter is a pointer , So this function is developed to inject dll Of API, And it won't go wrong , According to the grammar , But the running thread function is replaced with LoadLibrary function , Then run the thread = load dll, And then dllmain It runs all kinds of code .
2.
But in fact, this way is not hidden enough , Because you loaded... In the program DLL, There is a great chance that such a big movement will be detected , And since you have dll The entity will respond quickly by the cloud , let me put it another way , This injection dll The way is easy to intercept
3.
We can actually use it CreateRemoteThread The original function of the function , That is, run directly ThreadProc, And this ThreadProc The code of the function passes WriteProcessMemory To write , The parameters of the function lparam The content pointed to can be written in the same way , This call CreateRemoteThread Will directly run the ThreadProc function
4.
stay ThreadProc Call in function API At that time, we were not sure whether the program we injected had loaded this API Of DLL, So we usually need to call something API To join him LoadLibrary, To load this api Of dll
5.
Of the core principles of reverse engineering ThreadProc Yes LoadLibrary Plus call GetprocAddress Then dynamically call MessageBoxA This step , It feels complicated and unnecessary , Send it directly to him MessageBoxA The address of , After testing, there is no problem
6.
Call important API There is a parameter you need to know ThreadProc The size of the entire function , Here is a clever use of (DWORD)ThreadProc-(DWORD)Code_Inject To calculate the , You can imagine , These two functions are next to each other , And when writing ThreadProc stay Code_Inject Back , Is it possible to get ThreadProc The size of ? If you still don't know, you can debug it
CodeInject.h
#include "windows.h"
#include <iostream>
using namespace std;
//*******************************Params Definition *****************************************
typedef struct _THREAD_PARAM{
FARPROC pFunc[2];
char szbuf[2][128];
}THREAD_PARAM,*PTHREAD_PARAM;
//*******************************Windows API Definition *****************************************
typedef HMODULE (WINAPI *PFLOADLIBRARY)(LPCSTR lpLibFileName); //LoadLibrary()
typedef int (WINAPI *PFMESSAGEBOXA)(HWND hwnd,LPCSTR lptext,LPCSTR lpcaption,UINT utype); //MessageBoxA()
DWORD WINAPI ThreadProc(LPVOID lparam); //injected-thread
BOOL EnableDebugPrivilege(); // Weighting function
void Code_Inject(); // Code injection function CodeInject.cpp
#include "CodeInject.h"
DWORD Inject_Pid=0;
THREAD_PARAM param={
0,};
int main(){
cout<<"plz input pid you want to inject"<<endl;
cin>>Inject_Pid;
LoadLibraryA("user32.dll");
//**************************** Set up Params Parameters *********************************
HMODULE hmod1=GetModuleHandleA("kernel32.dll");
HMODULE hmod2=GetModuleHandleA("user32.dll");
param.pFunc[0]=GetProcAddress(hmod1,"LoadLibraryA"); // Make sure user32.dll Loaded
param.pFunc[1]=GetProcAddress(hmod2,"MessageBoxA"); // call MessageBoxA
strcpy_s(param.szbuf[0],"user32.dll"); //LoadLibraryA Parameters of
strcpy_s(param.szbuf[1],"Injected"); //MessageBoxA Parameters of
//**************************** Set up Params complete *********************************
Code_Inject();
return 0;
}
void Code_Inject(){
if(!EnableDebugPrivilege()){ // Raise the right
cout<<"EnableDebugPrivilege Failed";
system("pause");
return;
}
HANDLE hprocess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,Inject_Pid); // Get process handle
LPVOID param_address=VirtualAllocEx(hprocess,NULL,sizeof(THREAD_PARAM),MEM_COMMIT,PAGE_READWRITE); // Apply to deposit param Space
LPVOID ThreadProc_address=VirtualAllocEx(hprocess,NULL,(DWORD)ThreadProc-(DWORD)Code_Inject,MEM_COMMIT,PAGE_READWRITE);// Apply to deposit ThreadProc Space ,ThreadProc Size through Code_Inject-ThreadProc obtain ( Note that this cannot be a negative number ?)
WriteProcessMemory(hprocess,param_address,(LPVOID)¶m,sizeof(THREAD_PARAM),NULL); // write in param
WriteProcessMemory(hprocess,ThreadProc_address,(LPVOID)ThreadProc,(DWORD)ThreadProc-(DWORD)Code_Inject,NULL); // write in ThreadProc
HANDLE hthread=CreateRemoteThread(hprocess,NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc_address,param_address,0,NULL); // establish ThreadProc Threads
WaitForSingleObject(hthread,INFINITE);
CloseHandle(hthread);
CloseHandle(hprocess);
}
DWORD WINAPI ThreadProc(LPVOID lparam){
PTHREAD_PARAM pParam=(PTHREAD_PARAM)lparam;
((PFLOADLIBRARY)pParam->pFunc[0])(pParam->szbuf[0]); //LoadLibraryA("user32.dll")
((PFMESSAGEBOXA)pParam->pFunc[1])(NULL,pParam->szbuf[1],pParam->szbuf[1],NULL); //MessageBoxA(NULL,"Injected","Injected",NULL);
return 0;
}
BOOL EnableDebugPrivilege(){
HANDLE hToken;
BOOL fOk=FALSE;
if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken)){ //Get Token
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount=1;
if(!LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tp.Privileges[0].Luid))//Get Luid
cout<<"Can't lookup privilege value"<<endl;
tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;// That's the key , Modify its attribute to SE_PRIVILEGE_ENABLED
if(!AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(tp),NULL,NULL))//Adjust Token
cout<<"Can't adjust privilege value"<<endl;
fOk=(GetLastError()==ERROR_SUCCESS);
CloseHandle(hToken);
hToken=NULL;
}
return fOk;
}边栏推荐
- Recursion of subviews of view
- Qt5 plug-in production
- 阿里云开发板vscode开发环境搭建
- 当字节跳动在美国输出中国式 996
- Formal analysis of Woo Lam protocol with scyther tool
- Explanation of static and extern keywords
- Alibaba cloud development board haas510 responds to UART serial port instructions
- [semidrive source code analysis] [x9 chip startup process] 26 - LK of R5 safetyos_ INIT_ LEVEL_ Target phase code flow analysis (TP drvier, audio server initialization)
- Cmake basic tutorial - 01 a-hello-cmake
- Codeforces 1629 F1. Game on sum (easy version) - DP, game, thinking
猜你喜欢

Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform

Mold and remainder

Scyther工具形式化分析Woo-Lam协议

Alibaba cloud development board haas510 parses serial port JSON data and sends attributes

Démontage et modification de la machine publicitaire - décompression amateur

chrome://tracing Performance analysis artifact

阿里云开发板HaaS510响应UART串口指令

当字节跳动在美国输出中国式 996

Go language functions as parameters of functions

Acwing: topology sequence
随机推荐
Player screen orientation scheme
【mysql进阶】mysql索引数据结构的演变(四)
Dial up and Ethernet
CSDN blog points rule
Cdeforces 1638 C. inversion graph - simple thinking
Programmer interview golden classic good question / interview question 01.05 Edit once
Now you must know the pointer
Alibaba cloud development board haas510 submission device attributes
阿里云开发板HaaS510连接物联网平台--HaaS征文
一种快速创建测试窗口的方法
初学者入门阿里云haas510开板式DTU(2.0版本)--510-AS
2021-05-28
Codeforces 1629 D. pecuriar movie preferences - simple thinking, palindrome strings
M1 pod install pod lint failure solution
Cmake basic tutorial - 01 a-hello-cmake
Paw advanced user guide
阿里云开发板vscode开发环境搭建
Acwing: topology sequence
对于跨境电商,更侧重收入的出价策略 —Google SEM
go-zero 微服务实战系列(二、服务拆分)