首页 > 系统相关 >获取Windows内核对象的索引与对象名

获取Windows内核对象的索引与对象名

时间:2023-11-29 16:36:35浏览次数:40  
标签:INFORMATION typedef ULONG TypeName Windows OBJECT 对象 内核 TypesInfo

下列提出两种获取对象名的方式

  1. 通过_OBJECT_TYPE::Name获取对象名称,通过_OBJECT_TYPE::Index获取对象索引;
  2. 通过NtQueryObject的方式获取,r0与r3通用,代码如下:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <Windows.h>

#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)

#define STATUS_SUCCESS ((NTSTATUS)0x0L)
#define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L)
#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
#define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC0000023)

typedef enum _OBJECT_INFORMATION_CLASS {
    ObjectBasicInformation,
    ObjectNameInformation,
    ObjectTypeInformation,
    ObjectTypesInformation,
    ObjectHandleFlagInformation,
    ObjectSessionInformation,
} OBJECT_INFORMATION_CLASS;

typedef struct _UNICODE_STRING {
    USHORT  Length;
    USHORT  MaximumLength;
    PWSTR  Buffer;
} UNICODE_STRING, *PUNICODE_STRING;

typedef struct _OBJECT_TYPE_INFORMATION
{
    UNICODE_STRING TypeName;
    ULONG TotalNumberOfObjects;
    ULONG TotalNumberOfHandles;
    ULONG TotalPagedPoolUsage;
    ULONG TotalNonPagedPoolUsage;
    ULONG TotalNamePoolUsage;
    ULONG TotalHandleTableUsage;
    ULONG HighWaterNumberOfObjects;
    ULONG HighWaterNumberOfHandles;
    ULONG HighWaterPagedPoolUsage;
    ULONG HighWaterNonPagedPoolUsage;
    ULONG HighWaterNamePoolUsage;
    ULONG HighWaterHandleTableUsage;
    ULONG InvalidAttributes;
    GENERIC_MAPPING GenericMapping;
    ULONG ValidAccessMask;
    BOOLEAN SecurityRequired;
    BOOLEAN MaintainHandleCount;
    UCHAR TypeIndex; // since WINBLUE
    CHAR ReservedByte;
    ULONG PoolType;
    ULONG DefaultPagedPoolCharge;
    ULONG DefaultNonPagedPoolCharge;
} OBJECT_TYPE_INFORMATION, *POBJECT_TYPE_INFORMATION;

typedef struct _OBJECT_TYPES_INFORMATION {
    LONG NumberOfTypes;
    OBJECT_TYPE_INFORMATION TypeInformation [1];
} OBJECT_TYPES_INFORMATION, *POBJECT_TYPES_INFORMATION;

typedef NTSTATUS (__stdcall* typedef_NtQueryObject)(
              HANDLE Handle,
              OBJECT_INFORMATION_CLASS ObjectInformationClass,
              PVOID ObjectInformation,
              ULONG ObjectInformationLength,
              PULONG ReturnLength
              );

#define PAD_8(p)        (((p) + 7) & ~7)

int main(void) {
    HMODULE hNtDLL = GetModuleHandleW(L"ntdll.dll");
    if (!hNtDLL) {
        return -1;
    }

    typedef_NtQueryObject myNtQueryObject = (typedef_NtQueryObject)GetProcAddress(hNtDLL, "NtQueryObject");
    if (!myNtQueryObject) {
        return -1;
    }

    NTSTATUS status = STATUS_SUCCESS;
    POBJECT_TYPES_INFORMATION TypesInfo = NULL;
    ULONG BufferLength = 0;

    // 如果需要获取指定Handle的对象类型,可以把第一个参数的NULL换成Handle值
    status = myNtQueryObject(NULL, ObjectTypesInformation, NULL, 0, &BufferLength);
    if (status != STATUS_INFO_LENGTH_MISMATCH) {
        return -1;
    }
    
    while (status == STATUS_INFO_LENGTH_MISMATCH) {
        ULONG StartBufferLength = BufferLength;    
        if (TypesInfo) {
            free(TypesInfo);
            TypesInfo = NULL;
        }
        TypesInfo = (POBJECT_TYPES_INFORMATION)malloc(StartBufferLength);
        memset(TypesInfo, 0, StartBufferLength);
        if (!TypesInfo) {
            return -1;
        }
        status = myNtQueryObject(NULL, ObjectTypesInformation, TypesInfo, StartBufferLength, &BufferLength);
    }
    
    POBJECT_TYPE_INFORMATION TypeInfoIterator = &TypesInfo->TypeInformation[0];
    for (LONG i = 0; i < TypesInfo->NumberOfTypes; i++) {
        std::wstring typeName;
        typeName.resize(TypeInfoIterator->TypeName.Length / sizeof(WCHAR));
        memcpy_s(&typeName[0], TypeInfoIterator->TypeName.Length, TypeInfoIterator->TypeName.Buffer, TypeInfoIterator->TypeName.Length);
        std::wcout << "TypeIdx=" << i << ", TypeName=" << typeName.c_str() << std::endl;
        
        // 注意:这里的结构在x64上必须按照8字节对齐
        TypeInfoIterator = (POBJECT_TYPE_INFORMATION)(PAD_8((ULONG_PTR)TypeInfoIterator->TypeName.Buffer + TypeInfoIterator->TypeName.MaximumLength));
    }
    
    free(TypesInfo);
    TypesInfo = NULL;

    system("pause");
    return 0;
}

运行结果:

标签:INFORMATION,typedef,ULONG,TypeName,Windows,OBJECT,对象,内核,TypesInfo
From: https://www.cnblogs.com/PeterZ1997/p/17865190.html

相关文章

  • bbed包(linux和Windows).zip
     一.10glinux编译BBED[oracle@node3~]$cd$ORACLE_HOME/rdbms/lib[oracle@node3lib]$make-fins_rdbms.mkBBED=$ORACLE_HOME/bin/bbed$ORACLE_HOME/bin/bbed直接生成到bin下为可执行文件编译完成后:编译成功后登陆BBED,登陆时需要密码(BBED的默认密码是blockedit)[ora......
  • windows安装zip和unzip命令
    进下面网页下载GnuWinGnuWindownload|SourceForge.net打开安装GnuWin,安装好在里面勾选这6项,zip和unzip各三个勾好点ApplyChanges安装过程装好会在这个目录里剩下就是配置环境变量,把上面路径加入系统路径“运行”输入“sysdm.cpl”,一键打开系统属性参考:Windows......
  • windows 获取 序列号 wwid方法
     以下任意一条命令都可以:wmicdiskdrivegetserialnumberwmicpathwin32_physicalmediagetSerialNumberwmicpathWin32_DiskDrivegetSerialNumber运行结果: **注意**:windows7下获取的序列号格式可能和Windows10下的不一样获取硬盘的更多信息wmicdis......
  • Windows10+Qt+OpenCV出现“ACCESS_MASK”: 不明确的符号
    一、概述背景:Qt+OpenCV一模一样的代码在Mac上运行是正常的,在Windows10上运行就会报一下错误。“ACCESS_MASK”:不明确的符号二、原因及解决方案原因:OpenCV4.5.3的命名空间usingnamespacecv;和windows中的ACCESS_MASK定义冲突,在windows.h头文件中。......
  • 入门Windows驱动程序
    来自:https://www.anquanke.com/post/id/85972入门Windows驱动程序:0x1 背景笔者在学习中发现,关于Windows驱动编程的文章多不胜数,但是其中很多文章的内容繁杂不便于了解与学习,缺少对内容精准的概括与总结,所以本篇文章将对Windows驱动编程进行一次总结性介绍。文章将分为两个部......
  • Windows电脑上的多开工具与云桌面有何异同
    在Windows电脑上,多开工具和云桌面是两种常见的应用方式,它们各自具有一些特点和优势。本文将就多开工具和云桌面在实际使用中的异同进行比较。多开工具多开工具是指能够帮助用户同时打开多个相同软件或游戏的应用程序。在Windows操作系统中,有许多第三方多开工具可以实现这一功能,......
  • windows
     在64位系统system32是64位程序,Syswow64是32位程序。     参考:https://blog.51cto.com/u_15287666/5805954 ......
  • C++20高级编程 第五章 面向对象程序设计
    第五章面向对象设计面向过程思想众所周知的,C语言是一门面向过程编程的语言,而C++是一门半面向对象编程(ObjectOrientedProgramming,OOP)的语言.面向过程编程的语言通常将代码分割成小块,每个小块理论上完成单一的任务.如果在C程序中没有过程,所有代码都会集中于main()......
  • Windows下使用wget获取内容
    在根据Pytorch从0开始实现YOLOV3指南part2——搭建网络结构层-cwpeng-博客园(cnblogs.com) 尝试实现yolo的目标检测项目时,文中配置文件部分提到若在linux下可以用mkdircfgcdcfgwgethttps://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3.cfg在w......
  • vue3 reactive对象的watch
    代码<scriptsetup>import{ref,reactive,watch}from'vue'constobj=reactive({count:0})constyourKnow0=ref('')constyourKnow1=ref('')letcnt=0watch(obj,(newValue,oldValue)=>{//在嵌套的属性变更时触发......