Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query: Implement to traverse feature for stored evtx files #45

Merged
merged 2 commits into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions ext/winevt/winevt_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ static VALUE
rb_winevt_query_initialize(VALUE argc, VALUE *argv, VALUE self)
{
PWSTR evtChannel, evtXPath;
VALUE channel, xpath, session;
VALUE channel, xpath, session, rb_flags;
struct WinevtQuery* winevtQuery;
struct WinevtSession* winevtSession;
EVT_HANDLE hRemoteHandle = NULL;
DWORD len;
DWORD len, flags = 0;
VALUE wchannelBuf, wpathBuf;
DWORD err = ERROR_SUCCESS;

rb_scan_args(argc, argv, "21", &channel, &xpath, &session);
rb_scan_args(argc, argv, "22", &channel, &xpath, &session, &rb_flags);
Check_Type(channel, T_STRING);
Check_Type(xpath, T_STRING);

Expand All @@ -111,6 +111,17 @@ rb_winevt_query_initialize(VALUE argc, VALUE *argv, VALUE self)
}
}

switch (TYPE(rb_flags)) {
case T_FIXNUM:
flags = NUM2LONG(rb_flags);
break;
case T_NIL:
flags = EvtQueryChannelPath | EvtQueryTolerateQueryErrors;
break;
default:
rb_raise(rb_eArgError, "Expected a String, a Symbol, a Fixnum, or a NilClass instance");
}

// channel : To wide char
len =
MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(channel), RSTRING_LEN(channel), NULL, 0);
Expand All @@ -128,7 +139,7 @@ rb_winevt_query_initialize(VALUE argc, VALUE *argv, VALUE self)
TypedData_Get_Struct(self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

winevtQuery->query = EvtQuery(
hRemoteHandle, evtChannel, evtXPath, EvtQueryChannelPath | EvtQueryTolerateQueryErrors);
hRemoteHandle, evtChannel, evtXPath, flags);
err = GetLastError();
if (err != ERROR_SUCCESS) {
if (err == ERROR_EVT_CHANNEL_NOT_FOUND) {
Expand Down Expand Up @@ -613,6 +624,37 @@ Init_winevt_query(VALUE rb_cEventLog)
* @see https://msdn.microsoft.com/en-us/windows/desktop/aa385575#EvtSeekStrict
*/
rb_define_const(rb_cFlag, "Strict", LONG2NUM(EvtSeekStrict));

/*
* EVT_QUERY_FLAGS enumeration: EvtQueryChannelPath
* @since 0.10.3
* @see https://learn.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_query_flags
*/
rb_define_const(rb_cFlag, "ChannelPath", LONG2NUM(EvtQueryChannelPath));
/*
* EVT_QUERY_FLAGS enumeration: EvtQueryFilePath
* @since 0.10.3
* @see https://learn.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_query_flags
*/
rb_define_const(rb_cFlag, "FilePath", LONG2NUM(EvtQueryFilePath));
/*
* EVT_QUERY_FLAGS enumeration: EvtQueryForwardDirection
* @since 0.10.3
* @see https://learn.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_query_flags
*/
rb_define_const(rb_cFlag, "ForwardDirection", LONG2NUM(EvtQueryForwardDirection));
/*
* EVT_QUERY_FLAGS enumeration: EvtQueryReverseDirection
* @since 0.10.3
* @see https://learn.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_query_flags
*/
rb_define_const(rb_cFlag, "ReverseDirection", LONG2NUM(EvtQueryReverseDirection));
/*
* EVT_QUERY_FLAGS enumeration: EvtSeekOriginMask
* @since 0.10.3
* @see https://learn.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_query_flags
*/
rb_define_const(rb_cFlag, "TolerateQueryErrors", LONG2NUM(EvtQueryTolerateQueryErrors));
/* clang-format on */

rb_define_method(rb_cQuery, "initialize", rb_winevt_query_initialize, -1);
Expand Down
Loading