用程式注入來實現一個殼(原理)
用程式注入來實現一個殼(原理)
作者: Simonzh2000[US]
感謝 too2y , 老王.
這是我在學習老王的殼後, 參考網上的一些例子做的。
比老王的殼差的太遠, 希望各位不要見笑.
如果各位寫出了類似的加殼器, 希望發一個給我。
啟動前先啟動 Calc.exe, 改一下, 用 Explorer.exe 也可以。
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <tchar.h>
#include <conio.h>
#include <psapi.h>
typedef struct _remoteparameter
{
DWORD rpwinexec;
DWORD rpcreatemutex;
DWORD rpsleep;
DWORD rpclosehandle;
char rpwinexecname[MAX_PATH];
HANDLE rphMutex;
TCHAR rpMutex[30];
}REMOTEPARAMETER, *PREMOTEPARAMETER;
DWORD WINAPI remote(LPVOID);
DWORD processtopid(TCHAR*);
HANDLE createremote(PTSTR);
TCHAR cMutex[8];
int main()
{
TCHAR ExeName[MAX_PATH];
HANDLE hRemoteThread;
HANDLE hMutex;
int ret;
_tcscpy(cMutex,_T("simonzh"));
hMutex = OpenMutex(SYNCHRONIZE, TRUE, cMutex );
if (hMutex == NULL)
{
ret=GetModuleFileName(NULL,ExeName,MAX_PATH);
if(ret==0)
{
OutputDebugString(_T("GetModuleFileName Error\n"));
getche();
return -1;
}
if((hRemoteThread=createremote(ExeName))==NULL)
{
OutputDebugString(_T("CreateRemote Error\n"));
getche();
return -1;
}
return 0;
}
CloseHandle(hMutex);
// 上面相當於一個殼的 Loader
// 下面相當於被加殼的原程式.
_tprintf(_T("---[ This is not me. HaHaHa... ]---\n"));
getche();
return 0;
}
DWORD processtopid(TCHAR *processname)
{
DWORD lpidprocesses[1024],cbneeded,cprocesses;
HANDLE hprocess;
HMODULE hmodule;
UINT i;
TCHAR normalname[MAX_PATH]=_T("UnknownProcess");
if(!EnumProcesses(lpidprocesses,sizeof(lpidprocesses),&cbneeded))
{
OutputDebugString(_T("EnumProcesses Error\n"));
return -1;
}
cprocesses=cbneeded/sizeof(DWORD);
for(i=0;i<cprocesses;i++)
{
hprocess=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,lpidprocesses[i]);
if(hprocess)
{
if(EnumProcessModules(hprocess,&hmodule,sizeof(hmodule),&cbneeded))
{
GetModuleBaseName(hprocess,hmodule,normalname,sizeof(normalname));
if(!_tcsicmp(normalname,processname))
{
CloseHandle(hprocess);
return (lpidprocesses[i]);
}
}
}
}
CloseHandle(hprocess);
return 0;
}
HANDLE createremote(PTSTR ExeName)
{
HANDLE ethread;
HANDLE rphandle;
TCHAR name[15];
TCHAR *remotethr;
TCHAR *remotepar;
DWORD remotepid;
int cb;
HINSTANCE hkernel32;
REMOTEPARAMETER rp;
_tcscpy(name,_T("Calc.exe"));
while(1)
{
remotepid=processtopid(name);
if(remotepid==-1)
{
return NULL;
}
else if(remotepid==0)
{
OutputDebugString(_T("Remote Process isn't running\n"));
Sleep(1000);
continue;
}
rphandle=OpenProcess(PROCESS_CREATE_THREAD |
PROCESS_VM_OPERATION |
PROCESS_VM_WRITE,
FALSE,remotepid);
if(rphandle==NULL)
{
Sleep(1000);
continue;
}
else
{
break;
}
}
cb=sizeof(TCHAR)*4*1024;
remotethr=(PTSTR)VirtualAllocEx(rphandle,NULL,cb,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
if(remotethr==NULL)
{
OutputDebugString(_T("VirtualAllocEx for Thread Error\n"));
CloseHandle(rphandle);
return NULL;
}
if(WriteProcessMemory(rphandle,remotethr,(LPVOID)remote,cb,NULL)==FALSE)
{
OutputDebugString(_T("WriteProcessMemory for Thread Error\n"));
CloseHandle(rphandle);
return NULL;
}
{
memset(&rp,0,sizeof(rp));
_tcscpy(rp.rpMutex, cMutex);
WideCharToMultiByte(CP_ACP,0,ExeName,-1,rp.rpwinexecname,_tcslen(ExeName),NULL,NULL);
hkernel32=GetModuleHandle(_T("kernel32.dll"));
rp.rpwinexec=(DWORD)GetProcAddress(hkernel32,"WinExec");
rp.rpcreatemutex=(DWORD)GetProcAddress(hkernel32,"CreateMutexW");
rp.rpsleep=(DWORD)GetProcAddress(hkernel32,"Sleep");
rp.rpclosehandle=(DWORD)GetProcAddress(hkernel32,"CloseHandle");
}
cb=sizeof(TCHAR)*sizeof(rp);
remotepar=(PTSTR)VirtualAllocEx(rphandle,NULL,cb,MEM_COMMIT,PAGE_READWRITE);
if(remotepar==NULL)
{
OutputDebugString(_T("VirtualAllocEx for Parameter Error\n"));
CloseHandle(rphandle);
return NULL;
}
if(WriteProcessMemory(rphandle,remotepar,(LPVOID)&rp,cb,NULL)==FALSE)
{
OutputDebugString(_T("WriteProcessMemory for Parameter Error:"));
CloseHandle(rphandle);
return NULL;
}
ethread=CreateRemoteThread(rphandle,NULL,0,(LPTHREAD_START_ROUTINE)remotethr,(LPVOID)remotepar,0,NULL);
if(ethread==NULL)
{
OutputDebugString(_T("CreateRemoteThread Error\n"));
CloseHandle(rphandle);
return NULL;
}
return ethread;
}
DWORD WINAPI remote(LPVOID pvparam)
{
PREMOTEPARAMETER erp=(PREMOTEPARAMETER)pvparam;
typedef UINT (WINAPI *EWinExec)(LPCSTR, UINT);
typedef HANDLE (WINAPI *ECreateMutex)(LPSECURITY_ATTRIBUTES , BOOL, LPCTSTR);
typedef VOID (WINAPI *ESleep)(DWORD);
typedef BOOL (WINAPI *ECloseHandle)(HANDLE);
EWinExec tWinExec;
ECreateMutex tCreateMutex;
ESleep tSleep;
ECloseHandle tCloseHandle;
tWinExec=(EWinExec)erp->rpwinexec;
tCreateMutex=(ECreateMutex)erp->rpcreatemutex;
tSleep=(ESleep)erp->rpsleep;
tCloseHandle=(ECloseHandle)erp->rpclosehandle;
erp->rphMutex=tCreateMutex(NULL, TRUE, erp->rpMutex);
if(tWinExec(erp->rpwinexecname, SW_SHOW)<=31)
{
return -1;
}
tSleep(4000);
tCloseHandle(erp->rphMutex);
return 0;
}
相關文章
- iOS應用程式的脫殼實現原理淺析2019-03-04iOS
- 【ZJOF】用來個棧來實現一個佇列2018-08-14佇列
- 十幾行程式碼實現一個ts依賴注入2019-01-28行程依賴注入
- 安卓整體加殼(一代殼)原理及實踐2024-09-15安卓
- 用kotlin來實現一個餅圖2017-12-08Kotlin
- [翻譯]利用程式碼注入脫殼2015-11-15
- Flutter動畫:用Flutter來實現一個拍手動畫2019-09-30Flutter動畫
- Spring方法注入的使用與實現原理2020-05-13Spring
- 來來來,探究一下CommonJs的實現原理2018-08-27JS
- 如何實現一個簡易版的 Spring - 如何實現 Setter 注入2020-11-29Spring
- 求教:注入的具體實現類是哪一個?2012-07-15
- 利用反射機制實現依賴注入的原理2019-02-16反射依賴注入
- SAP Spartacus 標準的 Effects 實現的注入原理2021-09-11
- 殼的工作原理脫殼2013-04-10
- c++實現的一種程式碼膨脹變形殼2018-01-10C++
- Scala的Cake實現依賴注入是一個謊言2014-12-24依賴注入
- 用kotlin來實現一個打方塊的小遊戲2017-12-25Kotlin遊戲
- 實現呼叫加殼的外殼中的子程式的一點見解。。 (1千字)2002-10-21
- 如何自己實現一個 mobx – 原理解析2019-03-04
- 如何自己實現一個 mobx - 原理解析2017-04-25
- 用 Dagger 2 實現依賴注入2017-06-19依賴注入
- Guice指南-用Guice實現依賴注入2008-03-21GUI依賴注入
- 一個新殼,附主程式和原始碼2004-11-11原始碼
- promise原理—一步一步實現一個promise2019-04-27Promise
- java實現一個洗牌程式2011-02-24Java
- 用程式碼探討KVC/KVO的實現原理2019-02-27
- 用程式碼探討 KVC/KVO 的實現原理2018-04-05
- 用java實現一個簡單的房屋管理程式。 (轉)2007-12-11Java
- 用一個實際例子理解Dockervolume工作原理2018-11-23Docker
- Angular 依賴注入機制實現原理的深入介紹2022-07-20Angular依賴注入
- 用 Go 實現一個 LRU cache2021-12-20Go
- AsyncTask實現程式碼原理2013-11-13
- 【半小時大話.net依賴注入】(一)理論基礎+實戰控制檯程式實現AutoFac注入2019-07-08依賴注入
- 用一個棧實現另一個棧的排序2017-05-09排序
- 利用ConcurrentHashMap來實現一個ConcurrentHashSet2018-09-03HashMap
- 來實現一個簡易版的 Promise2018-12-02Promise
- 用 PHP 來實現微信跳一跳2018-01-06PHP
- 窺探原理:實現一個簡單的前端程式碼打包器 Roid2019-02-16前端