Trying to create a custom log that contains a JSON object


#1 jonwalz

The endpoint I will be sending logs to expects this format:

{ "timestamp":"2019-08-06T19:00:03Z", "body":"This is a sample body", "attributes":{ "token":"some token value", "log_type":"error", "(extensible keys)":"(string value)" } }

I need to convert Windows eventlogs to the target format and I'm having an issue creating "attributes." Here's my latest attempt, but "attributes" is getting converted to JSON twice which is messing up the format.

Here's my .conf:

Panic Soft #NoFreeOnExit TRUE

define ROOT C:\Program Files (x86)\nxlog define CERTDIR %ROOT%\cert define CONFDIR %ROOT%\conf define LOGDIR %ROOT%\data define LOGFILE %LOGDIR%\nxlog.log LogFile %LOGFILE%

Moduledir %ROOT%\modules CacheDir %ROOT%\data Pidfile %ROOT%\data\nxlog.pid SpoolDir %ROOT%\data

<Extension json> Module xm_json </Extension>

<Extension _exec> Module xm_exec </Extension>

<Input in> Module im_msvistalog <QueryXML> <QueryList> <Query Id='1'> <Select Path='Application'></Select> <Select Path='Security'></Select> </Query> </QueryList> </QueryXML> <Exec>

#$attributes = $raw_event;
$log_type = $EventType;
$token = 'token goes here';
$raw_event = $raw_event + $log_type + $token;
$attributes = to_json();
rename_field(&quot;EventTime&quot;, &quot;timestamp&quot;);
$body = $message;
delete($log_type);
delete($token);
delete($AccountName);
delete($AccountType);
delete($ActivityID);
delete($Category);
delete($Channel);
delete($Domain);
delete($EventID);
delete($EventTime);
delete($EventType);
delete($HandleId);
delete($Hostname);
delete($Keywords);
delete($MandatoryLabel);
delete($Message);
delete($NewProcessId);
delete($NewProcessName);
delete($ObjectServer);
delete($Opcode);
delete($OpcodeValue);
delete($ParentProcessName);
delete($PrivilegeList);
delete($ProcessName);
delete($ProcessID);
delete($ProviderGuid);
delete($RecordNumber);
delete($RelatedActivityID);
delete($Service);
delete($Severity);
delete($SeverityValue);
delete($SourceName);
delete($SubjectUserSid);
delete($SubjectUserName);
delete($SubjectDomainName);
delete($SubjectLogonId);
delete($Task);
delete($ThreadID);
delete($TargetUserSid);
delete($TargetUserSid);
delete($TargetUserName);
delete($TargetDomainName);
delete($TargetLogonId);
delete($TokenElevationType);
delete($TransactionId);
delete($UserID);
delete($Version);
delete($EventReceivedTime);
delete($SourceModuleName);
delete($SourceModuleType);
to_json();

</Exec> </Input>

<Output file> Module om_file file "c:\temp\logdata.log" </Output>

<Route 2> Path in => file </Route>

My results end up looking like this:

{ "timestamp": "2019-08-27 15:45:39", "attributes": "{&quot;EventTime&quot;:&quot;2019-08-27 15:45:39&quot;,&quot;Hostname&quot;:&quot;fakehost&quot;,&quot;Keywords&quot;:-9214364837600034816,&quot;EventType&quot;:&quot;AUDIT_SUCCESS&quot;,&quot;SeverityValue&quot;:2,&quot;Severity&quot;:&quot;INFO&quot;,&quot;EventID&quot;:4688,&quot;SourceName&quot;:&quot;Microsoft-Windows-Security-Auditing&quot;,&quot;ProviderGuid&quot;:&quot;{guid}&quot;,&quot;Version&quot;:2,&quot;Task&quot;:13312,&quot;OpcodeValue&quot;:0,&quot;RecordNumber&quot;:30506980,&quot;ProcessID&quot;:4,&quot;ThreadID&quot;:19308,&quot;Channel&quot;:&quot;Security&quot;,&quot;Message&quot;:&quot;A new process has been created.\r\n\r\nCreator Subject:\r\n\tSecurity ID:\t\tS-1-5-18\r\n\tAccount Name:\t\fake$\r\n\tAccount Domain:\t\tFake\r\n\tLogon ID:\t\FakeID\r\n\r\nTarget Subject:\r\n\tSecurity ID:\t\tS-1-0-0\r\n\tAccount Name:\t\t-\r\n\tAccount Domain:\t\t-\r\n\tLogon ID:\t\t0x0\r\n\r\nProcess Information:\r\n\tNew Process ID:\t\t0xc0c\r\n\tNew Process Name:\tC:\\Windows\\SysWOW64\\cmd.exe\r\n\tToken Elevation Type:\t%%1936\r\n\tMandatory Label:\t\tS-1-16-16384\r\n\tCreator Process ID:\t0x30f8\r\n\tCreator Process Name:\tC:\\Program Files (x86)\\Stackdriver\\LoggingAgent\\Main\\bin\\ruby.exe\r\n\tProcess Command Line:\t\r\n\r\nToken Elevation Type indicates the type of token that was assigned to the new process in accordance with User Account Control policy.\r\n\r\nType 1 is a full token with no privileges removed or groups disabled. A full token is only used if User Account Control is disabled or if the user is the built-in Administrator account or a service account.\r\n\r\nType 2 is an elevated token with no privileges removed or groups disabled. An elevated token is used when User Account Control is enabled and the user chooses to start the program using Run as administrator. An elevated token is also used when an application is configured to always require administrative privilege or to always require maximum privilege, and the user is a member of the Administrators group.\r\n\r\nType 3 is a limited token with administrative privileges removed and administrative groups disabled. The limited token is used when User Account Control is enabled, the application does not require administrative privilege, and the user does not choose to start the program using Run as administrator.&quot;,&quot;Category&quot;:&quot;Process Creation&quot;,&quot;Opcode&quot;:&quot;Info&quot;,&quot;SubjectUserSid&quot;:&quot;S-1-5-18&quot;,&quot;SubjectUserName&quot;:&quot;FakeUser$&quot;,&quot;SubjectDomainName&quot;:&quot;FakeDomain&quot;,&quot;SubjectLogonId&quot;:&quot;0x3e7&quot;,&quot;NewProcessId&quot;:&quot;0xc0c&quot;,&quot;NewProcessName&quot;:&quot;C:\\Windows\\SysWOW64\\cmd.exe&quot;,&quot;TokenElevationType&quot;:&quot;%%1936&quot;,&quot;TargetUserSid&quot;:&quot;S-1-0-0&quot;,&quot;TargetUserName&quot;:&quot;-&quot;,&quot;TargetDomainName&quot;:&quot;-&quot;,&quot;TargetLogonId&quot;:&quot;0x0&quot;,&quot;ParentProcessName&quot;:&quot;C:\\Program Files (x86)\\Stackdriver\\LoggingAgent\\Main\\bin\\ruby.exe&quot;,&quot;MandatoryLabel&quot;:&quot;S-1-16-16384&quot;,&quot;EventReceivedTime&quot;:&quot;2019-08-27 15:45:41&quot;,&quot;SourceModuleName&quot;:&quot;in&quot;,&quot;SourceModuleType&quot;:&quot;im_msvistalog&quot;,&quot;log_type&quot;:&quot;AUDIT_SUCCESS&quot;,&quot;token&quot;:&quot;token goes here&quot;}", "body": "A new process has been created.\r\n\r\nCreator Subject:\r\n\tSecurity ID:\t\tS-1-5-18\r\n\tAccount Name:\t\Fake$\r\n\tAccount Domain:\t\tFake\r\n\tLogon ID:\t\t0x3E7\r\n\r\nTarget Subject:\r\n\tSecurity ID:\t\tS-1-0-0\r\n\tAccount Name:\t\t-\r\n\tAccount Domain:\t\t-\r\n\tLogon ID:\t\t0x0\r\n\r\nProcess Information:\r\n\tNew Process ID:\t\t0xc0c\r\n\tNew Process Name:\tC:\Windows\SysWOW64\cmd.exe\r\n\tToken Elevation Type:\t%%1936\r\n\tMandatory Label:\t\tS-1-16-16384\r\n\tCreator Process ID:\t0x30f8\r\n\tCreator Process Name:\tC:\Program Files (x86)\Stackdriver\LoggingAgent\Main\bin\ruby.exe\r\n\tProcess Command Line:\t\r\n\r\nToken Elevation Type indicates the type of token that was assigned to the new process in accordance with User Account Control policy.\r\n\r\nType 1 is a full token with no privileges removed or groups disabled. A full token is only used if User Account Control is disabled or if the user is the built-in Administrator account or a service account.\r\n\r\nType 2 is an elevated token with no privileges removed or groups disabled. An elevated token is used when User Account Control is enabled and the user chooses to start the program using Run as administrator. An elevated token is also used when an application is configured to always require administrative privilege or to always require maximum privilege, and the user is a member of the Administrators group.\r\n\r\nType 3 is a limited token with administrative privileges removed and administrative groups disabled. The limited token is used when User Account Control is enabled, the application does not require administrative privilege, and the user does not choose to start the program using Run as administrator." }

How can I get "attributes" to be a regular JSON object?

Thanks!

#2 Zhengshi Nxlog ✓
#1 jonwalz
The endpoint I will be sending logs to expects this format: { "timestamp":"2019-08-06T19:00:03Z", "body":"This is a sample body", "attributes":{ "token":"some token value", "log_type":"error", "(extensible keys)":"(string value)" } } I need to convert Windows eventlogs to the target format and I'm having an issue creating "attributes." Here's my latest attempt, but "attributes" is getting converted to JSON twice which is messing up the format. Here's my .conf: Panic Soft #NoFreeOnExit TRUE define ROOT C:\Program Files (x86)\nxlog define CERTDIR %ROOT%\cert define CONFDIR %ROOT%\conf define LOGDIR %ROOT%\data define LOGFILE %LOGDIR%\nxlog.log LogFile %LOGFILE% Moduledir %ROOT%\modules CacheDir %ROOT%\data Pidfile %ROOT%\data\nxlog.pid SpoolDir %ROOT%\data <Extension json> Module xm_json </Extension> <Extension _exec> Module xm_exec </Extension> <Input in> Module im_msvistalog <QueryXML> <QueryList> <Query Id='1'> <Select Path='Application'></Select> <Select Path='Security'></Select> </Query> </QueryList> </QueryXML> <Exec> #$attributes = $raw_event; $log_type = $EventType; $token = 'token goes here'; $raw_event = $raw_event + $log_type + $token; $attributes = to_json(); rename_field(&quot;EventTime&quot;, &quot;timestamp&quot;); $body = $message; delete($log_type); delete($token); delete($AccountName); delete($AccountType); delete($ActivityID); delete($Category); delete($Channel); delete($Domain); delete($EventID); delete($EventTime); delete($EventType); delete($HandleId); delete($Hostname); delete($Keywords); delete($MandatoryLabel); delete($Message); delete($NewProcessId); delete($NewProcessName); delete($ObjectServer); delete($Opcode); delete($OpcodeValue); delete($ParentProcessName); delete($PrivilegeList); delete($ProcessName); delete($ProcessID); delete($ProviderGuid); delete($RecordNumber); delete($RelatedActivityID); delete($Service); delete($Severity); delete($SeverityValue); delete($SourceName); delete($SubjectUserSid); delete($SubjectUserName); delete($SubjectDomainName); delete($SubjectLogonId); delete($Task); delete($ThreadID); delete($TargetUserSid); delete($TargetUserSid); delete($TargetUserName); delete($TargetDomainName); delete($TargetLogonId); delete($TokenElevationType); delete($TransactionId); delete($UserID); delete($Version); delete($EventReceivedTime); delete($SourceModuleName); delete($SourceModuleType); to_json(); </Exec> </Input> <Output file> Module om_file file "c:\temp\logdata.log" </Output> <Route 2> Path in => file </Route> My results end up looking like this: { "timestamp": "2019-08-27 15:45:39", "attributes": "{&quot;EventTime&quot;:&quot;2019-08-27 15:45:39&quot;,&quot;Hostname&quot;:&quot;fakehost&quot;,&quot;Keywords&quot;:-9214364837600034816,&quot;EventType&quot;:&quot;AUDIT_SUCCESS&quot;,&quot;SeverityValue&quot;:2,&quot;Severity&quot;:&quot;INFO&quot;,&quot;EventID&quot;:4688,&quot;SourceName&quot;:&quot;Microsoft-Windows-Security-Auditing&quot;,&quot;ProviderGuid&quot;:&quot;{guid}&quot;,&quot;Version&quot;:2,&quot;Task&quot;:13312,&quot;OpcodeValue&quot;:0,&quot;RecordNumber&quot;:30506980,&quot;ProcessID&quot;:4,&quot;ThreadID&quot;:19308,&quot;Channel&quot;:&quot;Security&quot;,&quot;Message&quot;:&quot;A new process has been created.\r\n\r\nCreator Subject:\r\n\tSecurity ID:\t\tS-1-5-18\r\n\tAccount Name:\t\fake$\r\n\tAccount Domain:\t\tFake\r\n\tLogon ID:\t\FakeID\r\n\r\nTarget Subject:\r\n\tSecurity ID:\t\tS-1-0-0\r\n\tAccount Name:\t\t-\r\n\tAccount Domain:\t\t-\r\n\tLogon ID:\t\t0x0\r\n\r\nProcess Information:\r\n\tNew Process ID:\t\t0xc0c\r\n\tNew Process Name:\tC:\\Windows\\SysWOW64\\cmd.exe\r\n\tToken Elevation Type:\t%%1936\r\n\tMandatory Label:\t\tS-1-16-16384\r\n\tCreator Process ID:\t0x30f8\r\n\tCreator Process Name:\tC:\\Program Files (x86)\\Stackdriver\\LoggingAgent\\Main\\bin\\ruby.exe\r\n\tProcess Command Line:\t\r\n\r\nToken Elevation Type indicates the type of token that was assigned to the new process in accordance with User Account Control policy.\r\n\r\nType 1 is a full token with no privileges removed or groups disabled. A full token is only used if User Account Control is disabled or if the user is the built-in Administrator account or a service account.\r\n\r\nType 2 is an elevated token with no privileges removed or groups disabled. An elevated token is used when User Account Control is enabled and the user chooses to start the program using Run as administrator. An elevated token is also used when an application is configured to always require administrative privilege or to always require maximum privilege, and the user is a member of the Administrators group.\r\n\r\nType 3 is a limited token with administrative privileges removed and administrative groups disabled. The limited token is used when User Account Control is enabled, the application does not require administrative privilege, and the user does not choose to start the program using Run as administrator.&quot;,&quot;Category&quot;:&quot;Process Creation&quot;,&quot;Opcode&quot;:&quot;Info&quot;,&quot;SubjectUserSid&quot;:&quot;S-1-5-18&quot;,&quot;SubjectUserName&quot;:&quot;FakeUser$&quot;,&quot;SubjectDomainName&quot;:&quot;FakeDomain&quot;,&quot;SubjectLogonId&quot;:&quot;0x3e7&quot;,&quot;NewProcessId&quot;:&quot;0xc0c&quot;,&quot;NewProcessName&quot;:&quot;C:\\Windows\\SysWOW64\\cmd.exe&quot;,&quot;TokenElevationType&quot;:&quot;%%1936&quot;,&quot;TargetUserSid&quot;:&quot;S-1-0-0&quot;,&quot;TargetUserName&quot;:&quot;-&quot;,&quot;TargetDomainName&quot;:&quot;-&quot;,&quot;TargetLogonId&quot;:&quot;0x0&quot;,&quot;ParentProcessName&quot;:&quot;C:\\Program Files (x86)\\Stackdriver\\LoggingAgent\\Main\\bin\\ruby.exe&quot;,&quot;MandatoryLabel&quot;:&quot;S-1-16-16384&quot;,&quot;EventReceivedTime&quot;:&quot;2019-08-27 15:45:41&quot;,&quot;SourceModuleName&quot;:&quot;in&quot;,&quot;SourceModuleType&quot;:&quot;im_msvistalog&quot;,&quot;log_type&quot;:&quot;AUDIT_SUCCESS&quot;,&quot;token&quot;:&quot;token goes here&quot;}", "body": "A new process has been created.\r\n\r\nCreator Subject:\r\n\tSecurity ID:\t\tS-1-5-18\r\n\tAccount Name:\t\Fake$\r\n\tAccount Domain:\t\tFake\r\n\tLogon ID:\t\t0x3E7\r\n\r\nTarget Subject:\r\n\tSecurity ID:\t\tS-1-0-0\r\n\tAccount Name:\t\t-\r\n\tAccount Domain:\t\t-\r\n\tLogon ID:\t\t0x0\r\n\r\nProcess Information:\r\n\tNew Process ID:\t\t0xc0c\r\n\tNew Process Name:\tC:\Windows\SysWOW64\cmd.exe\r\n\tToken Elevation Type:\t%%1936\r\n\tMandatory Label:\t\tS-1-16-16384\r\n\tCreator Process ID:\t0x30f8\r\n\tCreator Process Name:\tC:\Program Files (x86)\Stackdriver\LoggingAgent\Main\bin\ruby.exe\r\n\tProcess Command Line:\t\r\n\r\nToken Elevation Type indicates the type of token that was assigned to the new process in accordance with User Account Control policy.\r\n\r\nType 1 is a full token with no privileges removed or groups disabled. A full token is only used if User Account Control is disabled or if the user is the built-in Administrator account or a service account.\r\n\r\nType 2 is an elevated token with no privileges removed or groups disabled. An elevated token is used when User Account Control is enabled and the user chooses to start the program using Run as administrator. An elevated token is also used when an application is configured to always require administrative privilege or to always require maximum privilege, and the user is a member of the Administrators group.\r\n\r\nType 3 is a limited token with administrative privileges removed and administrative groups disabled. The limited token is used when User Account Control is enabled, the application does not require administrative privilege, and the user does not choose to start the program using Run as administrator." } How can I get "attributes" to be a regular JSON object? Thanks!

You are probably wanting to use Nested JSON.

Try setting fields with the following style:

$attributes.token = "Value"
$attributes.log_type = "error"

https://nxlog.co/documentation/nxlog-user-guide/xm_json.html