EaseFilter Demo Project
FileProtector/FileIOControlService.cs
Go to the documentation of this file.
1 //
3 // (C) Copyright 2012 EaseFilter Technologies Inc.
4 // All Rights Reserved
5 //
6 // This software is part of a licensed software product and may
7 // only be used or copied in accordance with the terms of that license.
8 //
10 
11 using System;
12 using System.IO;
13 using System.Collections.Generic;
14 using System.Linq;
15 using System.Text;
16 using System.Runtime.InteropServices;
17 using Microsoft.Win32.SafeHandles;
18 
20 
21 namespace FileProtector
22 {
24  {
25  public static bool IOAccessControl(FilterAPI.MessageSendData messageSend, ref FilterAPI.MessageReplyData messageReply)
26  {
27  bool ret = true;
28 
29  try
30  {
31 
32  messageReply.MessageId = messageSend.MessageId;
33  messageReply.MessageType = messageSend.MessageType;
34 
35  //
36  //here you can control all the registered IO requests,block the access or modify the I/O data base on the file IO information from MessageSend struture
37  //
38  //
39 
40  //if you don't want to change anything to this IO request, just let it pass through as below setting:
41  //messageReply.FilterStatus = 0;
42  //messageReply.ReturnStatus = (uint)NtStatus.Status.Success;
43 
44  //if you want to block the access this IO request before it goes down to the file system, you can return the status as below,
45  //it is only for pre IO requests, it means the user IO reuqests will be completed here instead of going down to the file system.
46  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
47  //messageReply.ReturnStatus = (uint)NtStatus.Status.AccessDenied;
48 
49  //if you want to modify the IO data and complete the pre IO with your own data, you can return status as below:
50  // messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION | (uint)FilterAPI.FilterStatus.FILTER_DATA_BUFFER_IS_UPDATED;
51  // messageReply.DataBufferLength = the return data buffer length.
52  // messageReply.DataBuffer = the data you want to return.
53  // messageReply.ReturnStatus = (uint)NtStatus.Status.Success;
54 
55  FilterAPI.MessageType messageType = (FilterAPI.MessageType)messageSend.MessageType;
56  WinData.FileInfomationClass infoClass = (WinData.FileInfomationClass)messageSend.InfoClass;
57 
58  uint dataLength = messageSend.DataBufferLength;
59  byte[] data = messageSend.DataBuffer;
60 
61  //here is some IO information for your reference:
62  if ((messageSend.CreateOptions & (uint)WinData.CreateOptions.FO_REMOTE_ORIGIN) > 0)
63  {
64  //this is file access request comes from remote network server
65  }
66 
67  //you can check the file create option with this data:
68  //"DesiredAccess: messageSend.DesiredAccess
69  //"Disposition:" + ((WinData.Disposition)messageSend.Disposition).ToString();
70  //"ShareAccess:" + ((WinData.ShareAccess)messageSend.SharedAccess).ToString();
71  //"CreateOptions:"messageSend.CreateOptions
72 
73 
74  //Here is the demo to copy file content before it was deleted.-----------------------------------------------
75  bool isFileDeleted = false;
76  if (messageSend.Status == (uint)NtStatus.Status.Success)
77  {
78  if (messageType == FilterAPI.MessageType.POST_CREATE)
79  {
80  if ((messageSend.CreateOptions & (uint)WinData.CreateOptions.FILE_DELETE_ON_CLOSE) > 0)
81  {
82  isFileDeleted = true;
83  }
84  }
85  else if (messageType == FilterAPI.MessageType.POST_SET_INFORMATION)
86  {
87  if (infoClass == WinData.FileInfomationClass.FileDispositionInformation)
88  {
89  isFileDeleted = true;
90  }
91  }
92 
93  if (isFileDeleted)
94  {
95 
96  IntPtr fileHandle = IntPtr.Zero;
97  bool retVal = FilterAPI.GetFileHandleInFilter(messageSend.FileName,(uint)FileAccess.Read, ref fileHandle);
98  if (retVal)
99  {
100  SafeFileHandle sHandle = new SafeFileHandle(fileHandle, true);
101  FileStream fileStream = new FileStream(sHandle, FileAccess.Read);
102 
103  //copy your data here...
104 
105  fileStream.Close();
106  }
107 
108  }
109  }
110  //End -----------------------------------------------
111 
112 
113 
114  switch (messageType)
115  {
116  case FilterAPI.MessageType.PRE_CREATE:
117  {
118 
119  //here you reparse the file open to another new file name
120 
121  //string newReparseFileName = "\\??\\c:\\myNewFile.txt";
122  //byte[] returnData = Encoding.Unicode.GetBytes(newReparseFileName);
123  //Array.Copy(returnData, messageReply.DataBuffer, returnData.Length);
124  //messageReply.DataBufferLength = (uint)returnData.Length;
125  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
126  //messageReply.ReturnStatus = (uint)NtStatus.Status.Reparse;
127 
128  break;
129  }
130 
131 
132  case FilterAPI.MessageType.PRE_CACHE_READ:
133  case FilterAPI.MessageType.POST_CACHE_READ:
134  case FilterAPI.MessageType.PRE_NOCACHE_READ:
135  case FilterAPI.MessageType.POST_NOCACHE_READ:
136  case FilterAPI.MessageType.PRE_PAGING_IO_READ:
137  case FilterAPI.MessageType.POST_PAGING_IO_READ:
138  case FilterAPI.MessageType.PRE_CACHE_WRITE:
139  case FilterAPI.MessageType.POST_CACHE_WRITE:
140  case FilterAPI.MessageType.PRE_NOCACHE_WRITE:
141  case FilterAPI.MessageType.POST_NOCACHE_WRITE:
142  case FilterAPI.MessageType.PRE_PAGING_IO_WRITE:
143  case FilterAPI.MessageType.POST_PAGING_IO_WRITE:
144  {
145 
146 
147  //byte[] returnData = //new data you want to modify the read/write data;
148  //Array.Copy(returnData, messageReply.DataBuffer, returnData.Length);
149  //messageReply.DataBufferLength = (uint)returnData.Length;
150 
153 
154  // messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_DATA_BUFFER_IS_UPDATED;
155  // messageReply.ReturnStatus = (uint)NtStatus.Status.Success;
156 
157 
158  break;
159  }
160  case FilterAPI.MessageType.PRE_SET_INFORMATION:
161  case FilterAPI.MessageType.POST_SET_INFORMATION:
162  case FilterAPI.MessageType.PRE_QUERY_INFORMATION:
163  case FilterAPI.MessageType.POST_QUERY_INFORMATION:
164  {
165  ret = true;
166  switch (infoClass)
167  {
168  case WinData.FileInfomationClass.FileRenameInformation:
169  {
170  //you can block file rename as below
171  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
172  //messageReply.ReturnStatus = (uint)NtStatus.Status.AccessDenied;
173  break;
174  }
175  case WinData.FileInfomationClass.FileDispositionInformation:
176  {
177  //you can block file delete as below
178  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
179  //messageReply.ReturnStatus = (uint)NtStatus.Status.AccessDenied;
180  break;
181  }
182  case WinData.FileInfomationClass.FileEndOfFileInformation:
183  {
184  //change file size
185  break;
186  }
187  case WinData.FileInfomationClass.FileBasicInformation:
188  {
189  //file basic information
190  break;
191  }
192 
193  case WinData.FileInfomationClass.FileStandardInformation:
194  {
195  //file standard information
196  break;
197  }
198  case WinData.FileInfomationClass.FileNetworkOpenInformation:
199  {
200  //file network information
201  break;
202  }
203 
204  case WinData.FileInfomationClass.FileInternalInformation:
205  {
206  //file internal inofrmation
207  break;
208  }
209  default:
210  {
211  ret = false;
212  break;
213  }
214  }
215 
216  break;
217  }
218  }
219 
220 
221  }
222  catch (Exception ex)
223  {
224  EventManager.WriteMessage(174, "IOAccessControl", EventLevel.Error, "IOAccessControl failed." + ex.Message);
225  }
226 
227  return ret;
228  }
229 
230  }
231 }
Status
A NT status value.
Definition: NtStatus.cs:23
static bool IOAccessControl(FilterAPI.MessageSendData messageSend, ref FilterAPI.MessageReplyData messageReply)
ULONG BYTE LONGLONG HANDLE fileHandle
Definition: FilterAPI.h:792
LONGLONG data
Definition: FilterAPI.h:521

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com