12 using System.Collections.Generic;
13 using System.ComponentModel;
15 using System.Runtime.InteropServices;
16 using System.Security.Principal;
25 public static class MessageInfo
27 static object displayLock =
new object();
29 public static void DisplayFilterMessage(FilterAPI.MessageSendData messageSend)
36 string strSid =
string.Empty;
38 IntPtr sidBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(messageSend.Sid, 0);
39 ConvertSidToUserNameAndStringSid(sidBuffer, out userName, out strSid);
45 System.Diagnostics.Process requestProcess =
System.Diagnostics.Process.GetProcessById((
int)messageSend.ProcessId);
46 processName = requestProcess.ProcessName;
50 Console.WriteLine(
"Convert process id to process name failed." + ex.Message);
53 Console.WriteLine(
string.Format(
"\r\n\r\nId#{0}", messageSend.MessageId.ToString()));
54 Console.WriteLine(
string.Format(
"TransactionTime:{0}", FormatDateTime(messageSend.TransactionTime)));
55 Console.WriteLine(
string.Format(
"UserName:{0}", userName));
56 Console.WriteLine(
string.Format(
"ProcessName:{0}", processName));
57 Console.WriteLine(
string.Format(
"ProcessId:{0}", messageSend.ProcessId.ToString()));
58 Console.WriteLine(
string.Format(
"ThreadId:{0}", messageSend.ThreadId.ToString()));
59 Console.WriteLine(
string.Format(
"FileObject:{0}", messageSend.FileObject.ToString(
"X")));
60 Console.WriteLine(
string.Format(
"FsContext:{0}", messageSend.FsContext.ToString(
"X")));
61 Console.WriteLine(
string.Format(
"MessageType:{0}", ((FilterAPI.MessageType)messageSend.MessageType).ToString()));
62 Console.WriteLine(
string.Format(
"FileName:{0}", messageSend.FileName));
63 Console.WriteLine(
string.Format(
"FileSize:{0}", messageSend.FileSize.ToString()));
64 Console.WriteLine(
string.Format(
"FileAttributes:{0}", ((FileAttributes)messageSend.FileAttributes).ToString()));
65 Console.WriteLine(
string.Format(
"CreationTime:{0}", FormatDateTime(messageSend.CreationTime)));
66 Console.WriteLine(
string.Format(
"LastWriteTime:{0}", FormatDateTime(messageSend.LastWriteTime)));
68 if (((FilterAPI.MessageType)messageSend.MessageType == FilterAPI.MessageType.PRE_CREATE)
69 || ((FilterAPI.MessageType)messageSend.MessageType == FilterAPI.MessageType.POST_CREATE))
71 Console.WriteLine(
"---------------Start create I/O related information ---------------");
72 Console.WriteLine(
string.Format(
"DesiredAccess:(0x{0:x}){1}", messageSend.DesiredAccess, FormatDesiredAccess(messageSend.DesiredAccess)));
73 Console.WriteLine(
string.Format(
"Disposition:(0x{0:x}){1}", messageSend.Disposition, ((WinData.Disposition)messageSend.Disposition).ToString()));
74 Console.WriteLine(
string.Format(
"SharedAccess:{0}", ((WinData.ShareAccess)messageSend.SharedAccess).ToString()));
75 Console.WriteLine(
string.Format(
"CreateOptions:(0x{0:x}){1}", messageSend.CreateOptions, FormatCreateOptions(messageSend.CreateOptions)));
78 && ((FilterAPI.MessageType)messageSend.MessageType == FilterAPI.MessageType.POST_CREATE))
81 Console.WriteLine(
string.Format(
"CreateStatus:{0}", ((WinData.CreateStatus)messageSend.CreateStatus).ToString()));
84 Console.WriteLine(
"---------------End create I/O related information ---------------");
87 Console.WriteLine(
string.Format(
"I/O return status:{0}", FormatStatus(messageSend.Status)));
89 if (messageSend.InfoClass > 0)
91 Console.WriteLine(
string.Format(
"InfoClass:{0}", FormatInfoClass((FilterAPI.MessageType)messageSend.MessageType, messageSend.InfoClass)));
94 if (messageSend.Length > 0)
96 Console.WriteLine(
string.Format(
"Read/Write Offset:{0}", messageSend.Offset.ToString()));
97 Console.WriteLine(
string.Format(
"Read/Write Length:{0}", messageSend.Length.ToString()));
100 if (messageSend.DataBufferLength > 0)
102 Console.WriteLine(
string.Format(
"Data Buffer Length:{0}", messageSend.DataBufferLength.ToString()));
103 Console.WriteLine(
string.Format(
"Data:{0}", FormatDataBuffer((FilterAPI.MessageType)messageSend.MessageType,
104 (WinData.FileInfomationClass)messageSend.InfoClass,
105 messageSend.DataBufferLength,
106 messageSend.DataBuffer)));
112 Console.WriteLine(
"DisplayFilterMessage failed." + ex.Message);
120 static bool ConvertSidToUserNameAndStringSid(IntPtr sidBuffer, out
string userName, out
string userSid)
124 IntPtr sidStringPtr = IntPtr.Zero;
125 string sidString =
string.Empty;
127 userName =
string.Empty;
128 userSid =
string.Empty;
132 if (FilterAPI.ConvertSidToStringSid(sidBuffer, out sidStringPtr))
134 sidString = Marshal.PtrToStringAuto(sidStringPtr);
135 SecurityIdentifier secIdentifier =
new SecurityIdentifier(sidString);
136 IdentityReference reference = secIdentifier.Translate(typeof(NTAccount));
137 userName = reference.Value;
138 userSid = secIdentifier.ToString();
142 string errorMessage =
"Convert sid to sid string failed, error code:" + Marshal.GetLastWin32Error();
143 Console.WriteLine(errorMessage);
148 string errorMessage =
string.Format(
"Convert sid to user name got exception:{0}", ex.Message);
149 Console.WriteLine(errorMessage);
150 userName = errorMessage;
156 if (sidStringPtr != null && sidStringPtr != IntPtr.Zero)
158 IntPtr res = FilterAPI.LocalFree(sidStringPtr);
165 static string FormatDesiredAccess(uint desiredAccess)
167 string ret =
string.Empty;
169 foreach (WinData.DisiredAccess access in Enum.GetValues(typeof(WinData.DisiredAccess)))
171 if (access == (WinData.DisiredAccess)((uint)access & desiredAccess))
173 ret += access.ToString() +
"; ";
180 static string FormatCreateOptions(uint createOptions)
182 string ret =
string.Empty;
184 foreach (WinData.CreateOptions option in Enum.GetValues(typeof(WinData.CreateOptions)))
186 if (option == (WinData.CreateOptions)((uint)option & createOptions))
188 ret += option.ToString() +
"|";
192 if (
string.IsNullOrEmpty(ret))
194 ret =
"(0x)" + createOptions.ToString(
"X");
200 static string FormatDateTime(
long lDateTime)
209 DateTime dateTime = DateTime.FromFileTime(lDateTime);
210 string ret = dateTime.ToShortDateString() +
" " + dateTime.ToShortTimeString();
215 Console.WriteLine(
"FormatDateTime :" + lDateTime.ToString() +
" failed." + ex.Message);
220 static string FormatDataBuffer(FilterAPI.MessageType messageType, WinData.FileInfomationClass infoClass, uint dataLength, byte[]
data)
222 string ret =
string.Empty;
228 case FilterAPI.MessageType.PRE_SET_INFORMATION:
229 case FilterAPI.MessageType.POST_SET_INFORMATION:
230 case FilterAPI.MessageType.PRE_QUERY_INFORMATION:
231 case FilterAPI.MessageType.POST_QUERY_INFORMATION:
234 case WinData.FileInfomationClass.FileRenameInformation:
237 ret =
"Destination name:" + Encoding.Unicode.GetString(data);
240 case WinData.FileInfomationClass.FileDispositionInformation:
242 ret =
"Delete file.";
245 case WinData.FileInfomationClass.FileEndOfFileInformation:
247 long newFileSize = BitConverter.ToInt64(data, 0);
248 ret =
"Change file size to:" + newFileSize.ToString();
251 case WinData.FileInfomationClass.FileBasicInformation:
253 WinData.FileBasicInformation basiInfo =
new WinData.FileBasicInformation();
254 GCHandle pinnedPacket = GCHandle.Alloc(data, GCHandleType.Pinned);
255 basiInfo = (WinData.FileBasicInformation)Marshal.PtrToStructure(
256 pinnedPacket.AddrOfPinnedObject(), typeof(WinData.FileBasicInformation));
259 ret =
"creation time:" + FormatDateTime(basiInfo.CreationTime) +
" ";
260 ret +=
"last access time:" + FormatDateTime(basiInfo.LastAccessTime) +
" ";
261 ret +=
"last write time:" + FormatDateTime(basiInfo.LastWriteTime) +
" ";
262 ret +=
"file attributes:" + ((FileAttributes)basiInfo.FileAttributes).ToString();
266 case WinData.FileInfomationClass.FileStandardInformation:
268 WinData.FileStandardInformation standardInfo =
new WinData.FileStandardInformation();
269 GCHandle pinnedPacket = GCHandle.Alloc(data, GCHandleType.Pinned);
270 standardInfo = (WinData.FileStandardInformation)Marshal.PtrToStructure(
271 pinnedPacket.AddrOfPinnedObject(), typeof(WinData.FileStandardInformation));
274 ret =
"File size:" + standardInfo.EndOfFile.ToString() +
" ";
275 ret +=
"Allocation size:" + standardInfo.AllocationSize.ToString() +
" ";
276 ret +=
"IsDirectory:" + standardInfo.Directory.ToString();
279 case WinData.FileInfomationClass.FileNetworkOpenInformation:
281 WinData.FileNetworkInformation networkInfo =
new WinData.FileNetworkInformation();
282 GCHandle pinnedPacket = GCHandle.Alloc(data, GCHandleType.Pinned);
283 networkInfo = (WinData.FileNetworkInformation)Marshal.PtrToStructure(
284 pinnedPacket.AddrOfPinnedObject(), typeof(WinData.FileNetworkInformation));
287 ret =
"creation time:" + FormatDateTime(networkInfo.CreationTime) +
" ";
288 ret +=
"last access time:" + FormatDateTime(networkInfo.LastAccessTime) +
" ";
289 ret +=
"last write time:" + FormatDateTime(networkInfo.LastWriteTime) +
" ";
290 ret +=
"file size:" + networkInfo.FileSize.ToString() +
" ";
291 ret +=
"file attributes:" + ((FileAttributes)networkInfo.FileAttributes).ToString();
295 case WinData.FileInfomationClass.FileInternalInformation:
297 long fileId = BitConverter.ToInt64(data, 0);
298 ret =
"FileId: (0x)" + fileId.ToString(
"X");
308 Console.WriteLine(
"Format data failed." + ex.Message);
315 static string FormatStatus(uint status)
317 string ret =
string.Empty;
321 if (status == (uint)ntStatus)
323 ret = ntStatus.ToString() +
"(0x" + status.ToString(
"X") +
")";
327 if (
string.IsNullOrEmpty(ret))
329 ret =
"(0x" + status.ToString(
"X") +
")";
335 static string FormatInfoClass(FilterAPI.MessageType messageType, uint infoClass)
337 string ret =
string.Empty;
339 if (FilterAPI.MessageType.PRE_QUERY_SECURITY == messageType
340 || FilterAPI.MessageType.PRE_SET_SECURITY == messageType
341 || FilterAPI.MessageType.POST_QUERY_SECURITY == messageType
342 || FilterAPI.MessageType.POST_SET_SECURITY == messageType)
344 ret = ((WinData.SecurityInformation)(infoClass)).ToString();
346 else if (infoClass > 0)
348 ret = ((WinData.FileInfomationClass)infoClass).ToString();