码迷,mamicode.com
首页 > 其他好文 > 详细

文件重命名的几种写法

时间:2014-06-03 11:40:58      阅读:386      评论:0      收藏:0      [点我收藏+]

标签:des   c   class   blog   code   a   

现在主要的是通过往ZwSetInformationFile发送HANDLE和改名请求 
再者就是 往 IoSetInformation 发送FILEOBJECT和改名请求
以及我自己模仿iosetinformation写成的创建IRP改名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
void
RenameFileROutineByHandle()
{
 UNICODE_STRING  UniFileString;
 OBJECT_ATTRIBUTES object;
 NTSTATUS   status;
 HANDLE    hFile;
 IO_STATUS_BLOCK  io_status = {0};
 PFILE_RENAME_INFORMATION RenamInfo = NULL;
 UNICODE_STRING  UniRenameStr;
 PFILE_OBJECT  fileObject;
 
 RtlInitUnicodeString(&UniFileString,L"\\Device\\HarddiskVolume2\\1.txt");
  
 InitializeObjectAttributes(
  &object,
  &UniFileString,
  OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
  NULL,
  NULL);
 //打开文件,存在打开,不存在返回错误
 status = ZwCreateFile( &hFile,
  GENERIC_READ | GENERIC_WRITE,
  &object,
  &io_status,
  NULL,
  FILE_ATTRIBUTE_NORMAL,
  FILE_SHARE_READ,
  FILE_OPEN,
  FILE_NON_DIRECTORY_FILE |FILE_SYNCHRONOUS_IO_NONALERT,
  NULL,
  0);
 if(!NT_SUCCESS(status))
 {
  _asm int 3;
 }
 
 RenamInfo = (PFILE_RENAME_INFORMATION)ExAllocatePoolWithTag(NonPagedPool,1024,0);
 
 RenamInfo->ReplaceIfExists = FALSE;
 RenamInfo->RootDirectory = NULL;
  
 UniRenameStr.Buffer = RenamInfo->FileName;
 RenamInfo->FileNameLength = sizeof(ReNamePath) - sizeof(WCHAR);
 
 RtlCopyMemory(UniRenameStr.Buffer,ReNamePath,
  sizeof(ReNamePath));
 
 status = ZwSetInformationFile(hFile,&io_status,RenamInfo,1024,FileRenameInformation);
 if(!NT_SUCCESS(status))
 {
  _asm int 3;
 }
 
 ZwClose(hFile);
 return;
}
 
void
RenameFileROutineByObj()
{
 UNICODE_STRING  UniFileString;
 OBJECT_ATTRIBUTES object;
 NTSTATUS   status;
 HANDLE    hFile;
 IO_STATUS_BLOCK  io_status = {0};
 PFILE_RENAME_INFORMATION RenamInfo = NULL;
 UNICODE_STRING  UniRenameStr;
 PFILE_OBJECT  fileObject;
 
 RtlInitUnicodeString(&UniFileString,L"\\Device\\HarddiskVolume2\\1.txt");
 
 InitializeObjectAttributes(
  &object,
  &UniFileString,
  OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
  NULL,
  NULL);
 //打开文件,存在打开,不存在返回错误
 status = ZwCreateFile( &hFile,
  GENERIC_READ | GENERIC_WRITE,
  &object,
  &io_status,
  NULL,
  FILE_ATTRIBUTE_NORMAL,
  FILE_SHARE_READ,
  FILE_OPEN,
  FILE_NON_DIRECTORY_FILE |FILE_SYNCHRONOUS_IO_NONALERT,
  NULL,
  0);
 if(!NT_SUCCESS(status))
 {
  _asm int 3;
 }
 
 RenamInfo = (PFILE_RENAME_INFORMATION)ExAllocatePoolWithTag(NonPagedPool,1024,0);
 
 RenamInfo->ReplaceIfExists = FALSE;
 RenamInfo->RootDirectory = NULL;
 
 UniRenameStr.Buffer = RenamInfo->FileName;
 RenamInfo->FileNameLength = sizeof(ReNamePath) - sizeof(WCHAR);
 
 RtlCopyMemory(UniRenameStr.Buffer,ReNamePath,
  sizeof(ReNamePath));
 
 ///////////////////////////////////////////////////////////
   status = ObReferenceObjectByHandle( hFile,
    0,
    *IoFileObjectType,
    KernelMode,
    &fileObject,
    NULL);
   if (!NT_SUCCESS(status))
   {
    _asm int 3;
   }
   
   status = IoSetInformation( fileObject,
    FileRenameInformation,
    1024,
    RenamInfo);
   if (!NT_SUCCESS(status))
   {
    _asm int 3;
   }
    
   ObDereferenceObject(fileObject);
 //////////////////////////////////////////////////////////
 
 ZwClose(hFile);
 return;
}
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
RenameFileROutineByIrp()
{
UNICODE_STRING  UniFileString;
 OBJECT_ATTRIBUTES object;
 NTSTATUS   status;
 HANDLE    hFile;
 IO_STATUS_BLOCK  io_status = {0};
 PFILE_RENAME_INFORMATION RenamInfo = NULL;
 UNICODE_STRING  UniRenameStr;
 PFILE_OBJECT  FileObject;
 PDEVICE_OBJECT   deviceObject = NULL;
 PIRP     irp = NULL;
 KEVENT     event;
 IO_STATUS_BLOCK  localIoStatus;
 PIO_STACK_LOCATION  irpSp = NULL;
 UNICODE_STRING   newFileName;
 HANDLE    handle;
 PFILE_OBJECT   targetFileObject;
 
 RtlInitUnicodeString(&UniFileString,L"\\Device\\HarddiskVolume2\\1.txt");
 
 InitializeObjectAttributes(
  &object,
  &UniFileString,
  OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
  NULL,
  NULL);
 //打开文件,存在打开,不存在返回错误
 status = ZwCreateFile( &hFile,
  GENERIC_READ | GENERIC_WRITE,
  &object,
  &io_status,
  NULL,
  FILE_ATTRIBUTE_NORMAL,
  FILE_SHARE_READ,
  FILE_OPEN,
  FILE_NON_DIRECTORY_FILE |FILE_SYNCHRONOUS_IO_NONALERT,
  NULL,
  0);
 if(!NT_SUCCESS(status))
 {
  _asm int 3;
 }
 
 RenamInfo = (PFILE_RENAME_INFORMATION)ExAllocatePoolWithTag(NonPagedPool,1024,0);
 
 RenamInfo->ReplaceIfExists = FALSE;
 RenamInfo->RootDirectory = NULL;
 
 UniRenameStr.Buffer = RenamInfo->FileName;
 RenamInfo->FileNameLength = sizeof(ReNamePath) - sizeof(WCHAR);
 
 RtlCopyMemory(UniRenameStr.Buffer,ReNamePath,
  sizeof(ReNamePath));
 
 status = ObReferenceObjectByHandle( hFile,
    0,
    *IoFileObjectType,
    KernelMode,
    &FileObject,
    NULL);
 if (!NT_SUCCESS(status))
 {
   _asm int 3;
 }
  
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// 以下为iosetinformationfile
 ObReferenceObject( FileObject ); 
 KeInitializeEvent( &event, SynchronizationEvent, FALSE );
 deviceObject = IoGetRelatedDeviceObject( FileObject );
   
 irp = IoAllocateIrp( deviceObject->StackSize, TRUE );
    if (!irp)
 {
  _asm int 3;
 }
  
 irp->Tail.Overlay.OriginalFileObject = FileObject;
    irp->Tail.Overlay.Thread = PsGetCurrentThread();
    irp->RequestorMode = KernelMode;
  
 irp->UserEvent = &event;
    irp->Flags = IRP_SYNCHRONOUS_API;
  
    irp->UserIosb = &localIoStatus;
  
    irpSp = IoGetNextIrpStackLocation( irp );
    irpSp->MajorFunction = IRP_MJ_SET_INFORMATION;
    irpSp->FileObject = FileObject;
  
  
 irp->AssociatedIrp.SystemBuffer = RenamInfo;
    irp->Flags |= IRP_BUFFERED_IO;
  
 irpSp->Parameters.SetFile.Length = 1024;
    irpSp->Parameters.SetFile.FileInformationClass = FileRenameInformation;
  
 irpSp->Parameters.SetFile.ReplaceIfExists = RenamInfo->ReplaceIfExists;
  
 if (RenamInfo->FileName[0] == (UCHAR) OBJ_NAME_PATH_SEPARATOR ||
            RenamInfo->RootDirectory != NULL)
 {
 /////////////////////////////////////////////////////////
 // 以下IopOpenLinkOrRenameTarget
  ACCESS_MASK   accessMask = FILE_WRITE_DATA;
  OBJECT_ATTRIBUTES  objectAttributes;
  IO_STATUS_BLOCK  ioStatus;
  OBJECT_HANDLE_INFORMATION  handleInformation;
   
  newFileName.Length = (USHORT) RenamInfo->FileNameLength;
  newFileName.MaximumLength = (USHORT) RenamInfo->FileNameLength;
  newFileName.Buffer = RenamInfo->FileName;
   
  InitializeObjectAttributes( &objectAttributes,
                                &newFileName,
                                OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE,
                                NULL,
                                NULL );
         
  status = IoCreateFile( &handle,
                               FILE_WRITE_DATA | SYNCHRONIZE,
                               &objectAttributes,
                               &ioStatus,
                               (PLARGE_INTEGER) NULL,
                               0,
                               FILE_SHARE_READ | FILE_SHARE_WRITE,
                               FILE_OPEN,
                               FILE_OPEN_FOR_BACKUP_INTENT,
                               (PVOID) NULL,
                               0L,
                               CreateFileTypeNone,
                               (PVOID) NULL,
          //IO_OPEN_TARGET_DIRECTORY |
                               IO_NO_PARAMETER_CHECKING |
          0x0004 |
                               IO_FORCE_ACCESS_CHECK ); 
  if(!NT_SUCCESS(status))
  {
   _asm int 3;
  }
  status = ObReferenceObjectByHandle( handle,
                                              accessMask,
                                              *IoFileObjectType,
                                              KernelMode,
                                              (PVOID *) &targetFileObject,
                                              &handleInformation ); 
   
    
  if(!NT_SUCCESS(status))
  {
   _asm int 3;
  }
  ObDereferenceObject( targetFileObject );
   
                if (IoGetRelatedDeviceObject( targetFileObject) !=
                    IoGetRelatedDeviceObject( FileObject )) {
 
                    _asm int 3;
 
                } else {
                    irpSp->Parameters.SetFile.FileObject = targetFileObject;
                    //*TargetHandle = handle;
                    status = STATUS_SUCCESS;
 
                }     
 
 
 }
 // 以上IopOpenLinkOrRenameTarget
 /////////////////////////////////////////////////////
 status = IoCallDriver( deviceObject, irp );
 if (status == STATUS_PENDING)
  KeWaitForSingleObject(&event, Executive, KernelMode, TRUE, 0);
 if(!NT_SUCCESS(status))
 {
  _asm int 3;
 }
  
 ZwClose( handle);
//  以上为IOSETINFORMATIONFILE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
 ZwClose(hFile);
 ObDereferenceObject(FileObject);
 return STATUS_SUCCESS;
}

 

文件重命名的几种写法,布布扣,bubuko.com

文件重命名的几种写法

标签:des   c   class   blog   code   a   

原文地址:http://www.cnblogs.com/itdef/p/3759975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!