ToB企服应用市场:ToB评测及商务社交产业平台

标题: 驱动开发:内核远程线程实现DLL注入 [打印本页]

作者: 石小疯    时间: 2023-6-25 11:23
标题: 驱动开发:内核远程线程实现DLL注入
在笔者上一篇文章《内核RIP劫持实现DLL注入》介绍了通过劫持RIP指针控制程序执行流实现插入DLL的目的,本章将继续探索全新的注入方式,通过NtCreateThreadEx这个内核函数实现注入DLL的目的,需要注意的是该函数在微软系统中未被导出使用时需要首先得到该函数的入口地址,NtCreateThreadEx函数最终会调用ZwCreateThread,本章在寻找函数的方式上有所不同,前一章通过内存定位的方法得到所需地址,本章则是通过解析导出表实现。
内核导出表远程线程是一种实现DLL注入的常见技术之一。通过使用该技术,注入代码可以利用目标进程的导出表中已有的函数来加载DLL,并在远程线程中执行DLL代码,从而实现DLL注入。
具体而言,内核导出表远程线程实现DLL注入的过程包括以下步骤:
需要注意的是,内核导出表远程线程作为一种内核级别的注入技术,可能会被安全软件或操作系统检测到,并对其进行防御。因此,在使用这种技术进行DLL注入时,需要谨慎使用,并采取必要的安全措施,以防止被检测和防御。
在内核模式中实现这一过程的具体方法可分为如下步骤;
首先需要定义一个标准头文件,并将其命名为lyshark.h其定义部分如下所示,此部分内容包含了微软官方结构定义,以及一些通用函数的规整,已做较为详细的分析和备注,由于前面课程中都有介绍,此处不再介绍具体原理,如果需要了解结构体内的含义,请去自行查阅微软官方文档。
  1. // 署名权
  2. // right to sign one's name on a piece of work
  3. // PowerBy: LyShark
  4. // Email: me@lyshark.com
  5. #include <ntifs.h>
  6. #include <ntimage.h>
  7. #include <ntstrsafe.h>
  8. #define THREAD_CREATE_FLAGS_HIDE_FROM_DEBUGGER 0x00000004
  9. // -----------------------------------------------------------------------------------
  10. // 声明未导出函数
  11. // -----------------------------------------------------------------------------------
  12. NTKERNELAPI PPEB NTAPI PsGetProcessPeb(IN PEPROCESS Process);
  13. NTKERNELAPI UCHAR* PsGetProcessImageFileName(IN PEPROCESS Process);
  14. NTKERNELAPI PVOID NTAPI PsGetProcessWow64Process(IN PEPROCESS Process);
  15. NTKERNELAPI HANDLE PsGetProcessInheritedFromUniqueProcessId(IN PEPROCESS Process);
  16. NTSYSAPI NTSTATUS NTAPI ZwQueryInformationThread(
  17.         IN HANDLE ThreadHandle,
  18.         IN THREADINFOCLASS ThreadInformationClass,
  19.         OUT PVOID ThreadInformation,
  20.         IN ULONG ThreadInformationLength,
  21.         OUT PULONG ReturnLength OPTIONAL
  22.         );
  23. typedef NTSTATUS(NTAPI* LPFN_NTCREATETHREADEX)(
  24.         OUT PHANDLE ThreadHandle,
  25.         IN ACCESS_MASK DesiredAccess,
  26.         IN PVOID ObjectAttributes,
  27.         IN HANDLE ProcessHandle,
  28.         IN PVOID StartAddress,
  29.         IN PVOID Parameter,
  30.         IN ULONG Flags,
  31.         IN SIZE_T StackZeroBits,
  32.         IN SIZE_T SizeOfStackCommit,
  33.         IN SIZE_T SizeOfStackReserve,
  34.         OUT PVOID ByteBuffer
  35.         );
  36. // -----------------------------------------------------------------------------------
  37. // 结构体声明
  38. // -----------------------------------------------------------------------------------
  39. // SSDT表结构
  40. typedef struct _SYSTEM_SERVICE_TABLE
  41. {
  42.         PVOID       ServiceTableBase;
  43.         PVOID       ServiceCounterTableBase;
  44.         ULONGLONG   NumberOfServices;
  45.         PVOID       ParamTableBase;
  46. } SYSTEM_SERVICE_TABLE, *PSYSTEM_SERVICE_TABLE;
  47. PSYSTEM_SERVICE_TABLE KeServiceDescriptorTable;
  48. typedef struct _PEB_LDR_DATA32
  49. {
  50.         ULONG Length;
  51.         UCHAR Initialized;
  52.         ULONG SsHandle;
  53.         LIST_ENTRY32 InLoadOrderModuleList;
  54.         LIST_ENTRY32 InMemoryOrderModuleList;
  55.         LIST_ENTRY32 InInitializationOrderModuleList;
  56. } PEB_LDR_DATA32, *PPEB_LDR_DATA32;
  57. typedef struct _PEB_LDR_DATA
  58. {
  59.         ULONG Length;
  60.         UCHAR Initialized;
  61.         PVOID SsHandle;
  62.         LIST_ENTRY InLoadOrderModuleList;
  63.         LIST_ENTRY InMemoryOrderModuleList;
  64.         LIST_ENTRY InInitializationOrderModuleList;
  65. } PEB_LDR_DATA, *PPEB_LDR_DATA;
  66. // PEB32/PEB64
  67. typedef struct _PEB32
  68. {
  69.         UCHAR InheritedAddressSpace;
  70.         UCHAR ReadImageFileExecOptions;
  71.         UCHAR BeingDebugged;
  72.         UCHAR BitField;
  73.         ULONG Mutant;
  74.         ULONG ImageBaseAddress;
  75.         ULONG Ldr;
  76.         ULONG ProcessParameters;
  77.         ULONG SubSystemData;
  78.         ULONG ProcessHeap;
  79.         ULONG FastPebLock;
  80.         ULONG AtlThunkSListPtr;
  81.         ULONG IFEOKey;
  82.         ULONG CrossProcessFlags;
  83.         ULONG UserSharedInfoPtr;
  84.         ULONG SystemReserved;
  85.         ULONG AtlThunkSListPtr32;
  86.         ULONG ApiSetMap;
  87. } PEB32, *PPEB32;
  88. typedef struct _PEB
  89. {
  90.         UCHAR InheritedAddressSpace;
  91.         UCHAR ReadImageFileExecOptions;
  92.         UCHAR BeingDebugged;
  93.         UCHAR BitField;
  94.         PVOID Mutant;
  95.         PVOID ImageBaseAddress;
  96.         PPEB_LDR_DATA Ldr;
  97.         PVOID ProcessParameters;
  98.         PVOID SubSystemData;
  99.         PVOID ProcessHeap;
  100.         PVOID FastPebLock;
  101.         PVOID AtlThunkSListPtr;
  102.         PVOID IFEOKey;
  103.         PVOID CrossProcessFlags;
  104.         PVOID KernelCallbackTable;
  105.         ULONG SystemReserved;
  106.         ULONG AtlThunkSListPtr32;
  107.         PVOID ApiSetMap;
  108. } PEB, *PPEB;
  109. typedef struct _LDR_DATA_TABLE_ENTRY32
  110. {
  111.         LIST_ENTRY32 InLoadOrderLinks;
  112.         LIST_ENTRY32 InMemoryOrderLinks;
  113.         LIST_ENTRY32 InInitializationOrderLinks;
  114.         ULONG DllBase;
  115.         ULONG EntryPoint;
  116.         ULONG SizeOfImage;
  117.         UNICODE_STRING32 FullDllName;
  118.         UNICODE_STRING32 BaseDllName;
  119.         ULONG Flags;
  120.         USHORT LoadCount;
  121.         USHORT TlsIndex;
  122.         LIST_ENTRY32 HashLinks;
  123.         ULONG TimeDateStamp;
  124. } LDR_DATA_TABLE_ENTRY32, *PLDR_DATA_TABLE_ENTRY32;
  125. typedef struct _LDR_DATA_TABLE_ENTRY
  126. {
  127.         LIST_ENTRY InLoadOrderLinks;
  128.         LIST_ENTRY InMemoryOrderLinks;
  129.         LIST_ENTRY InInitializationOrderLinks;
  130.         PVOID DllBase;
  131.         PVOID EntryPoint;
  132.         ULONG SizeOfImage;
  133.         UNICODE_STRING FullDllName;
  134.         UNICODE_STRING BaseDllName;
  135.         ULONG Flags;
  136.         USHORT LoadCount;
  137.         USHORT TlsIndex;
  138.         LIST_ENTRY HashLinks;
  139.         ULONG TimeDateStamp;
  140. } LDR_DATA_TABLE_ENTRY, *PLDR_DATA_TABLE_ENTRY;
  141. typedef struct _THREAD_BASIC_INFORMATION
  142. {
  143.         NTSTATUS ExitStatus;
  144.         PVOID TebBaseAddress;
  145.         CLIENT_ID ClientId;
  146.         ULONG_PTR AffinityMask;
  147.         LONG Priority;
  148.         LONG BasePriority;
  149. } THREAD_BASIC_INFORMATION, *PTHREAD_BASIC_INFORMATION;
  150. typedef struct _NT_PROC_THREAD_ATTRIBUTE_ENTRY
  151. {
  152.         ULONG Attribute;    // PROC_THREAD_ATTRIBUTE_XXX
  153.         SIZE_T Size;
  154.         ULONG_PTR Value;
  155.         ULONG Unknown;
  156. } NT_PROC_THREAD_ATTRIBUTE_ENTRY, *NT_PPROC_THREAD_ATTRIBUTE_ENTRY;
  157. typedef struct _NT_PROC_THREAD_ATTRIBUTE_LIST
  158. {
  159.         ULONG Length;
  160.         NT_PROC_THREAD_ATTRIBUTE_ENTRY Entry[1];
  161. } NT_PROC_THREAD_ATTRIBUTE_LIST, *PNT_PROC_THREAD_ATTRIBUTE_LIST;
  162. // 注入ShellCode结构
  163. typedef struct _INJECT_BUFFER
  164. {
  165.         UCHAR Code[0x200];
  166.         union
  167.         {
  168.                 UNICODE_STRING Path64;
  169.                 UNICODE_STRING32 Path32;
  170.         };
  171.         wchar_t Buffer[488];
  172.         PVOID ModuleHandle;
  173.         ULONG Complete;
  174.         NTSTATUS Status;
  175. } INJECT_BUFFER, *PINJECT_BUFFER;
  176. // -----------------------------------------------------------------------------------
  177. // 一些开发中的通用函数封装,可任意拷贝使用
  178. // -----------------------------------------------------------------------------------
  179. // 传入函数名获取SSDT导出表RVA
  180. // 参数1:传入函数名称
  181. ULONG GetSSDTRVA(UCHAR *function_name)
  182. {
  183.         NTSTATUS Status;
  184.         HANDLE FileHandle;
  185.         IO_STATUS_BLOCK ioStatus;
  186.         FILE_STANDARD_INFORMATION FileInformation;
  187.         // 设置NTDLL路径
  188.         UNICODE_STRING uniFileName;
  189.         RtlInitUnicodeString(&uniFileName, L"\\SystemRoot\\system32\\ntoskrnl.exe");
  190.         // 初始化打开文件的属性
  191.         OBJECT_ATTRIBUTES objectAttributes;
  192.         InitializeObjectAttributes(&objectAttributes, &uniFileName, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);
  193.         // 打开文件
  194.         Status = IoCreateFile(&FileHandle, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &objectAttributes, &ioStatus, 0, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0, CreateFileTypeNone, NULL, IO_NO_PARAMETER_CHECKING);
  195.         if (!NT_SUCCESS(Status))
  196.         {
  197.                 return 0;
  198.         }
  199.         // 获取文件信息
  200.         Status = ZwQueryInformationFile(FileHandle, &ioStatus, &FileInformation, sizeof(FILE_STANDARD_INFORMATION), FileStandardInformation);
  201.         if (!NT_SUCCESS(Status))
  202.         {
  203.                 ZwClose(FileHandle);
  204.                 return 0;
  205.         }
  206.         // 判断文件大小是否过大
  207.         if (FileInformation.EndOfFile.HighPart != 0)
  208.         {
  209.                 ZwClose(FileHandle);
  210.                 return 0;
  211.         }
  212.         // 取文件大小
  213.         ULONG uFileSize = FileInformation.EndOfFile.LowPart;
  214.         // 分配内存
  215.         PVOID pBuffer = ExAllocatePoolWithTag(PagedPool, uFileSize + 0x100, (ULONG)"PGu");
  216.         if (pBuffer == NULL)
  217.         {
  218.                 ZwClose(FileHandle);
  219.                 return 0;
  220.         }
  221.         // 从头开始读取文件
  222.         LARGE_INTEGER byteOffset;
  223.         byteOffset.LowPart = 0;
  224.         byteOffset.HighPart = 0;
  225.         Status = ZwReadFile(FileHandle, NULL, NULL, NULL, &ioStatus, pBuffer, uFileSize, &byteOffset, NULL);
  226.         if (!NT_SUCCESS(Status))
  227.         {
  228.                 ZwClose(FileHandle);
  229.                 return 0;
  230.         }
  231.         // 取出导出表
  232.         PIMAGE_DOS_HEADER pDosHeader;
  233.         PIMAGE_NT_HEADERS pNtHeaders;
  234.         PIMAGE_SECTION_HEADER pSectionHeader;
  235.         ULONGLONG FileOffset;
  236.         PIMAGE_EXPORT_DIRECTORY pExportDirectory;
  237.         // DLL内存数据转成DOS头结构
  238.         pDosHeader = (PIMAGE_DOS_HEADER)pBuffer;
  239.         // 取出PE头结构
  240.         pNtHeaders = (PIMAGE_NT_HEADERS)((ULONGLONG)pBuffer + pDosHeader->e_lfanew);
  241.         // 判断PE头导出表表是否为空
  242.         if (pNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress == 0)
  243.         {
  244.                 return 0;
  245.         }
  246.         // 取出导出表偏移
  247.         FileOffset = pNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
  248.         // 取出节头结构
  249.         pSectionHeader = (PIMAGE_SECTION_HEADER)((ULONGLONG)pNtHeaders + sizeof(IMAGE_NT_HEADERS));
  250.         PIMAGE_SECTION_HEADER pOldSectionHeader = pSectionHeader;
  251.         // 遍历节结构进行地址运算
  252.         for (UINT16 Index = 0; Index < pNtHeaders->FileHeader.NumberOfSections; Index++, pSectionHeader++)
  253.         {
  254.                 if (pSectionHeader->VirtualAddress <= FileOffset && FileOffset <= pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData)
  255.                 {
  256.                         FileOffset = FileOffset - pSectionHeader->VirtualAddress + pSectionHeader->PointerToRawData;
  257.                 }
  258.         }
  259.         // 导出表地址
  260.         pExportDirectory = (PIMAGE_EXPORT_DIRECTORY)((ULONGLONG)pBuffer + FileOffset);
  261.         // 取出导出表函数地址
  262.         PULONG AddressOfFunctions;
  263.         FileOffset = pExportDirectory->AddressOfFunctions;
  264.         // 遍历节结构进行地址运算
  265.         pSectionHeader = pOldSectionHeader;
  266.         for (UINT16 Index = 0; Index < pNtHeaders->FileHeader.NumberOfSections; Index++, pSectionHeader++)
  267.         {
  268.                 if (pSectionHeader->VirtualAddress <= FileOffset && FileOffset <= pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData)
  269.                 {
  270.                         FileOffset = FileOffset - pSectionHeader->VirtualAddress + pSectionHeader->PointerToRawData;
  271.                 }
  272.         }
  273.         AddressOfFunctions = (PULONG)((ULONGLONG)pBuffer + FileOffset);
  274.         // 取出导出表函数名字
  275.         PUSHORT AddressOfNameOrdinals;
  276.         FileOffset = pExportDirectory->AddressOfNameOrdinals;
  277.         // 遍历节结构进行地址运算
  278.         pSectionHeader = pOldSectionHeader;
  279.         for (UINT16 Index = 0; Index < pNtHeaders->FileHeader.NumberOfSections; Index++, pSectionHeader++)
  280.         {
  281.                 if (pSectionHeader->VirtualAddress <= FileOffset && FileOffset <= pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData)
  282.                 {
  283.                         FileOffset = FileOffset - pSectionHeader->VirtualAddress + pSectionHeader->PointerToRawData;
  284.                 }
  285.         }
  286.         AddressOfNameOrdinals = (PUSHORT)((ULONGLONG)pBuffer + FileOffset);
  287.         //取出导出表函数序号
  288.         PULONG AddressOfNames;
  289.         FileOffset = pExportDirectory->AddressOfNames;
  290.         //遍历节结构进行地址运算
  291.         pSectionHeader = pOldSectionHeader;
  292.         // 循环所有节
  293.         for (UINT16 Index = 0; Index < pNtHeaders->FileHeader.NumberOfSections; Index++, pSectionHeader++)
  294.         {
  295.                 // 寻找符合条件的节
  296.                 if (pSectionHeader->VirtualAddress <= FileOffset && FileOffset <= pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData)
  297.                 {
  298.                         // 得到文件偏移
  299.                         FileOffset = FileOffset - pSectionHeader->VirtualAddress + pSectionHeader->PointerToRawData;
  300.                 }
  301.         }
  302.         AddressOfNames = (PULONG)((ULONGLONG)pBuffer + FileOffset);
  303.         //DbgPrint("\n AddressOfFunctions %llX AddressOfNameOrdinals %llX AddressOfNames %llX  \n", (ULONGLONG)AddressOfFunctions- (ULONGLONG)pBuffer, (ULONGLONG)AddressOfNameOrdinals- (ULONGLONG)pBuffer, (ULONGLONG)AddressOfNames- (ULONGLONG)pBuffer);
  304.         //DbgPrint("\n AddressOfFunctions %llX AddressOfNameOrdinals %llX AddressOfNames %llX  \n", pExportDirectory->AddressOfFunctions, pExportDirectory->AddressOfNameOrdinals, pExportDirectory->AddressOfNames);
  305.         // 开始分析导出表
  306.         ULONG uOffset;
  307.         LPSTR FunName;
  308.         ULONG uAddressOfNames;
  309.         ULONG TargetOff = 0;
  310.         // 循环导出表
  311.         for (ULONG uIndex = 0; uIndex < pExportDirectory->NumberOfNames; uIndex++, AddressOfNames++, AddressOfNameOrdinals++)
  312.         {
  313.                 uAddressOfNames = *AddressOfNames;
  314.                 pSectionHeader = pOldSectionHeader;
  315.                 for (UINT16 Index = 0; Index < pNtHeaders->FileHeader.NumberOfSections; Index++, pSectionHeader++)
  316.                 {
  317.                         // 函数地址在某个范围内
  318.                         if (pSectionHeader->VirtualAddress <= uAddressOfNames && uAddressOfNames <= pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData)
  319.                         {
  320.                                 uOffset = uAddressOfNames - pSectionHeader->VirtualAddress + pSectionHeader->PointerToRawData;
  321.                         }
  322.                 }
  323.                 // 得到函数名
  324.                 FunName = (LPSTR)((ULONGLONG)pBuffer + uOffset);
  325.                 // 判断是否符合要求
  326.                 if (!_stricmp((const char *)function_name, FunName))
  327.                 {
  328.                         // 返回函数地址
  329.                         TargetOff = (ULONG)AddressOfFunctions[*AddressOfNameOrdinals];
  330.                         DbgPrint("索引 [ %p ] 函数名 [ %s ] 相对RVA [ %p ] \n", *AddressOfNameOrdinals, FunName, TargetOff);
  331.                 }
  332.         }
  333.         ExFreePoolWithTag(pBuffer, (ULONG)"PGu");
  334.         ZwClose(FileHandle);
  335.         return TargetOff;
  336. }
  337. // 传入函数名 获取该函数所在模块下标
  338. ULONG GetIndexByName(UCHAR *function_name)
  339. {
  340.         NTSTATUS Status;
  341.         HANDLE FileHandle;
  342.         IO_STATUS_BLOCK ioStatus;
  343.         FILE_STANDARD_INFORMATION FileInformation;
  344.         // 设置NTDLL路径
  345.         UNICODE_STRING uniFileName;
  346.         RtlInitUnicodeString(&uniFileName, L"\\SystemRoot\\system32\\ntdll.dll");
  347.         // 初始化打开文件的属性
  348.         OBJECT_ATTRIBUTES objectAttributes;
  349.         InitializeObjectAttributes(&objectAttributes, &uniFileName, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);
  350.         // 打开文件
  351.         Status = IoCreateFile(&FileHandle, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &objectAttributes, &ioStatus, 0, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0, CreateFileTypeNone, NULL, IO_NO_PARAMETER_CHECKING);
  352.         if (!NT_SUCCESS(Status))
  353.         {
  354.                 return 0;
  355.         }
  356.         // 获取文件信息
  357.         Status = ZwQueryInformationFile(FileHandle, &ioStatus, &FileInformation, sizeof(FILE_STANDARD_INFORMATION), FileStandardInformation);
  358.         if (!NT_SUCCESS(Status))
  359.         {
  360.                 ZwClose(FileHandle);
  361.                 return 0;
  362.         }
  363.         // 判断文件大小是否过大
  364.         if (FileInformation.EndOfFile.HighPart != 0)
  365.         {
  366.                 ZwClose(FileHandle);
  367.                 return 0;
  368.         }
  369.         // 取文件大小
  370.         ULONG uFileSize = FileInformation.EndOfFile.LowPart;
  371.         // 分配内存
  372.         PVOID pBuffer = ExAllocatePoolWithTag(PagedPool, uFileSize + 0x100, (ULONG)"Ntdl");
  373.         if (pBuffer == NULL)
  374.         {
  375.                 ZwClose(FileHandle);
  376.                 return 0;
  377.         }
  378.         // 从头开始读取文件
  379.         LARGE_INTEGER byteOffset;
  380.         byteOffset.LowPart = 0;
  381.         byteOffset.HighPart = 0;
  382.         Status = ZwReadFile(FileHandle, NULL, NULL, NULL, &ioStatus, pBuffer, uFileSize, &byteOffset, NULL);
  383.         if (!NT_SUCCESS(Status))
  384.         {
  385.                 ZwClose(FileHandle);
  386.                 return 0;
  387.         }
  388.         // 取出导出表
  389.         PIMAGE_DOS_HEADER pDosHeader;
  390.         PIMAGE_NT_HEADERS pNtHeaders;
  391.         PIMAGE_SECTION_HEADER pSectionHeader;
  392.         ULONGLONG FileOffset;
  393.         PIMAGE_EXPORT_DIRECTORY pExportDirectory;
  394.         // DLL内存数据转成DOS头结构
  395.         pDosHeader = (PIMAGE_DOS_HEADER)pBuffer;
  396.         // 取出PE头结构
  397.         pNtHeaders = (PIMAGE_NT_HEADERS)((ULONGLONG)pBuffer + pDosHeader->e_lfanew);
  398.         // 判断PE头导出表表是否为空
  399.         if (pNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress == 0)
  400.         {
  401.                 return 0;
  402.         }
  403.         // 取出导出表偏移
  404.         FileOffset = pNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
  405.         // 取出节头结构
  406.         pSectionHeader = (PIMAGE_SECTION_HEADER)((ULONGLONG)pNtHeaders + sizeof(IMAGE_NT_HEADERS));
  407.         PIMAGE_SECTION_HEADER pOldSectionHeader = pSectionHeader;
  408.         // 遍历节结构进行地址运算
  409.         for (UINT16 Index = 0; Index < pNtHeaders->FileHeader.NumberOfSections; Index++, pSectionHeader++)
  410.         {
  411.                 if (pSectionHeader->VirtualAddress <= FileOffset && FileOffset <= pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData)
  412.                 {
  413.                         FileOffset = FileOffset - pSectionHeader->VirtualAddress + pSectionHeader->PointerToRawData;
  414.                 }
  415.         }
  416.         // 导出表地址
  417.         pExportDirectory = (PIMAGE_EXPORT_DIRECTORY)((ULONGLONG)pBuffer + FileOffset);
  418.         // 取出导出表函数地址
  419.         PULONG AddressOfFunctions;
  420.         FileOffset = pExportDirectory->AddressOfFunctions;
  421.         // 遍历节结构进行地址运算
  422.         pSectionHeader = pOldSectionHeader;
  423.         for (UINT16 Index = 0; Index < pNtHeaders->FileHeader.NumberOfSections; Index++, pSectionHeader++)
  424.         {
  425.                 if (pSectionHeader->VirtualAddress <= FileOffset && FileOffset <= pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData)
  426.                 {
  427.                         FileOffset = FileOffset - pSectionHeader->VirtualAddress + pSectionHeader->PointerToRawData;
  428.                 }
  429.         }
  430.         // 此处需要注意foa和rva转换过程
  431.         AddressOfFunctions = (PULONG)((ULONGLONG)pBuffer + FileOffset);
  432.         // 取出导出表函数名字
  433.         PUSHORT AddressOfNameOrdinals;
  434.         FileOffset = pExportDirectory->AddressOfNameOrdinals;
  435.         // 遍历节结构进行地址运算
  436.         pSectionHeader = pOldSectionHeader;
  437.         for (UINT16 Index = 0; Index < pNtHeaders->FileHeader.NumberOfSections; Index++, pSectionHeader++)
  438.         {
  439.                 if (pSectionHeader->VirtualAddress <= FileOffset && FileOffset <= pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData)
  440.                 {
  441.                         FileOffset = FileOffset - pSectionHeader->VirtualAddress + pSectionHeader->PointerToRawData;
  442.                 }
  443.         }
  444.         // 此处需要注意foa和rva转换过程
  445.         AddressOfNameOrdinals = (PUSHORT)((ULONGLONG)pBuffer + FileOffset);
  446.         // 取出导出表函数序号
  447.         PULONG AddressOfNames;
  448.         FileOffset = pExportDirectory->AddressOfNames;
  449.         // 遍历节结构进行地址运算
  450.         pSectionHeader = pOldSectionHeader;
  451.         for (UINT16 Index = 0; Index < pNtHeaders->FileHeader.NumberOfSections; Index++, pSectionHeader++)
  452.         {
  453.                 if (pSectionHeader->VirtualAddress <= FileOffset && FileOffset <= pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData)
  454.                 {
  455.                         FileOffset = FileOffset - pSectionHeader->VirtualAddress + pSectionHeader->PointerToRawData;
  456.                 }
  457.         }
  458.         // 此处需要注意foa和rva转换过程
  459.         AddressOfNames = (PULONG)((ULONGLONG)pBuffer + FileOffset);
  460.         // 分析导出表
  461.         ULONG uNameOffset;
  462.         ULONG uOffset;
  463.         LPSTR FunName;
  464.         PVOID pFuncAddr;
  465.         ULONG uServerIndex;
  466.         ULONG uAddressOfNames;
  467.         for (ULONG uIndex = 0; uIndex < pExportDirectory->NumberOfNames; uIndex++, AddressOfNames++, AddressOfNameOrdinals++)
  468.         {
  469.                 uAddressOfNames = *AddressOfNames;
  470.                 pSectionHeader = pOldSectionHeader;
  471.                 for (UINT32 Index = 0; Index < pNtHeaders->FileHeader.NumberOfSections; Index++, pSectionHeader++)
  472.                 {
  473.                         if (pSectionHeader->VirtualAddress <= uAddressOfNames && uAddressOfNames <= pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData)
  474.                         {
  475.                                 uOffset = uAddressOfNames - pSectionHeader->VirtualAddress + pSectionHeader->PointerToRawData;
  476.                         }
  477.                 }
  478.                 FunName = (LPSTR)((ULONGLONG)pBuffer + uOffset);
  479.                 // 判断开头是否是Zw
  480.                 if (FunName[0] == 'Z' && FunName[1] == 'w')
  481.                 {
  482.                         pSectionHeader = pOldSectionHeader;
  483.                         // 如果是则根据AddressOfNameOrdinals得到文件偏移
  484.                         uOffset = (ULONG)AddressOfFunctions[*AddressOfNameOrdinals];
  485.                         for (UINT32 Index = 0; Index < pNtHeaders->FileHeader.NumberOfSections; Index++, pSectionHeader++)
  486.                         {
  487.                                 if (pSectionHeader->VirtualAddress <= uOffset && uOffset <= pSectionHeader->VirtualAddress + pSectionHeader->SizeOfRawData)
  488.                                 {
  489.                                         uNameOffset = uOffset - pSectionHeader->VirtualAddress + pSectionHeader->PointerToRawData;
  490.                                 }
  491.                         }
  492.                         pFuncAddr = (PVOID)((ULONGLONG)pBuffer + uNameOffset);
  493.                         uServerIndex = *(PULONG)((ULONGLONG)pFuncAddr + 4);
  494.                         FunName[0] = 'N';
  495.                         FunName[1] = 't';
  496.                         // 获得指定的编号
  497.                         if (!_stricmp(FunName, (const char *)function_name))
  498.                         {
  499.                                 ExFreePoolWithTag(pBuffer, (ULONG)"Ntdl");
  500.                                 ZwClose(FileHandle);
  501.                                 return uServerIndex;
  502.                         }
  503.                 }
  504.         }
  505.         ExFreePoolWithTag(pBuffer, (ULONG)"Ntdl");
  506.         ZwClose(FileHandle);
  507.         return 0;
  508. }
  509. // 获取模块导出函数
  510. PVOID GetModuleExportAddress(IN PVOID ModuleBase, IN PCCHAR FunctionName, IN PEPROCESS EProcess)
  511. {
  512.         PIMAGE_DOS_HEADER ImageDosHeader = (PIMAGE_DOS_HEADER)ModuleBase;
  513.         PIMAGE_NT_HEADERS32 ImageNtHeaders32 = NULL;
  514.         PIMAGE_NT_HEADERS64 ImageNtHeaders64 = NULL;
  515.         PIMAGE_EXPORT_DIRECTORY ImageExportDirectory = NULL;
  516.         ULONG ExportDirectorySize = 0;
  517.         ULONG_PTR FunctionAddress = 0;
  518.         if (ModuleBase == NULL)
  519.         {
  520.                 return NULL;
  521.         }
  522.         if (ImageDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
  523.         {
  524.                 return NULL;
  525.         }
  526.         ImageNtHeaders32 = (PIMAGE_NT_HEADERS32)((PUCHAR)ModuleBase + ImageDosHeader->e_lfanew);
  527.         ImageNtHeaders64 = (PIMAGE_NT_HEADERS64)((PUCHAR)ModuleBase + ImageDosHeader->e_lfanew);
  528.         // 判断PE结构位数
  529.         if (ImageNtHeaders64->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
  530.         {
  531.                 ImageExportDirectory = (PIMAGE_EXPORT_DIRECTORY)(ImageNtHeaders64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress + (ULONG_PTR)ModuleBase);
  532.                 ExportDirectorySize = ImageNtHeaders64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
  533.         }
  534.         else
  535.         {
  536.                 ImageExportDirectory = (PIMAGE_EXPORT_DIRECTORY)(ImageNtHeaders32->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress + (ULONG_PTR)ModuleBase);
  537.                 ExportDirectorySize = ImageNtHeaders32->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
  538.         }
  539.         // 解析内存导出表
  540.         PUSHORT pAddressOfOrds = (PUSHORT)(ImageExportDirectory->AddressOfNameOrdinals + (ULONG_PTR)ModuleBase);
  541.         PULONG  pAddressOfNames = (PULONG)(ImageExportDirectory->AddressOfNames + (ULONG_PTR)ModuleBase);
  542.         PULONG  pAddressOfFuncs = (PULONG)(ImageExportDirectory->AddressOfFunctions + (ULONG_PTR)ModuleBase);
  543.         for (ULONG i = 0; i < ImageExportDirectory->NumberOfFunctions; ++i)
  544.         {
  545.                 USHORT OrdIndex = 0xFFFF;
  546.                 PCHAR  pName = NULL;
  547.                 // 如果函数名小于等于0xFFFF 则说明是序号导出
  548.                 if ((ULONG_PTR)FunctionName <= 0xFFFF)
  549.                 {
  550.                         OrdIndex = (USHORT)i;
  551.                 }
  552.                 // 否则则说明是名字导出
  553.                 else if ((ULONG_PTR)FunctionName > 0xFFFF && i < ImageExportDirectory->NumberOfNames)
  554.                 {
  555.                         pName = (PCHAR)(pAddressOfNames[i] + (ULONG_PTR)ModuleBase);
  556.                         OrdIndex = pAddressOfOrds[i];
  557.                 }
  558.                 // 未知导出函数
  559.                 else
  560.                 {
  561.                         return NULL;
  562.                 }
  563.                 // 对比模块名是否是我们所需要的
  564.                 if (((ULONG_PTR)FunctionName <= 0xFFFF && (USHORT)((ULONG_PTR)FunctionName) == OrdIndex + ImageExportDirectory->Base) || ((ULONG_PTR)FunctionName > 0xFFFF && strcmp(pName, FunctionName) == 0))
  565.                 {
  566.                         // 是则保存下来
  567.                         FunctionAddress = pAddressOfFuncs[OrdIndex] + (ULONG_PTR)ModuleBase;
  568.                         break;
  569.                 }
  570.         }
  571.         return (PVOID)FunctionAddress;
  572. }
  573. // 获取指定用户模块基址
  574. PVOID GetUserModuleAddress(IN PEPROCESS EProcess, IN PUNICODE_STRING ModuleName, IN BOOLEAN IsWow64)
  575. {
  576.         if (EProcess == NULL)
  577.         {
  578.                 return NULL;
  579.         }
  580.         __try
  581.         {
  582.                 // 定时250ms毫秒
  583.                 LARGE_INTEGER Time = { 0 };
  584.                 Time.QuadPart = -250ll * 10 * 1000;
  585.                 // 32位执行
  586.                 if (IsWow64)
  587.                 {
  588.                         // 得到进程PEB进程环境块
  589.                         PPEB32 Peb32 = (PPEB32)PsGetProcessWow64Process(EProcess);
  590.                         if (Peb32 == NULL)
  591.                         {
  592.                                 return NULL;
  593.                         }
  594.                         // 等待 250ms * 10
  595.                         for (INT i = 0; !Peb32->Ldr && i < 10; i++)
  596.                         {
  597.                                 // 等待一会在执行
  598.                                 KeDelayExecutionThread(KernelMode, TRUE, &Time);
  599.                         }
  600.                         // 没有找到返回空
  601.                         if (!Peb32->Ldr)
  602.                         {
  603.                                 return NULL;
  604.                         }
  605.                         // 搜索 InLoadOrderModuleList
  606.                         for (PLIST_ENTRY32 ListEntry = (PLIST_ENTRY32)((PPEB_LDR_DATA32)Peb32->Ldr)->InLoadOrderModuleList.Flink; ListEntry != &((PPEB_LDR_DATA32)Peb32->Ldr)->InLoadOrderModuleList; ListEntry = (PLIST_ENTRY32)ListEntry->Flink)
  607.                         {
  608.                                 UNICODE_STRING UnicodeString;
  609.                                 PLDR_DATA_TABLE_ENTRY32 LdrDataTableEntry32 = CONTAINING_RECORD(ListEntry, LDR_DATA_TABLE_ENTRY32, InLoadOrderLinks);
  610.                                 RtlUnicodeStringInit(&UnicodeString, (PWCH)LdrDataTableEntry32->BaseDllName.Buffer);
  611.                                 // 判断模块名是否符合要求
  612.                                 if (RtlCompareUnicodeString(&UnicodeString, ModuleName, TRUE) == 0)
  613.                                 {
  614.                                         // 符合则返回模块基址
  615.                                         return (PVOID)LdrDataTableEntry32->DllBase;
  616.                                 }
  617.                         }
  618.                 }
  619.                 // 64位执行
  620.                 else
  621.                 {
  622.                         // 得到进程PEB进程环境块
  623.                         PPEB Peb = PsGetProcessPeb(EProcess);
  624.                         if (!Peb)
  625.                         {
  626.                                 return NULL;
  627.                         }
  628.                         // 等待
  629.                         for (INT i = 0; !Peb->Ldr && i < 10; i++)
  630.                         {
  631.                                 // 将当前线程置于指定间隔的可警报或不可操作的等待状态
  632.                                 KeDelayExecutionThread(KernelMode, TRUE, &Time);
  633.                         }
  634.                         if (!Peb->Ldr)
  635.                         {
  636.                                 return NULL;
  637.                         }
  638.                         // 遍历链表
  639.                         for (PLIST_ENTRY ListEntry = Peb->Ldr->InLoadOrderModuleList.Flink; ListEntry != &Peb->Ldr->InLoadOrderModuleList; ListEntry = ListEntry->Flink)
  640.                         {
  641.                                 PLDR_DATA_TABLE_ENTRY LdrDataTableEntry = CONTAINING_RECORD(ListEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
  642.                                 // 判断模块名是否符合要求
  643.                                 if (RtlCompareUnicodeString(&LdrDataTableEntry->BaseDllName, ModuleName, TRUE) == 0)
  644.                                 {
  645.                                         // 返回模块基址
  646.                                         return LdrDataTableEntry->DllBase;
  647.                                 }
  648.                         }
  649.                 }
  650.         }
  651.         __except (EXCEPTION_EXECUTE_HANDLER)
  652.         {
  653.                 return NULL;
  654.         }
  655.         return NULL;
  656. }
  657. //得到ntos的基址
  658. ULONGLONG GetOsBaseAddress(PDRIVER_OBJECT pDriverObject)
  659. {
  660.         UNICODE_STRING osName = { 0 };
  661.         WCHAR wzData[0x100] = L"ntoskrnl.exe";
  662.         RtlInitUnicodeString(&osName, wzData);
  663.         LDR_DATA_TABLE_ENTRY *pDataTableEntry, *pTempDataTableEntry;
  664.         // 双循环链表定义
  665.         PLIST_ENTRY pList;
  666.         // 指向驱动对象的DriverSection
  667.         pDataTableEntry = (LDR_DATA_TABLE_ENTRY*)pDriverObject->DriverSection;
  668.         // 判断是否为空
  669.         if (!pDataTableEntry)
  670.         {
  671.                 return 0;
  672.         }
  673.         // 得到链表地址
  674.         pList = pDataTableEntry->InLoadOrderLinks.Flink;
  675.         // 判断是否等于头部
  676.         while (pList != &pDataTableEntry->InLoadOrderLinks)
  677.         {
  678.                 pTempDataTableEntry = (LDR_DATA_TABLE_ENTRY *)pList;
  679.                 // 如果是ntoskrnl.exe则返回该模块基址
  680.                 if (RtlEqualUnicodeString(&pTempDataTableEntry->BaseDllName, &osName, TRUE))
  681.                 {
  682.                         return (ULONGLONG)pTempDataTableEntry->DllBase;
  683.                 }
  684.                 pList = pList->Flink;
  685.         }
  686.         return 0;
  687. }
  688. // 得到SSDT表的基地址
  689. ULONGLONG GetKeServiceDescriptorTable64(PDRIVER_OBJECT DriverObject)
  690. {
  691.         /*
  692.         nt!KiSystemServiceUser+0xdc:
  693.         fffff806`42c79987 8bf8            mov     edi,eax
  694.         fffff806`42c79989 c1ef07          shr     edi,7
  695.         fffff806`42c7998c 83e720          and     edi,20h
  696.         fffff806`42c7998f 25ff0f0000      and     eax,0FFFh
  697.         nt!KiSystemServiceRepeat:
  698.         fffff806`42c79994 4c8d15e59e3b00  lea     r10,[nt!KeServiceDescriptorTable (fffff806`43033880)]
  699.         fffff806`42c7999b 4c8d1dde203a00  lea     r11,[nt!KeServiceDescriptorTableShadow (fffff806`4301ba80)]
  700.         fffff806`42c799a2 f7437880000000  test    dword ptr [rbx+78h],80h
  701.         fffff806`42c799a9 7413            je      nt!KiSystemServiceRepeat+0x2a (fffff806`42c799be)
  702.         */
  703.         char KiSystemServiceStart_pattern[14] = "\x8B\xF8\xC1\xEF\x07\x83\xE7\x20\x25\xFF\x0F\x00\x00";
  704.         /*
  705.         ULONG rva = GetRvaFromModule(L"\\SystemRoot\\system32\\ntoskrnl.exe", "_stricmp");
  706.         DbgPrint("NtReadFile VA = %p \n", rva);
  707.         ULONG _stricmp_offset = 0x19d710;
  708.         */
  709.         ULONGLONG CodeScanStart = GetSSDTRVA((UCHAR *)"_stricmp") + GetOsBaseAddress(DriverObject);
  710.         ULONGLONG i, tbl_address, b;
  711.         for (i = 0; i < 0x50000; i++)
  712.         {
  713.                 // 比较特征
  714.                 if (!memcmp((char*)(ULONGLONG)CodeScanStart + i, (char*)KiSystemServiceStart_pattern, 13))
  715.                 {
  716.                         for (b = 0; b < 50; b++)
  717.                         {
  718.                                 tbl_address = ((ULONGLONG)CodeScanStart + i + b);
  719.                                 // 4c 8d 15 e5 9e 3b 00  lea r10,[nt!KeServiceDescriptorTable (fffff802`64da4880)]
  720.                                 // if (*(USHORT*)((ULONGLONG)tbl_address) == (USHORT)0x158d4c)
  721.                                 if (*(USHORT*)((ULONGLONG)tbl_address) == (USHORT)0x8d4c)
  722.                                 {
  723.                                         return ((LONGLONG)tbl_address + 7) + *(LONG*)(tbl_address + 3);
  724.                                 }
  725.                         }
  726.                 }
  727.         }
  728.         return 0;
  729. }
  730. // 根据SSDT序号得到函数基址
  731. ULONGLONG GetSSDTFuncCurAddr(ULONG index)
  732. {
  733.         /*
  734.         mov rax, rcx                   ; rcx=Native API 的 index
  735.         lea r10,[rdx]                  ; rdx=ssdt 基址
  736.         mov edi,eax                    ; index
  737.         shr edi,7
  738.         and edi,20h
  739.         mov r10, qword ptr [r10+rdi]   ; ServiceTableBase
  740.         movsxd r11,dword ptr [r10+rax] ; 没有右移的假ssdt的地址
  741.         mov rax,r11
  742.         sar r11,4
  743.         add r10,r11
  744.         mov rax,r10
  745.         ret
  746.         */
  747.         LONG dwtmp = 0;
  748.         PULONG ServiceTableBase = NULL;
  749.         ServiceTableBase = (PULONG)KeServiceDescriptorTable->ServiceTableBase;
  750.         dwtmp = ServiceTableBase[index];
  751.         // 先右移4位之后加上基地址 就可以得到ssdt的地址
  752.         dwtmp = dwtmp >> 4;
  753.         return (LONGLONG)dwtmp + (ULONGLONG)ServiceTableBase;
  754. }
  755. // 根据进程ID返回进程EPROCESS
  756. PEPROCESS LookupProcess(HANDLE Pid)
  757. {
  758.         PEPROCESS eprocess = NULL;
  759.         if (NT_SUCCESS(PsLookupProcessByProcessId(Pid, &eprocess)))
  760.         {
  761.                 return eprocess;
  762.         }
  763.         else
  764.         {
  765.                 return NULL;
  766.         }
  767. }
  768. // 根据用户传入进程名得到该进程PID
  769. HANDLE GetProcessID(PCHAR ProcessName)
  770. {
  771.         ULONG i = 0;
  772.         PEPROCESS eproc = NULL;
  773.         for (i = 4; i<100000000; i = i + 4)
  774.         {
  775.                 eproc = LookupProcess((HANDLE)i);
  776.                 if (eproc != NULL)
  777.                 {
  778.                         ObDereferenceObject(eproc);
  779.                         // 根据进程名得到进程EPEPROCESS
  780.                         if (strstr(PsGetProcessImageFileName(eproc), ProcessName) != NULL)
  781.                         {
  782.                                 return PsGetProcessId(eproc);
  783.                         }
  784.                 }
  785.         }
  786.         return NULL;
  787. }
复制代码
编译并运行如上代码片段,即可输出下标为10,11,12的SSDT函数基址,效果图如下所示;

GetSSDTRVA: 根据传入的函数名获取该函数的RVA地址,用户传入一个特定模块下导出函数的函数名,动态得到该函数的相对偏移地址。
  1. // 署名权
  2. // right to sign one's name on a piece of work
  3. // PowerBy: LyShark
  4. // Email: me@lyshark.com
  5. #include "lyshark.h"
  6. VOID Unload(PDRIVER_OBJECT pDriverObj)
  7. {
  8.         DbgPrint("[-] 驱动卸载 \n");
  9. }
  10. NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegPath)
  11. {
  12.         DbgPrint("Hello LyShark.com \n");
  13.         ULONGLONG kernel_base = GetOsBaseAddress(DriverObject);
  14.         DbgPrint("ntoskrnl.exe 模块基址: %p \n", kernel_base);
  15.         DriverObject->DriverUnload = Unload;
  16.         return STATUS_SUCCESS;
  17. }
复制代码
编译并运行如上代码片段,即可输出NtReadFile,NtCreateEnlistment两个内核函数的RVA地址,效果图如下所示;

GetIndexByName: 该函数接收用户传入的一个SSDT函数名,并返回该函数所对应的下标,调用代码如下;
  1. // 署名权
  2. // right to sign one's name on a piece of work
  3. // PowerBy: LyShark
  4. // Email: me@lyshark.com
  5. #include "lyshark.h"
  6. VOID Unload(PDRIVER_OBJECT pDriverObj)
  7. {
  8.         DbgPrint("[-] 驱动卸载 \n");
  9. }
  10. NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegPath)
  11. {
  12.         DbgPrint("Hello LyShark.com \n");
  13.         // 得到SSDT基地址
  14.         KeServiceDescriptorTable = (PSYSTEM_SERVICE_TABLE)GetKeServiceDescriptorTable64(DriverObject);
  15.         DbgPrint("SSDT基地址: %p \n", KeServiceDescriptorTable->ServiceTableBase);
  16.         // 根据序号得到指定函数地址
  17.         ULONGLONG address = NULL;
  18.        
  19.         address = GetSSDTFuncCurAddr(10);
  20.         DbgPrint("得到函数地址: %p \n", address);
  21.         address = GetSSDTFuncCurAddr(11);
  22.         DbgPrint("得到函数地址: %p \n", address);
  23.         address = GetSSDTFuncCurAddr(12);
  24.         DbgPrint("得到函数地址: %p \n", address);
  25.         DriverObject->DriverUnload = Unload;
  26.         return STATUS_SUCCESS;
  27. }
复制代码
编译并运行如上代码片段,即可输出NtCreateThreadEx,NtReadFile两个内核函数的下标,效果图如下所示;

GetUserModuleAddress: 该函数用于获取进程模块基址,在内核模式下附加到应用层指定进程上,并动态获取到该进程所加载的指定模块的基址,调用代码如下;
  1. // 署名权
  2. // right to sign one's name on a piece of work
  3. // PowerBy: LyShark
  4. // Email: me@lyshark.com
  5. #include "lyshark.h"
  6. VOID Unload(PDRIVER_OBJECT pDriverObj)
  7. {
  8.         DbgPrint("[-] 驱动卸载 \n");
  9. }
  10. NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegPath)
  11. {
  12.         DbgPrint("Hello LyShark.com \n");
  13.         ULONG64 ReadFile_RVA = GetSSDTRVA("NtReadFile");
  14.         DbgPrint("NtReadFile = %p \n", ReadFile_RVA);
  15.         ULONG64 NtCreateEnlistment_RVA = GetSSDTRVA("NtCreateEnlistment");
  16.         DbgPrint("NtCreateEnlistment = %p \n", NtCreateEnlistment_RVA);
  17.         DriverObject->DriverUnload = Unload;
  18.         return STATUS_SUCCESS;
  19. }
复制代码
编译并运行如上代码片段,则获取进程PID=6932里面的ntdll.dll模块的基址,输出效果图如下所示;

GetModuleExportAddress: 该函数可用于获取特定模块中特定函数的基址,此功能需要配合获取模块基址一起使用,调用代码如下;
  1. // 署名权
  2. // right to sign one's name on a piece of work
  3. // PowerBy: LyShark
  4. // Email: me@lyshark.com
  5. #include "lyshark.h"
  6. VOID Unload(PDRIVER_OBJECT pDriverObj)
  7. {
  8.         DbgPrint("[-] 驱动卸载 \n");
  9. }
  10. NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegPath)
  11. {
  12.         DbgPrint("Hello LyShark.com \n");
  13.         ULONG index1 = GetIndexByName((UCHAR *)"NtCreateThreadEx");
  14.         DbgPrint("函数NtCreateThreadEx下标: %d \n", index1);
  15.         ULONG index2 = GetIndexByName((UCHAR *)"NtReadFile");
  16.         DbgPrint("函数NtReadFile下标: %d \n", index2);
  17.         DriverObject->DriverUnload = Unload;
  18.         return STATUS_SUCCESS;
  19. }
复制代码
编译并运行如上代码片段,则获取进程PID=6932里面的ntdll.dll模块里的LdrLoadDll函数基址,输出效果图如下所示;

SeCreateThreadEx: 该函数则是实际执行注入的函数,此段代码中需要注意的是pPrevMode中的偏移值,每个系统中都不相同,用户需要自行在WinDBG中输入!_KTHREAD得到线程信息,并找到PreviousMode字段,该字段中的偏移值需要(PUCHAR)PsGetCurrentThread() + 0x232才可得到正确位置。
  1. // 署名权
  2. // right to sign one's name on a piece of work
  3. // PowerBy: LyShark
  4. // Email: me@lyshark.com
  5. #include "lyshark.h"
  6. VOID Unload(PDRIVER_OBJECT pDriverObj)
  7. {
  8.         DbgPrint("[-] 驱动卸载 \n");
  9. }
  10. NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegPath)
  11. {
  12.         DbgPrint("Hello LyShark.com \n");
  13.         HANDLE ProcessID = (HANDLE)6932;
  14.         PEPROCESS EProcess = NULL;
  15.         NTSTATUS Status = STATUS_SUCCESS;
  16.         KAPC_STATE ApcState;
  17.         // 根据PID得到进程EProcess结构
  18.         Status = PsLookupProcessByProcessId(ProcessID, &EProcess);
  19.         if (Status != STATUS_SUCCESS)
  20.         {
  21.                 DbgPrint("[-] 获取EProcessID失败 \n");
  22.                 return Status;
  23.         }
  24.         // 判断目标进程是32位还是64位
  25.         BOOLEAN IsWow64 = (PsGetProcessWow64Process(EProcess) != NULL) ? TRUE : FALSE;
  26.         // 验证地址是否可读
  27.         if (!MmIsAddressValid(EProcess))
  28.         {
  29.                 DbgPrint("[-] 地址不可读 \n");
  30.                 DriverObject->DriverUnload = Unload;
  31.                 return STATUS_SUCCESS;
  32.         }
  33.         // 将当前线程连接到目标进程的地址空间(附加进程)
  34.         KeStackAttachProcess((PRKPROCESS)EProcess, &ApcState);
  35.         __try
  36.         {
  37.                 UNICODE_STRING NtdllUnicodeString = { 0 };
  38.                 PVOID NtdllAddress = NULL;
  39.                 // 得到进程内ntdll.dll模块基地址
  40.                 RtlInitUnicodeString(&NtdllUnicodeString, L"Ntdll.dll");
  41.                 NtdllAddress = GetUserModuleAddress(EProcess, &NtdllUnicodeString, IsWow64);
  42.                 if (!NtdllAddress)
  43.                 {
  44.                         DbgPrint("[-] 没有找到基址 \n");
  45.                         DriverObject->DriverUnload = Unload;
  46.                         return STATUS_SUCCESS;
  47.                 }
  48.                 DbgPrint("[*] 模块ntdll.dll基址: %p \n", NtdllAddress);
  49.         }
  50.         __except (EXCEPTION_EXECUTE_HANDLER)
  51.         {
  52.         }
  53.         // 取消附加
  54.         KeUnstackDetachProcess(&ApcState);
  55.         DriverObject->DriverUnload = Unload;
  56.         return STATUS_SUCCESS;
  57. }
复制代码
运行如上这段代码片段,将编译好的DLL文件放入到C:\\hook.dll目录下,并运行x32.exe程序,手动加载驱动即可注入成功,输出效果图如下所示;

回到应用层进程中,可看到我们的DLL已经被注入到目标进程内了,效果图如下所示;

参考文献

Blackbone OpenSource

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4