免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 发帖

枚举进程、线程创建回调,枚举映像加载回调,枚举DPC时钟

Driver.h
  1. #ifdef __cplusplus
  2. extern "C"
  3. {
  4. #endif
  5. #include <ntddk.h>
  6. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath);

  7. #ifdef __cplusplus
  8. }
  9. #endif

  10. #define PAGEDCODE code_seg("PAGE")
  11. #define LOCKEDCODE code_seg()
  12. #define INITCODE code_seg("INIT")

  13. #define PAGEDDATA data_seg("PAGE")
  14. #define LOCKEDDATA data_seg()
  15. #define INITDATA data_seg("INIT")

  16. #define arraysize(p) (sizeof(p)/sizeof((p)[0]))

  17. VOID DriverUnload(IN PDRIVER_OBJECT DriverObject);

  18. VOID MyCreateProcessNotifyRoutine(
  19.                                                                   IN HANDLE  ParentId,
  20.                                                                   IN HANDLE  ProcessId,
  21.                                                                   IN BOOLEAN  Create
  22.                                                                   );

  23. VOID MyCreateThreadNotifyRoutine(
  24.                                                                   IN HANDLE  ProcessId,
  25.                                                                   IN HANDLE  ThreadId,
  26.                                                                   IN BOOLEAN  Create
  27.                                                                   );

  28. VOID MyLoadImageNotifyRoutine(
  29.                                                            IN PUNICODE_STRING  FullImageName,
  30.                                                            IN HANDLE  ProcessId, // where image is mapped
  31.                                                            IN PIMAGE_INFO  ImageInfo
  32.                                                            );

  33. typedef PVOID (*pExReferenceCallBackBlock) (PVOID CallBack);
  34. typedef PVOID (*pExGetCallBackBlockRoutine) (PVOID CallBackBlock);
  35. typedef VOID (*pExDereferenceCallBackBlock) (PVOID CallBack, PVOID CallBackBlock);

  36. BOOLEAN GetNotifyRoutineAddr();
  37. void EnumNotifyRoutine(ULONG uNotifyRoutineAddr);

  38. ULONG GetKiTimerTableListHeadAddr();
  39. void EnumDpcTimer();
复制代码
Driver.cpp
  1. #include "Driver.h"

  2. ULONG g_uPspCreateProcessNotifyRoutineAddr = 0;
  3. ULONG g_uPspCreateThreadNotifyRoutineAddr = 0;
  4. ULONG g_uPspLoadImageNotifyRoutineAddr = 0;

  5. ULONG g_uKiTimerTableListHeadAddr = 0;

  6. pExReferenceCallBackBlock g_pfnExReferenceCallBackBlock = NULL;
  7. pExGetCallBackBlockRoutine g_pfnExGetCallBackBlockRoutine = NULL;
  8. pExDereferenceCallBackBlock g_pfnExDereferenceCallBackBlock = NULL;

  9. BOOLEAN GetNotifyRoutineAddr()
  10. {
  11.         ULONG uAddr = (ULONG)PsSetCreateProcessNotifyRoutine;
  12.        
  13.         PUCHAR p = (PUCHAR)uAddr;
  14.         ULONG i = 0;
  15.         BOOLEAN bFind = FALSE;

  16.         while(i < 0x50)
  17.         {
  18.                 if (*p == 0xbf
  19.                         && *(p + 5) == 0x57
  20.                         && *(p + 6) == 0xe8
  21.                         && *(p - 2) == 0x74)
  22.                 {
  23.                         bFind = TRUE;
  24.                         break;
  25.                 }
  26.                 i++;
  27.                 p++;
  28.         }
  29.        
  30.         if (!bFind)
  31.         {
  32.                 KdPrint(("Find g_uPspCreateProcessNotifyRoutineAddr faile!\n"));
  33.                 return FALSE;
  34.         }

  35.         g_uPspCreateProcessNotifyRoutineAddr = *(PULONG)(p + 1);

  36.        
  37.         p = (PUCHAR)uAddr;
  38.         i = 0;
  39.         bFind = FALSE;

  40.         while(i < 0x100)
  41.         {
  42.                 if (*p == 0xe8
  43.                         && *(p + 5) == 0x8b
  44.                         && *(p + 6) == 0xf0
  45.                         && *(p - 1) == 0x57)
  46.                 {
  47.                         bFind = TRUE;
  48.                         break;
  49.                 }
  50.                 i++;
  51.                 p++;
  52.         }

  53.         if (!bFind)
  54.         {
  55.                 KdPrint(("Find uExReferenceCallBackBlockAddr faile!\n"));
  56.                 return FALSE;
  57.         }
  58.        
  59.         ULONG uExReferenceCallBackBlockAddr = (ULONG)(*(PULONG)(p + 1) + p + 5);


  60.         p = (PUCHAR)uAddr;
  61.         i = 0;
  62.         bFind = FALSE;

  63.         while(i < 0x100)
  64.         {
  65.                 if (*p == 0xe8
  66.                         && *(p + 5) == 0x3b
  67.                         && *(p + 6) == 0x45
  68.                         && *(p - 1) == 0x56)
  69.                 {
  70.                         bFind = TRUE;
  71.                         break;
  72.                 }
  73.                 i++;
  74.                 p++;
  75.         }

  76.         if (!bFind)
  77.         {
  78.                 KdPrint(("Find uExGetCallBackBlockRoutineAddr faile!\n"));
  79.                 return FALSE;
  80.         }

  81.         ULONG uExGetCallBackBlockRoutineAddr = (ULONG)(*(PULONG)(p + 1) + p + 5);


  82.         p = (PUCHAR)uAddr;
  83.         i = 0;
  84.         bFind = FALSE;

  85.         while(i < 0x100)
  86.         {
  87.                 if (*p == 0xe8
  88.                         && *(p + 5) == 0x43
  89.                         && *(p + 6) == 0x83
  90.                         && *(p - 1) == 0x57)
  91.                 {
  92.                         bFind = TRUE;
  93.                         break;
  94.                 }
  95.                 i++;
  96.                 p++;
  97.         }

  98.         if (!bFind)
  99.         {
  100.                 KdPrint(("Find uExDereferenceCallBackBlockAddr faile!\n"));
  101.                 return FALSE;
  102.         }

  103.         ULONG uExDereferenceCallBackBlockAddr = (ULONG)(*(PULONG)(p + 1) + p + 5);

  104.         g_pfnExReferenceCallBackBlock = (pExReferenceCallBackBlock)uExReferenceCallBackBlockAddr;
  105.         g_pfnExGetCallBackBlockRoutine = (pExGetCallBackBlockRoutine)uExGetCallBackBlockRoutineAddr;
  106.         g_pfnExDereferenceCallBackBlock = (pExDereferenceCallBackBlock)uExDereferenceCallBackBlockAddr;



  107.         uAddr = (ULONG)PsRemoveCreateThreadNotifyRoutine;

  108.         p = (PUCHAR)uAddr;
  109.         i = 0;
  110.         bFind = FALSE;

  111.         while(i < 0x50)
  112.         {
  113.                 if (*p == 0xbf
  114.                         && *(p + 5) == 0x57
  115.                         && *(p + 6) == 0xe8
  116.                         && *(p - 2) == 0x33)
  117.                 {
  118.                         bFind = TRUE;
  119.                         break;
  120.                 }
  121.                 i++;
  122.                 p++;
  123.         }

  124.         if (!bFind)
  125.         {
  126.                 KdPrint(("Find g_uPspCreateThreadNotifyRoutineAddr faile!\n"));
  127.                 return FALSE;
  128.         }
  129.        
  130.         g_uPspCreateThreadNotifyRoutineAddr = *(PULONG)(p + 1);


  131.         uAddr = (ULONG)PsRemoveLoadImageNotifyRoutine;

  132.         p = (PUCHAR)uAddr;
  133.         i = 0;
  134.         bFind = FALSE;

  135.         while(i < 0x50)
  136.         {
  137.                 if (*p == 0xbf
  138.                         && *(p + 5) == 0x57
  139.                         && *(p + 6) == 0xe8
  140.                         && *(p - 2) == 0x33)
  141.                 {
  142.                         bFind = TRUE;
  143.                         break;
  144.                 }
  145.                 i++;
  146.                 p++;
  147.         }

  148.         if (!bFind)
  149.         {
  150.                 KdPrint(("Find g_uPspLoadImageNotifyRoutineAddr faile!\n"));
  151.                 return FALSE;
  152.         }

  153.         g_uPspLoadImageNotifyRoutineAddr = *(PULONG)(p + 1);


  154.         return TRUE;
  155. }

  156. void EnumNotifyRoutine(ULONG uNotifyRoutineAddr)
  157. {
  158.         if (uNotifyRoutineAddr == 0
  159.                 || g_pfnExReferenceCallBackBlock == NULL
  160.                 || g_pfnExGetCallBackBlockRoutine == NULL
  161.                 || g_pfnExDereferenceCallBackBlock == NULL)
  162.         {
  163.                 return;
  164.         }

  165.         for (ULONG i = 0; i < 8; i++)
  166.         {
  167.                 PVOID CallBack = g_pfnExReferenceCallBackBlock((PVOID)&((PULONG)uNotifyRoutineAddr)[i]);

  168.                 if (CallBack != NULL)
  169.                 {
  170.                         PVOID NotifyRoutine = g_pfnExGetCallBackBlockRoutine(CallBack);
  171.                         if (NotifyRoutine != NULL)
  172.                         {
  173.                                 KdPrint(("NotifyRoutine : 0x%08x\n", NotifyRoutine));
  174.                                 g_pfnExDereferenceCallBackBlock((PVOID)&((PULONG)uNotifyRoutineAddr)[i], CallBack);
  175.                         }
  176.                 }
  177.         }
  178. }

  179. VOID MyCreateProcessNotifyRoutine(
  180.     IN HANDLE  ParentId,
  181.     IN HANDLE  ProcessId,
  182.     IN BOOLEAN  Create
  183.     )
  184. {
  185.         KdPrint(("MyCreateProcessNotifyRoutine\n"));
  186.         return;
  187. }

  188. VOID MyCreateThreadNotifyRoutine(
  189.                                                                  IN HANDLE  ProcessId,
  190.                                                                  IN HANDLE  ThreadId,
  191.                                                                  IN BOOLEAN  Create
  192.                                                                  )
  193. {
  194.         KdPrint(("MyCreateThreadNotifyRoutine\n"));
  195.         return;
  196. }

  197. VOID MyLoadImageNotifyRoutine(
  198.                                                           IN PUNICODE_STRING  FullImageName,
  199.                                                           IN HANDLE  ProcessId, // where image is mapped
  200.                                                           IN PIMAGE_INFO  ImageInfo
  201.                                                           )
  202. {
  203.         KdPrint(("MyLoadImageNotifyRoutine\n"));
  204.         return;
  205. }



  206. ULONG GetKiTimerTableListHeadAddr()
  207. {
  208.         UNICODE_STRING ustrFuncName;
  209.         RtlInitUnicodeString(&ustrFuncName, L"KeUpdateSystemTime");

  210.         PVOID pfn = MmGetSystemRoutineAddress(&ustrFuncName);
  211.         if (pfn == NULL)
  212.         {
  213.                 return 0;
  214.         }

  215.         ULONG uAddr = (ULONG)pfn;


  216.         PUCHAR p = (PUCHAR)uAddr;
  217.         ULONG i = 0;
  218.         BOOLEAN bFind = FALSE;

  219.         while(i < 0x200)
  220.         {
  221.                 if (*p == 0x8d
  222.                         && *(p + 1) == 0x0c
  223.                         && *(p + 2) == 0xc5
  224.                         && *(p - 5) == 0x25)
  225.                 {
  226.                         bFind = TRUE;
  227.                         break;
  228.                 }
  229.                 i++;
  230.                 p++;
  231.         }

  232.         if (!bFind)
  233.         {
  234.                 KdPrint(("Find faile!\n"));
  235.                 return 0;
  236.         }

  237.         uAddr = *(PULONG)(p + 3);

  238.         return uAddr;
  239. }

  240. void EnumDpcTimer()
  241. {
  242.         PLIST_ENTRY ListHead;
  243.         PLIST_ENTRY NextEntry;
  244.         PKTIMER NextTimer;
  245.         ULONG uDpcAddr;

  246.         for (ULONG i = 0; i < 0x100; i++)
  247.         {
  248.                 ListHead = (PLIST_ENTRY)(&((PLIST_ENTRY)g_uKiTimerTableListHeadAddr)[i]);

  249.                 if (!MmIsAddressValid(ListHead->Blink))
  250.                 {
  251.                         continue;
  252.                 }

  253.                 if (!MmIsAddressValid(ListHead->Flink))
  254.                 {
  255.                         continue;
  256.                 }

  257.                 NextEntry = ListHead->Blink;

  258.                 while (NextEntry != ListHead)
  259.                 {
  260.                         NextTimer = CONTAINING_RECORD(NextEntry, KTIMER, TimerListEntry);

  261.                         if (NextEntry->Blink == NextEntry->Flink)
  262.                         {
  263.                                 break;
  264.                         }

  265.                         if (MmIsAddressValid(NextTimer)
  266.                                 && MmIsAddressValid(NextTimer->Dpc)
  267.                                 && MmIsAddressValid(NextTimer->Dpc->DeferredRoutine)
  268.                                 && MmIsAddressValid(&NextTimer->Period))
  269.                         {
  270.                                         uDpcAddr = (ULONG)NextTimer->Dpc->DeferredRoutine;
  271.                                         ULONG uTimer = NextTimer->Period / 1000;
  272.                                         KdPrint(("uDpcAddr : 0x%08x %d\n", uDpcAddr, uTimer));
  273.                         }
  274.                
  275.                         if (!MmIsAddressValid(NextEntry->Blink))
  276.                         {
  277.                                 break;
  278.                         }
  279.                         NextEntry = NextEntry->Blink;
  280.                 }

  281.         }

  282.        
  283. }



  284. #pragma INITCODE
  285. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
  286. {
  287.     NTSTATUS status = STATUS_SUCCESS;

  288.         PsSetCreateProcessNotifyRoutine(MyCreateProcessNotifyRoutine, FALSE);
  289.         PsSetCreateThreadNotifyRoutine(MyCreateThreadNotifyRoutine);
  290.         PsSetLoadImageNotifyRoutine(MyLoadImageNotifyRoutine);

  291.         if (GetNotifyRoutineAddr())
  292.         {
  293.                 EnumNotifyRoutine(g_uPspCreateProcessNotifyRoutineAddr);
  294.                 EnumNotifyRoutine(g_uPspCreateThreadNotifyRoutineAddr);
  295.                 EnumNotifyRoutine(g_uPspLoadImageNotifyRoutineAddr);
  296.         }



  297.         g_uKiTimerTableListHeadAddr = GetKiTimerTableListHeadAddr();
  298.         if (g_uKiTimerTableListHeadAddr != 0)
  299.         {
  300.                 EnumDpcTimer();
  301.         }

  302.     DriverObject->DriverUnload = DriverUnload;
  303.     return status;
  304. }

  305. #pragma PAGEDCODE
  306. VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
  307. {
  308.   
  309.         PsSetCreateProcessNotifyRoutine(MyCreateProcessNotifyRoutine, TRUE);
  310.         PsRemoveCreateThreadNotifyRoutine(MyCreateThreadNotifyRoutine);
  311.         PsRemoveLoadImageNotifyRoutine(MyLoadImageNotifyRoutine);

  312.     KdPrint(("DriverEntry unLoading...\n"));
  313.    
  314. }
复制代码

返回列表