首页
论坛
专栏
课程

[转帖]远程卸载DLL代码

2008-8-11 07:47 8714

[转帖]远程卸载DLL代码

2008-8-11 07:47
8714
BOOL UnLoadModules( LPCTSTR processname , LPCTSTR modulename) 
{ 
HANDLE hModuleSnap = INVALID_HANDLE_VALUE; 
MODULEENTRY32 me32; 
HANDLE hpro;
DWORD modulebase;
DWORD pid=GetProcessIdByName(processname);

hpro= OpenProcess
   (
   PROCESS_ALL_ACCESS,
   TRUE,
   pid
       );

hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, pid ); 
if( hModuleSnap == INVALID_HANDLE_VALUE ) 
{ 
   
    return( FALSE ); 
}

me32.dwSize = sizeof( MODULEENTRY32 ); 

if( !Module32First( hModuleSnap, &me32 ) ) 
{ 

    CloseHandle( hModuleSnap );
    return( FALSE ); 
}

do 
{ 
printf( "\n\n     MODULE NAME:     %s",             me32.szModule ); 
    printf( "\n     executable     = %s",             me32.szExePath ); 
    printf( "\n     process ID     = 0x%08X",         me32.th32ProcessID ); 
    printf( "\n     ref count (g) =     0x%04X",     me32.GlblcntUsage ); 
    printf( "\n     ref count (p) =     0x%04X",     me32.ProccntUsage ); 
    printf( "\n     base address   = 0x%08X", (DWORD) me32.modBaseAddr ); 
    printf( "\n     base size      = %d",             me32.modBaseSize );


if(!strcmpi(me32.szModule, modulename))
{
modulebase=(DWORD)me32.modBaseAddr;
printf("module :%s found at :%x\n",modulename,modulebase);
break;

}

} while( Module32Next( hModuleSnap, &me32 ) );

ZwUnmapViewOfSection(hpro,(DWORD)modulebase);
CloseHandle( hModuleSnap ); 
return( TRUE ); 
}

DWORD GetProcessIdByName(LPCTSTR name)
{

PROCESSENTRY32 prostruct;
    DWORD id = 0;
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    prostruct.dwSize = sizeof(PROCESSENTRY32);

if(!Process32First(hSnapshot,&prostruct))
    return 0;

do

{
   prostruct.dwSize = sizeof(PROCESSENTRY32);
        if(!Process32Next(hSnapshot,&prostruct))
           break;

   if(strcmp(prostruct.szExeFile,name) == 0)

   {
    id = prostruct.th32ProcessID;
    break;
   }

}while(TRUE);

CloseHandle(hSnapshot);
return id;
}

ZwUnmapViewOfSection这个NTDLL中的函数的地址自己用GetProcAddress就可以得到引用了



[公告]安全服务和外包项目请将项目需求发到看雪企服平台:https://qifu.kanxue.com

最新回复 (8)
combojiang 26 2008-8-11 09:02
2
0
学习123456
鸡蛋壳 2008-8-11 23:02
3
0
//少女式卸载,1参数为PID,2参数为dll名称

int UnInjectDll(ULONG dwProcessId,CString DllName)
{

const DWORD dwThreadSize = 4096;
     Quanxian->EnablePriv();   //提升权限的,这个函数你自己写吧,贴出来占地方
//以下为创建远程线程做准备
// 取得LoadLibraryA函数的地址,我们将以它作为远程线程函数启动
HMODULE hModule=::GetModuleHandle("kernel32.dll");
LPTHREAD_START_ROUTINE pfnStartRoutine = (LPTHREAD_START_ROUTINE)::GetProcAddress(hModule, "GetModuleHandleA");
LPTHREAD_START_ROUTINE pfnFreeRoutine = (LPTHREAD_START_ROUTINE)::GetProcAddress(hModule, "FreeLibrary");
char DllPath[MAX_PATH];
memset(DllPath, 0, MAX_PATH);
int nSel = m_modulelist.GetSelectionMark();
m_modulelist.GetItemText(nSel, 0, DllPath, MAX_PATH);
// 在目标进程中申请空间,存放字符串szDllName,作为远程线程的参数
int cbSize = (strlen(DllPath) + 1);
//以上为创建远程线程做准备

HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
if(NULL != hProcess)
{  
   DWORD dwHandle;
   LPVOID lpRemoteDllName = ::VirtualAllocEx(hProcess, NULL, cbSize, MEM_COMMIT, PAGE_READWRITE);
   ::WriteProcessMemory(hProcess, lpRemoteDllName, DllPath, cbSize, NULL);   
   // 启动远程线程,并立刻运行
   HANDLE hRemoteThread = ::CreateRemoteThread(hProcess, NULL, 0, pfnStartRoutine, lpRemoteDllName, 0, NULL);
   if(hRemoteThread == NULL)
   {
    CloseHandle(hProcess);
    return FALSE;
   }
   // 等待目标线程运行结束,即LoadLibraryA函数返回
   ::WaitForSingleObject(hRemoteThread, INFINITE);   
   GetExitCodeThread( hRemoteThread, &dwHandle );
   // 释放目标进程中申请的空间
  
   VirtualFreeEx( hProcess, lpRemoteDllName, cbSize, MEM_RELEASE|MEM_DECOMMIT);
  
   ::CloseHandle(hRemoteThread);
  
   hRemoteThread = CreateRemoteThread( hProcess, NULL, 0,
    (LPTHREAD_START_ROUTINE)pfnFreeRoutine, (LPVOID)dwHandle, 0, &dwProcessId );
   // 等待FreeLibrary卸载完毕
   WaitForSingleObject( hRemoteThread, INFINITE );
   CloseHandle( hRemoteThread );
   CloseHandle(hProcess);
   m_modulelist.DeleteItem(nSel);
}
     return 0;

}

//少妇式卸载

typedef DWORD (_stdcall *XXXNtUnmapViewOfSection)( HANDLE hProcess, PVOID Address);

void UnloadNtdll()
{
PVOID   NtdllAddress;
HANDLE   hProcess;
XXXNtUnmapViewOfSection NtUnmapViewOfSection;
HWND   hWindow;
BOOL   bRet = TRUE;

hWindow = FindWindow( NULL, "你要找的名称");

hProcess = OpenProcess( PROCESS_VM_OPERATION, FALSE, PID号);

NtUnmapViewOfSection = (XXXNtUnmapViewOfSection)GetProcAddress( GetModuleHandle("你要卸载的dll文件"), "NtUnmapViewOfSection" );

NtdllAddress = (PVOID)NtUnmapViewOfSection;

NtUnmapViewOfSection( hProcess, NtdllAddress);

CloseHandle( hProcess );

PostMessage( hWindow, WM_MOUSEMOVE, 0 , 0);

}
Bughoho 8 2008-8-12 22:37
4
0
还是少妇好用
wqrsksk 2008-8-13 16:14
5
0
  都不好用...太长了.有没两三句搞定的.
morning 1 2008-8-13 16:32
6
0
写成子函数,那就只有一句了
木桩 8 2008-8-13 21:49
7
0
其实就是用NtUnmapViewOfSection()卸载处理目标进程DLL的基址,所以精简一下,写成函数是这样:

// 输入参数:dwProcessId = 目标进程ID,dwBaseAddr = DLL基址
function UnmapViewOfModule(dwProcessId: DWORD; dwBaseAddr: DWORD): DWORD;
var
  hModule, hProcess: THandle;
  NtUnmapViewOfSection: function (ProcessHandle: DWORD; BaseAddress: Pointer): DWORD; stdcall;
begin
  hModule := GetModuleHandle('ntdll.dll');
  if (hModule = 0) then
    hModule := LoadLibrary('ntdll.dll');
  @NtUnmapViewOfSection := GetProcAddress( hModule, 'NtUnmapViewOfSection');

  hProcess := OpenProcess( PROCESS_ALL_ACCESS, TRUE, dwProcessId );
  Result := NtUnmapViewOfSection( hProcess, Pointer(dwBaseAddr) );
  CloseHandle( hProcess );
  FreeLibrary(hModule);
end;

设置一下 dwBaseAddr  就行了,调用起来也就两句话:
tmpModule := LoadLibrary('ntdll.dll');     // 卸载目标进程的ntdll.dll
UnmapViewOfModule(<目标进程ID>, tmpModule);  // 效果是直接杀死目标进程
Sysnap 14 2008-8-14 09:36
8
0
MS就是从我那里转的
http://hi.baidu.com/sysnap/blog/item/c535d30f1802e12f6159f31f.html

呵呵。。。其实这只是RING3的。。。不过现在是用MM系列来卸载DLL了
垃圾一个 2008-8-16 15:55
9
0
哎,为什么不把来源讲讲,莫非LZ就是,
http://hi.baidu.com/antirootkit/blog
游客
登录 | 注册 方可回帖
返回