zwSetSystemInformation函数是个未公开的函数,调用38号会加载驱动,对应的第二个参数为SYSTEM_LOAD_AND_CALL_IMAGE结构体,第三个参数为SYSTEM_LOAD_AND_CALL_IMAGE结构体的长度..

 

 

  1. #include <Windows.h>  
  2. #include <stdio.h>  
  3.   
  4. #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)  
  5.   
  6. #define SystemLoadAndCallImage  38  
  7.   
  8. typedef struct _UNICODE_STRING {  
  9.     USHORT Length;  
  10.     USHORT MaximumLength;  
  11.     PVOID  Buffer;  
  12. }UNICODE_STRING, *PUNICODE_STRING;  
  13.   
  14. //typedef unsigned long NTSTATUS;  
  15.   
  16. typedef struct _SYSTEM_LOAD_AND_CALL_IMAGE{  
  17.     UNICODE_STRING ModuleName;  
  18. } SYSTEM_LOAD_AND_CALL_IMAGE,*PSYSTEM_LOAD_AND_CALL_IMAGE;  
  19.   
  20. //声明三个函数的原型  
  21. typedef DWORD (CALLBACK* ZWSETSYSTEMINFORMATION)(DWORDPVOIDLONG );  
  22. ZWSETSYSTEMINFORMATION ZwSetSystemInformation;  
  23. //typedef DWORD (CALLBACK* RTLINITUNICODESTRING)(PUNICODE_STRING, PCWSTR);  
  24. //RTLINITUNICODESTRING RtlInitUnicodeString;  
  25. typedef DWORD (CALLBACK* RTLANSISTRINGTOUNICODESTRING)(PVOIDPVOIDDWORD);  
  26. RTLANSISTRINGTOUNICODESTRING RtlAnsiStringToUnicodeString;  
  27.   
  28.   
  29. int main(int argc, char* argv)  
  30. {  
  31.     SYSTEM_LOAD_AND_CALL_IMAGE gregsImage;  
  32.     UNICODE_STRING TmpBuff;  
  33.   
  34.     char szDrvFullPath[256];  
  35.     int iBufflen;  
  36.   
  37.     //手工加载这三个函数  
  38.     //if (!(RtlInitUnicodeString = (RTLINITUNICODESTRING)GetProcAddress(GetModuleHandle("ntdll.dll"),"RtlInitUnicodeString")))  
  39.     //{  
  40.     //  printf("GetProcAddrss error:%d\n", GetLastError());  
  41.     //  exit(-1);  
  42.     //}  
  43.     if (!(RtlAnsiStringToUnicodeString = (RTLANSISTRINGTOUNICODESTRING)GetProcAddress(GetModuleHandle("ntdll.dll"),"RtlAnsiStringToUnicodeString")))  
  44.     {  
  45.         printf("GetProcAddrss error:%d\n", GetLastError());  
  46.         exit(-1);  
  47.     }  
  48.     if (!(ZwSetSystemInformation = (ZWSETSYSTEMINFORMATION)GetProcAddress(GetModuleHandle("ntdll.dll"),"ZwSetSystemInformation")))  
  49.     {  
  50.         printf("GetProcAddrss error:%d\n", GetLastError());  
  51.         exit(-1);  
  52.     }  
  53.   
  54.     //ring 0 访问磁盘时要用\\??\\%s 的方式, 以前出错时少了一个?  
  55.     iBufflen = sprintf(szDrvFullPath, "\\??\\%s""c:\\aaa.sys");  
  56.     szDrvFullPath[iBufflen] = 0;  
  57.   
  58.     TmpBuff.Buffer = (PVOID)szDrvFullPath;  
  59.     TmpBuff.Length = iBufflen;  
  60.   
  61.     RtlAnsiStringToUnicodeString(&(gregsImage.ModuleName), &TmpBuff, 1);  
  62.     if (NT_SUCCESS(ZwSetSystemInformation(SystemLoadAndCallImage, &gregsImage, sizeof(SYSTEM_LOAD_AND_CALL_IMAGE))))  
  63.     {  
  64.         printf("drive %s was loaded....", szDrvFullPath);  
  65.     }  
  66.     else  
  67.     {  
  68.         printf("dirve load error...");  
  69.     }  
  70.     getchar();  
  71.     return 1;  
  72.   
  73.       
  74. }  


其中,aaa.sys的代码为:

 

 

  1. #include <ntddk.h>  
  2.   
  3. DRIVER_UNLOAD Unload;  
  4.   
  5. VOID Unload(  
  6.     __in  struct _DRIVER_OBJECT *DriverObject  
  7.     )  
  8. {  
  9.     KdPrint(("unload ....."));  
  10. }  
  11.   
  12.   
  13. NTSTATUS DriverEntry(  
  14.     __in  PDRIVER_OBJECT DriverObject,  
  15.     __in  PUNICODE_STRING RegistryPath  
  16.     )  
  17. {  
  18.     DriverObject->DriverUnload = Unload;  
  19.     KdPrint(("dirver entry...."));  
  20.     return STATUS_SUCCESS;  
  21. }  


将生成的aaa.sys放到C盘根目录,运行ring 3的程序,结果如下: