ZwSetSystemInformation 动态加载驱动

来源:互联网 发布:qq社工数据库 编辑:程序博客网 时间:2024/05/22 00:36

////////////////////////////////////////
// New Deployment Module for rootkit 040
// -------------------------------------
// -Greg Hoglund http://www.rootkit.com
////////////////////////////////////////
#include <windows.h>
#include <stdio.h>


typedef struct _UNICODE_STRING {
 USHORT Length;
 USHORT MaximumLength;
#ifdef MIDL_PASS
 [size_is(MaximumLength / 2), length_is((Length) / 2) ] USHORT * Buffer;
#else // MIDL_PASS
 PWSTR Buffer;
#endif // MIDL_PASS
} UNICODE_STRING, *PUNICODE_STRING;


typedef unsigned long NTSTATUS; //我认为这里应该是typedef long NTSTATUS,
//否则一个unsigned的值总是不小于0,下面这个宏就会出问题
#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)

//
// NTSTATUS (__stdcall *ZwSetSystemInformation)(
// IN DWORD SystemInformationClass,
// IN OUT PVOID SystemInformation,
// IN ULONG SystemInformationLength
//);

/*
//有关ZwSetSystemInformation的用法可以参考Gary Nebbett的《Windows NT/2000

  //Native API //Reference》
  //ZwSetSystemInformation设置影响操作系统的信息,定义如下:
  // NTSYSAPI
  // NTSTATUS
  // NTAPI
  // ZwSetSystemInformation(
  // IN SYSTEM_IMFORMATION_CALSS SystemInformationClass,
  // IN OUT PVOID SystemInformation,
  // IN ULONG SystemInformationLength);
  //
  //参数:
  // SystemInformationClass:将被设置的系统信息的类型,值为SYSTEM_IMFORMATION_CALSS枚举的一个
  // 子集,SystemLoadAndCallImage就是其中一个:
  // typedef struct _SYSTEM_LOAD_AND_CALL_IMAGE{//Information Class 38
  // UNICODE_STRING ModuleName;
  // }SYSTEM_LOAD_AND_CALL_IMAGE,*PSYSTEM_LOAD_AND_CALL_IMAGE;
  //
  // 成员:
  // Module:要加载模块的NATIVE NT格式的完整路径
  //
 
 // 备注:
 // 这个信息类只能被设置,不是设置任何信息,而是执行把一个模块加载
 
   到内核地址空间和调用其入口点的操作。期望入口点例程是一个带两个参
  
  数的__stdcall例程(与设备驱动程序的 // DriverEntry例程一致)。如果入
 

 
    口点例程返回一个失败代码,则卸载模块。
    
//
    
// SystemInformation:指向含有被设置信息的一个调用者分配的缓冲区或变量
    
// SystemInformationLength:以字节为单位的SystemInformaiton的大小,根据给定的
    
// SystemInformationClass来设置它
    
//
*/

typedef  VOID (__stdcall 
*tagRtlInitUnicodeString)( 
               IN OUT PUNICODE_STRING DestinationString, 
               IN PCWSTR SourceString 
               ); 

typedef unsigned 
long (__stdcall * tagZwSetSystemInformation)(
                 IN ULONG,
                 IN OUT LPVOID,
                 IN ULONG
                 );

tagRtlInitUnicodeString RtlInitUnicodeString 
= NULL;
tagZwSetSystemInformation ZwSetSystemInformation 
= NULL;

typedef 
struct _SYSTEM_LOAD_AND_CALL_IMAGE 

 UNICODE_STRING ModuleName; 
} SYSTEM_LOAD_AND_CALL_IMAGE, 
*PSYSTEM_LOAD_AND_CALL_IMAGE; 


#define SystemLoadAndCallImage 38 


void main(void

 
/////////////////////////////////////////////////////////////// 
 // Why mess with Drivers? 
 /////////////////////////////////////////////////////////////// 
 SYSTEM_LOAD_AND_CALL_IMAGE GregsImage; 
 WCHAR daPath[] 
= L"/??/d:/samplehello.sys"
 
 
 
////////////////////////////////////////////////////////////// 
 // get DLL entry points 
 ////////////////////////////////////////////////////////////// 
 if!(RtlInitUnicodeString = 
  (tagRtlInitUnicodeString) GetProcAddress( GetModuleHandle(
"ntdll.dll"), 
  
"RtlInitUnicodeString" )) ) //在ntdll.dll中获取RtlInitUnicodeString地址
  exit(1); 
 
 
 
if!(ZwSetSystemInformation = 
  (tagZwSetSystemInformation) GetProcAddress( GetModuleHandle(
"ntdll.dll"), 
  
"ZwSetSystemInformation" )) ) //在ntdll.dll中获取ZwSetSystemInformation地址
  exit(1); 
 
 
 RtlInitUnicodeString( 
&(GregsImage.ModuleName), daPath ); //建立设备
 
 
 
if( NT_SUCCESS( ZwSetSystemInformation( SystemLoadAndCallImage, &GregsImage, sizeof(SYSTEM_LOAD_AND_CALL_IMAGE)) )) //加载进内核空间
 { 
  printf(
"Rootkit Loaded. "); 
 } 
 
else 
 { 
  printf(
"Rootkit not loaded. "); 
 } 


 

======使用此方法加载驱动的一些问题==

关于CreateService与SystemLoadAndCallImage的区别

几个众所周知的区别:
 
CreateService是一个文档化Win32 API,
而ZwSetSystemInformation是一个没有文档化的Native API
 
使用StartService加载的驱动需要注册,ZwSetSystemInformation则不用
 
ControlService提供停止驱动的服务,且驱动可以被系统自动的卸载;使用ZwSetSystemInformation的话,你就需要另外想办法来卸载驱动映像
 
一些容易被忽视的区别:
 
由StartService加载的驱动,执行DriverEntry时的进程上下文是System Process,而ZwSetSystemInformation的进程上下文是Current Process
 
StartService调用ZwLoadDriver加载的驱动映像默认是不可分页的,ZwSetSystemInformation则正好相反。如果没有妥善处理这个细节上的问题,你的某些代码将可能在执行期间,由于调用线程的IRQL>PASSIVE_LEVEL兼代码页没有被换回物理内存而引发一个BSOD
原创粉丝点击