| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 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 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
typedef enum _FILEMONITOR_COMMAND { |
|---|
| 48 |
|
|---|
| 49 |
GetFileEvents, |
|---|
| 50 |
SetupMonitor |
|---|
| 51 |
|
|---|
| 52 |
} FILEMONITOR_COMMAND; |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
typedef enum _FILE_NOTIFY_CLASS { |
|---|
| 65 |
FilePreRead, |
|---|
| 66 |
FilePreWrite, |
|---|
| 67 |
FilePreClose, |
|---|
| 68 |
FilePreDelete, |
|---|
| 69 |
FilePreCreate |
|---|
| 70 |
} FILE_NOTIFY_CLASS; |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 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 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 150 |
#define FILE_EVENTS_BUFFER_SIZE 5*65536 |
|---|
| 151 |
|
|---|
| 152 |
#define FILE_EVENT_WAIT_TIME 50 |
|---|
| 153 |
|
|---|
| 154 |
#define FILE_EVENT_BUFFER_FULL_WAIT_TIME 5 |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 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 |
}; |
|---|