Paste: SSDT Hook Exemple[by santabug] - PasteKorbe

PasteKorbe



SSDT Hook Exemple[by santabug] par KPCR en cpp

la description |

Un exemple de SSDT Hooking.

avec / sans numéros de ligne

  1. /*
  2. A SSDT HOOK EXAMPLE of ZwCreateFile.
  3. .'\ /`. -- by santabug
  4. .'.-.`-'.-.`.
  5. ..._: .-. .-. :_...
  6. .' '-.(o ) (o ).-' `. http://santabug.blogspot.com/
  7. : _ _ _`~(_)~`_ _ _ : santabug[at]live[dot]fr
  8. : /: ' .-=_ _=-. ` ;\ :
  9. : :|-.._ ' ` _..-|: :
  10. : `:| |`:-:-.-:-:'| |:' :
  11. `. `.| | | | | | |.' .'
  12. `. `-:_| | |_:-' .'
  13. `-._ ```` _.-'
  14. ``-------''
  15. The ZwCreateFile API is hooked and returns an error
  16. if the created file is "c:\pron.txt".
  17. */
  18.  
  19.  
  20. #include <ntddk.h>
  21.  
  22. // usefull <3
  23. #define SYSTEMSERVICE(_name) KeServiceDescriptorTable.ServiceTable[*(DWORD *) ((unsigned char *)_name + 1)]
  24.  
  25. #define DEBUG
  26. typedef unsigned long DWORD, *PDWORD;
  27. typedef unsigned char BYTE, *PBYTE;
  28.  
  29.  
  30. void hooking(void);
  31. void Unhooking();
  32. NTSTATUS DriverEntry(PDRIVER_OBJECT, PUNICODE_STRING); // main() du driver
  33. void Unload_driver(IN PDRIVER_OBJECT);
  34.  
  35.  
  36.  
  37.  
  38. #pragma pack(1)
  39. typedef struct ServiceDescriptorEntry
  40. {
  41. PDWORD ServiceTable;
  42. PDWORD CounterTableBase;
  43. DWORD ServiceLimit;
  44. PBYTE ArgumentTable;
  45. } SDT;
  46. #pragma pack()
  47.  
  48. __declspec(dllimport) SDT KeServiceDescriptorTable;
  49.  
  50.  
  51. typedef NTSYSAPI NTSTATUS (*ZWCREATEFILE)
  52. (
  53.  
  54. OUT PHANDLE FileHandle,
  55. IN ACCESS_MASK DesiredAccess,
  56. IN POBJECT_ATTRIBUTES ObjectAttributes,
  57. OUT PIO_STATUS_BLOCK IoStatusBlock,
  58. IN PLARGE_INTEGER AllocationSize OPTIONAL,
  59. IN ULONG FileAttributes,
  60. IN ULONG ShareAccess,
  61. IN ULONG CreateDisposition,
  62. IN ULONG CreateOptions,
  63. IN PVOID EaBuffer OPTIONAL,
  64. IN ULONG EaLength
  65. );
  66.  
  67. ZWCREATEFILE OrigZwCreateFile;
  68. extern ZWCREATEFILE OrigZwCreateFile;
  69. /*
  70. NTSTATUS RtlCompareUnicodeString(
  71. IN PUNICODE_STRING String1,
  72. IN PUNICODE_STRING String2,
  73. IN BOOLEAN CaseInSensitive
  74. );
  75. typedef struct _OBJECT_ATTRIBUTES {
  76. ULONG Length;
  77. HANDLE RootDirectory;
  78. PUNICODE_STRING ObjectName; <= Yawn :]
  79. ULONG Attributes;
  80. PVOID SecurityDescriptor;
  81. PVOID SecurityQualityOfService;
  82. } OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES;
  83. typedef CONST OBJECT_ATTRIBUTES *PCOBJECT_ATTRIBUTES;
  84. */
  85.  
  86. NTSTATUS FakeZwCreateFile(
  87.  
  88. OUT PHANDLE FileHandle,
  89. IN ACCESS_MASK DesiredAccess,
  90. IN POBJECT_ATTRIBUTES ObjectAttributes,
  91. OUT PIO_STATUS_BLOCK IoStatusBlock,
  92. IN PLARGE_INTEGER AllocationSize OPTIONAL,
  93. IN ULONG FileAttributes,
  94. IN ULONG ShareAccess,
  95. IN ULONG CreateDisposition,
  96. IN ULONG CreateOptions,
  97. IN PVOID EaBuffer OPTIONAL,
  98. IN ULONG EaLength)
  99. {
  100. UNICODE_STRING FileName;
  101. RtlInitUnicodeString(&FileName, L"\\??\\C:\\pron.txt");
  102. //KdPrint(("FILEZ : %wZ\n", ObjectAttributes->ObjectName)); // unicode string
  103. if(RtlCompareUnicodeString(ObjectAttributes->ObjectName,&FileName, TRUE)==0x00)
  104. {
  105. DbgPrint("pron text file detected !\n");
  106. return(STATUS_OBJECT_NAME_INVALID);
  107. }
  108. else
  109. {
  110. // on appelle la vraie fonction
  111. ((ZWCREATEFILE)(OrigZwCreateFile)) (
  112. FileHandle,
  113. DesiredAccess,
  114. ObjectAttributes,
  115. IoStatusBlock,
  116. AllocationSize,
  117. FileAttributes,
  118. ShareAccess,
  119. CreateDisposition,
  120. CreateOptions,
  121. EaBuffer,
  122. EaLength);
  123. return(STATUS_SUCCESS);
  124. // ZwCreateFile returns STATUS_SUCCESS on success or an appropriate NTSTATUS error code on failure
  125. }
  126. }
  127.  
  128. void Hooking(void) {
  129.  
  130. //cli, sti - désactive/rétablit les interruptions
  131. _asm{cli}
  132. // on pose notre hook ainsi
  133. OrigZwCreateFile = (ZWCREATEFILE) (SYSTEMSERVICE(ZwCreateFile));
  134. (ZWCREATEFILE) (SYSTEMSERVICE(ZwCreateFile)) = FakeZwCreateFile;
  135. _asm{sti}
  136. }
  137.  
  138. void Unhooking()
  139. {
  140.  
  141. _asm{cli}
  142. (ZWCREATEFILE) (SYSTEMSERVICE(ZwCreateFile)) = OrigZwCreateFile; // on restaure
  143. _asm{sti}
  144. }
  145.  
  146. void Unload_driver(IN PDRIVER_OBJECT DriverObject) {
  147. DbgPrint("And now, unhooking API :] \n");
  148. Unhooking();
  149. }
  150.  
  151. NTSTATUS DriverEntry(PDRIVER_OBJECT driverObject, PUNICODE_STRING RegistryPath) {
  152. driverObject->DriverUnload = Unload_driver;
  153. DbgPrint("Hooking API!\n");
  154. Hooking();
  155. return(STATUS_SUCCESS);
  156. }