首页
社区
课程
招聘
[求助]导出内核函数问题
发表于: 2008-4-24 21:24 4702

[求助]导出内核函数问题

2008-4-24 21:24
4702
我想导出ntdll.dll中的ZwQuerySystemInformation在ring3下用,程序看起来已经成功导出了,但是调用的时候要报错,请高手指点下,导出代码如下:
HMODULE g_hNtDLL = LoadLibrary( _T("ntdll.dll") );
if ( !g_hNtDLL )
{
    return FALSE;
}

ZwQSI ZwQuerySystemInformation =
(ZwQSI)GetProcAddress( g_hNtDLL, "ZwQuerySystemInformation");
       
if( ZwQuerySystemInformation == NULL)
{
   return FALSE;
}

调用代码如下:
Status = ZwQuerySystemInformation(SystemHandleInformation, pBuffer, 0, &size);

函数声明如下:
typedef
NTSTATUS
(*ZwQSI)( IN SYSTEM_INFORMATION_CLASS,
         OUT PVOID,
         IN ULONG,
                 OUT PULONG );

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 212
活跃值: (775)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
2
#include   <windows.h>   
  #include   <stdio.h>   
  #include   <stdlib.h>   
   
  typedef   unsigned   long   NTSTATUS;   
  typedef   unsigned   short   USHORT;   
  typedef   unsigned   long   ULONG;   
  typedef   unsigned   long   DWORD;   
  typedef   long   LONG;   
   
  typedef   __int64   LONGLONG;   
   
  typedef   struct   {   
  USHORT   Length;   
  USHORT   MaxLen;   
  USHORT   *Buffer;   
  }   UNICODE_STRING;   
   
  struct   process_info   {   
  ULONG   NextEntryDelta;   
  ULONG   ThreadCount;   
  ULONG   Reserved1[6];   
  LARGE_INTEGER   CreateTime;   
  LARGE_INTEGER   UserTime;   
  LARGE_INTEGER   KernelTime;   
  UNICODE_STRING   ProcessName;   
  ULONG   BasePriority;   
  ULONG   ProcessId;   
  };   
   
  typedef   NTSTATUS   (__stdcall   *NtQuerySystemInformation1)(   
  IN   ULONG   SysInfoClass,   
  IN   OUT   PVOID   SystemInformation,   
  IN   ULONG   SystemInformationLength,   
  OUT   PULONG   RetLen   
  );   
   
   
  int   main()   
   
  {   
   
  HINSTANCE   hNtDll;   
  NtQuerySystemInformation1   NtQuerySystemInformation;   
   
  NTSTATUS   rc;   
   
  ULONG   ulNeed   =   0;   
  void   *buf   =   NULL;   
  size_t   len   =   0;   
  struct   process_info   *p   ;   
  int   done;   
  hNtDll   =   LoadLibrary   ("NTDLL");   
   
  if   (!hNtDll)   
   
  return   0;   
   
  NtQuerySystemInformation   =   (NtQuerySystemInformation1)GetProcAddress   (hNtDll,   
  "NtQuerySystemInformation");   
  if   (!NtQuerySystemInformation)   
  return   0;   
  do   {   
   
  len   +=   0x1000;   
  buf   =   realloc   (buf,   len);   
   
  if   (!buf)   
  return   0;   
  rc   =   NtQuerySystemInformation   (5,   buf,   len,   &ulNeed);     
  }   while   (rc   ==   0xc0000004);//   STATUS_INFO_LEN_MISMATCH   
   
  if   (rc   <0)   {   
   
  free   (buf);   
   
  return   0;   
  }   
   
  printf("\nProcessName       ProcessID");   
  p   =   (struct   process_info   *)buf;   
  done   =   0;   
   
  while   (!done)   {   
   
  if   ((p->ProcessName.Buffer   !=   0))   
  {   
    printf("\n%-20S%d",p->ProcessName.Buffer,p->ProcessId);   
  }   
  done   =   p->NextEntryDelta   ==   0;   
  p   =   (struct   process_info   *)(((char   *)p)   +   p->NextEntryDelta);   
  }   
   
  free   (buf);   
   
  FreeLibrary   (hNtDll);   
   
  return   0;   
   
  }
2008-4-25 10:33
0
雪    币: 2105
活跃值: (424)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
3
Zw要在内核下调用,Nt可直接调用
2008-4-25 10:45
0
雪    币: 722
活跃值: (123)
能力值: ( LV12,RANK:300 )
在线值:
发帖
回帖
粉丝
4
2楼的代码已经很详细了,主要注意的问题:
1.ZwQuerySystemInformation的第二个参数:
IN OUT PVOID
2.ZwQuerySystemInformation第一次调用必定不成功,从返回的size参数中得到所需要的字节数,然后再重新给pBuffer分配内存,再调用才会成功。

不知道楼主所指的报错,具体是怎么样的,是报什么样的错。
2008-4-25 21:50
0
游客
登录 | 注册 方可回帖
返回
//