CreateRemoteThread远程注入 使用例子

// CreateRemoteThread 使用 关闭远程进程句柄 processID远程进程的进程ID  handle远程进程的进程句柄
CloseRemoteHandle( DWORD processID, HANDLE handle )
{
    HANDLE ht 
= 0;
    DWORD rc 
= 0;

    
// open the process
    HANDLE hProcess = OpenProcess( PROCESS_CREATE_THREAD|PROCESS_VM_OPERATION|PROCESS_VM_WRITE|PROCESS_VM_READ,    FALSE, processID );

    
if ( hProcess == NULL )
    
{
        rc 
= GetLastError();
        MessageBox( _T(
"OpenProcess() failed ") );
        
return rc;
    }


    
// load kernel32.dll
    HMODULE hKernel32 = LoadLibrary( _T("kernel32.dll") );

    
// CreateRemoteThread()
    ht = CreateRemoteThread(
        hProcess,
        
0,
        
0,
     (DWORD(__stdcall 
*)(void*))GetProcAddress(hKernel32,"CloseHandle"),
        handle,
        
0,
        
&rc );

    
if ( ht == NULL )
    
{
        
//Something is wrong with the privileges, or the process doesn't like us
        rc = GetLastError();
        MessageBox( _T(
"CreateRemoteThread() failed ") );

        
//Free up the kernel32.dll
        FreeLibrary( hKernel32 );
        CloseHandle( hProcess );
    }


    
switch ( WaitForSingleObject( ht, 2000 ) )
    
{
    
case WAIT_OBJECT_0:
        
//Well done
        rc = 0;
        MessageBox( _T(
"Ok "));
        
break;

    
default:
        
//Oooops, shouldn't be here
        rc = GetLastError();
        MessageBox( _T(
"WaitForSingleObject() failed ") );
        
break;
    }


    
//Closes the remote thread handle
    CloseHandle( ht );

    
//Free up the kernel32.dll
    if ( hKernel32 != NULL)
        FreeLibrary( hKernel32 );

    
//Close the process handle
    CloseHandle( hProcess );

    
return rc;
}



// CreateRemoteThread 使用 释放远程dll句柄  processID占用dll的远程进程的进程ID  lpDllPath dll路径
CloseRemoteDll( DWORD processID, LPCTSTR lpDllPath )
{
    HANDLE ht 
= 0;
    DWORD rc 
= 0;
    DWORD dwHandle;   

    HANDLE hProcess;
    hProcess
= OpenProcess(PROCESS_CREATE_THREAD | //允许远程创建线程  
                  PROCESS_VM_OPERATION | //允许远程VM操作 
                  PROCESS_VM_WRITE,     //允许远程VM写
                  FALSE, processID );

    
if ( hProcess == NULL )
    
{
        rc 
= GetLastError();
    
//MessageBox( _T("OpenProcess() failed ") );
        return rc;
    }


    HMODULE hKernel32 
= LoadLibrary("kernel32.dll");

    
//向目标进程地址空间写入DLL名称   
    DWORD   dwSize,   dwWritten;   
    CString str;
    str
=lpDllPath;
    dwSize
=str.GetLength()+1;

    LPVOID lpBuf 
= VirtualAllocEx(hProcess,NULL,dwSize, MEM_COMMIT, PAGE_READWRITE );   

    
if(!WriteProcessMemory(hProcess,lpBuf,(LPVOID)lpDllPath, dwSize,&dwWritten))   
    
{   
        rc
=GetLastError();
        VirtualFreeEx(hProcess,lpBuf,dwSize,MEM_DECOMMIT);   
        CloseHandle(hProcess);   
        
return rc;   
    }
   

    HANDLE  hThread 
= CreateRemoteThread(hProcess, NULL, 0,
         (DWORD(__stdcall 
*)(void*))GetProcAddress(hKernel32,"GetModuleHandleA"),
             lpBuf ,
0, NULL);  

    
if(hThread  == NULL)   
    
{   
        rc
=GetLastError();
        CloseHandle(hProcess);   
        
return rc ;   
    }
   

    
//等待GetModuleHandle运行完毕   
    WaitForSingleObject(hThread, INFINITE);   
    
//获得GetModuleHandle的返回值   
    GetExitCodeThread(hThread,&dwHandle);   

    
//释放目标进程中申请的空间   
    VirtualFreeEx( hProcess, lpBuf, dwSize, MEM_DECOMMIT);   
    CloseHandle(hThread);   

    
// CreateRemoteThread()
    ht = CreateRemoteThread(
        hProcess,
        
0,
        
0,
        (DWORD(__stdcall 
*)(void*))GetProcAddress(hKernel32,"FreeLibrary"),
        (LPVOID)dwHandle,
        
0,
        
&rc );

    
if ( ht == NULL )
    
{
        rc 
= GetLastError();
      MessageBox( _T(
"CreateRemoteThread() failed ") );
        FreeLibrary( hKernel32 );
        CloseHandle( hProcess );
        
return rc;
    }


    
switch ( WaitForSingleObject( ht, 2000 ) )
    
{
    
case WAIT_OBJECT_0:
        rc 
= 0;
         MessageBox( _T(
"Ok "));
        
break;

    
default:
        rc 
= GetLastError();
      MessageBox( _T(
"WaitForSingleObject() failed ") );
        
break;
    }


    
//Closes the remote thread handle
    CloseHandle(ht );

    
//Free up the kernel32.dll
    if ( hKernel32 != NULL)
        FreeLibrary( hKernel32 );

    
//Close the process handle
    CloseHandle( hProcess );

    
return rc;

}
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一段远程线程注入代码的例子: ``` #include <Windows.h> #include <TlHelp32.h> DWORD GetProcessIdByName(const wchar_t* processName) { DWORD processId = 0; HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (snapshot != INVALID_HANDLE_VALUE) { PROCESSENTRY32 processEntry = { 0 }; processEntry.dwSize = sizeof(PROCESSENTRY32); if (Process32First(snapshot, &processEntry)) { do { if (_wcsicmp(processEntry.szExeFile, processName) == 0) { processId = processEntry.th32ProcessID; break; } } while (Process32Next(snapshot, &processEntry)); } CloseHandle(snapshot); } return processId; } int main() { const wchar_t* targetProcessName = L"notepad.exe"; const wchar_t* dllPath = L"C:\\path\\to\\mydll.dll"; DWORD targetProcessId = GetProcessIdByName(targetProcessName); if (targetProcessId == 0) { printf("Target process not found.\n"); return 1; } HANDLE targetProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, targetProcessId); if (targetProcess == NULL) { printf("Failed to open target process.\n"); return 1; } LPVOID dllPathAddress = VirtualAllocEx(targetProcess, NULL, wcslen(dllPath) * sizeof(wchar_t), MEM_COMMIT, PAGE_READWRITE); if (dllPathAddress == NULL) { printf("Failed to allocate memory in target process.\n"); CloseHandle(targetProcess); return 1; } if (!WriteProcessMemory(targetProcess, dllPathAddress, dllPath, wcslen(dllPath) * sizeof(wchar_t), NULL)) { printf("Failed to write to target process memory.\n"); VirtualFreeEx(targetProcess, dllPathAddress, 0, MEM_RELEASE); CloseHandle(targetProcess); return 1; } HMODULE kernel32Module = GetModuleHandle(L"kernel32.dll"); if (kernel32Module == NULL) { printf("Failed to get kernel32 module handle.\n"); VirtualFreeEx(targetProcess, dllPathAddress, 0, MEM_RELEASE); CloseHandle(targetProcess); return 1; } LPVOID loadLibraryAddress = GetProcAddress(kernel32Module, "LoadLibraryW"); if (loadLibraryAddress == NULL) { printf("Failed to get LoadLibraryW address.\n"); VirtualFreeEx(targetProcess, dllPathAddress, 0, MEM_RELEASE); CloseHandle(targetProcess); return 1; } HANDLE remoteThread = CreateRemoteThread(targetProcess, NULL, 0, (LPTHREAD_START_ROUTINE)loadLibraryAddress, dllPathAddress, 0, NULL); if (remoteThread == NULL) { printf("Failed to create remote thread.\n"); VirtualFreeEx(targetProcess, dllPathAddress, 0, MEM_RELEASE); CloseHandle(targetProcess); return 1; } WaitForSingleObject(remoteThread, INFINITE); VirtualFreeEx(targetProcess, dllPathAddress, 0, MEM_RELEASE); CloseHandle(remoteThread); CloseHandle(targetProcess); printf("Injection succeeded.\n"); return 0; } ``` 请注意,这只是一个示例代码,实际使用时需要根据具体情况进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值