c语言win api函数,Win2K下的Api函数的拦截
这么多高手在这里,哎,小弟愿意向各位高手学习。
Api拦截并不是一个新的技术,很多商业软件都采用这种技术。对windows的Api函数的拦截,不外乎两种方法,第一种是Mr. Jeffrey Richter 的修改exe文件的模块输入节,种方法,很安全,但很复杂,而且有些exe文件,没有Dll的输入符号的列表,有可能出现拦截不到的情况。第二种方法就是常用的JMP XXX的方法,虽然很古老,却很简单实用。
本文一介绍第二种方法在Win2k下的使用。第二种方法,Win98/me 下因为进入Ring0级的方法很多,有LDT,IDT,Vxd等方法,很容易在内存中动态修改代码,但在Win2k下,这些方法都不能用,写WDM太过复杂,表面上看来很难实现,
其实不然。Win2k为我们提供了一个强大的内存Api操作函数—VirtualProtectEx,WriteProcessMemeory,ReadProcessMemeory,有了它们我们就能在内存中动态修改代码了,其原型为:
BOOL VirtualProtectEx(
HANDLE hProcess, // 要修改内存的进程句柄
LPVOID lpAddress, // 要修改内存的起始地址
DWORD dwSize, // 修改内存的字节
DWORD flNewProtect, // 修改后的内存属性
PDWORD lpflOldProtect // 修改前的内存属性的地址
);
BOOL WriteProcessMemory(
HANDLE hProcess, // 要写进程的句柄
LPVOID lpBaseAddress, // 写内存的起始地址
LPVOID lpBuffer, // 写入数据的地址
DWORD nSize, // 要写的字节数
LPDWORD lpNumberOfBytesWritten // 实际写入的子节数
);
BOOL ReadProcessMemory(
HANDLE hProcess, // 要读进程的句柄
LPCVOID lpBaseAddress, // 读内存的起始地址
LPVOID lpBuffer, // 读入数据的地址
DWORD nSize, // 要读入的字节数
LPDWORD lpNumberOfBytesRead // 实际读入的子节数
);
具体的参数请参看MSDN帮助。在Win2k下因为Dll和所属进程在同一地址空间,这点又和Win9x/me存在所有进程存在共享的地址空间不同,
因此,必须通过钩子函数和远程注入进程的方法,现以一个简单采用钩子函数对MessageBoxA进行拦截例子来说明:
其中Dll文件为:
//———————————————————- —————–
#include
//———————————————————- —————–
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE’s to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using “char *” or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//———————————————————- —————–
#pragma argsused
HHOOK g_hHook;
HINSTANCE g_hinstDll;
FARPROC fpMessageBoxA;
HMODULE hModule ;
BYTE OldMessageBoxACode[5], NewMessageBoxACode[5];
DWORD dwIdOld, dwIdNew;
BOOL bHook = false;
void HookOn();
void HookOff();
BOOL Init();
int WINAPI MyMessageBoxA(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
//———————————————————- —————–
// 空的钩子函数
LRESULT WINAPI Hook(int nCode, WPARAM wParam, LPARAM lParam)
{
return(CallNextHookEx(g_hHook, nCode, wParam, lParam));
}
//———————————————————- —————–
// 输出,安装空的钩子函数
extern “C” __declspec(dllexport) __stdcall
BOOL InstallHook()
{
g_hinstDll = LoadLibrary(“project1.dll”); // 这里的文件名为Dll本身的文件名
g_hHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)Hook, g_hinstDll, 0);
if (!g_hHook)
{
MessageBoxA( NULL, “SET ERROR”, “ERROR”, MB_OK);
return(false );
}
return(true);
}
//———————————————————- —————–
// 输出,Uninstall钩子函数
extern “C” __declspec(dllexport) __stdcall
BOOL UninstallHook()
{
return(UnhookWindowsHookEx(g_hHook)) ;
}
//———————————————————- —————–
// 初始化得到MessageBoxA的地址,并生成Jmp XXX(MyMessageBoxA)的跳转指令
BOOL Init()
{
hModule = LoadLibrary(“user32.dll”);
fpMessageBoxA = GetProcAddress(hModule, “GetSystemDirectoryA”);
if(fpMessageBoxA == NULL)
return false;
_asm
{
pushad
lea edi, OldMessageBoxACode
mov esi, fpMessageBoxA
cld
movsd
movsb
popad
}
NewMessageBoxACode[0] = 0xe9; // jmp MyMessageBoxA的相对地址的指令
_asm
{
lea eax, MyGetSystemDirectory
mov ebx, fpMessageBoxA
sub eax, ebx
sub eax, 5
mov dword ptr [NewMessageBoxACode + 1], eax
}
dwIdNew = GetCurrentProcessId(); // 得到所属进程的ID
dwIdOld = dwIdNew;
HookOn(); // 开始拦截
return(true);
}
//———————————————————- —————–
// 首先关闭拦截,然后才能调用被拦截的Api 函数
int WINAPI MyMessageBoxA(HWND hWnd, LPCTSTR lpText,LPCTSTR lpCaption, UINT uType)
{
int nReturn;
HookOff();
nReturn = MessageBoxA(hWnd, “来自钩子中的内容”, lpCaption, MB_OK | MB_ICONINFORMATION);
HookOn();
return(nReturn);
}
//———————————————————- —————–
// 本文代码由ccrun整理并在BCB 6.0环境下调试通过。
// Welcome to C++ Builder 研究
// http://www.ccrun.com
//———————————————————- —————–
void HookOn()
{
HANDLE hProc;
dwIdOld = dwIdNew;
// 得到所属进程的句柄
hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, dwIdOld);
// 修改所属进程中MessageBoxA的前5个字节的属性为可写
VirtualProtectEx(hProc, fpMessageBoxA, 5, PAGE_READWRITE,&dwIdOld);
// 将所属进程中MessageBoxA的前5个字节改为JMP 到MyMessageBoxA
WriteProcessMemory(hProc, fpMessageBoxA, NewMessageBoxACode, 5, 0);
// 修改所属进程中MessageBoxA的前5个字节的属性为原来的属性
VirtualProtectEx(hProc, fpMessageBoxA, 5, dwIdOld, &dwIdOld);
bHook=true;
}
//———————————————————- —————–
// 将所属进程中JMP MyMessageBoxA的代码改为Jmp MessageBoxA
void HookOff()
{
HANDLE hProc;
dwIdOld = dwIdNew;
hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, dwIdOld);
VirtualProtectEx(hProc, fpMessageBoxA,5, PAGE_READWRITE, &dwIdOld);
WriteProcessMemory(hProc, fpMessageBoxA, OldMessageBoxACode, 5, 0);
VirtualProtectEx(hProc, fpMessageBoxA, 5, dwIdOld, &dwIdOld);
bHook = false;
}
//———————————————————- —————–
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
if(!Init())
{
MessageBoxA( NULL,”Init”,”ERROR”,MB_OK);
return(false );
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
if(bHook)
UninstallHook();
break;
}
return TRUE;
}
// 测试文件:
extern “C” __declspec(dllimport) __stdcall
BOOL InstallHook();
extern “C” __declspec(dllimport) __stdcall
BOOL UninstallHook();
//———————————————————- —————–
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//———————————————————- —————–
// 本文代码由ccrun整理并在BCB 6.0环境下调试通过。
// Welcome to C++ Builder 研究
// http://www.ccrun.com
//———————————————————- —————–
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(!InstallHook())
{
Label1->C aption = “Hook Error!”;
}
MessageBoxA(NULL, “内容”, “标题”, MB_OK);
// 可以看见”内容变成了”来自钩子中的内容”
if(!UninstallHook())
{
Label1->C aption = “Uninstall Error!”;
}
}
//———————————————————- —————–
c语言win api函数,Win2K下的Api函数的拦截相关推荐
- python基础十 函数(下)匿名函数、高阶函数、闭包、装饰器
目录 1. 匿名函数(lambda表达式) 2. 高阶函数 3. 函数的嵌套 4. nonlocal 关键字使用 5. 闭包 6. 装饰器 1. 匿名函数(lambda表达式) 简介 用一句话来表达只 ...
- c 执行oracle 函数,Oracle 下医嘱执行函数
//*********************************************************************** //函数名称:of_zy_yzfy for n_me ...
- linux下rename函数,linux下的rename函数说明
rename(更改文件名称或位置) 相关函数 link,unlink,symlink 表头文件 #include 定义函数 int rename(const char * oldpath,const ...
- linux内核的延时函数,linux中内核延时函数 (转)
第一类延时函数原型是:(忙等) void ndelay(unsigned long nsecs); void udelay(unsigned long usecs); void mdelay(unsi ...
- C语言函数大全--f开头的函数(下)
C语言函数大全 本篇介绍C语言中f开头的函数(下) 21. floor,floorf,floorl 21.1 函数说明 函数声明 函数功能 double floor (double x); 获取小于或 ...
- C语言函数大全--g开头的函数(下)
C语言函数大全 本篇介绍C语言函数大全–g开头的函数(下) 17. getmodename 17.1 函数说明 函数声明 函数功能 char * getmodename(int mode_name); ...
- linux的gets函数,Linux 下使用C语言 gets()函数报错
在Linux下,使用 gets(cmd) 函数报错:warning: the 'gets' function is dangerous and should not be used. 解决办法:采用 ...
- R语言使用pwr包的pwr.t.test函数对分组样本数相同的t检验进行效用分析(power analysis)、在已知效应量(effect size)、显著性水平、效用值的情况下计算需要的样本量
R语言使用pwr包的pwr.t.test函数对分组样本数相同的t检验进行效用分析(power analysis).在已知效应量(effect size).显著性水平(sig).效用值(power)的情 ...
- R语言使用pwr包的pwr.t.test函数对分组样本数相同的t检验进行效用分析(power analysis)、在已知效应量(effect size)、显著性水平、样本量的情况下计算假设检验的效用值
R语言使用pwr包的pwr.t.test函数对分组样本数相同的t检验进行效用分析(power analysis).在已知效应量(effect size).显著性水平(sig).样本量(sample s ...
最新文章
- C语言的有序单链表合并
- Ubuntu定时任务crontab命令介绍
- Guice:最好用的依赖注入框架
- opencv学习笔记7:图像加法与图像融合
- uniapp H5页面使用uni.request时,出现跨域问题
- C++ KMP算法之next数组的生成
- Vue-cli项目中路由的基础用法,以及路由嵌套
- 怎样求信号中的RMS值?
- 利用.Net中的反射动态调用方法
- 3d激光雷达开发(多雷达标定)
- 重装华为服务器系统教程视频教程,服务器系统重装步骤
- linux提示有新邮件,/var/spool/mail/root 中有新邮件 解决方法
- vasp和ms_采用MS建模的基本步骤以及vasp新手入门需要注意的十个简单问题
- 扫描枪无限连服务器,无线一维扫描枪配置无线网络使用的方法
- 计算机单位厘米 像素,英尺和厘米的换算计算器 显示器的像素解析度可能不是...
- 借势氢能源发展热潮,重塑股份持续加速行业布局
- 为什么要升级PLM系统
- 使用高德地图获取拍照图片地理位置
- 支配集(dominating set)问题是NP完全问题的一个证明
- XMind 2022 for Mac(思维导图软件)中文免费版
热门文章
- jdbc与java数据库编程_JDBC与JAVA数据库编程
- android xml 多行注释,C#中的XML多行注释 - 我做错了什么?
- unity中链接字符串和变量显示_unity3d根据字符串读取属性.
- __call__()和call的区别_python中的__init__ 、__new__、__call__小结
- android4.0网络编程配书源码_linux网络编程之epoll源码重要部分详解
- echart 高度 不用 不撑满_你担心的高度近视老了之后是这样的
- yolo系列外文翻译_Yolo系列其三:Yolo_v3
- python获取未读邮箱数目_python imaplib 获取未读邮件,email解析并按照原名下载附件...
- php中颜色的索引值,PHP imagecolorsforindex - 取得某索引的颜色
- matlab7.x答案,MATLAB习题及参考答案