打开创建文件

  • 初始化对象属性
  • InitializeObjectAttributes();有5个参数
    • 第一个为对象属性
    • 第二个为unicode 文件路径(符号链接路径)
    • 第三个为属性,我们设置为obj 不区分大小写,使我们大小写不区分和object 内核句柄
    • 最后两个不经常用 设置为null
  • 创建文件 ZwCreateFile 包含大量参数,并且每个参数包含很多选项
    • 第一个参数为文件handle
    • 第二个为访问类型:GENERIC_READ OR GENERIC_WRITE
    • 第三个为obj 初始化对象
    • 第四个为IO_STATUS_BLOCK类型的值
    • 第五个为allocationsize ,通常设置为null,我们的文件从0字节开始
    • 下一个为文件属性 我们使用normal
    • 接下来是文件共享最常用,共享读取或共享写入,如果设置为共享写,那么其他模块使用读访问打开文件,它将无法打开
    • 下一个为createdispostion:我们使用FILE_OPEN_IF,文件不存在会自动创建,否则打开它
    • file_non_directory_file 表示目标文件不是目录
    • file_synchronous_io_nonalert 进行同步操作,表示函数返回时,文件操作已经完成
    • 最后两个参数是可选的, ```c

      include

VOID Unload(IN PDRIVER_OBJECT DriverObject) {

  1. DbgPrint("driver unload\r\n");

}

NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {

  1. DriverObject->DriverUnload = Unload;
  2. NTSTATUS status;
  3. HANDLE filehandle = NULL;
  4. IO_STATUS_BLOCK iostatusblok = { 0 };
  5. OBJECT_ATTRIBUTES object_attribute;
  6. UNICODE_STRING filename = RTL_CONSTANT_STRING(L"\\??\\c:\\1.txt");
  7. InitializeObjectAttributes(&object_attribute,&filename,OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE,NULL,NULL);
  8. status = ZwCreateFile(&filehandle,GENERIC_READ,&object_attribute,&iostatusblok,
  9. NULL,FILE_ATTRIBUTE_NORMAL,FILE_SHARE_READ,FILE_OPEN_IF,
  10. FILE_NON_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_NONALERT,
  11. NULL,0);
  12. if (!NT_SUCCESS(status))
  13. {
  14. DbgPrint("create file failed \r\n");
  15. }
  16. if (filehandle!=NULL)
  17. {
  18. ZwClose(filehandle);
  19. }
  20. return STATUS_SUCCESS;

}

  1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/2616496/1657809792137-26374ded-58b7-41c6-abf9-cf83772b780c.png#clientId=u317b8354-01ea-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=179&id=u0d8fe8b1&margin=%5Bobject%20Object%5D&name=image.png&originHeight=179&originWidth=334&originalType=binary&ratio=1&rotation=0&showTitle=false&size=3805&status=done&style=none&taskId=ub034947a-694a-4829-a995-1bae68d5523&title=&width=334)
  2. <a name="nrPjv"></a>
  3. # 文件拷贝
  4. 创建文件用 FILE_OPEN_IF<br />读取存在的文件用 FILE_OPEN<br />写入文件 需要把权限加上读取权限,GENERIC_READ | GENERIC_WRITE
  5. ```c
  6. #include <ntddk.h>
  7. #define tag 'file'
  8. VOID Unload(IN PDRIVER_OBJECT DriverObject) {
  9. DbgPrint("driver unload\r\n");
  10. }
  11. NTSTATUS copyfile(HANDLE fileR,HANDLE filew)
  12. {
  13. NTSTATUS status;
  14. IO_STATUS_BLOCK iostatusblock = { 0 };
  15. PVOID buffer = NULL;
  16. ULONG length;
  17. length = 4096;
  18. LARGE_INTEGER offset = { 0 };
  19. buffer = ExAllocatePoolWithTag(NonPagedPool,length,tag);
  20. if (buffer ==NULL)
  21. {
  22. goto error;
  23. }
  24. while (1)
  25. {
  26. length = 4096;
  27. status = ZwReadFile(fileR,NULL,NULL,NULL,&iostatusblock,buffer,length,&offset,NULL);
  28. if (!NT_SUCCESS(status))
  29. {
  30. if (status == STATUS_END_OF_FILE)
  31. {
  32. DbgPrint("file end\r\n");
  33. break;
  34. }
  35. else
  36. {
  37. goto error;
  38. }
  39. }
  40. length = iostatusblock.Information;
  41. status = ZwWriteFile(filew,NULL,NULL,NULL,&iostatusblock,buffer,length,&offset,NULL);
  42. if (!NT_SUCCESS(status))
  43. {
  44. goto error;
  45. }
  46. offset.QuadPart += length;
  47. }
  48. ExFreePool(buffer);
  49. return STATUS_SUCCESS;
  50. error:
  51. if (buffer!=NULL)
  52. {
  53. ExFreePool(buffer);
  54. }
  55. return STATUS_UNSUCCESSFUL;
  56. }
  57. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {
  58. DriverObject->DriverUnload = Unload;
  59. NTSTATUS status;
  60. HANDLE filehandleW = NULL,filehandleR = NULL;
  61. IO_STATUS_BLOCK iostatusblokR = { 0 };
  62. IO_STATUS_BLOCK iostatusblokW = { 0 };
  63. OBJECT_ATTRIBUTES object_attributeR;
  64. OBJECT_ATTRIBUTES object_attributeW;
  65. UNICODE_STRING filenameW = RTL_CONSTANT_STRING(L"\\??\\c:\\1.txt");
  66. UNICODE_STRING filenameR = RTL_CONSTANT_STRING(L"\\??\\c:\\2.txt");
  67. InitializeObjectAttributes(&object_attributeW, &filenameW, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
  68. status = ZwCreateFile(&filehandleW,GENERIC_READ|GENERIC_WRITE,&object_attributeW,&iostatusblokW,
  69. NULL,FILE_ATTRIBUTE_NORMAL,FILE_SHARE_READ,FILE_OPEN_IF,
  70. FILE_NON_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_NONALERT,
  71. NULL,0);
  72. if (!NT_SUCCESS(status))
  73. {
  74. DbgPrint("create file failed \r\n");
  75. goto end;
  76. }
  77. InitializeObjectAttributes(&object_attributeR, &filenameR, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
  78. status = ZwCreateFile(&filehandleR, GENERIC_READ | GENERIC_WRITE, &object_attributeR, &iostatusblokR,
  79. NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_OPEN,
  80. FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,
  81. NULL, 0);
  82. if (!NT_SUCCESS(status))
  83. {
  84. DbgPrint("open file failed \r\n");
  85. goto end;
  86. }
  87. status = copyfile(filehandleR,filehandleW);
  88. if (!NT_SUCCESS(status))
  89. {
  90. DbgPrint("writing file failed!\r\n");
  91. goto end;
  92. }
  93. end:
  94. if (filehandleW!=NULL)
  95. {
  96. ZwClose(filehandleW);
  97. }if (filehandleR!=NULL)
  98. {
  99. ZwClose(filehandleR);
  100. }
  101. return STATUS_SUCCESS;
  102. }

获取文件大小

  1. #include <ntddk.h>
  2. VOID Unload(IN PDRIVER_OBJECT DriverObject) {
  3. DbgPrint("driver unload\r\n");
  4. }
  5. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {
  6. DriverObject->DriverUnload = Unload;
  7. NTSTATUS status;
  8. HANDLE filehandleR = NULL;
  9. IO_STATUS_BLOCK iostatusblokR = { 0 };
  10. OBJECT_ATTRIBUTES object_attributeR;
  11. FILE_STANDARD_INFORMATION fileinfo = {0};
  12. UNICODE_STRING filenameR = RTL_CONSTANT_STRING(L"\\??\\c:\\2.txt");
  13. InitializeObjectAttributes(&object_attributeR, &filenameR, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
  14. status = ZwCreateFile(&filehandleR, GENERIC_READ , &object_attributeR, &iostatusblokR,
  15. NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_OPEN,
  16. FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,
  17. NULL, 0);
  18. status = ZwQueryInformationFile(filehandleR,&iostatusblokR,&fileinfo,sizeof(FILE_STANDARD_INFORMATION),FileStandardInformation);
  19. //文件句柄
  20. //fileinfo 结构体
  21. //结构体大小
  22. //文件信息格式
  23. //文件大小存在EndOfFile.QuadPart 中
  24. if (NT_SUCCESS(status))
  25. {
  26. DbgPrint("file size is:%d \r\n",fileinfo.EndOfFile.QuadPart);
  27. }
  28. return STATUS_SUCCESS;
  29. }

image.png

删除文件

我们需要一个 ntifs.h 文件头
删除文件非常简单,我们不需要打开文件,只需要初始化一个指定文件路径的 属性对象
如果在自己的程序中,即包含ntddk.h和ntifs.h的时候,编译的时候会出现如下编译错误:
7600.16385.0\inc\ddk\ntifs.h(85) : error C2371: ‘PEPROCESS’ : redefinition; different basic types,7600.16385.0\inc\ddk\wdm.h(79) : see declaration of ‘PEPROCESS’
解决方法是先include ntifs.h,然后再include ntddk.h,就可以解决。

  1. include <ntifs.h>
  2. #include <ntddk.h>
  3. VOID Unload(IN PDRIVER_OBJECT DriverObject) {
  4. DbgPrint("driver unload\r\n");
  5. }
  6. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {
  7. DriverObject->DriverUnload = Unload;
  8. NTSTATUS status;
  9. HANDLE filehandleR = NULL;
  10. IO_STATUS_BLOCK iostatusblokR = { 0 };
  11. OBJECT_ATTRIBUTES object_attributeR;
  12. UNICODE_STRING filenameR = RTL_CONSTANT_STRING(L"\\??\\C:\\2.txt");
  13. InitializeObjectAttributes(&object_attributeR, &filenameR, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
  14. status = ZwDeleteFile(&object_attributeR);
  15. if (NT_SUCCESS(status))
  16. {
  17. DbgPrint("del file \r\n");
  18. }
  19. return STATUS_SUCCESS;
  20. }

重命名文件