找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 8978|回复: 9

Hook MmFlushImageSection

 火.. [复制链接]

47

主题

265

回帖

8

精华

核心会员

积分
10349
发表于 2011-3-27 00:23:42 | 显示全部楼层 |阅读模式
以前做文件保护写过object type,dispatch hook,文件过滤驱动。
一直没在ntfs驱动的做手脚,昨天突然来兴趣了,于是。。。
在一天+一夜内,通过nt4src中的ntfs+windbg+ida。
对ntfs的删除文件流程(IRP_MJ_SET_INFORMATION的例程),以及xcb的结构了解了一通。
流程如下:
NtfsFsdSetInformation---->NtfsCommonSetInformation---->NtfsSetDispositionInfo
最后在NtfsSetDispositionInfo里,我发现了MmFlushImageSection这个导出函数,调用如下:
  1. if (!MmFlushImageSection( &Scb->NonpagedScb->SegmentObject,
  2.                                       MmFlushForDelete ))
复制代码
很容易想到hook,于是就有了下面的代码:
  1. #include <wdm.h>
  2. #include <windef.h>

  3. NTSTATUS
  4. DriverEntry(        PDRIVER_OBJECT        DriverObject,
  5.                 PUNICODE_STRING        RegisterPath
  6.                 );
  7.                
  8. #pragma alloc_text(INIT, DriverEntry)

  9. //////////////////////////////////////////////////
  10. //global
  11. /////////////////////////////////////////////////
  12. PVOID        g_section_pointer;
  13. PVOID        g_orig_address;
  14. BYTE        origcode[5] = {0};
  15. BOOL        isHook = FALSE;
  16. typedef enum _MMFLUSH_TYPE {
  17.     MmFlushForDelete,
  18.     MmFlushForWrite
  19. } MMFLUSH_TYPE;
  20. ///////////////////////////////////////////////
  21. //function
  22. //////////////////////////////////////////////

  23. VOID WPON()
  24. {
  25.      __asm{
  26.           mov eax,cr0
  27.           or eax,0x10000
  28.           mov cr0,eax
  29.           STI
  30.   }

  31. }

  32. VOID WPOFF()
  33. {
  34.   __asm{
  35.           cli  
  36.       mov eax, cr0
  37.           and eax,not 0x10000
  38.       mov cr0,eax
  39.       }
  40.   
  41. }
  42. __declspec(naked)
  43. BOOLEAN
  44. fake_MmFlushImageSection (
  45.         __in PSECTION_OBJECT_POINTERS SectionPointer,
  46.         __in MMFLUSH_TYPE FlushType
  47.         )
  48. {
  49.         _asm
  50.         {
  51.                 mov        edi,edi
  52.                 push        ebp
  53.                 mov        ebp,esp
  54.                 push        eax
  55.                 mov        eax,SectionPointer
  56.                 cmp        eax, g_section_pointer
  57.                 jnz        laborig
  58.                 mov        eax, FlushType
  59.                 test        eax,eax
  60.                 jnz        laborig
  61.                 pop        eax
  62.                 mov        eax, 0
  63.                 jmp        labret
  64.         laborig:
  65.                 pop        eax
  66.                 mov        eax, g_orig_address
  67.                 add        eax, 5
  68.                 jmp        eax
  69.         labret:
  70.                 pop        ebp
  71.                 ret        8
  72.         }
  73. }


  74. VOID
  75. HookMmFlushImageSection()
  76. {
  77.         UNICODE_STRING        hook_function;
  78.         PVOID                hook_addr;
  79.         BYTE                jmpcode[5] = {0xe9, 0x00, 0x00, 0x00, 0x00};
  80.         ULONG                distance;
  81.        
  82.         //获得MmFlushImageSection地址
  83.         RtlInitUnicodeString( &hook_function, L"MmFlushImageSection" );
  84.         hook_addr = MmGetSystemRoutineAddress( &hook_function );
  85.         g_orig_address = hook_addr;
  86.         //计算偏移量
  87.         distance = (ULONG)fake_MmFlushImageSection - (ULONG)hook_addr - 5;
  88.         RtlCopyMemory( jmpcode + 1, (BYTE*)&distance , 4);
  89.        
  90.         KdBreakPoint();
  91.         //开始hook
  92.         WPOFF();
  93.         RtlCopyMemory( origcode, (BYTE*)g_orig_address, 5);
  94.         RtlCopyMemory( (BYTE*)g_orig_address, jmpcode, 5 );
  95.         isHook = TRUE;
  96.         WPON();
  97. }

  98. VOID
  99. DriverUnload(IN PDRIVER_OBJECT  DriverObject )
  100. {
  101.         if (isHook == TRUE)
  102.                 RtlCopyMemory( (BYTE*)g_orig_address, origcode, 5);
  103. }

  104. NTSTATUS
  105. DriverEntry(        PDRIVER_OBJECT        DriverObject,
  106.                 PUNICODE_STRING        RegisterPath
  107.                 )
  108. {
  109.         UNICODE_STRING                protect_path;
  110.         HANDLE                        profile_handle;
  111.         PFILE_OBJECT                profile_obj;
  112.         OBJECT_ATTRIBUTES        obja;
  113.         IO_STATUS_BLOCK        io_status;
  114.         NTSTATUS                status = STATUS_SUCCESS;
  115.        
  116.         DriverObject->DriverUnload = DriverUnload;
  117.        
  118.         RtlInitUnicodeString( &protect_path, L"\\??\\c:\\ywledoc.txt" );
  119.         InitializeObjectAttributes( &obja,
  120.                         &protect_path,
  121.                         OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
  122.                         NULL,
  123.                         NULL);
  124.         status = ZwCreateFile(        &profile_handle,
  125.                         FILE_ANY_ACCESS,
  126.                         &obja,
  127.                         &io_status,
  128.                         NULL,
  129.                         FILE_ATTRIBUTE_NORMAL,
  130.                         FILE_SHARE_READ | FILE_SHARE_WRITE,
  131.                         FILE_OPEN,
  132.                         FILE_NON_DIRECTORY_FILE | FILE_RANDOM_ACCESS |
  133.                         FILE_SYNCHRONOUS_IO_NONALERT | FILE_NO_INTERMEDIATE_BUFFERING,
  134.                         NULL,
  135.                         0);
  136.         if (!NT_SUCCESS(status)) {
  137.                 KdPrint(("XCB @ DriverEntry : IoCreateFile Error %08x\n", status));
  138.                 return        STATUS_SUCCESS;
  139.         }
  140.        
  141.         status = ObReferenceObjectByHandle( profile_handle,
  142.                                 FILE_READ_DATA,
  143.                                 NULL,
  144.                                 KernelMode,
  145.                                 &profile_obj,
  146.                                 NULL);
  147.         if (!NT_SUCCESS(status)) {
  148.                 KdPrint(("XCB @ DriverEntry : ObReferenceObjectByHandle Error %08x\n", status));
  149.                 return        STATUS_SUCCESS;
  150.         }
  151.        
  152.         KdPrint(("XCB @ DriverEntry : file_object is %08x\n", profile_obj));
  153.         g_section_pointer = profile_obj->SectionObjectPointer;
  154.        
  155.         HookMmFlushImageSection();
  156.        
  157.         return STATUS_SUCCESS;
  158. }
复制代码
虽然网上有类似的代码,但是本代码是作者独自编写的,思路也是自己想出来的。所以,我把它归为原创一类去了。

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
发表于 2011-3-27 16:55:58 | 显示全部楼层
加精华,鼓励原创。
再有一篇,进核心会员。

29

主题

134

回帖

4

精华

论坛元老

积分
5970
QQ
发表于 2011-3-31 17:16:37 | 显示全部楼层
MmFlushImageSection  这个挺有意思 有人用他删文件有人用他保护文件
typedef struct _SECTION_OBJECT_POINTERS {
PVOID DataSectionObject; //Control Area
PVOID SharedCacheMap;
PVOID ImageSectionObject; //Control Area
} SECTION_OBJECT_POINTERS;

kd> dt _Control_Area 0x82735ca8
nt!_CONTROL_AREA
   +0x000 Segment          : 0xe18a8a48 _SEGMENT
   +0x004 DereferenceList  : _LIST_ENTRY [ 0x828a3cc4 - 0x827e96d4 ]
   +0x00c NumberOfSectionReferences : 0
   +0x010 NumberOfPfnReferences : 1
   +0x014 NumberOfMappedViews : 0
   +0x018 NumberOfSubsections : 2
   +0x01a FlushInProgressCount : 0
   +0x01c NumberOfUserReferences : 0
   +0x020 u                : __unnamed
   +0x024 FilePointer      : 0x825eb540 _FILE_OBJECT
   +0x028 WaitingForDeletion : (null)
   +0x02c ModifiedWriteCount : 0
   +0x02e NumberOfSystemCacheViews : 0
可以不用打开保护的 直接可以找到fileobject
花落时想你
花开时你在哪里

47

主题

265

回帖

8

精华

核心会员

积分
10349
 楼主| 发表于 2011-3-31 17:20:30 | 显示全部楼层
回复 jiedengye 的帖子

要的是这个函数,别的没什么~你那个文件系统的问题,咱QQ聊

29

主题

134

回帖

4

精华

论坛元老

积分
5970
QQ
发表于 2011-3-31 17:24:51 | 显示全部楼层

花落时想你
花开时你在哪里

29

主题

134

回帖

4

精华

论坛元老

积分
5970
QQ
发表于 2011-3-31 17:25:18 | 显示全部楼层
你什么时候有时间 我晚上才能上qq 444150135
花落时想你
花开时你在哪里

47

主题

265

回帖

8

精华

核心会员

积分
10349
 楼主| 发表于 2011-3-31 17:26:17 | 显示全部楼层
回复 jiedengye 的帖子

我学生,不上课,都有时间。

275

主题

3017

回帖

1

精华

管理员

嗷嗷叫的老马

积分
17064

论坛牛人贡献奖关注奖最佳版主进步奖人气王疯狂作品奖精英奖赞助论坛勋章乐于助人勋章

QQ
发表于 2011-4-1 19:59:21 | 显示全部楼层
回复 ywledoc 的帖子

现在的学生真是厉害啊...........管理员TA也是在校学生.

完全跟不上了,哈哈.
我就是嗷嗷叫的老马了......

47

主题

265

回帖

8

精华

核心会员

积分
10349
 楼主| 发表于 2011-4-1 21:08:06 | 显示全部楼层
回复 马大哈 的帖子

这个怎么说好呢。学生没有生活压力吧,有更多时间可以弄自己喜欢的东西。上班了,编一天程序下来,回家就累虚脱了。

275

主题

3017

回帖

1

精华

管理员

嗷嗷叫的老马

积分
17064

论坛牛人贡献奖关注奖最佳版主进步奖人气王疯狂作品奖精英奖赞助论坛勋章乐于助人勋章

QQ
发表于 2011-4-2 09:52:01 | 显示全部楼层
回复 ywledoc 的帖子

我现在确实是一写代码就想吐.......

不过要是有什么有意思的东西,还是会有热情的
我就是嗷嗷叫的老马了......

您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表