Skip to content

Commit cd54540

Browse files
fujimotosedsiper
authored andcommitted
in_winlog: fix "invalid UTF-8 bytes, skipping"
This adds the first cut at a more complete record serializer for Windows Event logs. In particular, this patch implements the message formatting using registry keys. The format of a new data record looks something like this: {"RecordNumber": 408, "TimeGenerated": "2020-06-22 06:26:31 +0900", "TimeWritten": "2020-06-22 06:26:31 +0900", "EventType": "Information", "EventCategory": 0, "Channel": "Application", "SourceName": "Microsoft-Windows-RestartManager", "ComputerName": "winsvr2019", "Data": "", "Sid": "", "Message": "Starting session 0 - 2020-06-22T06: 26:31.710405700Z."} Note that every field is explicitly encoded into UTF-8 - this guarantees that we can always safely convert records into JSON. Signed-off-by: Fujimoto Seiji <[email protected]>
1 parent 7533d1f commit cd54540

File tree

5 files changed

+437
-143
lines changed

5 files changed

+437
-143
lines changed

plugins/in_winlog/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(src
22
in_winlog.c
3+
pack.c
34
winlog.c)
45

56
FLB_PLUGIN(in_winlog "${src}" "advapi32")

plugins/in_winlog/in_winlog.c

Lines changed: 14 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,12 @@
2424
#include <fluent-bit/flb_pack.h>
2525
#include <fluent-bit/flb_utils.h>
2626
#include <fluent-bit/flb_sqldb.h>
27-
#include <sddl.h>
2827
#include "winlog.h"
2928

3029
#define DEFAULT_INTERVAL_SEC 1
3130
#define DEFAULT_INTERVAL_NSEC 0
3231
#define DEFAULT_BUFFER_SIZE 0x7fff /* Max size allowed by Win32 (32kb) */
3332

34-
struct flb_in_winlog_config {
35-
unsigned int interval_sec;
36-
unsigned int interval_nsec;
37-
unsigned int bufsize;
38-
char *buf;
39-
40-
/* Event Log channels */
41-
struct mk_list *active_channel;
42-
43-
/* SQLite DB */
44-
struct flb_sqldb *db;
45-
46-
/* Collector */
47-
flb_pipefd_t coll_fd;
48-
49-
/* Plugin input instance */
50-
struct flb_input_instance *ins;
51-
};
52-
53-
struct flb_input_plugin in_winlog_plugin;
54-
5533
static int in_winlog_collect(struct flb_input_instance *ins,
5634
struct flb_config *config, void *in_context);
5735

@@ -62,10 +40,10 @@ static int in_winlog_init(struct flb_input_instance *in,
6240
const char *tmp;
6341
struct mk_list *head;
6442
struct winlog_channel *ch;
65-
struct flb_in_winlog_config *ctx;
43+
struct winlog_config *ctx;
6644

6745
/* Initialize context */
68-
ctx = flb_calloc(1, sizeof(struct flb_in_winlog_config));
46+
ctx = flb_calloc(1, sizeof(struct winlog_config));
6947
if (!ctx) {
7048
flb_errno();
7149
return -1;
@@ -151,47 +129,12 @@ static int in_winlog_init(struct flb_input_instance *in,
151129
return 0;
152130
}
153131

154-
static int in_winlog_pack_sid(struct flb_in_winlog_config *ctx,
155-
msgpack_packer *ppck, PEVENTLOGRECORD evt)
156-
{
157-
int len;
158-
char *str;
159-
char *sid = (char *) evt + evt->UserSidOffset;
160-
161-
if (!evt->UserSidLength) {
162-
msgpack_pack_str(ppck, 0);
163-
msgpack_pack_str_body(ppck, "", 0);
164-
return 0;
165-
}
166-
167-
if (!ConvertSidToStringSidA(sid, &str)) {
168-
flb_plg_error(ctx->ins, "cannot pack sid (%i)", GetLastError());
169-
msgpack_pack_str(ppck, 0);
170-
msgpack_pack_str_body(ppck, "", 0);
171-
return -1;
172-
}
173-
174-
len = strlen(str);
175-
msgpack_pack_str(ppck, len);
176-
msgpack_pack_str_body(ppck, str, len);
177-
178-
LocalFree(str);
179-
return 0;
180-
}
181-
182-
183132
static int in_winlog_read_channel(struct flb_input_instance *ins,
184-
struct flb_in_winlog_config *ctx,
133+
struct winlog_config *ctx,
185134
struct winlog_channel *ch)
186135
{
187-
int i;
188-
int ret;
189136
unsigned int read;
190-
unsigned int off;
191-
int len;
192-
int len_sn;
193-
int len_cn;
194-
char *p;
137+
char *ptr;
195138
PEVENTLOGRECORD evt;
196139
msgpack_packer mp_pck;
197140
msgpack_sbuffer mp_sbuf;
@@ -209,87 +152,16 @@ static int in_winlog_read_channel(struct flb_input_instance *ins,
209152
msgpack_sbuffer_init(&mp_sbuf);
210153
msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
211154

212-
p = ctx->buf;
213-
while (p < ctx->buf + read) {
214-
evt = (PEVENTLOGRECORD) p;
155+
ptr = ctx->buf;
156+
while (ptr < ctx->buf + read) {
157+
evt = (PEVENTLOGRECORD) ptr;
158+
159+
winlog_pack_event(&mp_pck, evt, ch, ctx);
215160

216-
/* Update the */
217161
ch->record_number = evt->RecordNumber;
218162
ch->time_written = evt->TimeWritten;
219163

220-
/* Initialize local msgpack buffer */
221-
msgpack_pack_array(&mp_pck, 2);
222-
flb_pack_time_now(&mp_pck);
223-
224-
/* Pack the data */
225-
msgpack_pack_map(&mp_pck, 11);
226-
227-
msgpack_pack_str(&mp_pck, 12);
228-
msgpack_pack_str_body(&mp_pck, "RecordNumber", 12);
229-
msgpack_pack_uint32(&mp_pck, evt->RecordNumber);
230-
231-
msgpack_pack_str(&mp_pck, 13);
232-
msgpack_pack_str_body(&mp_pck, "TimeGenerated", 13);
233-
msgpack_pack_uint32(&mp_pck, evt->TimeGenerated);
234-
235-
msgpack_pack_str(&mp_pck, 11);
236-
msgpack_pack_str_body(&mp_pck, "TimeWritten", 11);
237-
msgpack_pack_uint32(&mp_pck, evt->TimeWritten);
238-
239-
msgpack_pack_str(&mp_pck, 7);
240-
msgpack_pack_str_body(&mp_pck, "EventID", 7);
241-
msgpack_pack_uint32(&mp_pck, evt->EventID);
242-
243-
msgpack_pack_str(&mp_pck, 9);
244-
msgpack_pack_str_body(&mp_pck, "EventType", 9);
245-
msgpack_pack_uint16(&mp_pck, evt->EventType);
246-
247-
msgpack_pack_str(&mp_pck, 13);
248-
msgpack_pack_str_body(&mp_pck, "EventCategory", 13);
249-
msgpack_pack_uint16(&mp_pck, evt->EventCategory);
250-
251-
/* Source Name */
252-
msgpack_pack_str(&mp_pck, 10);
253-
msgpack_pack_str_body(&mp_pck, "SourceName", 10);
254-
255-
len_sn = strlen(p + sizeof(EVENTLOGRECORD));
256-
msgpack_pack_str(&mp_pck, len_sn);
257-
msgpack_pack_str_body(&mp_pck, p + sizeof(EVENTLOGRECORD), len_sn);
258-
259-
/* Computer Name */
260-
msgpack_pack_str(&mp_pck, 12);
261-
msgpack_pack_str_body(&mp_pck, "ComputerName", 12);
262-
263-
len_cn = strlen(p + sizeof(EVENTLOGRECORD) + len_sn + 1);
264-
msgpack_pack_str(&mp_pck, len_cn);
265-
msgpack_pack_str_body(&mp_pck, p + sizeof(EVENTLOGRECORD) + len_sn + 1, len_cn);
266-
267-
/* StringInserts */
268-
msgpack_pack_str(&mp_pck, 13);
269-
msgpack_pack_str_body(&mp_pck, "StringInserts", 13);
270-
271-
msgpack_pack_array(&mp_pck, evt->NumStrings);
272-
273-
off = evt->StringOffset;
274-
for (i = 0; i < evt->NumStrings; i++) {
275-
len = strlen(p + off);
276-
msgpack_pack_str(&mp_pck, len);
277-
msgpack_pack_str_body(&mp_pck, p + off , len);
278-
off += len + 1;
279-
}
280-
281-
/* Sid */
282-
msgpack_pack_str(&mp_pck, 3);
283-
msgpack_pack_str_body(&mp_pck, "Sid", 3);
284-
in_winlog_pack_sid(ctx, &mp_pck, evt);
285-
286-
/* Data */
287-
msgpack_pack_str(&mp_pck, 4);
288-
msgpack_pack_str_body(&mp_pck, "Data", 4);
289-
msgpack_pack_bin(&mp_pck, evt->DataLength);
290-
msgpack_pack_bin_body(&mp_pck, p + evt->DataOffset, evt->DataLength);
291-
292-
p += evt->Length;
164+
ptr += evt->Length;
293165
}
294166

295167
if (ctx->db) {
@@ -307,7 +179,7 @@ static int in_winlog_read_channel(struct flb_input_instance *ins,
307179
static int in_winlog_collect(struct flb_input_instance *ins,
308180
struct flb_config *config, void *in_context)
309181
{
310-
struct flb_in_winlog_config *ctx = in_context;
182+
struct winlog_config *ctx = in_context;
311183
struct mk_list *head;
312184
struct winlog_channel *ch;
313185

@@ -320,19 +192,19 @@ static int in_winlog_collect(struct flb_input_instance *ins,
320192

321193
static void in_winlog_pause(void *data, struct flb_config *config)
322194
{
323-
struct flb_in_winlog_config *ctx = data;
195+
struct winlog_config *ctx = data;
324196
flb_input_collector_pause(ctx->coll_fd, ctx->ins);
325197
}
326198

327199
static void in_winlog_resume(void *data, struct flb_config *config)
328200
{
329-
struct flb_in_winlog_config *ctx = data;
201+
struct winlog_config *ctx = data;
330202
flb_input_collector_resume(ctx->coll_fd, ctx->ins);
331203
}
332204

333205
static int in_winlog_exit(void *data, struct flb_config *config)
334206
{
335-
struct flb_in_winlog_config *ctx = data;
207+
struct winlog_config *ctx = data;
336208

337209
if (!ctx) {
338210
return 0;

0 commit comments

Comments
 (0)