登录  | 立即注册

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

查看: 116|回复: 0

驱动开发与系统原理-设置文件属性

[复制链接]

78

主题

-6

回帖

71

积分

网站编辑

积分
71
发表于 2025-3-5 21:29:49 | 显示全部楼层 |阅读模式

0环代码

            #define CTL_SETARR_PATH IRP_IOCTRL_CODE(11)
            ...
            ...
            NTSTATUS SetArrIoCall(char *szFilePath){
            DbgBreakPoint();
                NTSTATUS ntSTATUS = STATUS_SUCCESS;
                //文件句柄 
                HANDLE hFile = NULL;
                //文件属性结构体
                FILE_STANDARD_INFORMATION fsi = { 0 };
                FILE_POSITION_INFORMATION fpi = { 0 };
                //完成状态
                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_WRITE, &ObjectAtt, &Iostatus, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_WRITE, FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
                if (!NT_SUCCESS(ntSTATUS))
                {
                    return ntSTATUS;
                }
                fpi.CurrentByteOffset.QuadPart = 100;//设置文件指针放到距离文件头100的地方
                ntSTATUS = ZwSetInformationFile(hFile, &Iostatus, &fpi, sizeof(FILE_POSITION_INFORMATION), FilePositionInformation);
                ntSTATUS = ZwQueryInformationFile(hFile, &Iostatus, &fsi, sizeof(FILE_STANDARD_INFORMATION), FileStandardInformation);
                if (!NT_SUCCESS(ntSTATUS))
                {
                    return ntSTATUS;
                }
                memset(g_Buffer, 0, sizeof(g_Buffer));
                ntSTATUS = ZwReadFile(hFile, NULL, NULL, NULL, &Iostatus, g_Buffer, fsi.EndOfFile.QuadPart, NULL, NULL);
                ZwClose(hFile);
                return ntSTATUS;
           }
           ...
           ...
                     case CTL_SETARR_PATH: {
              ntStatus = SetArrIoCall(pInputBuffer);
              if (NT_SUCCESS(ntStatus))
              {
                  RtlZeroMemory(pOutputBuffer, 1024);
                  ULONG uRetlength = strlen(g_Buffer);
                  RtlCopyMemory(pOutputBuffer, g_Buffer, uRetlength);
                  pIrp->IoStatus.Status = STATUS_SUCCESS;
                  pIrp->IoStatus.Information = uRetlength;
                  IoCompleteRequest(pIrp, IO_NO_INCREMENT);
              }
              else {
                  RtlZeroMemory(pOutputBuffer, 1024);
                  ULONG uRetlength = strlen("SetArr Failed!");
                  RtlCopyMemory(pOutputBuffer, "SetArr Failed!", uRetlength);
                  pIrp->IoStatus.Status = STATUS_SUCCESS;
                  pIrp->IoStatus.Information = uRetlength;
                  IoCompleteRequest(pIrp, IO_NO_INCREMENT);

              }

3环代码


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

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

本版积分规则

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

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

Powered by XiunoBBS

Copyright © 2001-2025, 断点社区.

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