|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionSome days back, I was searching for code snippets for opening an event log file. Even though there are a lot of samples available for getting the event log of a local system, there was no help for opening a *.evt file. So, I thought of coming up with this article. Using the codeThe code included uses Reading the event log fileThe structure of an event log file is a little complex. The file has a 48 byte header which we can use to validate it. The bytes 5 - 8 will hold the signature of the file, which is a Each event log record entered in the log file will have the following structure: [StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct EventLogRecord
{
public uint Length;
public uint Reserved;
public uint RecordNumber;
public uint TimeGenerated;
public uint TimeWritten;
public uint EventID;
public ushort EventType;
public ushort NumStrings;
public ushort EventCategory;
public ushort ReservedFlags;
public uint ClosingRecordNumber;
public uint StringOffset;
public uint UserSidLength;
public uint UserSidOffset;
public uint DataLength;
public uint DataOffset;
//
// Followed by:
//
// String SourceName
// String Computername
// SID UserSid
// String[] Description
// byte[] Data
// char[] Pad
// uint Length
//
}
We can clearly see how the structure would be saved in the log file, but this structure is different to many others. Since this particular structure doesn't have a fixed size, it has a fixed part and a variable part. The fixed part occupies 56 bytes, and the variable part has a size which is the difference of the ' The property ' The private string GetUserInfo(byte[] buff)
{
StringBuilder name = new StringBuilder();
uint cchName = (uint)name.Capacity;
StringBuilder referencedDomainName = new StringBuilder();
uint cchReferencedDomainName = (uint)referencedDomainName.Capacity;
SID_NAME_USE sidUse;
int err = NO_ERROR;
if (!LookupAccountSid(null, buff, name, ref cchName,
referencedDomainName, ref cchReferencedDomainName, out sidUse))
{
err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
if (err == ERROR_INSUFFICIENT_BUFFER)
{
name.EnsureCapacity((int)cchName);
referencedDomainName.EnsureCapacity((int)cchReferencedDomainName);
err = NO_ERROR;
if (!LookupAccountSid(null, buff, name, ref cchName,
referencedDomainName,
ref cchReferencedDomainName,
out sidUse))
err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
}
}
if (err == 0)
return String.Format(@"{0}\{1}",
referencedDomainName.ToString(), name.ToString());
else
return @"N\A";
}[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool LookupAccountSid(
string lpSystemName,
[MarshalAs(UnmanagedType.LPArray)] byte[] Sid,
System.Text.StringBuilder lpName,
ref uint cchName,
System.Text.StringBuilder ReferencedDomainName,
ref uint cchReferencedDomainName,
out SID_NAME_USE peUse);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetTimeZoneInformation(out TimeZoneInformation
lpTimeZoneInformation);private DateTime GetTime(uint time)
{
TimeZoneInformation tzi;
uint offset;
GetTimeZoneInformation(out tzi);
offset = (uint)(tzi.bias * 60) - (uint)(tzi.daylightBias * 60);
DateTime output = new DateTime(1970, 1, 1, 0, 0, 0);
time = time - offset;
output = output.AddSeconds(time);
return output;
}
Points of interestIf you have any suggestions or questions, you are welcome! TODO: Use the References
|
||||||||||||||||||||||