程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 一個簡單的HOOK API的DLL

一個簡單的HOOK API的DLL

編輯:.NET實例教程

#include "Windows.h"
#include "process.h"
#include "tlhelp32.h"
#include "stdio.h"

#pragma comment(lib,"th32.lib")

PIMAGE_DOS_HEADER pDOSHeader;
PIMAGE_NT_HEADERS pNTHeaders;
PIMAGE_OPTIONAL_HEADER    pOptHeader;
PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor;
PIMAGE_THUNK_DATA        pThunkData;
PIMAGE_IMPORT_BY_NAME    pImportByName;
HMODULE hMod;


// 定義MessageBoxA函數原型
typedef int (WINAPI *PFNMESSAGEBOX)(HWND, LPCSTR, LPCSTR, UINT uType);
int WINAPI MessageBoxProxy(IN HWND hWnd, IN LPCSTR lpText, IN LPCSTR lpCaption, IN UINT uType);

int * addr

lass="pun">= (int *)MessageBoxA;     //保存函數的入口地址
int * myaddr = (int *)MessageBoxProxy;


void ThreadProc(void *param);//線程函數

//---------------------------主函數開始

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWord fdwReason,LPVOID lpvReserved)
{
    if(fdwReason==DLL_PROCESS_ATTACH)     
           _beginthread(ThreadProc,0,NULL);     

    return TRUE;
}




//結束進程的函數

void ThreadProc(void *param)
{
    //------------hook api----------------
     hMod = GetModuleHandle(NULL);//當前進程空間的模塊句柄

     pDosHeader = (PIMAGE_DOS_HEADER)hMod;
     pNTHeaders = (PIMAGE_NT_HEADERS)((BYTE *)hMod + pDOSHeader->e_lfanew);
     pOptHeader = (PIMAGE_OPTIONAL_HEADER)&(pNTHeaders->OptionalHeader);
     pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)((BYTE *)hMod + pOptHeader

>->DataDirectory[1].VirtualAddress);

    while(pImportDescriptor->FirstThunk)
    {
          char * dllname = (char *)((BYTE *)hMod + pImportDescriptor->Name);

           pThunkData = (PIMAGE_THUNK_DATA)((BYTE *)hMod + pImportDescriptor->OriginalFirstThunk);

          int no = 1;
          while(pThunkData->u1.Function)
          {
                char * funname = (char *)((BYTE *)hMod + (DWord)pThunkData->u1.AddressOfData + 2);
                 PDWORD lpAddr = (DWORD *)((BYTE *)hMod + (DWord)pImportDescriptor->FirstThunk) +(no-1);
          
                //修改內存的部分
                if((*lpAddr) == <span class="pun">(int)addr)
                {
                    //修改內存頁的屬性
                     DWord dwOLD;
                     MEMORY_BASIC_INFORMATION mbi;
                    VirtualQuery(lpAddr,&mbi,sizeof(mbi));
                    VirtualProtect(lpAddr,sizeof(DWord),PAGE_READWRITE,&dwOLD);
                    
                    WriteProcessMemory(GetCurrentProcess(),
     &nbsp;                           lpAddr, &myaddr, sizeof(DWord), NULL);
                    //恢復內存頁的屬性
            VirtualProtect(lpAddr,sizeof(DWord),dwOLD,0);
                }
                 no++;
                 pThunkData++;
          }

           pImportDescriptor++;
    }
}

//自己定義的方法
int WINAPI MessageBoxProxy(IN HWND hWnd,

IN LPCSTR lpText, IN LPCSTR lpCaption, IN UINT uType)
{
    return       ((PFNMESSAGEBOX)addr)(NULL, "gxter_test", "gxter_title", 0);
    //可以寫代碼進程其它操作
}
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved