逆向XignCode3驱动程序:分析init初始化函数(part2)

系统
在第1部分中,我确定了驱动程序的Dispatcher函数以及两个正在初始化驱动程序的某些变量的函数(**fn_InitDispatchMethodArray**和**fn_ObtainKernelFunctions**)。 逆向这两个函数,分析一下它们的功能,这会帮助我了解在此驱动程序上实现的Dispatcher函数。

 在第1部分中,我确定了驱动程序的Dispatcher函数以及两个正在初始化驱动程序的某些变量的函数(fn_InitDispatchMethodArray和fn_ObtainKernelFunctions)。

逆向这两个函数,分析一下它们的功能,这会帮助我了解在此驱动程序上实现的Dispatcher函数。

0x01 概述

1. 该驱动程序的一些基本初始化机制

2. 确定用于索引和存储所有可用方法的自定义结构。

3. 确定函数地址在内存中的位置,例如ObRegisterCallbacks。

0x02 fn_InitDispatchMethodArray(0x1400015F8)

该函数的代码片段:

 

  1. __int64 sub_1400015F8() 
  2.  __int64 result; // rax 
  3.  
  4.  memset(&dword_140009E40, 0, 0x400ui64); 
  5.  dword_140009E40 = 774; 
  6.  qword_140009E48 = (__int64)sub_140001058; 
  7.  qword_140009E58 = (__int64)sub_14000101C; 
  8.  qword_140009E68 = (__int64)sub_140001CC8; 
  9.  qword_140009E78 = (__int64)sub_140001BFC; 
  10.  qword_140009E88 = (__int64)sub_140001DC0; 
  11.  qword_140009E98 = (__int64)sub_140001B50; 
  12.  qword_140009EA8 = (__int64)sub_140001C3C; 
  13.  qword_140009EB8 = (__int64)sub_140001D04; 
  14.  qword_140009EC8 = (__int64)sub_14000137C; 
  15.  qword_140009ED8 = (__int64)sub_14000191C; 
  16.  qword_140009EE8 = (__int64)sub_140001340; 
  17.  qword_140009EF8 = (__int64)sub_140001A58; 
  18.  qword_140009F08 = (__int64)sub_1400019A4; 
  19.  qword_140009F18 = (__int64)sub_140001224; 
  20.  qword_140009F28 = (__int64)sub_14000187C; 
  21.  qword_140009F38 = (__int64)sub_140001488; 
  22.  qword_140009F48 = (__int64)sub_140001548; 
  23.  qword_140009F58 = (__int64)sub_1400013B8; 
  24.  qword_140009F68 = (__int64)sub_140001264; 
  25.  qword_140009F78 = (__int64)sub_14000150C; 
  26.  qword_140009F88 = (__int64)sub_140001174; 
  27.  qword_140009F98 = (__int64)sub_1400015CC; 
  28.  qword_140009FA8 = (__int64)sub_14000107C; 
  29.  qword_140009FB8 = (__int64)sub_140001D4C; 
  30.  qword_140009FC8 = (__int64)sub_140001D88; 
  31.  result = 0i64; 
  32.  dword_140009E50 = 775; 
  33.  dword_140009E60 = 776; 
  34.  dword_140009E70 = 777; 
  35.  dword_140009E80 = 778; 
  36.  dword_140009E90 = 779; 
  37.  dword_140009EA0 = 790; 
  38.  dword_140009EB0 = 782; 
  39.  dword_140009EC0 = 783; 
  40.  dword_140009ED0 = 785; 
  41.  dword_140009EE0 = 786; 
  42.  dword_140009EF0 = 787; 
  43.  dword_140009F00 = 788; 
  44.  dword_140009F10 = 789; 
  45.  dword_140009F20 = 791; 
  46.  dword_140009F30 = 792; 
  47.  dword_140009F40 = 793; 
  48.  dword_140009F50 = 794; 
  49.  dword_140009F60 = 796; 
  50.  dword_140009F70 = 797; 
  51.  dword_140009F80 = 798; 
  52.  dword_140009F90 = 799; 
  53.  dword_140009FA0 = 800; 
  54.  dword_140009FB0 = 801; 
  55.  dword_140009FC0 = 802; 
  56.  dword_14000A240 = 25; 
  57.  return result; 

 

汇编代码段:

 

  1. .text:00000001400015F8 ; =============== S U B R O U T I N E ======================================= 
  2. .text:00000001400015F8 
  3. .text:00000001400015F8 
  4. .text:00000001400015F8 sub_1400015F8   proc near               ; CODE XREF: DriverEntry+110↓p 
  5. .text:00000001400015F8                                         ; DATA XREF: .pdata:000000014000D084↓o 
  6. .text:00000001400015F8                 sub     rsp, 28h 
  7. .text:00000001400015FC                 xor     edx, edx        ; Val 
  8. .text:00000001400015FE                 lea     rcx, dword_140009E40 ; Dst 
  9. .text:0000000140001605                 mov     r8d, 400h       ; Size 
  10. .text:000000014000160B                 call    memset 
  11. .text:0000000140001610                 lea     rax, sub_140001058 
  12. .text:0000000140001617                 mov     cs:dword_140009E40, 306h 
  13. .text:0000000140001621                 mov     cs:qword_140009E48, rax 
  14. .text:0000000140001628                 lea     rax, sub_14000101C 
  15. .text:000000014000162F                 mov     cs:qword_140009E58, rax 
  16. .text:0000000140001636                 lea     rax, sub_140001CC8 
  17. .text:000000014000163D                 mov     cs:qword_140009E68, rax 
  18. .text:0000000140001644                 lea     rax, sub_140001BFC 
  19. .text:000000014000164B                 mov     cs:qword_140009E78, rax 
  20. .text:0000000140001652                 lea     rax, sub_140001DC0 
  21. .text:0000000140001659                 mov     cs:qword_140009E88, rax 
  22. .text:0000000140001660                 lea     rax, sub_140001B50 
  23. .text:0000000140001667                 mov     cs:qword_140009E98, rax 
  24. .text:000000014000166E                 lea     rax, sub_140001C3C 
  25. .text:0000000140001675                 mov     cs:qword_140009EA8, rax 
  26. .text:000000014000167C                 lea     rax, sub_140001D04 
  27. .text:0000000140001683                 mov     cs:qword_140009EB8, rax 
  28. .text:000000014000168A                 lea     rax, sub_14000137C 
  29. .text:0000000140001691                 mov     cs:qword_140009EC8, rax 
  30. .text:0000000140001698                 lea     rax, sub_14000191C 
  31. .text:000000014000169F                 mov     cs:qword_140009ED8, rax 
  32. .text:00000001400016A6                 lea     rax, sub_140001340 
  33. .text:00000001400016AD                 mov     cs:qword_140009EE8, rax 
  34. .text:00000001400016B4                 lea     rax, sub_140001A58 
  35. .text:00000001400016BB                 mov     cs:qword_140009EF8, rax 
  36. .text:00000001400016C2                 lea     rax, sub_1400019A4 
  37. .text:00000001400016C9                 mov     cs:qword_140009F08, rax 
  38. .text:00000001400016D0                 lea     rax, sub_140001224 
  39. .text:00000001400016D7                 mov     cs:qword_140009F18, rax 
  40. .text:00000001400016DE                 lea     rax, sub_14000187C 
  41. .text:00000001400016E5                 mov     cs:qword_140009F28, rax 
  42. .text:00000001400016EC                 lea     rax, sub_140001488 
  43. .text:00000001400016F3                 mov     cs:qword_140009F38, rax 
  44. .text:00000001400016FA                 lea     rax, sub_140001548 
  45. .text:0000000140001701                 mov     cs:qword_140009F48, rax 
  46. .text:0000000140001708                 lea     rax, sub_1400013B8 
  47. .text:000000014000170F                 mov     cs:qword_140009F58, rax 
  48. .text:0000000140001716                 lea     rax, sub_140001264 
  49. .text:000000014000171D                 mov     cs:qword_140009F68, rax 
  50. .text:0000000140001724                 lea     rax, sub_14000150C 
  51. .text:000000014000172B                 mov     cs:qword_140009F78, rax 
  52. .text:0000000140001732                 lea     rax, sub_140001174 
  53. .text:0000000140001739                 mov     cs:qword_140009F88, rax 
  54. .text:0000000140001740                 lea     rax, sub_1400015CC 
  55. .text:0000000140001747                 mov     cs:qword_140009F98, rax 
  56. .text:000000014000174E                 lea     rax, sub_14000107C 
  57. .text:0000000140001755                 mov     cs:qword_140009FA8, rax 
  58. .text:000000014000175C                 lea     rax, sub_140001D4C 
  59. .text:0000000140001763                 mov     cs:qword_140009FB8, rax 
  60. .text:000000014000176A                 lea     rax, sub_140001D88 
  61. .text:0000000140001771                 mov     cs:qword_140009FC8, rax 
  62. .text:0000000140001778                 xor     eax, eax 
  63. .text:000000014000177A                 mov     cs:dword_140009E50, 307h 
  64. .text:0000000140001784                 mov     cs:dword_140009E60, 308h 
  65. .text:000000014000178E                 mov     cs:dword_140009E70, 309h 
  66. .text:0000000140001798                 mov     cs:dword_140009E80, 30Ah 
  67. .text:00000001400017A2                 mov     cs:dword_140009E90, 30Bh 
  68. .text:00000001400017AC                 mov     cs:dword_140009EA0, 316h 
  69. .text:00000001400017B6                 mov     cs:dword_140009EB0, 30Eh 
  70. .text:00000001400017C0                 mov     cs:dword_140009EC0, 30Fh 
  71. .text:00000001400017CA                 mov     cs:dword_140009ED0, 311h 
  72. .text:00000001400017D4                 mov     cs:dword_140009EE0, 312h 
  73. .text:00000001400017DE                 mov     cs:dword_140009EF0, 313h 
  74. .text:00000001400017E8                 mov     cs:dword_140009F00, 314h 
  75. .text:00000001400017F2                 mov     cs:dword_140009F10, 315h 
  76. .text:00000001400017FC                 mov     cs:dword_140009F20, 317h 
  77. .text:0000000140001806                 mov     cs:dword_140009F30, 318h 
  78. .text:0000000140001810                 mov     cs:dword_140009F40, 319h 
  79. .text:000000014000181A                 mov     cs:dword_140009F50, 31Ah 
  80. .text:0000000140001824                 mov     cs:dword_140009F60, 31Ch 
  81. .text:000000014000182E                 mov     cs:dword_140009F70, 31Dh 
  82. .text:0000000140001838                 mov     cs:dword_140009F80, 31Eh 
  83. .text:0000000140001842                 mov     cs:dword_140009F90, 31Fh 
  84. .text:000000014000184C                 mov     cs:dword_140009FA0, 320h 
  85. .text:0000000140001856                 mov     cs:dword_140009FB0, 321h 
  86. .text:0000000140001860                 mov     cs:dword_140009FC0, 322h 
  87. .text:000000014000186A                 mov     cs:dword_14000A240, 19h 
  88. .text:0000000140001874                 add     rsp, 28h 
  89. .text:0000000140001878                 retn 
  90. .text:0000000140001878 sub_1400015F8   endp 
  91. .text:0000000140001878 

 

还原后的代码:

 

  1. __int64 fn_InitDispatchMethodArray() 
  2.  __int64 result; // rax 
  3.  
  4.  memset(IOCTLFunctionArray, 0, 0x400ui64); 
  5.  IOCTLFunctionArray[0].Index = 774;            // 9E40 
  6.  IOCTLFunctionArray[0].FnPtr = sub_140001058; 
  7.  IOCTLFunctionArray[1].FnPtr = sub_14000101C; 
  8.  IOCTLFunctionArray[2].FnPtr = sub_140001CC8; 
  9.  IOCTLFunctionArray[3].FnPtr = sub_140001BFC; 
  10.  IOCTLFunctionArray[4].FnPtr = sub_140001DC0; 
  11.  IOCTLFunctionArray[5].FnPtr = sub_140001B50; 
  12.  IOCTLFunctionArray[6].FnPtr = sub_140001C3C; 
  13.  IOCTLFunctionArray[7].FnPtr = sub_140001D04; 
  14.  IOCTLFunctionArray[8].FnPtr = sub_14000137C; 
  15.  IOCTLFunctionArray[9].FnPtr = sub_14000191C; 
  16.  IOCTLFunctionArray[10].FnPtr = sub_140001340; 
  17.  IOCTLFunctionArray[11].FnPtr = sub_140001A58; 
  18.  IOCTLFunctionArray[12].FnPtr = sub_1400019A4; 
  19.  IOCTLFunctionArray[13].FnPtr = sub_140001224; 
  20.  IOCTLFunctionArray[14].FnPtr = sub_14000187C; 
  21.  IOCTLFunctionArray[15].FnPtr = sub_140001488; 
  22.  IOCTLFunctionArray[16].FnPtr = sub_140001548; 
  23.  IOCTLFunctionArray[17].FnPtr = sub_1400013B8; 
  24.  IOCTLFunctionArray[18].FnPtr = fn_ReadFileContent_; 
  25.  IOCTLFunctionArray[19].FnPtr = fn_IOCTL_ValidatePidPEB; 
  26.  IOCTLFunctionArray[20].FnPtr = fn_IOCTL_ControlCallbackRoutines; 
  27.  IOCTLFunctionArray[21].FnPtr = sub_1400015CC; 
  28.  IOCTLFunctionArray[22].FnPtr = sub_14000107C; 
  29.  IOCTLFunctionArray[23].FnPtr = sub_140001D4C; // CR0 
  30.  IOCTLFunctionArray[24].FnPtr = sub_140001D88; 
  31.  result = 0i64; 
  32.  IOCTLFunctionArray[1].Index = 775; 
  33.  IOCTLFunctionArray[2].Index = 776; 
  34.  IOCTLFunctionArray[3].Index = 777; 
  35.  IOCTLFunctionArray[4].Index = 778; 
  36.  IOCTLFunctionArray[5].Index = 779; 
  37.  IOCTLFunctionArray[6].Index = 790; 
  38.  IOCTLFunctionArray[7].Index = 782; 
  39.  IOCTLFunctionArray[8].Index = 783; 
  40.  IOCTLFunctionArray[9].Index = 785; 
  41.  IOCTLFunctionArray[10].Index = 786; 
  42.  IOCTLFunctionArray[11].Index = 787; 
  43.  IOCTLFunctionArray[12].Index = 788; 
  44.  IOCTLFunctionArray[13].Index = 789; 
  45.  IOCTLFunctionArray[14].Index = 791; 
  46.  IOCTLFunctionArray[15].Index = 792; 
  47.  IOCTLFunctionArray[16].Index = 793; 
  48.  IOCTLFunctionArray[17].Index = 794; 
  49.  IOCTLFunctionArray[18].Index = 796; 
  50.  IOCTLFunctionArray[19].Index = 797; 
  51.  IOCTLFunctionArray[20].Index = 798; 
  52.  IOCTLFunctionArray[21].Index = 799; 
  53.  IOCTLFunctionArray[22].Index = 800; 
  54.  IOCTLFunctionArray[23].Index = 801; 
  55.  IOCTLFunctionArray[24].Index = 802; 
  56.  FunctionsCount = 0x19; 
  57.  return result; 

 

可以在此函数中看到正在初始化的自定义结构:

 

  1. .text:0000000140001617                 mov     cs:dword_140009E40, 306h 
  2. .text:0000000140001621                 mov     cs:qword_140009E48, rax 
  3. .text:0000000140001628                 lea     rax, sub_14000101C 
  4. .text:000000014000162F                 mov     cs:qword_140009E58, rax 
  5. .text:0000000140001636                 lea     rax, sub_140001CC8 
  6. .text:000000014000163D                 mov     cs:qword_140009E68, rax 

 

上面的汇编代码中,可以看到它们首先将int值分配给特定的内存地址,然后再移动8个字节将指针写入函数,我将这种结构称为IOCTLFunctionArray,该数组将在调度请求时发挥重要作用。

结构如下所示:

 

  1. typedef struct DispatcherStruct  { 
  2.     int Index
  3.     char padding[4]; 
  4.     PVOID FnPtr; 
  5. }; 

 

在IDA Pro中:

 

  1. 00000000 DispatcherStruct struc ; (sizeof=0x10, mappedto_424) 
  2. 00000000                                         ; XREF: .data:_IOCTLFunctionArray/r 
  3. 00000000 Index           dd ?                    ; XREF: fn_InitDispatchMethodArray+1F/t 
  4. 00000004 padding         db 4 dup(?) 
  5. 00000008 FnPtr           dq ? 
  6. 00000010 DispatcherStruct ends 

 

此函数重复了25次,所以称其为array,它们在数组中存储相同结构25次。

将dword_14000A240重命名为FunctionsCount的变量:

 

  1. .text:000000014000186A                 mov     cs:FunctionsCount , 19h 

之后将看到在Dispatcher上如何使用此变量,基于此函数,可以确定驱动程序没有调用所有可用方法的列表,并提供某种索引值,可以调用它们。

 最终整理的代码片段:

https://github.com/niemand-sec/Reversing-XignCode3-Driver/blob/master/XC3/fn_InitDispatchMethodArray_reversed.c

 

  1. __int64 fn_InitDispatchMethodArray() 
  2.  __int64 result; // rax 
  3.  
  4.  memset(IOCTLFunctionArray, 0, 0x400ui64); 
  5.  IOCTLFunctionArray[0].Index = 774;            // 9E40 
  6.  IOCTLFunctionArray[0].FnPtr = sub_140001058; 
  7.  IOCTLFunctionArray[1].FnPtr = sub_14000101C; 
  8.  IOCTLFunctionArray[2].FnPtr = sub_140001CC8; 
  9.  IOCTLFunctionArray[3].FnPtr = sub_140001BFC; 
  10.  IOCTLFunctionArray[4].FnPtr = sub_140001DC0; 
  11.  IOCTLFunctionArray[5].FnPtr = sub_140001B50; 
  12.  IOCTLFunctionArray[6].FnPtr = sub_140001C3C; 
  13.  IOCTLFunctionArray[7].FnPtr = sub_140001D04; 
  14.  IOCTLFunctionArray[8].FnPtr = sub_14000137C; 
  15.  IOCTLFunctionArray[9].FnPtr = sub_14000191C; 
  16.  IOCTLFunctionArray[10].FnPtr = sub_140001340; 
  17.  IOCTLFunctionArray[11].FnPtr = sub_140001A58; 
  18.  IOCTLFunctionArray[12].FnPtr = sub_1400019A4; 
  19.  IOCTLFunctionArray[13].FnPtr = sub_140001224; 
  20.  IOCTLFunctionArray[14].FnPtr = sub_14000187C; 
  21.  IOCTLFunctionArray[15].FnPtr = sub_140001488; 
  22.  IOCTLFunctionArray[16].FnPtr = sub_140001548; 
  23.  IOCTLFunctionArray[17].FnPtr = sub_1400013B8; 
  24.  IOCTLFunctionArray[18].FnPtr = fn_ReadFileContent_; 
  25.  IOCTLFunctionArray[19].FnPtr = fn_IOCTL_ValidatePidPEB; 
  26.  IOCTLFunctionArray[20].FnPtr = fn_IOCTL_ControlCallbackRoutines; 
  27.  IOCTLFunctionArray[21].FnPtr = sub_1400015CC; 
  28.  IOCTLFunctionArray[22].FnPtr = sub_14000107C; 
  29.  IOCTLFunctionArray[23].FnPtr = sub_140001D4C; // CR0 
  30.  IOCTLFunctionArray[24].FnPtr = sub_140001D88; 
  31.  result = 0i64; 
  32.  IOCTLFunctionArray[1].Index = 775; 
  33.  IOCTLFunctionArray[2].Index = 776; 
  34.  IOCTLFunctionArray[3].Index = 777; 
  35.  IOCTLFunctionArray[4].Index = 778; 
  36.  IOCTLFunctionArray[5].Index = 779; 
  37.  IOCTLFunctionArray[6].Index = 790; 
  38.  IOCTLFunctionArray[7].Index = 782; 
  39.  IOCTLFunctionArray[8].Index = 783; 
  40.  IOCTLFunctionArray[9].Index = 785; 
  41.  IOCTLFunctionArray[10].Index = 786; 
  42.  IOCTLFunctionArray[11].Index = 787; 
  43.  IOCTLFunctionArray[12].Index = 788; 
  44.  IOCTLFunctionArray[13].Index = 789; 
  45.  IOCTLFunctionArray[14].Index = 791; 
  46.  IOCTLFunctionArray[15].Index = 792; 
  47.  IOCTLFunctionArray[16].Index = 793; 
  48.  IOCTLFunctionArray[17].Index = 794; 
  49.  IOCTLFunctionArray[18].Index = 796; 
  50.  IOCTLFunctionArray[19].Index = 797; 
  51.  IOCTLFunctionArray[20].Index = 798; 
  52.  IOCTLFunctionArray[21].Index = 799; 
  53.  IOCTLFunctionArray[22].Index = 800; 
  54.  IOCTLFunctionArray[23].Index = 801; 
  55.  IOCTLFunctionArray[24].Index = 802; 
  56.  FunctionsCount = 0x19; 
  57.  return result; 

 

0x03 fn_ObtainKernelFunctions(0x140002A18)

下面的函数很简单,为了继续进行初始化,驱动程序需要一些特定例程的地址:

这样做可以确保这些函数在运行的Windows版本上可用,并获得指向它们的指针,只需要将它们存储在一个变量中,然后通过将其转换为属性函数定义来使用它们来调用这些例程。

也可以在汇编函数中发现:

 

  1. .text:0000000140002A1C                 lea     rdx, SourceString ; "ObGetFilterVersion" 
  2. .text:0000000140002A23                 lea     rcx, [rsp+38h+DestinationString] ; DestinationString 
  3. .text:0000000140002A28                 call    cs:RtlInitUnicodeString 
  4. .text:0000000140002A2E                 lea     rcx, [rsp+38h+DestinationString] ; SystemRoutineName 
  5. .text:0000000140002A33                 call    cs:MmGetSystemRoutineAddress 
  6. .text:0000000140002A39                 lea     rdx, aObregistercall ; "ObRegisterCallbacks" 
  7. .text:0000000140002A40                 mov     cs:qword_14000A288, rax 
  8. .text:0000000140002A47                 lea     rcx, [rsp+38h+DestinationString] ; DestinationString 
  9. .text:0000000140002A4C                 call    cs:RtlInitUnicodeString 
  10. .text:0000000140002A52                 lea     rcx, [rsp+38h+DestinationString] ; SystemRoutineName 
  11. .text:0000000140002A57                 call    cs:MmGetSystemRoutineAddress 

 

本文翻译自:https://niemand.com.ar/2020/01/16/reversing-xigncode3-driver-part-2-analyzing-init-functions/如若转载,请注明原文地址。

 

责任编辑:姜华 来源: 嘶吼网
相关推荐

2020-12-03 10:20:42

Dispatcher

2021-10-11 08:51:05

Linux console Linux 系统

2022-12-19 09:02:04

深入布局协议HStack

2021-03-12 10:30:11

SpringMVC流程初始化

2022-07-06 10:37:45

SpringServlet初始化

2011-04-01 16:30:26

T-SQLDateTime

2009-07-20 18:01:38

Oracle JDBC

2009-07-06 18:17:46

JDBC驱动程序

2009-09-17 16:06:22

C#数组初始化

2016-03-29 10:39:46

iOS函数初始化

2022-01-26 11:00:58

源码层面Vue3

2023-11-12 23:08:17

C++初始化

2017-03-30 07:56:30

测试前端代码

2010-02-05 17:16:05

C++构造函数

2021-09-27 07:39:52

Go初始化函数package

2010-01-07 13:27:22

Linux驱动程序

2009-08-12 18:20:39

C#事件驱动程序

2011-01-10 18:21:38

linux编写程序

2017-03-03 08:40:32

2009-07-16 13:26:43

DB2 JDBC驱动
点赞
收藏

51CTO技术栈公众号