看流星社区

 找回密码
 注册账号
查看: 2121|回复: 0

[源码]监控系统加载模块-猥琐流

[复制链接]

该用户从未签到

发表于 2013-3-21 08:41:51 | 显示全部楼层 |阅读模式
可动态监控驱动、dll、exe加载,这两天刚倒腾出来的
windows2003 x86/x64 window7 x86 windows2008 R2 x64测试通过
  1. #include <ntddk.h>
  2. #include "nt_help.h"

  3. DRIVER_INITIALIZE DriverEntry;

  4. typedef struct _OBJECT_TYPE_INITIALIZER {
  5.     USHORT Length;
  6.     BOOLEAN UseDefaultObject;
  7.     BOOLEAN CaseInsensitive;
  8. #if WINVER>=0x0600
  9.     ULONG ObjectTypeCode;
  10. #endif
  11.     ULONG InvalidAttributes;
  12.     GENERIC_MAPPING GenericMapping;
  13.     ULONG ValidAccessMask;
  14.     BOOLEAN SecurityRequired;
  15.     BOOLEAN MaintainHandleCount;
  16.     BOOLEAN MaintainTypeList;
  17.     POOL_TYPE PoolType;
  18.     ULONG DefaultPagedPoolCharge;
  19.     ULONG DefaultNonPagedPoolCharge;
  20.     PVOID DumpProcedure;
  21.     PVOID OpenProcedure;
  22.     PVOID CloseProcedure;
  23.     PVOID DeleteProcedure;
  24.     PVOID ParseProcedure;
  25.     PVOID SecurityProcedure;
  26.     PVOID QueryNameProcedure;
  27.     PVOID OkayToCloseProcedure;
  28. } OBJECT_TYPE_INITIALIZER, *POBJECT_TYPE_INITIALIZER;

  29. typedef struct _OBJECT_TYPE {
  30. #if WINVER<0x0600
  31.     ERESOURCE Mutex;
  32. #endif
  33.     LIST_ENTRY TypeList;
  34.     UNICODE_STRING Name;            // Copy from object header for convenience
  35.     PVOID DefaultObject;
  36.     ULONG Index;
  37.     ULONG TotalNumberOfObjects;
  38.     ULONG TotalNumberOfHandles;
  39.     ULONG HighWaterNumberOfObjects;
  40.     ULONG HighWaterNumberOfHandles;
  41.     OBJECT_TYPE_INITIALIZER TypeInfo;
  42. } OBJECT_TYPE, *POBJECT_TYPE;

  43. extern POBJECT_TYPE* MmSectionObjectType;
  44. PVOID pNtCreateSection = NULL;
  45. SYSTEM_MODULE_INFORMATION ntModInfo = {0};

  46. #pragma alloc_text(INIT, DriverEntry)

  47. NTSTATUS DevicePassthrough(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
  48. {
  49.         NTSTATUS status = STATUS_SUCCESS;
  50.         PIO_STACK_LOCATION  irpSp;
  51.         
  52.         irpSp = IoGetCurrentIrpStackLocation(Irp);
  53.         Irp->IoStatus.Status = status;
  54.         IoCompleteRequest(Irp, IO_NO_INCREMENT);
  55.         return status;
  56. }

  57. VOID DriverUnload (IN PDRIVER_OBJECT DriverObject)
  58. {
  59.         (*MmSectionObjectType)->TypeInfo.OpenProcedure = NULL;
  60.         KdPrint(("DriverUnload Done!\n"));
  61. }

  62. #if WINVER>=0x0600
  63. NTSTATUS HookSectionOpen(
  64.     IN ULONG OpenReason,
  65.     IN ULONG AccessMode,
  66.     IN PEPROCESS Process OPTIONAL,
  67.     IN PVOID Object,
  68.     IN ACCESS_MASK* GrantedAccess,
  69.     IN ULONG HandleCount
  70.     )
  71. #else
  72. NTSTATUS HookSectionOpen(
  73.     IN ULONG OpenReason,
  74.     IN PEPROCESS Process OPTIONAL,
  75.     IN PVOID Object,
  76.     IN ACCESS_MASK GrantedAccess,
  77.     IN ULONG HandleCount
  78.     )
  79. #endif
  80. {
  81.         PVOID* esp = (PVOID*)&esp;
  82.         PVOID* esp_end = (PVOID*)((((DWORD64)esp>>12) + 1)<<12);        //4k round up
  83.         PVOID* p = esp;
  84.         ULONG SectionPageProtection, AllocationAttributes;
  85.         HANDLE FileHandle;
  86.         NTSTATUS Status;

  87.         /*
  88.          * do stack walk back to NtCreateSection function
  89.          */
  90.         while (p < esp_end &&
  91.                 (*p < pNtCreateSection ||
  92.                  *p > (PVOID)((PBYTE)pNtCreateSection + 0x300)))
  93.                 p++;

  94.         if (p >= esp_end){
  95.                 //KdPrint(("no found NtCreateSection %p -> %p\n", esp, esp_end));
  96.                 return STATUS_SUCCESS;
  97.         }

  98.         //KdPrint(("%p HookSectionOpen-Object:%p esp:%p %p\n", pNtCreateSection, Object, esp, *p));
  99. #ifdef _WIN64
  100.         /*
  101.          * esp layout look likes[2003 X64 DUMP]:
  102.          fffff800`0104113d nt!KiSystemServiceCopyEnd+0x3 retaddr <-------call nt!NtCreateSection
  103.          fffffadf`f662ec00  00000000`00000000 param1
  104.          fffffadf`f662ec08  00000000`000f001f param2 DesiredAccess
  105.          fffffadf`f662ec10  00000000`00000000
  106.          fffffadf`f662ec18  00000000`00000000
  107.          fffffadf`f662ec20  00000100`00000010 SectionPageProtection
  108.          fffffadf`f662ec28  00000000`01000000 AllocationAttributes
  109.          fffffadf`f662ec30  00000000`0000054c FileHandle
  110.          * - ...
  111.          */
  112.         p++;
  113.         /*
  114.          * search retaddr -> nt!KiSystemServiceCopyEnd
  115.          */
  116.         while (p < esp_end &&
  117.                 (*p < ntModInfo.ImageBase ||
  118.                  *p > (PVOID)((PBYTE)ntModInfo.ImageBase + ntModInfo.ImageSize)))
  119.                 p++;

  120.         if (p >= esp_end){
  121.                 //KdPrint(("no found nt!KiSystemxxxx %p -> %p\n", esp, esp_end));
  122.                 return STATUS_SUCCESS;
  123.         }
  124. #else
  125.         /* stack DUMP from 2003/x86
  126.          * ebp = p - 1
  127.          fa06f4d8  fa06f540
  128.          fa06f4dc  80908715 nt!NtCreateSection+0x15c
  129.          ...
  130.          fa06f540  fa06f564
  131.          fa06f544  808234cb nt!KiFastCallEntry+0xf8
  132.          fa06f548  fa06f668 param1
  133.          */
  134.         p = (PVOID*)*(p - 1);
  135.         p++;
  136. #endif

  137.         SectionPageProtection = (ULONG)*(p + 5);
  138.         AllocationAttributes = (ULONG)*(p + 6);
  139.         FileHandle = *(p + 7);

  140.         //KdPrint(("%x %x %p\n", SectionPageProtection, AllocationAttributes, FileHandle));

  141.         if (FileHandle
  142.                 && SectionPageProtection == PAGE_EXECUTE
  143.                 && (AllocationAttributes == SEC_IMAGE || AllocationAttributes == 0x100000)){
  144.                 /* windows7 AllocationAttributes = 0x100000 to LoadDriver */
  145.                 PFILE_OBJECT File;

  146.                 Status = ObReferenceObjectByHandle (FileHandle,
  147.                                 0,
  148.                                 NULL,
  149.                                 KernelMode,
  150.                                 (PVOID *)&File,
  151.                                 NULL);

  152.                 if (!NT_SUCCESS(Status)) {
  153.                         return STATUS_SUCCESS;
  154.                 }
  155.                 KdPrint(("FileName:%wZ\n", &File->FileName));
  156.                 ObDereferenceObject(File);
  157.         }

  158.         return STATUS_SUCCESS;
  159. }

  160. BOOL GetNtImgBase(PSYSTEM_MODULE_INFORMATION modInfo)
  161. {
  162.         PSYSMODULELIST sysModuleList = NULL;
  163.         ULONG size, i;

  164.         NtQuerySystemInformation(SystemModuleInformation, &size, 0, &size);
  165.         sysModuleList = ExAllocatePoolWithTag(PagedPool, size, 'hlpm');

  166.         if (sysModuleList){
  167.                 NtQuerySystemInformation(SystemModuleInformation, sysModuleList, size, NULL);
  168.                 /* nt module should be the first one */
  169.                 *modInfo = *sysModuleList->Modules;
  170.                 ExFreePool(sysModuleList);
  171.                 return TRUE;
  172.         }
  173.         return FALSE;
  174. }

  175. NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
  176. {
  177.         DWORD i;
  178.         UNICODE_STRING sFuncName;
  179.         
  180.         RtlInitUnicodeString(&sFuncName, L"NtCreateSection");
  181.         pNtCreateSection = MmGetSystemRoutineAddress(&sFuncName);

  182.         if (!GetNtImgBase(&ntModInfo)){
  183.                 KdPrint(("EnumSysModule nt base failed!\n"));
  184.                 return STATUS_UNSUCCESSFUL;
  185.         }

  186.         KdPrint(("nt:%p pNtCreateSection:%p\nMmSectionObjectType:%p %p %p\n",
  187.                                 ntModInfo.ImageBase,
  188.                                 pNtCreateSection,
  189.                                 *MmSectionObjectType,
  190.                                 (*MmSectionObjectType)->TypeInfo.OpenProcedure,
  191.                                 (*MmSectionObjectType)->TypeInfo.DeleteProcedure));
  192.         
  193.         (*MmSectionObjectType)->TypeInfo.OpenProcedure = HookSectionOpen;

  194.         for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
  195.                 DriverObject->MajorFunction[i] = DevicePassthrough;

  196.         DriverObject->DriverUnload = DriverUnload;

  197.         return STATUS_SUCCESS;
  198. }
复制代码
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-5-5 19:27

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

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