某些情况下需要exe运行结束删除自身,而自删除程序有好几种方法,如下面的两方法:
a.先使自己快速退出,然后再在命令行中删除自己;或是先使自己快速退出,然后再在bat文件或脚本文件中删除自己
b. 非常经典的,关闭硬编码为4的句柄,撤销自己在内存的映射,再删除自己,再结束自己,也就是进程在未真正退出的时候就已经被删除了(只能在win2k或98使用)
int main(int argc, char *argv[])
{
HMODULE module = GetModuleHandle(0);
CHAR buf[MAX_PATH];
GetModuleFileName(module, buf, sizeof buf);
CloseHandle(HANDLE(4));
__asm {
lea eax, buf
push 0
push 0
push eax
push ExitProcess
push module
push DeleteFile
push UnmapViewOfFile//FreeLibrary;98
ret
}
return 0;
}
本文所使用的是另外一种方法:使用注入其他进程的shellcode来实现自删除,其基本原理就是:让shellcode先终止自己,然后再让shellcode删除自己。执行shellcode完毕之后,仅在内存保留一段地址,而不会产生磁盘文件,利用这个特点可以用来做'干净的'自删除程序,但会让被注入的进程造成内存泄漏,理由是注入的代码不能被释放,否则会造成调用方的访问违规。
要点: 1.本地修改API函数在远程进程的实际地址,有效的减少shellcode的代码长度,利用预定义的宏作为可能被调用的API函数的值,宏 的一个特点是,在编译的时候,只会编译成宏定义的值,而不是像变量一样被优化成地址(因为本地变量的地址在其他进程是无效的)
2.在运行时修改寄存器指向的地址,在进程virtualallocex分配一块内存,然后把得到的地址由esi指向它,
然后writeprocessmemory,esi持有的地址就会发生变化,这样就可以修改预先编码的数据,就像使用函数参数一样来传递。
基本实现: 程序自杀的原理,注入远线程到其他进程,让线程的shellcode终止掉自己,然后再调用deletefile删除自己(有点笨挫,也许可以有更好的方法),(当然也可以用来干掉其他进程,但前提是可以被结束和删除的。。)
限制:可能被杀软报警,至少读写其他进程的数据是违规的吧?
下面是代码:
//h文件//////////////////////////
//查找指针内的一个无符号的32位整数值,这个可以是个标签
void* __cdecl find_ptrvalue(__in void* pfun,__in unsigned int findvalue,__out unsigned int *ioffset );
//更完善的
void* __cdecl FindCodeValue(__in void* pcode,__in size_t codelen,__in unsigned int findvalue,__out unsigned int *ioffset );
//查找全部匹配的,并替换为指定的,并返回最后一个找到的偏移量
unsigned int __cdecl FindCodeReplaceAll(__in void* pcode,__in size_t codelen,
__in unsigned int findvalue,__in unsigned int replacevalue);
/*获得指定模块路径的在某个进程中的基地址*/
DWORD __stdcall GetModulebaseProcessW(__in DWORD dwPid,__in LPWSTR lpszDllFile);
DWORD __stdcall GetModulebaseProcessA(__in DWORD dwPid,__in LPSTR lpszDllFile);
#ifdef UNICODE
#define GetModulebaseProcess GetModulebaseProcessW
#else
#define GetModulebaseProcess GetModulebaseProcessA
#endif
/*计算远程进程调用加载模块导出函数的地址,原理:利用DLL导出函数的RVA+模块基地址*/
DWORD __stdcall GetRemoteProcAddress(
__in DWORD dwRemoteModule,
__in DWORD dwLocalModule,
__in LPCSTR lpszProcName);
/*自删除的简单实现*/
BOOL __stdcall SeltKill(__in DWORD dwInjectPid/*被注入的进程PID*/);
//下面是cpp文件///////////////////////////////////////////////////////////////////////////////////
//查找指针内的一个无符号的32位整数值,这个可以是个标签
void* __cdecl find_ptrvalue(__in void* pfun,__in unsigned int findvalue,__out unsigned int *ioffset ){
void *pret;
int i=0;
__asm{
mov eax,pfun
mov ecx,0
jmp __loop
__goon:
inc eax
inc ecx
__loop:
mov ebx,[eax]
cmp ebx,findvalue
jnz __goon
mov pret,eax
mov i,ecx
}
if(ioffset) *ioffset = i;
return pret;/*这个函数没有使用,而是使用FindCodeReplaceAll*/
}
//更完善的
void* __cdecl FindCodeValue(__in void* pcode,__in size_t codelen,__in unsigned int findvalue,__out unsigned int *ioffset ){
if(!pcode || !codelen ) return NULL;
LPDWORD dwfind =NULL;
void* presult=NULL;
PBYTE pb=(PBYTE)pcode;
for(int i =0;i<codelen-3;i++,pb++){
dwfind=(LPDWORD)pb;
if(*dwfind==findvalue){
presult=pb;
if(ioffset) *ioffset=i;
return presult;
}
}
return NULL;/*这个函数没有使用,而使用FindCodeReplaceAll*/
}
//查找全部匹配的,并替换为指定的,并返回最后一个找到的偏移量
unsigned int __cdecl FindCodeReplaceAll(__in void* pcode,__in size_t codelen, __in unsigned int findvalue,__in unsigned int replacevalue){
if(!pcode || !codelen) return 0;
unsigned int iret=0;
unsigned int *pui=0;
unsigned char *puc=(unsigned char *)pcode;
for(int i =0;i<codelen-3;i++,puc++){
pui=(unsigned int*)puc;
if(*pui==findvalue){
*pui=replacevalue;
iret=i;
}
}
return iret;
}
//获得指定进程的模块的句柄并转换为DWORD,参数lpszDllFile指定的是完全路径
DWORD __stdcall GetModulebaseProcessW(__in DWORD dwPid,__in LPWSTR lpszDllFile){
if(!dwPid || !lpszDllFile) return 0;
HANDLE hSnap=NULL;
MODULEENTRY32W me32={sizeof(me32)};
__try{
hSnap=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,dwPid);
if(hSnap==INVALID_HANDLE_VALUE) __leave;
if(!Module32FirstW(hSnap,&me32)) __leave;
do{
if(lstrcmpiW(me32.szExePath,lpszDllFile)==0)
return (DWORD)me32.modBaseAddr;
}while(Module32NextW(hSnap,&me32));
}__finally{
if(hSnap!=INVALID_HANDLE_VALUE &&hSnap!=NULL) CloseHandle(hSnap);
}
return 0;
}
DWORD __stdcall GetModulebaseProcessA(__in DWORD dwPid,__in LPSTR lpszDllFile){
WCHAR szFile[MAX_PATH];
wsprintfW(szFile,L"%S/0",lpszDllFile);
return GetModulebaseProcessW(dwPid,szFile);
}
/*计算远程进程调用加载模块导出函数的地址,原理:利用DLL导出函数的RVA+模块基地址*/
DWORD __stdcall GetRemoteProcAddress(
__in DWORD dwRemoteModule,
__in DWORD dwLocalModule,
__in LPCSTR lpszProcName){
DWORD dwfun=(DWORD)GetProcAddress((HMODULE)dwLocalModule,lpszProcName);
dwfun-=dwLocalModule;/*从当前进程加载模块获取的函数(即便导入表中不存在的,只要模块被映射到进程的内存地址空间,整个模块的导出函数都可以使用GetProcAddress来获取),得到函数的地址后,减去模块基地址得到函数在模块的RVA,利用这个RVA+其他进程模块的基地址=该函数在其他进程的真实地址*/
return dwfun+dwRemoteModule;
}
/*预定义随机值的宏,每个宏代表一个函数的地址,这些仅作为“占位”的方式存在于shellcode,是无法调用,只有替换为真实的API地址后才能调用,否则调用异常*/
#define _PRE_PID 0x88aa77cc //随机数..
#define _PRE_OpenProcess 0x99bb1234
#define _PRE_GetExitCodeProcess 0x99aa4321
#define _PRE_TerminateProcess 0xFaFaFaFa
#define _PRE_MessageBoxA 0xFA971215
#define _PRE_szFile 0xAF792151
#define _PRE_CloseHandle 0x88aa77bc
#define _PRE_DeleteFileA 0xAF782161
#define _PRE_Sleep 0xBf782115
#define _PRE_GetCurrentThreadId 0xFb564646
#define _PRE_OpenThread 0xFb564791
//////////////////////////////自删除程序//////////////////////////////////////////
BOOL __stdcall SeltKill(__in DWORD dwInjectPid){
goto _l2;
_l1:
__asm{
pushad
mov ebp,esp
sub esp,4
mov eax,_PRE_PID
push eax
push FALSE
push PROCESS_QUERY_INFORMATION|PROCESS_TERMINATE
mov edx,_PRE_OpenProcess
call edx
test eax,eax
jz _end
mov ebx,eax
lea ecx,[ebp-4]
mov edx,_PRE_GetExitCodeProcess
push ecx
push ebx
call edx
push ecx
push ebx
mov edx,_PRE_TerminateProcess
call edx
push ebx
mov edx,_PRE_CloseHandle
call edx
push 500 ;等待0.5秒再删除,或许可以更短
mov edx,_PRE_Sleep
call edx
_end:
mov edx,_PRE_DeleteFileA
mov ecx,_PRE_szFile
lea esi,[ecx]
push esi
call edx
add esp,4
popad
ret
}
_l2:
PSTR pPageCode=NULL;
ULONG ulPageCodeLen=0;
__asm{
mov eax,_l1
mov ebx,_l2
mov pPageCode,eax
sub ebx,eax
mov ulPageCodeLen,ebx
}
DWORD dwCurPid=GetCurrentProcessId();
if(dwCurPid==dwInjectPid) return FALSE;
HANDLE hprocess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwInjectPid);
if(!hprocess) return FALSE;
LPVOID lpRmCode=NULL;
LPVOID lpRmSzFile=NULL;
HANDLE hRmthd=NULL;
PSTR pMemCode=(PSTR)GlobalAlloc(GPTR,ulPageCodeLen);
if(!pMemCode) return FALSE;
CopyMemory(pMemCode,pPageCode,ulPageCodeLen);
WCHAR szkerneldir[MAX_PATH];
WCHAR sysdir[MAX_PATH];
GetSystemDirectoryW(sysdir,(ULONG)(MAX_PATH)*sizeof(WCHAR));
lstrcpynW(szkerneldir,sysdir,(lstrlenW(sysdir)+1)*sizeof(WCHAR));
StrCatW(szkerneldir,L"//kernel32.dll/0");
char szThisFile[260];
GetModuleFileNameA(NULL,szThisFile,260);
DWORD dwRemoteKernel=GetModulebaseProcessW(dwInjectPid,szkerneldir);/* kernel32在任何进程的地址都是一样的*/
DWORD dwLocalKernel=(DWORD)GetModuleHandleW(L"kernel32");
DWORD dwExportFunc=0;
__try{
dwExportFunc=GetRemoteProcAddress(dwRemoteKernel,dwLocalKernel,"OpenProcess");
if(!FindCodeReplaceAll(pMemCode,ulPageCodeLen,_PRE_OpenProcess,dwExportFunc))
__leave;
dwExportFunc=GetRemoteProcAddress(dwRemoteKernel,dwLocalKernel,"GetExitCodeProcess");
if(!FindCodeReplaceAll(pMemCode,ulPageCodeLen,_PRE_GetExitCodeProcess,dwExportFunc))
__leave;
dwExportFunc=GetRemoteProcAddress(dwRemoteKernel,dwLocalKernel,"TerminateProcess");
if(!FindCodeReplaceAll(pMemCode,ulPageCodeLen,_PRE_TerminateProcess,dwExportFunc))
__leave;
dwExportFunc=GetRemoteProcAddress(dwRemoteKernel,dwLocalKernel,"CloseHandle");
if(!FindCodeReplaceAll(pMemCode,ulPageCodeLen,_PRE_CloseHandle,dwExportFunc))
__leave;
dwExportFunc=GetRemoteProcAddress(dwRemoteKernel,dwLocalKernel,"DeleteFileA");
if(!FindCodeReplaceAll(pMemCode,ulPageCodeLen,_PRE_DeleteFileA,dwExportFunc))
__leave;
dwExportFunc=GetRemoteProcAddress(dwRemoteKernel,dwLocalKernel,"Sleep");
if(!FindCodeReplaceAll(pMemCode,ulPageCodeLen,_PRE_Sleep,dwExportFunc))
__leave;
if(!FindCodeReplaceAll(pMemCode,ulPageCodeLen,_PRE_PID,dwCurPid))
__leave;
DWORD dwszlen=lstrlenA(szThisFile)+1;
lpRmSzFile=VirtualAllocEx(hprocess,NULL,dwszlen,MEM_COMMIT,PAGE_READWRITE);
if(!lpRmSzFile) __leave;//不能释放,否则远程崩溃
if(!WriteProcessMemory(hprocess,lpRmSzFile,szThisFile,dwszlen,NULL)){
VirtualFreeEx(hprocess,lpRmSzFile,0,MEM_RELEASE);
__leave;
}
if(!FindCodeReplaceAll(pMemCode,ulPageCodeLen,_PRE_szFile,(DWORD)lpRmSzFile)){
VirtualFreeEx(hprocess,lpRmSzFile,0,MEM_RELEASE);
__leave;
}
//写代码
lpRmCode=VirtualAllocEx(hprocess,0,ulPageCodeLen,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
if(!lpRmCode){
VirtualFreeEx(hprocess,lpRmSzFile,0,MEM_RELEASE);
__leave;
}
if(!WriteProcessMemory(hprocess,lpRmCode,pMemCode,ulPageCodeLen,NULL)){
VirtualFreeEx(hprocess,lpRmCode,0,MEM_RELEASE);
VirtualFreeEx(hprocess,lpRmSzFile,0,MEM_RELEASE);
__leave;
}
hRmthd=CreateRemoteThread(hprocess,NULL,0,
(LPTHREAD_START_ROUTINE)lpRmCode,NULL,NULL,NULL);
if(!hRmthd){
VirtualFreeEx(hprocess,lpRmCode,0,MEM_RELEASE);
VirtualFreeEx(hprocess,lpRmSzFile,0,MEM_RELEASE);
__leave;
}
return TRUE;
}__finally{
if(pMemCode) GlobalFree(pMemCode);
if(hprocess) CloseHandle(hprocess);
}
return FALSE;
}
//代码完毕
上面的代码可以实现程序的自删除,其基本原理就是终止进程,等待,再删除。或许可以有更好的算法。。。
补1:
上述代码是通过远线程强制终止调用方的进程的,可能出现不确定性,例如调用方进程并未做好退出进程的准备就退出。某些情况下需要调用方进程处理完毕之后方可退出并删除,因此远线程需要执行等待,直到调用方进程自身退出进程后再删除它,利用WaitForSingleObject函数即可等待调用方进程的结束,而作为等待状态的内核对象可以是调用方的的进程句柄,因为一旦进程退出后,该句柄就会无状态,那么WaitForSingleObject就不再等待,继而执行之后的删除函数。
即把上面的代码改这个,注意查找WaitForSingleObject的地址!
__asm{
pushad
mov eax,_PRE_PID
push eax
push FALSE
push SYNCHRONIZE
mov edx,_PRE_OpenProcess
call edx
test eax,eax
jz _end
mov ebx,eax
mov edx,_PRE_WaitForSingleObject
push INFINITE ;无限等待,也可另设时间
push ebx
call edx
;waiting...process exit
mov edx,_PRE_CloseHandle
push ebx
call edx
_end: ;假设进程句柄打不开就删除
mov ecx,_PRE_szFile
lea esi,[ecx]
mov edx,_PRE_DeleteFileA
push esi
call edx
popad
ret
}
也就是调用WaitForSingleObject等待进程,如果进程退出了,WaitForSingleObject就没状态了,就不会挂起线程了,那么之后的语句就会执行。
这样做有个前提,就是被注入的进程必须长时间驻机运行,如果先退出,就没有作用了。