登录  | 立即注册

游客您好!登录后享受更多精彩

查看: 104|回复: 0

驱动开发与系统原理-属性删除和重命名

[复制链接]

78

主题

-6

回帖

71

积分

网站编辑

积分
71
发表于 2025-3-9 17:53:29 | 显示全部楼层 |阅读模式

属性删除


**0环代码**

          #define CTL_SETARRDELETE_PATH IRP_IOCTRL_CODE(4)
          ...
          ...
          NTSTATUS SetArrDeleteIoCall(char* szFilePath) {
              NTSTATUS ntSTATUS = STATUS_SUCCESS;
              //文件句柄 
              HANDLE hFile = NULL;
              //文件属性结构体
              FILE_STANDARD_INFORMATION fsi = { 0 };
              FILE_POSITION_INFORMATION fpi = { 0 };
              FILE_DISPOSITION_INFORMATION fdi = { 0 };//这个结构体中有DeleteFile成员
              //完成状态
              IO_STATUS_BLOCK Iostatus = { 0 };
              //对象属性
              OBJECT_ATTRIBUTES ObjectAtt = { 0 };
              ANSI_STRING asFilePath = { 0 };
              UNICODE_STRING usFilePath = { 0 };
              UNICODE_STRING usDriverFilePath = { 0 };
              UNICODE_STRING usDrvPath = { 0 };
              WCHAR  wcBuffer[256];
              ULONG wcbufferLen = 256 * sizeof(WCHAR);
              RtlInitEmptyUnicodeString(&usDrvPath, &wcBuffer, wcbufferLen);
              RtlInitUnicodeString(&usDriverFilePath, L"\\??\\");
              RtlInitAnsiString(&asFilePath, szFilePath);
              RtlAnsiStringToUnicodeString(&usFilePath, &asFilePath, TRUE);
              RtlAppendUnicodeStringToString(&usDrvPath, &usDriverFilePath);
              RtlAppendUnicodeStringToString(&usDrvPath, &usFilePath);
              RtlFreeUnicodeString(&usFilePath);

              InitializeObjectAttributes(&ObjectAtt, &usDrvPath, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
              ntSTATUS = ZwCreateFile(&hFile, GENERIC_ALL, &ObjectAtt, &Iostatus, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_WRITE, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT|FILE_DELETE_ON_CLOSE, NULL, 0);
              if (!NT_SUCCESS(ntSTATUS))
              {
                  return ntSTATUS;
              }
              fdi.DeleteFile = TRUE;
              ntSTATUS = ZwSetInformationFile(hFile, &Iostatus, &fdi, sizeof(FILE_DISPOSITION_INFORMATION), FileDispositionInformation);//设置文件属性
              ZwClose(hFile);
              return ntSTATUS;
          }

          ...
          ...
              case CTL_SETARRDELETE_PATH: {
                ntStatus = SetArrDeleteIoCall(pInputBuffer);
                if (NT_SUCCESS(ntStatus))
                {
                    RtlZeroMemory(pOutputBuffer, 1024);
                    ULONG uRetlength = strlen("Delete Success!");
                    RtlCopyMemory(pOutputBuffer, "Delete Success!", uRetlength);
                    pIrp->IoStatus.Status = STATUS_SUCCESS;
                    pIrp->IoStatus.Information = uRetlength;
                    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
                }
                else {
                    RtlZeroMemory(pOutputBuffer, 1024);
                    ULONG uRetlength = strlen("Delete Failed!");
                    RtlCopyMemory(pOutputBuffer, "Delete Failed!", uRetlength);
                    pIrp->IoStatus.Status = STATUS_SUCCESS;
                    pIrp->IoStatus.Information = uRetlength;
                    IoCompleteRequest(pIrp, IO_NO_INCREMENT);

                }
                return STATUS_SUCCESS;
            }

3环代码

              case'6': {
                RetNumber = 0;
                memset(InputBuffer, 0, sizeof(InputBuffer));
                memset(OutputBuffer, 0, sizeof(OutputBuffer));
                printf("请输入需要删除的文件路径:\n");
                scanf("%s", InputBuffer);
                DeviceIoControl(hDriver, CTL_SETARRDELETE_PATH, InputBuffer, sizeof(InputBuffer), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
                printf("返回数据: % s\n", OutputBuffer);
                system("pause");
                break;
              }

重命名


          #define CTL_RENAME_SPATH IRP_IOCTRL_CODE(12)
          #define CTL_RENAME_DPATH IRP_IOCTRL_CODE(13)
          ...
          ...
          NTSTATUS ReNameFileIoCall(char* szFileName) {
              NTSTATUS ntSTATUS = STATUS_SUCCESS;
              //文件句柄 
              HANDLE hFile = NULL;
              //文件属性结构体
              FILE_STANDARD_INFORMATION fsi = { 0 };
              FILE_POSITION_INFORMATION fpi = { 0 };
              FILE_DISPOSITION_INFORMATION fdi = { 0 };
              PFILE_RENAME_INFORMATION pFri = NULL;
              //计算结构体长度
              ULONG uFriLength = sizeof(PFILE_RENAME_INFORMATION) * 1024;
              //申请内存
              pFri = (PFILE_RENAME_INFORMATION)ExAllocatePool(NonPagedPool, uFriLength);
              //判断申请内存是否成功
              if (pFri==NULL)
              {
                  ntSTATUS = 1;
                  return ntSTATUS;
              }
              //完成状态
              IO_STATUS_BLOCK Iostatus = { 0 };
              //对象属性
              OBJECT_ATTRIBUTES ObjectAtt = { 0 };
              ANSI_STRING asFilePath = { 0 };
              UNICODE_STRING usFilePath = { 0 };
              UNICODE_STRING usDriverFilePath = { 0 };
              UNICODE_STRING usDrvPath = { 0 };
              WCHAR  wcBuffer[256];
              ULONG wcbufferLen = 256 * sizeof(WCHAR);
              RtlInitEmptyUnicodeString(&usDrvPath, &wcBuffer, wcbufferLen);
              RtlInitUnicodeString(&usDriverFilePath, L"\\??\\");
              RtlInitAnsiString(&asFilePath, g_FilePath);
              RtlAnsiStringToUnicodeString(&usFilePath, &asFilePath, TRUE);
              RtlAppendUnicodeStringToString(&usDrvPath, &usDriverFilePath);
              RtlAppendUnicodeStringToString(&usDrvPath, &usFilePath);

              //把文件名转换成UNICODE_STRING
              ANSI_STRING asFileName = { 0 };
              UNICODE_STRING usDrvFileName = { 0 };
              //把重命名的字符串转换成UNICODE_STRING
              RtlInitAnsiString(&asFileName, szFileName);
              RtlAnsiStringToUnicodeString(&usDrvFileName, &asFileName, TRUE);
              RtlZeroMemory(pFri, uFriLength);
              //对pFri结构体进行赋值
              pFri->FileNameLength = usDrvFileName.Length;
              wcscpy(pFri->FileName, usDrvFileName.Buffer);
              pFri->ReplaceIfExists = 0;
              pFri->RootDirectory = NULL;
              //初始化对象
              InitializeObjectAttributes(&ObjectAtt, &usDrvPath, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
              ntSTATUS = ZwCreateFile(&hFile, GENERIC_ALL, &ObjectAtt, &Iostatus, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_WRITE, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT |FILE_NO_INTERMEDIATE_BUFFERING, NULL, 0);
              if (!NT_SUCCESS(ntSTATUS))
              {
                  ExFreePool(pFri);
                  return ntSTATUS;
              }
              //设置文件属性
              ntSTATUS = ZwSetInformationFile(hFile,&Iostatus,pFri,uFriLength,FileRenameInformation);
              RtlFreeUnicodeString(&usDrvFileName);
              ExFreePool(pFri);
              ZwClose(hFile);
              return ntSTATUS;
          }

            ...
            ...
          case CTL_RENAME_SPATH: {
              RtlZeroMemory(g_FilePath, 0x256);
              RtlCopyMemory(g_FilePath, pInputBuffer, uInputLength);
              RtlZeroMemory(pOutputBuffer, 1024);
              ULONG uRetlength = strlen("Success!");
              RtlCopyMemory(pOutputBuffer, "Success!", uRetlength);
              pIrp->IoStatus.Status = STATUS_SUCCESS;
              pIrp->IoStatus.Information = uRetlength;
              IoCompleteRequest(pIrp, IO_NO_INCREMENT);
              return STATUS_SUCCESS;
          }

        case CTL_RENAME_DPATH: {
            ntStatus = ReNameFileIoCall(pInputBuffer);
            if (NT_SUCCESS(ntStatus))
            {
                RtlZeroMemory(pOutputBuffer, 1024);
                ULONG uRetlength = strlen("Rename Success");
                RtlCopyMemory(pOutputBuffer, "Rename Success", uRetlength);
                pIrp->IoStatus.Status = STATUS_SUCCESS;
                pIrp->IoStatus.Information = uRetlength;
                IoCompleteRequest(pIrp, IO_NO_INCREMENT);
            }
            else {
                RtlZeroMemory(pOutputBuffer, 1024);
                ULONG uRetlength = strlen("Rename Failed!");
                RtlCopyMemory(pOutputBuffer, "Rename Failed!", uRetlength);
                pIrp->IoStatus.Status = STATUS_SUCCESS;
                pIrp->IoStatus.Information = uRetlength;
                IoCompleteRequest(pIrp, IO_NO_INCREMENT);
            }
            return STATUS_SUCCESS;

        }        

3环代码

            case'A': {
              memset(Sfilepath, 0, sizeof(InputBuffer));
              memset(Dfilepath, 0, sizeof(OutputBuffer));
              memset(OutputBuffer, 0, sizeof(OutputBuffer));
              printf("请输入需要重命名的文件路径:\n");
              scanf("%s", Sfilepath);
              printf("\n请输入目的文件新的名称 :\n");
              scanf("%s", Dfilepath);
              DeviceIoControl(hDriver, CTL_RENAME_SPATH, Sfilepath, sizeof(Sfilepath), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
              if (!strcmp(OutputBuffer, "Success!"))
              {
                memset(OutputBuffer, 0, sizeof(OutputBuffer));
                DeviceIoControl(hDriver, CTL_RENAME_DPATH, Dfilepath, sizeof(Dfilepath), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
                printf("%s\n", OutputBuffer);
                system("pause");
              }

              break;
            }

这种删除只是和3环的删除一样,如果有些文件有保护就删不了,并不是360、火绒那种强删

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|断点社区 |网站地图

GMT+8, 2025-4-4 14:36 , Processed in 0.107972 second(s), 23 queries , Yac On.

Powered by XiunoBBS

Copyright © 2001-2025, 断点社区.

快速回复 返回顶部 返回列表