EaseFilter Demo Project
SecureAgent/FilterWorker.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.Collections.Generic;
13 using System.Text;
14 using System.IO;
15 using Microsoft.Win32.SafeHandles;
16 using System.Runtime.InteropServices;
17 using System.Security.Principal;
18 using System.Threading;
19 using System.Diagnostics;
20 using System.Management;
21 using System.Collections;
22 using System.Windows.Forms;
23 
25 
26 
27 namespace SecureAgent
28 {
29 
30  public class FilterWorker
31  {
32 
34  DRM digitalRightsManagement = null;
35 
36  public FilterWorker(ListView listView_Info)
37  {
38  this.filterMessage = new FilterMessage(listView_Info);
39 
40  }
41 
42  public bool StartService(ref string lastError)
43  {
44  //Purchase a license key with the link: http://www.easefilter.com/Order.htm
45  //Email us to request a trial key: info@FilterAPI.com //free email is not accepted.
46  string registerKey = "**************************************";
47 
48  bool ret = false;
49  lastError = string.Empty;
50 
51  try
52  {
53 
54  ret = FilterAPI.StartFilter((int)GlobalConfig.FilterConnectionThreads
55  , registerKey
56  , new FilterAPI.FilterDelegate(FilterCallback)
57  , new FilterAPI.DisconnectDelegate(DisconnectCallback)
58  , ref lastError);
59  if (!ret)
60  {
61  lastError = "Start filter service failed with error " + lastError;
62  EventManager.WriteMessage(43, "StartFilter", EventLevel.Error, lastError);
63  return ret;
64  }
65 
66  if (GlobalConfig.FilterRules.Count == 0)
67  {
68  FilterRule filterRule = new FilterRule();
69  filterRule.IncludeFileFilterMask = "c:\\test\\*";
70  filterRule.ExcludeFileFilterMasks = "c:\\windows*";
71  filterRule.EventType = (uint)(FilterAPI.EVENTTYPE.WRITTEN | FilterAPI.EVENTTYPE.CREATED | FilterAPI.EVENTTYPE.DELETED | FilterAPI.EVENTTYPE.RENAMED);
72  filterRule.AccessFlag = (uint)FilterAPI.ALLOW_MAX_RIGHT_ACCESS;
73  GlobalConfig.FilterRules.Add(filterRule.IncludeFileFilterMask, filterRule);
74 
75  MessageBox.Show("You don't have any monitor folder setup, add c:\\test\\* as your default test folder, I/Os from c:\\test\\* will show up in the console.");
76  }
77 
78 
80 
81  EventManager.WriteMessage(102, "StartFilter", EventLevel.Information, "Start filter service succeeded.");
82  }
83  catch (Exception ex)
84  {
85  lastError = "Start filter service failed with error " + ex.Message;
86  EventManager.WriteMessage(104, "StartFilter", EventLevel.Error, lastError);
87  }
88 
89  return ret;
90  }
91 
92  static void DisconnectCallback()
93  {
94  EventManager.WriteMessage(697, "DisconnectCallback", EventLevel.Information, "Filter Disconnected." + FilterAPI.GetLastErrorMessage());
95  }
96 
97 
98  public bool StopService()
99  {
100  FilterAPI.StopFilter();
101  GlobalConfig.Stop();
102 
103  return true;
104  }
105 
106  public void ClearMessage()
107  {
108  filterMessage.InitListView();
109  }
110 
111  private Boolean FilterCallback(IntPtr sendDataPtr, IntPtr replyDataPtr)
112  {
113  Boolean ret = true;
114 
115  try
116  {
117  FilterAPI.MessageSendData messageSend = new FilterAPI.MessageSendData();
118  messageSend = (FilterAPI.MessageSendData)Marshal.PtrToStructure(sendDataPtr, typeof(FilterAPI.MessageSendData));
119 
120  if (FilterAPI.MESSAGE_SEND_VERIFICATION_NUMBER != messageSend.VerificationNumber)
121  {
122  EventManager.WriteMessage(139, "FilterCallback", EventLevel.Error, "Received message corrupted.Please check if the MessageSendData structure is correct.");
123  return false;
124  }
125 
126  ret = FilterRequestHandler(ref messageSend, replyDataPtr);
127 
128 
129  string info = "SecureAgent process request " + FilterMessage.FormatIOName(messageSend) + ",pid:" + messageSend.ProcessId +
130  " ,filename:" + messageSend.FileName + ",return status:" + FilterMessage.FormatStatus(messageSend.Status);
131 
132  if( messageSend.Status == (uint)NtStatus.Status.Success )
133  {
134  ret = false;
135  EventManager.WriteMessage(98, "FilterCallback", EventLevel.Verbose, info);
136  }
137  else
138  {
139  ret = true;
140  EventManager.WriteMessage(98, "FilterCallback", EventLevel.Error, info);
141  }
142 
143  return ret;
144  }
145  catch (Exception ex)
146  {
147  EventManager.WriteMessage(134, "FilterCallback", EventLevel.Error, "filter callback exception." + ex.Message);
148  return false;
149  }
150 
151  }
152 
153  private bool FilterRequestHandler(ref FilterAPI.MessageSendData messageSend, IntPtr replyDataPtr)
154  {
155  Boolean retVal = true;
156  string fileName = messageSend.FileName;
157  string lastError = string.Empty;
158 
159  try
160  {
161 
162  if ((replyDataPtr.ToInt64() != 0))
163  {
164  FilterAPI.MessageReplyData messageReply = (FilterAPI.MessageReplyData)Marshal.PtrToStructure(replyDataPtr, typeof(FilterAPI.MessageReplyData));
165  messageReply.MessageId = messageSend.MessageId;
166  messageReply.MessageType = messageSend.MessageType;
167 
168  if ( messageSend.MessageType == (uint)FilterAPI.FilterCommand.FILTER_REQUEST_USER_PERMIT
169  || messageSend.MessageType == (uint)FilterAPI.FilterCommand.FILTER_REQUEST_ENCRYPTION_IV_AND_KEY)
170  {
171  if (null == digitalRightsManagement)
172  {
173  digitalRightsManagement = new DRM();
174  }
175 
176  //get permission for secure shared file
177  retVal = digitalRightsManagement.GetUserPermission(messageSend, ref messageReply);
178 
179  }
180  else
181  {
182  //control the file I/O here
183  retVal = FilterService.IOAccessControl(messageSend, ref messageReply);
184  }
185 
186  if (retVal)
187  {
188  messageReply.ReturnStatus = (uint)FilterAPI.NTSTATUS.STATUS_SUCCESS;
189  }
190  else
191  {
192  //comple the PRE_CREATE,user get access denied for the file open.
193  messageReply.ReturnStatus = (uint)FilterAPI.NTSTATUS.STATUS_ACCESS_DENIED;
194  messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
195  }
196 
197  Marshal.StructureToPtr(messageReply, replyDataPtr, true);
198 
199  if (!retVal)
200  {
201  messageSend.Status = (uint)FilterAPI.NTSTATUS.STATUS_ACCESS_DENIED;
202  }
203  }
204 
205 
206  filterMessage.AddMessage(messageSend);
207 
208  return retVal;
209  }
210  catch (Exception ex)
211  {
212  EventManager.WriteMessage(134, "FilterRequestHandler", EventLevel.Error, "filter callback exception." + ex.Message);
213  return false;
214  }
215  }
216 
217 
218  }
219 
220 
221 }
uint EventType
The register the file I/O events
bool GetUserPermission(FilterAPI.MessageSendData messageSend, ref FilterAPI.MessageReplyData messageReply)
FilterWorker(ListView listView_Info)
static string FormatIOName(FilterAPI.MessageSendData messageSend)
void AddMessage(FilterAPI.MessageSendData messageSend)
static Dictionary< string, FilterRule > FilterRules
Status
A NT status value.
Definition: NtStatus.cs:23
static bool IOAccessControl(FilterAPI.MessageSendData messageSend, ref FilterAPI.MessageReplyData messageReply)
#define registerKey
bool StartService(ref string lastError)

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