root/honeyclient/branches/rel/1.0/Capture2/capture-client-xeno-mod/FileMonitor.h

Revision 881, 7.4 kB (checked in by xkovah, 1 year ago)

adding all the changes for the new DB schema (revert to the prev if there are problems)

Line 
1 /*
2  *  PROJECT: Capture
3  *  FILE: FileMonitor.h
4  *  AUTHORS: Ramon Steenson (rsteenson@gmail.com) & Christian Seifert (christian.seifert@gmail.com)
5  *
6  *  Developed by Victoria University of Wellington and the New Zealand Honeynet Alliance
7  *
8  *  This file is part of Capture.
9  *
10  *  Capture is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  Capture is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with Capture; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  */
24 #pragma once
25 #include "CaptureGlobal.h"
26 #include <winioctl.h>
27 #include <boost/signal.hpp>
28 #include <boost/bind.hpp>
29 #include <hash_set>
30 #include <shlobj.h>
31 #include "Thread.h"
32 #include "Monitor.h"
33 #include "MiniFilter.h"
34
35 typedef pair <wstring, wstring> DosPair;
36
37 #define CAPTURE_FILEMON_PORT_NAME   L"\\CaptureFileMonitorPort"
38
39 /*
40     Enum: FILEMONITOR_COMMAND
41
42     Commands that are sent through <commPort> to the kernel driver
43
44     GetFileEvents - Retrieve a buffer that contains a list of <FILE_EVENT>'s
45     SetupMonitor - Send the monitor setup to the kernel driver. Log directory etc
46 */
47 typedef enum _FILEMONITOR_COMMAND {
48
49     GetFileEvents,
50     SetupMonitor
51
52 } FILEMONITOR_COMMAND;
53
54 /*
55     Enum: FILE_NOTIFY_CLASS
56
57     DEPRECIATED - Not used anymore
58     
59     Enum containing the types of events that the kernel driver is monitoring
60
61     FilePreRead - File read event type
62     FilePreWrite - File write event type
63 */
64 typedef enum _FILE_NOTIFY_CLASS {
65     FilePreRead,
66     FilePreWrite,
67     FilePreClose,
68     FilePreDelete,
69     FilePreCreate
70 } FILE_NOTIFY_CLASS;
71
72 /*
73     Struct: FILEMONITOR_MESSAGE
74
75     Contains a command to be sent through to the kernel driver
76
77     Command - Actual command to be sent
78 */
79 typedef struct _FILEMONITOR_MESSAGE {
80     FILEMONITOR_COMMAND Command;
81 } FILEMONITOR_MESSAGE, *PFILEMONITOR_MESSAGE;
82
83 typedef struct _FILEMONITOR_SETUP {
84     BOOLEAN bCollectDeletedFiles;
85     UINT nLogDirectorySize;
86     WCHAR wszLogDirectory[1024];
87 } FILEMONITOR_SETUP, *PFILEMONITOR_SETUP;
88
89 /*
90     Struct: FILE_EVENT
91
92     File event that contains what event happened on what file and by what process
93
94     type - file event type of <FILE_NOTIFY_CLASS>
95     time - time when the event occured in the kernel
96     name - Contains the name of the event (file modified)
97     processID - The process id that caused the file event
98 */
99 typedef struct  _FILE_EVENT {
100     UCHAR majorFileEventType;
101     UCHAR minorFileEventType;
102     ULONG status;
103     ULONG information;
104     ULONG flags;
105     TIME_FIELDS time;
106     DWORD processId;
107     UINT filePathLength;
108     WCHAR filePath[];
109 } FILE_EVENT, *PFILE_EVENT;
110 #define FlagOn(_F,_SF)        ((_F) & (_SF))
111 /* File event status */
112 #define FILE_SUPERSEDED                 0x00000000
113 #define FILE_OPENED                     0x00000001
114 #define FILE_CREATED                    0x00000002
115 #define FILE_OVERWRITTEN                0x00000003
116 #define FILE_EXISTS                     0x00000004
117 #define FILE_DOES_NOT_EXIST             0x00000005
118
119 /* File event flags */
120 #define FO_FILE_OPEN                    0x00000001
121 #define FO_SYNCHRONOUS_IO               0x00000002
122 #define FO_ALERTABLE_IO                 0x00000004
123 #define FO_NO_INTERMEDIATE_BUFFERING    0x00000008
124 #define FO_WRITE_THROUGH                0x00000010
125 #define FO_SEQUENTIAL_ONLY              0x00000020
126 #define FO_CACHE_SUPPORTED              0x00000040
127 #define FO_NAMED_PIPE                   0x00000080
128 #define FO_STREAM_FILE                  0x00000100
129 #define FO_MAILSLOT                     0x00000200
130 #define FO_GENERATE_AUDIT_ON_CLOSE      0x00000400
131 #define FO_QUEUE_IRP_TO_THREAD          FO_GENERATE_AUDIT_ON_CLOSE
132 #define FO_DIRECT_DEVICE_OPEN           0x00000800
133 #define FO_FILE_MODIFIED                0x00001000
134 #define FO_FILE_SIZE_CHANGED            0x00002000
135 #define FO_CLEANUP_COMPLETE             0x00004000
136 #define FO_TEMPORARY_FILE               0x00008000
137 #define FO_DELETE_ON_CLOSE              0x00010000
138 #define FO_OPENED_CASE_SENSITIVE        0x00020000
139 #define FO_HANDLE_CREATED               0x00040000
140 #define FO_FILE_FAST_IO_READ            0x00080000
141 #define FO_RANDOM_ACCESS                0x00100000
142 #define FO_FILE_OPEN_CANCELLED          0x00200000
143 #define FO_VOLUME_OPEN                  0x00400000
144 #define FO_REMOTE_ORIGIN                0x01000000
145 #define FO_SKIP_COMPLETION_PORT         0x02000000
146 #define FO_SKIP_SET_EVENT               0x04000000
147 #define FO_SKIP_SET_FAST_IO             0x08000000
148
149 /* Max Buffer size to allocate */
150 #define FILE_EVENTS_BUFFER_SIZE 5*65536
151 /* Normal wait time to request events from the kernel driver */
152 #define FILE_EVENT_WAIT_TIME 50
153 /* If the buffer was semi full then we wait a lesser time to get the new events */
154 #define FILE_EVENT_BUFFER_FULL_WAIT_TIME 5
155
156 /*
157     Class: FileMonitor
158
159     The file monitor is responsible for interacting with the CaptureFileMonitor
160     minifilter driver (CaptureFileMonitor.c). This is responsible for communicating
161     with the driver which passes a user-space allocated buffer into kernel-space
162     which the driver will then fill with file events. It also keeps track of which
163     files have been modified so that when required it can copy those files into
164     a temporary directory. When a event is received the monitor checks to see if
165     it is excluded. If it is not excluded it is malicious and the onFileEvent slot
166     is signalled with the event information.
167
168     The FileMonitor listens for "file-exclusion" events from the <EventController>
169     so that the server or any external object can add/remove exclusions from the
170     monitors lists.
171
172     Implements: Runnable - So that events can be received in the background
173                 Monitor - Has access to exclusion lists and loading kernel drivers
174 */
175 class FileMonitor : public Runnable, public Monitor
176 {
177     public:
178     typedef boost::signal<void (wstring, wstring, wstring, wstring, vector<wstring> extra)> signal_fileEvent;
179 public:
180     FileMonitor(void);
181     virtual ~FileMonitor(void);
182
183     void start();
184     void stop();
185     void run();
186
187     inline bool isMonitorRunning() { return monitorRunning; }
188     inline bool isDriverInstalled() { return driverInstalled; }
189
190     void copyCreatedFiles();
191     void setMonitorModifiedFiles(bool monitor);
192
193     void onFileExclusionReceived(Element* pElement);
194
195     boost::signals::connection connect_onFileEvent(const signal_fileEvent::slot_type& s);
196
197 private:
198     bool getFileEventName(PFILE_EVENT pFileEvent, wstring* fileEventName);
199     wstring convertFileObjectNameToDosName(wstring fileObjectName);
200     void initialiseDosNameMap();
201     bool isDirectory(wstring filePath);
202     void createFilePathAndCopy(wstring* logPath, wstring* filePath);
203
204     BYTE* fileEvents;
205     Thread* fileMonitorThread;
206     HANDLE hDriver;
207     HANDLE communicationPort;
208     HANDLE hMonitorStoppedEvent;
209     signal_fileEvent signal_onFileEvent;
210     stdext::hash_map<wstring, wstring> dosNameMap;
211     stdext::hash_set<wstring> modifiedFiles;
212     bool monitorRunning;
213     bool driverInstalled;
214     bool monitorModifiedFiles;
215
216     boost::signals::connection onFileExclusionReceivedConnection;
217 };
Note: See TracBrowser for help on using the browser.