EaseFilter Demo Project
CommonObjects/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 
19 
20 namespace EaseFilter.CommonObjects
21 {
22  public class FilterService
23  {
24  public static bool IOAccessControl(FilterAPI.MessageSendData messageSend, ref FilterAPI.MessageReplyData messageReply)
25  {
26  bool ret = true;
27 
28  try
29  {
30  messageReply.MessageId = messageSend.MessageId;
31  messageReply.MessageType = messageSend.MessageType;
32  messageReply.ReturnStatus = (uint)FilterAPI.NTSTATUS.STATUS_SUCCESS;
33 
34  messageReply.MessageId = messageSend.MessageId;
35  messageReply.MessageType = messageSend.MessageType;
36 
37  //
38  //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
39  //
40  //
41 
42  //if you don't want to change anything to this IO request, just let it pass through as below setting:
43  //messageReply.FilterStatus = 0;
44  //messageReply.ReturnStatus = (uint)NtStatus.Status.Success;
45 
46  //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,
47  //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.
48  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
49  //messageReply.ReturnStatus = (uint)NtStatus.Status.AccessDenied;
50 
51  //if you want to modify the IO data and complete the pre IO with your own data, you can return status as below:
52  // messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION | (uint)FilterAPI.FilterStatus.FILTER_DATA_BUFFER_IS_UPDATED;
53  // messageReply.DataBufferLength = the return data buffer length.
54  // messageReply.DataBuffer = the data you want to return.
55  // messageReply.ReturnStatus = (uint)NtStatus.Status.Success;
56 
57  FilterAPI.MessageType messageType = (FilterAPI.MessageType)messageSend.MessageType;
58  WinData.FileInfomationClass infoClass = (WinData.FileInfomationClass)messageSend.InfoClass;
59 
60  uint dataLength = messageSend.DataBufferLength;
61  byte[] data = messageSend.DataBuffer;
62 
63  //here is some IO information for your reference:
64  if ((messageSend.CreateOptions & (uint)WinData.CreateOptions.FO_REMOTE_ORIGIN) > 0)
65  {
66  //this is file access request comes from remote network server
67  }
68 
69  //you can check the file create option with this data:
70  //"DesiredAccess: messageSend.DesiredAccess
71  //"Disposition:" + ((WinData.Disposition)messageSend.Disposition).ToString();
72  //"ShareAccess:" + ((WinData.ShareAccess)messageSend.SharedAccess).ToString();
73  //"CreateOptions:"messageSend.CreateOptions
74 
75 
76  //Here is the demo to copy file content before it was deleted.-----------------------------------------------
77  bool isFileDeleting = false;
78  if (messageSend.Status == (uint)NtStatus.Status.Success)
79  {
80  if (messageType == FilterAPI.MessageType.POST_CREATE)
81  {
82  if ((messageSend.CreateOptions & (uint)WinData.CreateOptions.FILE_DELETE_ON_CLOSE) > 0)
83  {
84  isFileDeleting = true;
85  }
86  }
87  else if (messageType == FilterAPI.MessageType.POST_SET_INFORMATION)
88  {
89  if (infoClass == WinData.FileInfomationClass.FileDispositionInformation)
90  {
91  isFileDeleting = true;
92  }
93  }
94 
95  if (isFileDeleting)
96  {
97 
98  IntPtr fileHandle = IntPtr.Zero;
99  bool retVal = FilterAPI.GetFileHandleInFilter(messageSend.FileName,(uint)FileAccess.Read, ref fileHandle);
100  if (retVal)
101  {
102  SafeFileHandle sHandle = new SafeFileHandle(fileHandle, true);
103  FileStream fileStream = new FileStream(sHandle, FileAccess.Read);
104 
105  //copy your data here...
106 
107  fileStream.Close();
108  }
109 
110  }
111  }
112  //End -----------------------------------------------
113 
114 
115 
116  switch (messageType)
117  {
118  case FilterAPI.MessageType.PRE_CREATE:
119  {
120 
121  //here you reparse the file open to another new file name
122 
123  //string newReparseFileName = "\\??\\c:\\myNewFile.txt";
124  //byte[] returnData = Encoding.Unicode.GetBytes(newReparseFileName);
125  //Array.Copy(returnData, messageReply.DataBuffer, returnData.Length);
126  //messageReply.DataBufferLength = (uint)returnData.Length;
127  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
128  //messageReply.ReturnStatus = (uint)NtStatus.Status.Reparse;
129 
130  break;
131  }
132 
133 
134  case FilterAPI.MessageType.PRE_CACHE_READ:
135  case FilterAPI.MessageType.POST_CACHE_READ:
136  case FilterAPI.MessageType.PRE_NOCACHE_READ:
137  case FilterAPI.MessageType.POST_NOCACHE_READ:
138  case FilterAPI.MessageType.PRE_PAGING_IO_READ:
139  case FilterAPI.MessageType.POST_PAGING_IO_READ:
140  case FilterAPI.MessageType.PRE_CACHE_WRITE:
141  case FilterAPI.MessageType.POST_CACHE_WRITE:
142  case FilterAPI.MessageType.PRE_NOCACHE_WRITE:
143  case FilterAPI.MessageType.POST_NOCACHE_WRITE:
144  case FilterAPI.MessageType.PRE_PAGING_IO_WRITE:
145  case FilterAPI.MessageType.POST_PAGING_IO_WRITE:
146  {
147 
148 
149  //byte[] returnData = //new data you want to modify the read/write data;
150  //Array.Copy(returnData, messageReply.DataBuffer, returnData.Length);
151  //messageReply.DataBufferLength = (uint)returnData.Length;
152 
155 
156  // messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_DATA_BUFFER_IS_UPDATED;
157  // messageReply.ReturnStatus = (uint)NtStatus.Status.Success;
158 
159 
160  break;
161  }
162  case FilterAPI.MessageType.PRE_SET_INFORMATION:
163  case FilterAPI.MessageType.POST_SET_INFORMATION:
164  case FilterAPI.MessageType.PRE_QUERY_INFORMATION:
165  case FilterAPI.MessageType.POST_QUERY_INFORMATION:
166  {
167  switch (infoClass)
168  {
169  case WinData.FileInfomationClass.FileRenameInformation:
170  {
171  if (FilterAPI.MessageType.PRE_SET_INFORMATION == messageType)
172  {
173  string blockFileName = @"c:\filterTest\blockRename.txt";
174  //test block rename to blockFileName, it needs to register PRE_SET_INFORMATION;
175  if (string.Compare(messageSend.FileName, blockFileName, true) == 0)
176  {
177  EventManager.WriteMessage(179, "IOAccessControl", EventLevel.Warning, "Block rename for file:" + messageSend.FileName);
178  ret = false;
179  break;
180  }
181  }
182 
183  //you can block file rename as below
184  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
185  //messageReply.ReturnStatus = (uint)NtStatus.Status.AccessDenied;
186  break;
187  }
188  case WinData.FileInfomationClass.FileDispositionInformation:
189  {
190  //you can block file delete as below
191  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
192  //messageReply.ReturnStatus = (uint)NtStatus.Status.AccessDenied;
193  break;
194  }
195  case WinData.FileInfomationClass.FileEndOfFileInformation:
196  {
197  //change file size
198  break;
199  }
200  case WinData.FileInfomationClass.FileBasicInformation:
201  {
202  //file basic information
203  break;
204  }
205 
206  case WinData.FileInfomationClass.FileStandardInformation:
207  {
208  //file standard information
209  break;
210  }
211  case WinData.FileInfomationClass.FileNetworkOpenInformation:
212  {
213  //file network information
214  break;
215  }
216 
217  case WinData.FileInfomationClass.FileInternalInformation:
218  {
219  //file internal inofrmation
220  break;
221  }
222  default:
223  {
224  break;
225  }
226  }
227 
228  break;
229  }
230  }
231 
232 
233  }
234  catch (Exception ex)
235  {
236  EventManager.WriteMessage(174, "IOAccessControl", EventLevel.Error, "IOAccessControl failed." + ex.Message);
237  }
238 
239  return ret;
240  }
241 
242  }
243 }
Status
A NT status value.
Definition: NtStatus.cs:23
ULONG BYTE LONGLONG HANDLE fileHandle
Definition: FilterAPI.h:792
static bool IOAccessControl(FilterAPI.MessageSendData messageSend, ref FilterAPI.MessageReplyData messageReply)
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