EaseFilter Demo Project
ProtectorForm.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.ComponentModel;
14 using System.Data;
15 using System.Drawing;
16 using System.Linq;
17 using System.Text;
18 using System.IO;
19 using System.Windows.Forms;
20 using System.Runtime.InteropServices;
21 
23 
24 namespace FileProtector
25 {
26  public partial class ProtectorForm : Form
27  {
28  //Purchase a license key with the link: http://www.easefilter.com/Order.htm
29  //Email us to request a trial key: info@FilterAPI.com //free email is not accepted.
30  string registerKey = "**************************************";
31 
32  FilterMessage filterMessage = null;
33 
34  public ProtectorForm()
35  {
36  GlobalConfig.filterType = FilterAPI.FilterType.FILE_SYSTEM_EASE_FILTER_ALL;
37 
38  InitializeComponent();
39 
40  StartPosition = FormStartPosition.CenterScreen;
41  filterMessage = new FilterMessage(listView_Info);
42 
43 
44  }
45 
46  ~ProtectorForm()
47  {
49  }
50 
51 
52  private void toolStripButton_StartFilter_Click(object sender, EventArgs e)
53  {
54  try
55  {
56  string lastError = string.Empty;
57 
58  bool ret = FilterAPI.StartFilter( (int)GlobalConfig.FilterConnectionThreads
59  , registerKey
60  , new FilterAPI.FilterDelegate(FilterCallback)
61  , new FilterAPI.DisconnectDelegate(DisconnectCallback)
62  , ref lastError);
63  if (!ret)
64  {
65  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
66  MessageBox.Show("Start filter failed." + lastError);
67  return;
68  }
69 
70  toolStripButton_StartFilter.Enabled = false;
71  toolStripButton_Stop.Enabled = true;
72 
73 
74  if (GlobalConfig.FilterRules.Count == 0)
75  {
76  FilterRule filterRule = new FilterRule();
77  filterRule.IncludeFileFilterMask = "c:\\test\\*";
78  filterRule.ExcludeFileFilterMasks = "c:\\windows*";
79  filterRule.EventType = (uint)(FilterAPI.EVENTTYPE.WRITTEN | FilterAPI.EVENTTYPE.CREATED | FilterAPI.EVENTTYPE.DELETED | FilterAPI.EVENTTYPE.RENAMED);
80  filterRule.AccessFlag = (uint)FilterAPI.ALLOW_MAX_RIGHT_ACCESS;
81  GlobalConfig.FilterRules.Add(filterRule.IncludeFileFilterMask, filterRule);
82 
83  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
84  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.");
85  }
86 
87 
89 
90  EventManager.WriteMessage(102, "StartFilter", EventLevel.Information, "Start filter service succeeded.");
91  }
92  catch (Exception ex)
93  {
94  EventManager.WriteMessage(104, "StartFilter", EventLevel.Error, "Start filter service failed with error " + ex.Message);
95  }
96 
97  }
98 
99  private void toolStripButton_Stop_Click(object sender, EventArgs e)
100  {
101  FilterAPI.StopFilter();
102 
103  toolStripButton_StartFilter.Enabled = true;
104  toolStripButton_Stop.Enabled = false;
105  }
106 
107  private void toolStripButton_ClearMessage_Click(object sender, EventArgs e)
108  {
109  filterMessage.InitListView();
110  }
111 
112  Boolean FilterCallback(IntPtr sendDataPtr, IntPtr replyDataPtr)
113  {
114  Boolean ret = true;
115 
116  try
117  {
118  FilterAPI.MessageSendData messageSend = new FilterAPI.MessageSendData();
119  messageSend = (FilterAPI.MessageSendData)Marshal.PtrToStructure(sendDataPtr, typeof(FilterAPI.MessageSendData));
120 
121  if (FilterAPI.MESSAGE_SEND_VERIFICATION_NUMBER != messageSend.VerificationNumber)
122  {
123  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
124  MessageBox.Show("Received message corrupted.Please check if the MessageSendData structure is correct.");
125 
126  EventManager.WriteMessage(139, "FilterCallback", EventLevel.Error, "Received message corrupted.Please check if the MessageSendData structure is correct.");
127  return false;
128  }
129 
130  filterMessage.AddMessage(messageSend);
131 
132  if (replyDataPtr.ToInt64() != 0)
133  {
134  FilterAPI.MessageReplyData messageReply = (FilterAPI.MessageReplyData)Marshal.PtrToStructure(replyDataPtr, typeof(FilterAPI.MessageReplyData));
135 
136  //here you can control the IO behaviour and modify the data.
137  if (!FilterService.IOAccessControl(messageSend, ref messageReply))
138  {
139  //to comple the PRE_IO
140  messageReply.ReturnStatus = (uint)FilterAPI.NTSTATUS.STATUS_ACCESS_DENIED;
141  messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
142 
143  EventManager.WriteMessage(160, "FilterCallback", EventLevel.Error, "Return error for I/O request:" + ((FilterAPI.MessageType)messageSend.MessageType).ToString() +
144  ",fileName:" + messageSend.FileName );
145  }
146  else
147  {
148 
149  messageReply.MessageId = messageSend.MessageId;
150  messageReply.MessageType = messageSend.MessageType;
151  messageReply.ReturnStatus = (uint)FilterAPI.NTSTATUS.STATUS_SUCCESS;
152 
153  }
154 
155  Marshal.StructureToPtr(messageReply, replyDataPtr, true);
156 
157  }
158 
159  string info = "FileProtector process request " + FilterMessage.FormatIOName(messageSend) + ",pid:" + messageSend.ProcessId +
160  " ,filename:" + messageSend.FileName + ",return status:" + FilterMessage.FormatStatus(messageSend.Status);
161 
162  if (messageSend.Status == (uint)NtStatus.Status.Success)
163  {
164  ret = false;
165  EventManager.WriteMessage(98, "FilterCallback", EventLevel.Verbose, info);
166  }
167  else
168  {
169  ret = true;
170  EventManager.WriteMessage(98, "FilterCallback", EventLevel.Error, info);
171  }
172 
173  return ret;
174  }
175  catch (Exception ex)
176  {
177  EventManager.WriteMessage(134, "FilterCallback", EventLevel.Error, "filter callback exception." + ex.Message);
178  return false;
179  }
180 
181  }
182 
183  void DisconnectCallback()
184  {
185  EventManager.WriteMessage(190, "DisconnectCallback", EventLevel.Information, "Filter Disconnected." + FilterAPI.GetLastErrorMessage());
186  }
187 
188 
189  private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
190  {
191  SettingsForm settingForm = new SettingsForm();
192  settingForm.StartPosition = FormStartPosition.CenterParent;
193  settingForm.ShowDialog();
194  }
195 
196  private void reportAProblemToolStripMenuItem_Click(object sender, EventArgs e)
197  {
198  System.Diagnostics.Process.Start("http://www.easefilter.com/ReportIssue.htm");
199  }
200 
201  private void helpTopicsToolStripMenuItem_Click(object sender, EventArgs e)
202  {
203  System.Diagnostics.Process.Start("http://www.easefilter.com/Forums_Files/FileProtector.htm");
204  }
205 
206  private void toolStripButton1_Click(object sender, EventArgs e)
207  {
209  }
210 
211  private void encryptFileWithToolStripMenuItem_Click(object sender, EventArgs e)
212  {
213  EncryptedFileForm encryptForm = new EncryptedFileForm("Encrypt file", FilterAPI.EncryptType.Encryption);
214  encryptForm.ShowDialog();
215  }
216 
217  private void decryptFileToolStripMenuItem_Click(object sender, EventArgs e)
218  {
219  EncryptedFileForm encryptForm = new EncryptedFileForm("Decrypt file", FilterAPI.EncryptType.Decryption);
220  encryptForm.ShowDialog();
221  }
222 
223  private void getEncryptedFileIVTagToolStripMenuItem_Click(object sender, EventArgs e)
224  {
225  InputForm inputForm = new InputForm("Input file name", "Plase input file name", "");
226 
227  if (inputForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
228  {
229  string fileName = inputForm.InputText;
230  byte[] iv = null;
231  string lastError = string.Empty;
232 
233  if (FilterAPI.GetIVTag(fileName, ref iv, out lastError))
234  {
235  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
236  MessageBox.Show("Get encrypted file " + fileName + " iv tag:" + Utils.ByteArrayToHexStr(iv), "IV Tag", MessageBoxButtons.OK, MessageBoxIcon.Information);
237  }
238  else
239  {
240  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
241  MessageBox.Show("Get encrypted file " + fileName + " iv tag failed with error " + lastError, "IV Tag", MessageBoxButtons.OK, MessageBoxIcon.Error);
242  }
243  }
244  }
245 
246  private void exitToolStripMenuItem_Click(object sender, EventArgs e)
247  {
248  Close();
249  }
250 
251 
252  private void ProtectorForm_FormClosed(object sender, FormClosedEventArgs e)
253  {
254  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
255  if (MessageBox.Show("Do you want to minimize to system tray?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
256  {
257 
258  }
259  else
260  {
261  GlobalConfig.Stop();
262  Application.Exit();
263  }
264  }
265 
266  private void unInstallFilterDriverToolStripMenuItem_Click(object sender, EventArgs e)
267  {
268  FilterAPI.StopFilter();
269  FilterAPI.UnInstallDriver();
270  }
271 
272 
273 
274  private void protectorTutorialToolStripMenuItem_Click(object sender, EventArgs e)
275  {
276  TutorialForm tutorialForm = new TutorialForm();
277  tutorialForm.ShowDialog();
278  }
279 
280  private void toolStripButton_LoadMessage_Click(object sender, EventArgs e)
281  {
282  filterMessage.LoadMessageFromLogToConsole();
283  }
284 
285 
286 
287  }
288 }
static string ByteArrayToHexStr(byte[] ba)
Definition: Utils.cs:174
uint EventType
The register the file I/O events
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
unsigned char iv[]
static bool IOAccessControl(FilterAPI.MessageSendData messageSend, ref FilterAPI.MessageReplyData messageReply)
static FilterAPI.FilterType filterType
Definition: GlobalConfig.cs:79

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