zoukankan      html  css  js  c++  java
  • windows创建远程线程

    创建远程进程是在其他进程(非当前进程)中创建一个线程,需要使用 CreateRemoteThread 函数。CreateRemoteThread 函数和 CreateThread 函数相比,只多了一个 HANDLE 类型的参数,这个参数是需要创建线程的进程的句柄。获取到进程句柄后调用 CreateRemoteThread  函数就行了。

    被创建线程的进程的源代码如下:

     1 #include <stdio.h>
     2 #include <Windows.h>
     3 
     4 void foo() {
     5     printf("this is foo function!
    ");
     6     return;
     7 }
     8 
     9 int main() {
    10 
    11     printf("The address of foo: %x
    ", &foo);
    12     foo();
    13 
    14     getchar();
    15 
    16     return 0;
    17 }

    当前进程的源代码:

     1 #include <stdio.h>
     2 #include <Windows.h>
     3 
     4 char msg[128];
     5 
     6 BOOL MyCreateRemoteThread(DWORD dwProcessId, DWORD fun) {
     7     /*
     8         1. 获取被创建线程的进程的句柄
     9     */
    10     HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, NULL, dwProcessId);
    11     if (!hProc) {
    12         sprintf(msg, "OpenProcess failed: %d", GetLastError());
    13         MessageBox(NULL, msg, NULL, MB_OK);
    14         return false;
    15     }
    16     /*
    17         2. 创建远程线程    
    18     */
    19     HANDLE hrThread = CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)fun, NULL, 0, NULL);
    20     if (!hrThread) {
    21         sprintf(msg, "CreateRemoteThread failed: %d", GetLastError());
    22         CloseHandle(hProc);
    23         MessageBox(NULL, msg, NULL, MB_OK);
    24         return false;
    25     }
    26 
    27 
    28     CloseHandle(hProc);
    29     CloseHandle(hrThread);
    30     return true;
    31 }
    32 
    33 int main() {
    34    /* 这里的两个参数分别是要被创建线程的进程的PID和创建进程后要执行的代码的地址(注意这个地址不能是当前进程的地址)*/
    35     MyCreateRemoteThread(8764, 0xf510f0);
    36     return 0;
    37 }

    执行结果

    创建远程进程前:

    创建远程线性后,foo 函数又执行了一遍。

     

  • 相关阅读:
    ASP.NET Core 程序集注入(三)
    ASP.NET Core 程序集注入(二)
    ASP.NET Core 程序集注入(一)
    EFCore DbFirst从数据库生成实体类
    Notepad++实现代码格式化
    EF6/EFCore Code-First Timestamp SQL Server
    MySQL 实现 EF Code First TimeStamp/RowVersion 并发控制
    EntityFramework系列:MySql的RowVersion
    ansible常用命令大全
    python内置函数大全
  • 原文地址:https://www.cnblogs.com/triangleowl/p/12744977.html
Copyright © 2011-2022 走看看