登录  | 立即注册

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

查看: 133|回复: 0

驱动开发与系统原理-删除拷贝剪切

[复制链接]

78

主题

-6

回帖

71

积分

网站编辑

积分
71
发表于 2025-3-4 22:30:26 | 显示全部楼层 |阅读模式

删除


        #define CTL_DELETE_PATH IRP_IOCTRL_CODE(5)
        #define CTL_COPY_SPATH IRP_IOCTRL_CODE(6)
        #define CTL_COPY_DPATH IRP_IOCTRL_CODE(7)
        #define CTL_MOVE_SPATH IRP_IOCTRL_CODE(8)
        #define CTL_MOVE_DPATH IRP_IOCTRL_CODE(9)

              ...
              ...
       //删除文件的函数实现
        NTSTATUS DeleteFileIoCall(char* filePath) {
                NTSYSAPI NTSTATUS ZwDeleteFile(
                   POBJECT_ATTRIBUTES ObjectAttributes
                );
                //状态码
                NTSTATUS ntSTATUS = STATUS_SUCCESS;
                //文件句柄 
                HANDLE hFile = NULL;
                //完成状态
                IO_STATUS_BLOCK Iostatus = { 0 };
                //对象属性
                OBJECT_ATTRIBUTES ObjectAtt = { 0 };
                //三环c:\a.txt
                //0环:\\??\\c:\a.txt
                //将三环路径转换成驱动使用的路径
                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, filePath);
                RtlAnsiStringToUnicodeString(&usFilePath, &asFilePath, TRUE);
                RtlAppendUnicodeStringToString(&usDrvPath, &usDriverFilePath);
                RtlAppendUnicodeStringToString(&usDrvPath, &usFilePath);
                RtlFreeUnicodeString(&usFilePath);

                InitializeObjectAttributes(&ObjectAtt, &usDrvPath, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
                ntSTATUS = ZwDeleteFile(&ObjectAtt);
                return ntSTATUS;

            }

            //复制文件的函数实现
               NTSTATUS CopyFileIoCall(char *szFilePath) {//目的路径
              //1.读取文件
              ReadFileIoCall(g_FilePath);//全局路径,如果直接写就写回去了,我们得修改全局路径
              RtlZeroMemory(g_FilePath, 0x256);
              ULONG uDFillPathLength = strlen(szFilePath);
              RtlCopyMemory(g_FilePath, szFilePath, uDFillPathLength);
              //2.写入文件
              WriteFileIoCall(g_Buffer);
               }   

             //文件操作-移动文件
              NTSTATUS MoveFileIoCall(char* szFilePath) {//目的路径
                  //1.读取文件
                  ReadFileIoCall(g_FilePath);//全局路径,如果直接写就写回去了,我们得修改全局路径
                  //源文件删除
                  DeleteFileIoCall(g_FilePath);//就多了一行删除文件
                  RtlZeroMemory(g_FilePath, 0x256);
                  ULONG uDFillPathLength = strlen(szFilePath);
                  RtlCopyMemory(g_FilePath, szFilePath, uDFillPathLength);
                  //2.写入文件
                  WriteFileIoCall(g_Buffer);
                   } 

              ...
              ...

              case CTL_DELETE_PATH: {
              ntStatus = DeleteFileIoCall(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;
          } 

                case CTL_COPY_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_COPY_DPATH: {
                    ntStatus = CopyFileIoCall(pInputBuffer);
                    if (NT_SUCCESS(ntStatus))
                    {
                        RtlZeroMemory(pOutputBuffer, 1024);
                        ULONG uRetlength = strlen("Copy Success!");
                        RtlCopyMemory(pOutputBuffer, "Copy Success!", uRetlength);
                        pIrp->IoStatus.Status = STATUS_SUCCESS;
                        pIrp->IoStatus.Information = uRetlength;
                        IoCompleteRequest(pIrp, IO_NO_INCREMENT);
                    }
                    else {
                        RtlZeroMemory(pOutputBuffer, 1024);
                        ULONG uRetlength = strlen("Copy Failed!");
                        RtlCopyMemory(pOutputBuffer, "Copy Failed!", uRetlength);
                        pIrp->IoStatus.Status = STATUS_SUCCESS;
                        pIrp->IoStatus.Information = uRetlength;
                        IoCompleteRequest(pIrp, IO_NO_INCREMENT);

                    }
                    return STATUS_SUCCESS;
                }

                    case CTL_MOVE_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_MOVE_DPATH: {
                      ntStatus = MoveFileIoCall(pInputBuffer);
                      if (NT_SUCCESS(ntStatus))
                      {
                          RtlZeroMemory(pOutputBuffer, 1024);
                          ULONG uRetlength = strlen("Move Success!");
                          RtlCopyMemory(pOutputBuffer, "Move Success!", uRetlength);
                          pIrp->IoStatus.Status = STATUS_SUCCESS;
                          pIrp->IoStatus.Information = uRetlength;
                          IoCompleteRequest(pIrp, IO_NO_INCREMENT);
                      }
                      else {
                          RtlZeroMemory(pOutputBuffer, 1024);
                          ULONG uRetlength = strlen("Move Failed!");
                          RtlCopyMemory(pOutputBuffer, "Move Failed!", uRetlength);
                          pIrp->IoStatus.Status = STATUS_SUCCESS;
                          pIrp->IoStatus.Information = uRetlength;
                          IoCompleteRequest(pIrp, IO_NO_INCREMENT);

                      }
                      return STATUS_SUCCESS;
                  }

3环代码



                  #define CTL_DELETE_PATH IRP_IOCTRL_CODE(5)
                  #define CTL_COPY_SPATH IRP_IOCTRL_CODE(6)
                  #define CTL_COPY_DPATH IRP_IOCTRL_CODE(7)
                  #define CTL_MOVE_SPATH IRP_IOCTRL_CODE(8)
                  #define CTL_MOVE_DPATH IRP_IOCTRL_CODE(9)

                  ...
                  ...
                  case'4':
                      {
                        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_COPY_SPATH, Sfilepath, sizeof(Sfilepath), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
                        if (!strcmp(OutputBuffer, "Success!"))
                        {
                          memset(OutputBuffer, 0, sizeof(OutputBuffer));
                          DeviceIoControl(hDriver, CTL_COPY_DPATH, Dfilepath, sizeof(Dfilepath), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
                          printf("%s\n", OutputBuffer);
                          system("pause");
                        }

                        break;
                      }

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

                          case'7': {
                        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_MOVE_SPATH, Sfilepath, sizeof(Sfilepath), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
                        if (!strcmp(OutputBuffer, "Success!"))
                        {
                          memset(OutputBuffer, 0, sizeof(OutputBuffer));
                          DeviceIoControl(hDriver, CTL_MOVE_DPATH, Dfilepath, sizeof(Dfilepath), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
                          printf("%s\n", OutputBuffer);
                          system("pause");
                        }

                        break;
                      }

复制文件

删除文件

移动文件

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

本版积分规则

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

GMT+8, 2025-4-4 14:21 , Processed in 0.224738 second(s), 17 queries , Yac On.

Powered by XiunoBBS

Copyright © 2001-2025, 断点社区.

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