c语言-----劫持系统03

1. 回顾

  在前2节我们已经实现了劫持原理、函数指针等一些概念,下面进行系统劫持

2. 工具

  vs2017

  Detours

3. windows如何创建一个进程?

  (1)创建进程函数

CreateProcessW(
    LPCWSTR lpApplicationName,        //执行程序名称
    LPWSTR lpCommandLine,        //命令行
    LPSECURITY_ATTRIBUTES lpProcessAttributes,  //进程安装
    LPSECURITY_ATTRIBUTES lpThreadAttributes, //进程主线程安装
    BOOL bInheritHandles,    //附加参数
    DWORD dwCreationFlags, //创建参数
    LPVOID lpEnvironment, //环境变量指针
    LPCWSTR lpCurrentDirectory, //进程当前路径
    LPSTARTUPINFOW lpStartupInfo, //进程启动的附加信息
    LPPROCESS_INFORMATION lpProcessInformation //进程标识符
);

  (2) 我们需要用到哪些参数?

    wchar_t str[100]; 用来指定输入的命令比如notepad mspaint...     对应第二个参数

         STARTUPINFO si; 保存进程信息                                 也就是倒数第二个参数

   PROCESS_INFORMATION pi; 进程标识符                 也就是最后一个参数

   其他都是NULL

  (3) 完整程序代码

#include<Windows.h>
#include<stdio.h>
#include<stdlib.h>
int main() {
    STARTUPINFO si = { sizeof(si) };  //保存进程信息
    si.dwFlags = STARTF_USESHOWWINDOW;//显示窗口
    si.wShowWindow = 1;//显示窗口
    PROCESS_INFORMATION pi; //进程信息
    wchar_t str[100] = L"notepad";
    CreateProcessW(NULL, str, NULL, NULL, 0, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
    return 0;
}

  (4) 原理解释

  为什么是wchar_t,不是char?

    汉语占两个字节,英语占一个字节,windows系统为了兼容函数,创建了宽字符wchar_t可以直接输入汉语,而不会出现乱码

4. 系统进程劫持

  (1) 上次劫持原理回顾

void (*pold)(参数) = system;
void pnew(参数){
  ...  
}
void hook(){
    ...
}

 (2) 这次函数由system()改为CreateProcess

 第一步:创建函数指针

BOOL(WINAPI *poldcreateprocess)(
    LPCWSTR lpApplicationName,
    LPWSTR lpCommandLine,
    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    BOOL bInheritHandles,
    DWORD dwCreationFlags,
    LPVOID lpEnvironment,
    LPCWSTR lpCurrentDirectory,
    LPSTARTUPINFOW lpStartupInfo,
    LPPROCESS_INFORMATION lpProcessInformation
    ) = CreateProcessW;//在中国使用宽字符更精准

 第二步:创建新函数

BOOL  NEWCreateProcessW(
    LPCWSTR lpApplicationName,
    LPWSTR lpCommandLine,
    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    BOOL bInheritHandles,
    DWORD dwCreationFlags,
    LPVOID lpEnvironment,
    LPCWSTR lpCurrentDirectory,
    LPSTARTUPINFOW lpStartupInfo,
    LPPROCESS_INFORMATION lpProcessInformation
)
{
    
    MessageBoxA(0, "劫持系统", "点有用吗???", 0);
    return 0;//执行失败
    
}

 第三步:实现劫持

void hook()
{
    DetourRestoreAfterWith();//恢复状态
    DetourTransactionBegin();//开始
    DetourUpdateThread(GetCurrentThread());//刷新当前线程
    DetourAttach((void**)&poldcreateprocess, NEWCreateProcessW);
    DetourTransactionCommit();//立刻生效
}

 第四步:编写dll函数

_declspec(dllexport)void go()
{

    hook();

}

 第五步:改Debug模式 -> Rease模式  -> 生成解决方案

完整源代码

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include "detours.h"
#pragma comment(lib,"detours.lib")

BOOL(WINAPI *poldcreateprocess)(
    LPCWSTR lpApplicationName,
    LPWSTR lpCommandLine,
    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    BOOL bInheritHandles,
    DWORD dwCreationFlags,
    LPVOID lpEnvironment,
    LPCWSTR lpCurrentDirectory,
    LPSTARTUPINFOW lpStartupInfo,
    LPPROCESS_INFORMATION lpProcessInformation
    ) = CreateProcessW;//宽字符的创建进程
BOOL  NEWCreateProcessW(
    LPCWSTR lpApplicationName,
    LPWSTR lpCommandLine,
    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    BOOL bInheritHandles,
    DWORD dwCreationFlags,
    LPVOID lpEnvironment,
    LPCWSTR lpCurrentDirectory,
    LPSTARTUPINFOW lpStartupInfo,
    LPPROCESS_INFORMATION lpProcessInformation
)
{
    MessageBoxA(0, "劫持系统", "点有用吗???", 0);
    return 0;//执行失败
}
void hook()
{
    DetourRestoreAfterWith();//恢复状态
    DetourTransactionBegin();//开始
    DetourUpdateThread(GetCurrentThread());//刷新当前线程
    DetourAttach((void**)&poldcreateprocess, NEWCreateProcessW);
    DetourTransactionCommit();//立刻生效
}
_declspec(dllexport)void go()
{
    hook();
}

4. dll注入

  打开dll注入工具,登陆qq后刷新dll注入工具选择qq  

  找到编写的dll,输入编写的函数go() 点击注入,在qq上打开qq空间

      如果有explorer.exe的选择explore.exe 注入后任何进程打开失败

猜你喜欢

转载自www.cnblogs.com/mofei1999/p/11768785.html