@@ -261,20 +261,82 @@ static int pack_filetime(struct winevtlog_config *ctx, ULONGLONG filetime)
261261 return 0 ;
262262}
263263
264- static int pack_sid (struct winevtlog_config * ctx , PSID sid )
264+ static int pack_sid (struct winevtlog_config * ctx , PSID sid , int extract_sid )
265265{
266+ #define MAX_NAME 256
266267 size_t size ;
267268 LPWSTR wide_sid = NULL ;
269+ DWORD len = MAX_NAME , err = ERROR_SUCCESS ;
268270 int ret = -1 ;
271+ SID_NAME_USE sid_type = SidTypeUnknown ;
272+ char account [MAX_NAME ];
273+ char domain [MAX_NAME ];
274+ PSID pSID = NULL ;
275+ DWORD result_len = 0 ;
276+ flb_sds_t formatted = NULL ;
269277
270278 if (ConvertSidToStringSidW (sid , & wide_sid )) {
279+ if (extract_sid == FLB_TRUE ) {
280+ if (!LookupAccountSidA (NULL , sid ,
281+ account , & len , domain ,
282+ & len , & sid_type )) {
283+ err = GetLastError ();
284+ if (err == ERROR_NONE_MAPPED ) {
285+ strcpy_s (account , MAX_NAME , "NONE_MAPPED" );
286+ }
287+ else {
288+ flb_plg_warn (ctx -> ins , "LookupAccountSid Error %u" , err );
289+ }
290+
291+ goto error ;
292+ }
293+
294+ result_len = strlen (domain ) + 1 + strlen (account ) + 1 ;
295+ formatted = flb_sds_create_size (result_len );
296+ if (formatted == NULL ) {
297+ flb_plg_warn (ctx -> ins , "create result buffer failed" );
298+
299+ goto error ;
300+ }
301+
302+ _snprintf_s (formatted , result_len , _TRUNCATE , "%s\\%s" , domain , account );
303+
304+ if (size > 0 ) {
305+ flb_log_event_encoder_append_body_cstring (ctx -> log_encoder , formatted );
306+
307+ ret = 0 ;
308+ }
309+ else {
310+ flb_plg_warn (ctx -> ins , "format domain\\account failed" );
311+ flb_sds_destroy (formatted );
312+
313+ ret = -1 ;
314+
315+ goto error ;
316+ }
317+
318+ LocalFree (wide_sid );
319+ flb_sds_destroy (formatted );
320+
321+ return ret ;
322+ }
323+ else {
324+ ret = pack_wstr (ctx , wide_sid );
325+ LocalFree (wide_sid );
326+
327+ return ret ;
328+ }
329+
330+ error :
271331 ret = pack_wstr (ctx , wide_sid );
272332
273333 LocalFree (wide_sid );
274- return ret ;
334+
335+ return -1 ;
275336 }
276337
277338 return ret ;
339+ #undef MAX_NAME
278340}
279341
280342static void pack_string_inserts (struct winevtlog_config * ctx , PEVT_VARIANT values , DWORD count )
@@ -355,7 +417,7 @@ static void pack_string_inserts(struct winevtlog_config *ctx, PEVT_VARIANT value
355417 }
356418 break ;
357419 case EvtVarTypeSid :
358- if (pack_sid (ctx , values [i ].SidVal )) {
420+ if (pack_sid (ctx , values [i ].SidVal , FLB_FALSE )) {
359421 pack_nullstr (ctx );
360422 }
361423 break ;
@@ -601,7 +663,7 @@ void winevtlog_pack_event(PEVT_VARIANT system, WCHAR *message,
601663 /* UserID */
602664 ret = flb_log_event_encoder_append_body_cstring (ctx -> log_encoder , "UserID" );
603665
604- if (pack_sid (ctx , system [EvtSystemUserID ].SidVal )) {
666+ if (pack_sid (ctx , system [EvtSystemUserID ].SidVal , FLB_TRUE )) {
605667 pack_nullstr (ctx );
606668 }
607669
0 commit comments