作为 DELPHI 版本,需要引用 jwaNative, JwaWinType ,他们是 JEDI API 的一部分。JEDI 官网有下载。

先给出 2 个辅助函数 和 一些结构体。

type
       
  PRecord = ^TRecord;  
  TRecord = record
  end;  
       
  PSystemInformationList = ^TSystemInformationList;  
  TSystemInformationList = record
    Count: ULONG;  
    List: array [0 .. 0] of TRecord;  
  end;  
       
  PSYSTEM_HANDLE_Informations = ^TSYSTEM_HANDLE_Informations;  
  _SYSTEM_HANDLE_Informations = record
    Count: ULONG;  
    SH: array [0 .. 0] of _SYSTEM_HANDLE_INFORMATION;  
  end;  
       
  TSYSTEM_HANDLE_Informations = _SYSTEM_HANDLE_Informations;  
  SYSTEM_HANDLE_Informations = _SYSTEM_HANDLE_Informations;  
       
  PNM_INFO = ^TNM_INFO;  
       
  _NM_INFO = record
    hFile: THandle;  
    Info: _FILE_NAME_Information;  
    Name: array [0 .. MAX_PATH - 1] of WideChar;  
  end;  
       
  TNM_INFO = _NM_INFO;  
  NM_INFO = _NM_INFO;  
       
Function GetSystemInformationClassSize(Const ATableType: SYSTEM_INFORMATION_CLASS; Const Count: ULONG): ULONG;  
begin
  Result := 0;  
  case ATableType of
    SystemBasicInformation:  
      Result := $002C;  
    SystemProcessorInformation:  
      Result := $0000C;  
    SystemPerformanceInformation:  
      Result := $0138;  
    // SystemTimeInformation: Result := $0020;  
    // SystemPathInformation: Result := $0;  
    // SystemProcessInformation: Result := $00C8 + Count;  
    // SystemCallInformation: Result := $0018 + (Count * $0004);  
    SystemConfigurationInformation:  
      Result := $0018;  
    // SystemProcessorCounters: Result := $0030 + Count;//per cpu  
    SystemGlobalFlag:  
      Result := $0004; // (fails if size != 4)  
    // SystemCallTimeInformation: Result := $0;  
    SystemModuleInformation:  
      Result := $0004 + (Count * Sizeof(SYSTEM_MODULE_INFORMATION)); //(n * 0x011C)  
    SystemLockInformation:  
      Result := $0004 + (Count * Sizeof(SYSTEM_LOCK_INFORMATION)); //(n * 0x0024)  
    // SystemStackTraceInformation: Result := $0;  
    // SystemPagedPoolInformation: Result := $0;  
    // SystemNonPagedPoolInformation: Result := $0;  
    SystemHandleInformation:  
      Result := $0004 + (Count * Sizeof(SYSTEM_HANDLE_INFORMATION)); //(n * 0x0010)  
    // SystemObjectTypeInformation: Result := $0038+ + (Count * $0030);// +)  
    SystemPageFileInformation:  
      Result := $0018 + (Count * Sizeof(SYSTEM_PAGEFILE_INFORMATION));  
    // SystemVdmInstemulInformation: Result := $0088;  
    // SystemVdmBopInformation: Result := $0;  
    SystemCacheInformation:  
      Result := $0024;  
    SystemPoolTagInformation:  
      Result := $0004 + (Count * Sizeof(SYSTEM_POOL_TAG_INFORMATION)); // (n * 0x001C)  
    // SystemInterruptInformation: Result := $0000 + Count;//, or 0x0018 per cpu  
    SystemDpcInformation:  
      Result := $0014;  
    // SystemFullMemoryInformation: Result := $0;  
    // SystemLoadDriver: Result := $0018;//, set mode only  
    // SystemUnloadDriver: Result := $0004;//, set mode only  
    // SystemTimeAdjustmentInformation: Result := $000C;//, 0x0008 writeable  
    // SystemSummaryMemoryInformation: Result := $0;  
    // SystemNextEventIdInformation: Result := $0;  
    // SystemEventIdsInformation: Result := $0;  
    SystemCrashDumpInformation:  
      Result := $0004;  
    SystemExceptionInformation:  
      Result := $0010;  
    SystemCrashDumpStateInformation:  
      Result := $0004;  
    // SystemDebuggerInformation: Result := $0002;  
    SystemContextSwitchInformation:  
      Result := $0030;  
    SystemRegistryQuotaInformation:  
      Result := $000C;  
    // SystemAddDriver: Result := $0008;//, set mode only  
    // SystemPrioritySeparationInformatio: Result := $0004;//, set mode only  
    // SystemPlugPlayBusInformation: Result := $0;  
    // SystemDockInformation: Result := $0;  
    // SystemPowerInfo: Result := $0060;// (XP only!)  
    // SystemProcessorSpeedInformation: Result := $000C;// (XP only!)  
    SystemTimeZoneInformation:  
      Result := $00AC;  
    SystemLookasideInformation:  
      Result := Count * $0020;  
    SystemSetTimeSlipEvent:  
      Result := $0;  
    SystemCreateSession:  
      Result := $0;  
    SystemDeleteSession:  
      Result := $0;  
    SystemInvalidInfoClass1:  
      Result := $0;  
    SystemRangeStartInformation:  
      Result := $0004; // (fails if size != 4)  
    SystemVerifierInformation:  
      Result := $0;  
    SystemAddVerifier:  
      Result := $0;  
    SystemSessionProcessesInformation:  
      Result := $0;  
  end;  
end;  
       
Function GetSystemInformationClassHasCount(Const ATableType: SYSTEM_INFORMATION_CLASS): BOOL;  
begin
  Result := False;  
  case ATableType of
    // SystemProcessInformation,  
    // SystemCallInformation,  
    // SystemProcessorCounters,  
    SystemModuleInformation,  
    SystemLockInformation,  
    SystemHandleInformation,  
    // SystemObjectTypeInformation,  
    //SystemPageFileInformation, //好像这个还不确定。  
    // SystemInterruptInformation,  
    SystemPoolTagInformation:  
      Result := True;  
  end;  
  //可以 和 GetSystemInformationClassSize 配合使用。  
end;

  

上面的 NM_INFO 和本文无关。

大家 可以 方便的使用 GetSystemInformationTable 来 获取所需的系统信息数据。

Function GetSystemInformationTable(hHeap: THandle; Const ATableType: SYSTEM_INFORMATION_CLASS; var buffSize: ULONG): PVOID;  
var
  OldPrivilegeAttributes: ULONG;  
  hFile, hQuery: THandle;  
  Status: NTSTATUS;  
  cbBuffer: Cardinal;  
  AVOID: PVOID;  
  MinBufSize,  
  ReturnLength: ULONG;  
  PReturnLength: PULONG;  
begin
  buffSize := ;  
  Result := nil;  
  if not EnableDebugPrivilege(GetCurrentProcess, True, OldPrivilegeAttributes) then
    Exit;  
  try
    ReturnLength := ;  
    cbBuffer := ;  
    AVOID := nil;  
    PReturnLength := Addr(ReturnLength);  
    Status := ZwQuerySystemInformation(ATableType, AVOID, , PReturnLength);  
    if (ReturnLength > ) then // ReturnLength 一个结构的大小。  
    begin
      cbBuffer := ReturnLength;  
      MinBufSize := ReturnLength;  
      AVOID := HeapAlloc(hHeap,  or HEAP_GENERATE_EXCEPTIONS, cbBuffer);  
      if not Assigned(AVOID) then
        Exit;  
      try
        ZeroMemory(AVOID, cbBuffer);  
        Status := ZwQuerySystemInformation(ATableType, AVOID, cbBuffer, PReturnLength);  
        if NTSTATUS_SUCCESS(Status) then
        begin
          Result := AVOID;  
          buffSize := cbBuffer;  
        end
        else if (Status = STATUS_INFO_LENGTH_MISMATCH) and GetSystemInformationClassHasCount(ATableType) then
        begin
          //调试中,下一秒,也许就不够用了。不调试,大概也需要多申请些空间  
          //cbBuffer := GetSystemInformationClassSize(ATableType, PSystemInformationList(AVOID).Count + );  
          cbBuffer := Sizeof(PSystemInformationList(AVOID).Count) +  
            (MinBufSize - Sizeof(PSystemInformationList(AVOID).Count)) * (PSystemInformationList(AVOID).Count + );  
          HeapFree(hHeap,  or HEAP_GENERATE_EXCEPTIONS, AVOID);  
          AVOID := HeapAlloc(hHeap,  or HEAP_GENERATE_EXCEPTIONS, cbBuffer);  
          if not Assigned(AVOID) then
            Exit;  
          try
            ZeroMemory(AVOID, cbBuffer);  
            Status := ZwQuerySystemInformation(ATableType, AVOID, cbBuffer, PReturnLength);  
            if NTSTATUS_SUCCESS(Status) then
            begin
              Result := AVOID;  
              buffSize := cbBuffer;  
            end
            else
            begin
              HeapFree(hHeap,  or HEAP_GENERATE_EXCEPTIONS, AVOID);  
            end;  
          finally
          end;  
        end
        else
        begin
          HeapFree(hHeap,  or HEAP_GENERATE_EXCEPTIONS, AVOID);  
          cbBuffer := $;  
          repeat
            AVOID := HeapAlloc(hHeap,  or HEAP_GENERATE_EXCEPTIONS, cbBuffer);  
            if not Assigned(AVOID) then
              Exit;  
            ZeroMemory(AVOID, cbBuffer);  
            Status := ZwQuerySystemInformation(ATableType, AVOID, cbBuffer, PReturnLength);  
            if (Status = STATUS_INFO_LENGTH_MISMATCH) then
            begin
              HeapFree(hHeap,  or HEAP_GENERATE_EXCEPTIONS, AVOID);  
              cbBuffer := cbBuffer * ;  
            end;  
            if cbBuffer > $ then
            begin
              Exit;  
            end;  
          until (Status <> STATUS_INFO_LENGTH_MISMATCH);  
          if NTSTATUS_SUCCESS(Status) then
          begin
            Result := AVOID;  
            buffSize := cbBuffer;  
          end
          else
          begin
            HeapFree(hHeap,  or HEAP_GENERATE_EXCEPTIONS, AVOID);  
          end;  
        end;  
      finally
      end;  
    end
    else
    begin
      Result := nil;  
      buffSize := ;  
    end;  
  finally
    SetDebugPrivilege(GetCurrentProcess, OldPrivilegeAttributes, OldPrivilegeAttributes);  
  end;  
end;

第一次 ZwQuerySystemInformation,主要是为了返回 ReturnLength,这个是 最小数据大小,一般对于单个的数据,就直接用这个值。但是对于多个的数据,这个值就是 每一个数据项的大小加上 4 。

第二次调用 ZwQuerySystemInformation,返回了一个数据区,如果多个的数据,肯定会 Status = STATUS_INFO_LENGTH_MISMATCH,所以需要第三次调用。大小有 2 种办法获取(具体看代码)。

如果不确定是不是 多个数据,但是又出现空间不够用的情况,就需要用网络上大家常见的循环增大空间的办法了。

另外 hHeap := GetProcessHeap; 可以得到 hHeap ,而且空间的申请,不一定非要用这个,也可以用  GetMem 等。

最后补上 权限提升函数。

Function SetDebugPrivilege(Const hProcess: THandle; Const PrivilegeAttributes: ULONG; 
  var OldPrivilegeAttributes: ULONG): Boolean; 
var
  hToken: THandle; 
  tp: TOKEN_PRIVILEGES; 
  NewPrivilegeAttributes: ULONG; 
  ReturnLength: ULONG; 
  hProcessToken: THandle; 
begin
  hToken := NULL_Handle; 
  Result := False; 
  OldPrivilegeAttributes := SE_PRIVILEGE_ENABLED_BY_DEFAULT; 
  NewPrivilegeAttributes := PrivilegeAttributes; 
  hProcessToken := hProcess; 
  if hProcessToken = NULL_Handle then
  begin
    hProcessToken := GetCurrentProcess; 
  end; 
  if (OpenProcessToken(hProcessToken, TOKEN_ADJUST_PRIVILEGES, hToken)) // 获得进程访问令牌的句柄 
  then
  begin
    try
      tp.PrivilegeCount := ; 
      LookupPrivilegeValue(nil, SE_DEBUG_NAME, tp.Privileges[].Luid); // 查询进程的权限,获取一个权限对应的LUID值 
      OldPrivilegeAttributes := tp.Privileges[].Attributes; 
      tp.Privileges[].Attributes := NewPrivilegeAttributes; 
      Result := AdjustTokenPrivileges(hToken, False, tp, sizeof(tp), nil, ReturnLength); // 判断令牌权限,对这个访问令牌进行修改 
    finally
      CloseHandle(hToken); 
    end; 
  end; 
end; 
                  
// 提升当前进程具有权限 
Function EnableDebugPrivilege(Const hProcess: THandle; Const Enable: Boolean; 
  var OldPrivilegeAttributes: ULONG): Boolean; 
var
  hToken: THandle; 
  tp: TOKEN_PRIVILEGES; 
  ReturnLength: ULONG; 
  hProcessToken: THandle; 
begin
  hToken := NULL_Handle; 
  Result := False; 
  OldPrivilegeAttributes := SE_PRIVILEGE_ENABLED_BY_DEFAULT; 
  hProcessToken := hProcess; 
  if hProcessToken = NULL_Handle then
  begin
    hProcessToken := GetCurrentProcess; 
  end; 
  if (OpenProcessToken(hProcessToken, TOKEN_ADJUST_PRIVILEGES, hToken)) // 获得进程访问令牌的句柄 
  then
  begin
    try
      tp.PrivilegeCount := ; 
      LookupPrivilegeValue(nil, SE_DEBUG_NAME, tp.Privileges[].Luid); // 查询进程的权限,获取一个权限对应的LUID值 
      OldPrivilegeAttributes := tp.Privileges[].Attributes; 
      if Enable then
      begin
        tp.Privileges[].Attributes := tp.Privileges[].Attributes or SE_PRIVILEGE_ENABLED; 
        // tp.Privileges[].Attributes := SE_PRIVILEGE_ENABLED; 
      end
      else
      begin
        tp.Privileges[].Attributes := tp.Privileges[].Attributes and (not SE_PRIVILEGE_ENABLED); 
        // tp.Privileges[].Attributes := SE_PRIVILEGE_ENABLED_BY_DEFAULT; 
        // tp.Privileges[].Attributes := ; 
      end; 
      Result := AdjustTokenPrivileges(hToken, False, tp, sizeof(tp), nil, ReturnLength); // 判断令牌权限,对这个访问令牌进行修改 
    finally
      CloseHandle(hToken); 
    end; 
  end; 
end;

特别声明
禁止 win2003 (楚凡) QQ635887 使用本人修改的控件。

禁止 qiuyan81 (苦恋树) QQ46494153 使用本人修改的内容。

禁止 gfuchao QQ82715485 使用本人修改的内容。

禁止 supersk QQ未知,使用本人修改的版本。

禁止 yesin119 QQ未知,使用本人修改的版本。

禁止 263378440 使用本人修改的版本。

禁止 yanse 使用本人修改的版本。

禁止 www123 使用本人修改的版本

禁止 eliyh 使用本人修改的版本

禁止 ltshdp、ltsh(禁卫军) 使用本人修改的版本

禁止 zwjchinazwj (蒲石) 使用本人修改的版本

禁止 zhipu QQ:2001972 使用本人修改的版本

禁止 jackalan (nVicen) QQ:875271757 使用本人修改的版本

禁止 kencc2016 (小宇) QQ:2601759381 使用本人修改的版本

以上用户名均为 2CCC 的

禁止 QQ:191909837 使用本人修改的版本
禁止 QQ 81604691 使用本人修改的版本

当然,如果你们脸皮比较厚,就偷偷的用吧。

凡是想要骂我的,都可以偷偷的用,反正我是控制不了。
只要你们不鄙视自己就行。

ZwQuerySystemInformation 安全使用心得 Delphi 版的更多相关文章

  1. 十六进制字符串转化成字符串输出HexToStr(Delphi版、C#版)

    //注意:Delphi2010以下版本默认的字符编码是ANSI,VS2010的默认编码是UTF-8,delphi版得到的字符串须经过Utf8ToAnsi()转码才能跟C#版得到的字符串显示结果一致. ...

  2. 通过崩溃地址找错误行数之Delphi版

    通过崩溃地址找错误行数之Delphi版2009-5-11 17:42:35 来源: 转载 作者:网络 访问:360 次 被顶:2 次 字号:[大 中 小]核心提示:什么是 MAP 文件?简单地讲, M ...

  3. Delphi版IP地址与整型互转

    Delphi版IP地址与整型互转 unit Unit11; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphic ...

  4. 从内存中加载DLL Delphi版(转)

    源:从内存中加载DLL DELPHI版 原文 : http://www.2ccc.com/article.asp?articleid=5784 MemLibrary.pas //从内存中加载DLL D ...

  5. 雪花算法(snowflake)delphi版

    雪花算法简单描述: + 最高位是符号位,始终为0,不可用. + 41位的时间序列,精确到毫秒级,41位的长度可以使用69年.时间位还有一个很重要的作用是可以根据时间进行排序. + 10位的机器标识,1 ...

  6. 二维码生成delphi版

    二维码生成delphi版 生成二维码的软件,代码从C语言转换过来(源地址:http://fukuchi.org/works/qrencode/),断断续续的差不多花了一周时间来转换和调试.在转换过程中 ...

  7. 从内存中加载DLL DELPHI版

    //从内存中加载DLL DELPHI版 unit MemLibrary; interface uses Windows; function memLoadLibrary(pLib: Pointer): ...

  8. [转载]《Delphi 版 everything、光速搜索代码》 关于获取文件全路径 GetFullFileName 函数的优化

    Delphi 版 everything.光速搜索代码>,文章中关于获取文件全路径的函数:GetFullFileName,有一个地方值得优化. 就是有多个文件,它们可能属于同一个目录. 譬如 Sy ...

  9. 内存中加载DLL DELPHI版

    //从内存中加载DLL DELPHI版 unit MemLibrary; interface uses Windows; function memLoadLibrary(pLib: Pointer): ...

随机推荐

  1. cxf webservice简单应用

    Server端 server部署到一个端口号为80的tomcat中 CXFController.java package com.lwj.controller; import java.io.IOEx ...

  2. WPF整理-使用逻辑资源

    "Traditional application resources consist of binary chunks of data, typically representing thi ...

  3. 谢欣伦 - OpenDev原创教程 - 无连接套接字类CxUdpSocket

    这是一个精练的无连接套接字类,类名.函数名和变量名均采用匈牙利命名法.小写的x代表我的姓氏首字母(谢欣伦),个人习惯而已,如有雷同,纯属巧合. CxUdpSocket的使用如下(以某个叫做CSomeC ...

  4. 学习使用html与css,并尝试写php

    这两天看了一点php,本想着实践一下,发现自己的服务器还没弄好,php的代码只写了两三行嵌在html中,还运行不了,同时还发现自己这几天学的html和css还不够,总是频频出现问题,学习的样式和布局都 ...

  5. javaScript中的空值和假值

    javaScript中有五种空值和假值,分别为false,null,undefined,"",0.从广义上来说,这五个值都是对应数据类型的无效值或空值. 这五个值的共同点是在执行i ...

  6. 为什么匿名内部类的参数必须为finalhttp://feiyeguohai.iteye.com/blog/1500108

    1)  从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方法内部(可出现在形式参数定义处或者方法体处),因而访问方法中的局部变量(形式参数或局部变量)是天经地义的.是很自 ...

  7. 使用github的使用,利用git shell命令行模式进行操作

    一.登录到git,新建一个版本仓库 二.在"Repository name"一栏里填写版本仓库的名称,如"test",Description栏是描述,可填可不填 ...

  8. Mac OS X:禁止崩溃报告-CrashReport

    Mac OS X:禁止崩溃报告 崩溃报告就是CrashReport 至于官方的有关CrashReport的文档在Technical Note TN212 . 一般的默认情况下,当一个应用程序因为各种原 ...

  9. 【iOS测试】【随笔】崩溃日志获取

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5942779.html 本打算按照Android的套路去写, ...

  10. Linux从零单排(二):setuptools、pip、anaconda2的环境配置

    为了更方便的使用Python的类库,需要进行相应的配置 (一)setuptools的配置 1.setuptools的下载 命令行输入wget https://pypi.python.org/packa ...