sunpeng
驱动牛犊
驱动牛犊
  • 注册日期2006-03-23
  • 最后登录2007-06-13
  • 粉丝0
  • 关注0
  • 积分180分
  • 威望19点
  • 贡献值0点
  • 好评度18点
  • 原创分0分
  • 专家分0分
阅读:2724回复:7

关于inline hook NtDeviceIoControlFile例子中的问题

楼主#
更多 发布于:2007-01-17 15:46
  VOID DetourFunctionNtDeviceIoControlFile()
{
    char *actual_function = (char *)NtDeviceIoControlFile;
    char *non_paged_memory;
    unsigned long detour_address;
    unsigned long reentry_address;
    int i = 0;

    // assembles to jmp far 0008:11223344 where 11223344 is address of
    // our detour function, plus one NOP to align up the patch
    char newcode[] = { 0xEA, 0x44, 0x33, 0x22, 0x11, 0x08, 0x00, 0x90 };

    // reenter the hooked function at a location past the overwritten opcodes
    // alignment is, of course, very important here
    reentry_address = ((unsigned long)NtDeviceIoControlFile) + 8;

    non_paged_memory = ExAllocatePool(NonPagedPool, 256);
    
    // copy contents of our function into non paged memory
    // with a cap at 256 bytes (beware of possible read off end of page FIXME)
    for(i=0;i<256;i++)
    {
        ((unsigned char *)non_paged_memory) = ((unsigned char *)my_function_detour_ntdeviceiocontrolfile);
    }

    detour_address = (unsigned long)non_paged_memory;
    
    // stamp in the target address of the far jmp
    *( (unsigned long *)(&newcode[1]) ) = detour_address;

    // now, stamp in the return jmp into our detour
    // function
    for(i=0;i<200;i++)
    {
        if( (0xAA == ((unsigned char *)non_paged_memory)) &&
            (0xAA == ((unsigned char *)non_paged_memory)[i+1]) &&
            (0xAA == ((unsigned char *)non_paged_memory)[i+2]) &&
            (0xAA == ((unsigned char *)non_paged_memory)[i+3]))
        {
            // we found the address 0xAAAAAAAA
            // stamp it w/ the correct address
            *( (unsigned long *)(&non_paged_memory) ) = reentry_address;
            break;
        }
    }

    //TODO, raise IRQL

    //overwrite the bytes in the kernel function
    //to apply the detour jmp
    for(i=0;i < 8;i++)
    {
        actual_function = newcode;
    }

    //TODO, drop IRQL
}
在这个例子中,首先将要hook的函数的前几个字节改成jmp far 0008:11223344,然后再用创建的地址替换11223344,就变成了jmp far 0008:实际地址,我不明白0008是怎么得来的?在windows的系统空间,内存地址是怎么转换的,也是选择子+偏移量吗??希望熟悉的兄弟指点一二
killvxk
论坛版主
论坛版主
  • 注册日期2005-10-03
  • 最后登录2014-04-14
  • 粉丝3
  • 关注1
  • 积分1082分
  • 威望2003点
  • 贡献值0点
  • 好评度1693点
  • 原创分2分
  • 专家分0分
沙发#
发布于:2007-01-17 18:15
Windows kernel里的DS吧
没有战争就没有进步 X3工作组 为您提供最好的军火
wowocock
VIP专家组
VIP专家组
  • 注册日期2002-04-08
  • 最后登录2016-01-09
  • 粉丝16
  • 关注2
  • 积分601分
  • 威望1651点
  • 贡献值1点
  • 好评度1227点
  • 原创分1分
  • 专家分0分
板凳#
发布于:2007-01-17 18:48
这种事情用C做,感觉就是不爽,还是直接用汇编实在.
花开了,然后又会凋零,星星是璀璨的,可那光芒也会消失。在这样 一瞬间,人降生了,笑者,哭着,战斗,伤害,喜悦,悲伤憎恶,爱。一切都只是刹那间的邂逅,而最后都要归入死亡的永眠
sunpeng
驱动牛犊
驱动牛犊
  • 注册日期2006-03-23
  • 最后登录2007-06-13
  • 粉丝0
  • 关注0
  • 积分180分
  • 威望19点
  • 贡献值0点
  • 好评度18点
  • 原创分0分
  • 专家分0分
地板#
发布于:2007-01-18 08:26
各位大侠能说说为什么地址用0008:11223344吗?这个0008到底是怎么来的呢?
rogerlong
驱动牛犊
驱动牛犊
  • 注册日期2007-03-01
  • 最后登录2008-02-18
  • 粉丝0
  • 关注0
  • 积分240分
  • 威望25点
  • 贡献值0点
  • 好评度24点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2007-03-07 23:13
此例子通不过。
msn:lxp8@sina.com
MSN: LXP8@SINA.COM
linestyle
驱动小牛
驱动小牛
  • 注册日期2004-01-28
  • 最后登录2010-01-05
  • 粉丝0
  • 关注0
  • 积分1000分
  • 威望139点
  • 贡献值0点
  • 好评度135点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2007-03-12 09:17
引用第1楼killvxk2007-01-17 18:15发表的“”:
Windows kernel里的DS吧


8是不是应该表示win的gdt代码段
10应该是数据段?

保护模式不熟见笑了:)
loading is waiting ...
wangjianfeng
驱动小牛
驱动小牛
  • 注册日期2004-05-28
  • 最后登录2013-10-02
  • 粉丝0
  • 关注0
  • 积分1002分
  • 威望263点
  • 贡献值0点
  • 好评度260点
  • 原创分0分
  • 专家分0分
6楼#
发布于:2007-03-12 20:58
觉的这种方法不大安全呀,突然CPU在其他线程跳过来怎么办.
killvxk
论坛版主
论坛版主
  • 注册日期2005-10-03
  • 最后登录2014-04-14
  • 粉丝3
  • 关注1
  • 积分1082分
  • 威望2003点
  • 贡献值0点
  • 好评度1693点
  • 原创分2分
  • 专家分0分
7楼#
发布于:2007-03-12 21:59
引用第6楼wangjianfeng2007-03-12 20:58发表的“”:
觉的这种方法不大安全呀,突然CPU在其他线程跳过来怎么办.



会非常不爽~
没有战争就没有进步 X3工作组 为您提供最好的军火
游客

返回顶部