遠程線程作為一項"合法"的代碼注入技術,在windows上被大量使用, 它的本質就是把一塊可執行代碼寫入到對方進程,然后讓其起運行起來。
一般它的實現過程是這樣的, 通過VirtualAllocEx在目標進程分配內存空間,然后通過WriteProcessMemory將我們的可執行代碼寫入到目標進程,最后通過CreateRemoteThread讓我們的可執行代碼在目標進程里運行起來。
一般實現遠程線程有2種方法, 一種是《windows核心編程》里介紹的,通過線程函數和LoadLibrary API函數申明的相似性, 直接在目標進程里調用LoadLibrary加載我們DLL,這樣我們只要在DLL_PROCESS_ATTACH里執行我們的代碼就可以了。代碼如下, 通過
InjectLib在目標進程加載我們的DLL, 通過EjectLib
卸載我們的DLL:
///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI InjectLibW(DWORD dwProcessId, PCWSTR pszLibFile)
{
BOOL fOk = FALSE; // Assume that the function fails
HANDLE hProcess = NULL, hThread = NULL;
PWSTR pszLibFileRemote = NULL;
__try {
// Get a handle for the target process.
hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | // Required by Alpha
PROCESS_CREATE_THREAD | // For CreateRemoteThread
PROCESS_VM_OPERATION | // For VirtualAllocEx/VirtualFreeEx
PROCESS_VM_WRITE, // For WriteProcessMemory
FALSE, dwProcessId);
if (hProcess == NULL)
{
__leave;
}
// Calculate the number of bytes needed for the DLL's pathname
int cch = 1 + lstrlenW(pszLibFile);
int cb = cch * sizeof(WCHAR);
// Allocate space in the remote process for the pathname
pszLibFileRemote = (PWSTR)
VirtualAllocEx(hProcess, NULL, cb, MEM_COMMIT, PAGE_READWRITE);
if (pszLibFileRemote == NULL)
{
__leave;
}
// Copy the DLL's pathname to the remote process's address space
if (!WriteProcessMemory(hProcess, pszLibFileRemote,
(PVOID) pszLibFile, cb, NULL))
{
__leave;
}
// Get the real address of LoadLibraryW in Kernel32.dll
PTHREAD_START_ROUTINE pfnThreadRtn = (PTHREAD_START_ROUTINE)
GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "LoadLibraryW");
if (pfnThreadRtn == NULL)
{
__leave;
}
// Create a remote thread that calls LoadLibraryW(DLLPathname)
hThread = CreateRemoteThread(hProcess, NULL, 0,
pfnThreadRtn, pszLibFileRemote, 0, NULL);
if (hThread == NULL)
{
__leave;
}
// Wait for the remote thread to terminate
WaitForSingleObject(hThread, INFINITE);
fOk = TRUE; // Everything executed successfully
}
__finally { // Now, we can clean everthing up
// Free the remote memory that contained the DLL's pathname
if (pszLibFileRemote != NULL)
VirtualFreeEx(hProcess, pszLibFileRemote, 0, MEM_RELEASE);
if (hThread != NULL)
CloseHandle(hThread);
if (hProcess != NULL)
CloseHandle(hProcess);
}
return(fOk);
}
///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI InjectLibA(DWORD dwProcessId, PCSTR pszLibFile)
{
// Allocate a (stack) buffer for the Unicode version of the pathname
PWSTR pszLibFileW = (PWSTR)
_alloca((lstrlenA(pszLibFile) + 1) * sizeof(WCHAR));
// Convert the ANSI pathname to its Unicode equivalent
wsprintfW(pszLibFileW, L"%S", pszLibFile);
// Call the Unicode version of the function to actually do the work.
return(InjectLibW(dwProcessId, pszLibFileW));
}
///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI EjectLibW(DWORD dwProcessId, PCWSTR pszLibFile)
{
BOOL fOk = FALSE; // Assume that the function fails
HANDLE hthSnapshot = NULL;
HANDLE hProcess = NULL, hThread = NULL;
__try {
// Grab a new snapshot of the process
hthSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId);
if (hthSnapshot == INVALID_HANDLE_VALUE)
{
__leave;
}
// Get the HMODULE of the desired library
MODULEENTRY32W me = { sizeof(me) };
BOOL fFound = FALSE;
BOOL fMoreMods = Module32FirstW(hthSnapshot, &me);
for (; fMoreMods; fMoreMods = Module32NextW(hthSnapshot, &me)) {
fFound = (lstrcmpiW(me.szModule, pszLibFile) == 0) ||
(lstrcmpiW(me.szExePath, pszLibFile) == 0);
if (fFound) break;
}
if (!fFound)
{
__leave;
}
// Get a handle for the target process.
hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | // Required by Alpha
PROCESS_CREATE_THREAD |
PROCESS_VM_OPERATION, // For CreateRemoteThread
FALSE, dwProcessId);
if (hProcess == NULL)
{
__leave;
}
// Get the real address of LoadLibraryW in Kernel32.dll
PTHREAD_START_ROUTINE pfnThreadRtn = (PTHREAD_START_ROUTINE)
GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "FreeLibrary");
if (pfnThreadRtn == NULL)
{
__leave;
}
// Create a remote thread that calls LoadLibraryW(DLLPathname)
hThread = CreateRemoteThread(hProcess, NULL, 0,
pfnThreadRtn, me.modBaseAddr, 0, NULL);
if (hThread == NULL)
{
__leave;
}
// Wait for the remote thread to terminate
WaitForSingleObject(hThread, INFINITE);
fOk = TRUE; // Everything executed successfully
}
__finally { // Now we can clean everything up
if (hthSnapshot != NULL)
CloseHandle(hthSnapshot);
if (hThread != NULL)
CloseHandle(hThread);
if (hProcess != NULL)
CloseHandle(hProcess);
}
return(fOk);
}
///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI EjectLibA(DWORD dwProcessId, PCSTR pszLibFile)
{
// Allocate a (stack) buffer for the Unicode version of the pathname
PWSTR pszLibFileW = (PWSTR)
_alloca((lstrlenA(pszLibFile) + 1) * sizeof(WCHAR));
// Convert the ANSI pathname to its Unicode equivalent
wsprintfW(pszLibFileW, L"%S", pszLibFile);
// Call the Unicode version of the function to actually do the work.
return(EjectLibW(dwProcessId, pszLibFileW));
}
///////////////////////////////////////////////////////////////////////////////
上面這種方法注入的代碼它的優點是開發比較簡單,我們只要用C++寫一個DLL,然后調用
InjectLibW(processID, dllName)就可以了,但是因為代碼是運行在一個DLL里,別人可以通過一些枚舉模塊的工具看到我們的DLL,所以隱蔽性不是很好。
還有一種遠程線程的實現方法是羅云彬
《Windows環境下32位匯編語言程序設計》里介紹的, 我們不通過DLL,而是直接把可執行代碼拷貝到目標進程后運行,所以它是真正的遠程線程,通過這種方法,我們的代碼和目標進程已經完全融為一體,其他人根本無法察覺。
用這種方法實現, 它的要點是:
(1) Kernel32.DLL加載的基址在任何進程里都是一樣的(其實上一種LoadLibrary方法也用到了這點), 所以GetProcAddress,GetModuleHandleA(W), LoadLibraryA(W)這些API的地址在任何進程里都是一樣的, 所以我們在其他進程中用和本進程相同的地址調用這些API。
(2) 因為涉及到全局變量的重定位問題, 所以注入的代碼需要用匯編編寫, 并用以下匯編解決重定位問題
call @F
@@:
pop ebx
sub ebx, offset @B
下面寫了一個無DLL實現遠程線程的測試代碼,他會在桌面Shell進程(explorer.exe)里彈一個MessageBox,按照這個例子,我們已經可以通過調用LoadLibraryA(W)和GetProcAddress來調用任何API了, 所以要實現一些復雜的功能也不是難事。
如果要避免用匯編來重定位,可以把全局變量都打包放在線程參數里,這樣單用C++就可以完全實現無DLL的遠程線程了,當然我們只能用Release版本(Debug版加了一些用到全局變量的調試信息, 堆棧檢測等)。
還有一個問題是遠程線程很難調試, 很多時候你在目標進程里注入代碼后可能就只會看到一個Crash的窗口,也不知道哪里代碼有問題。如何才能單步調試呢? 我的方法是在CreateRemoteThread之前把線程的入口地址用MessageBox打印出來,然后用Windbg Attach到目標進程,在該地址上設置斷點,這樣繼續運行就可以用Windbg單步調試了(當然只能以匯編的形式)。
遠程線程測試代碼下載(Asm):
RemoteThreadTest
posted on 2012-06-20 15:38
Richard Wei 閱讀(3991)
評論(5) 編輯 收藏 引用 所屬分類:
windows desktop