Functions/Send-SyslogMessage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
Add-Type -TypeDefinition @"
public enum Syslog_Facility
{
    kern,
    user,
    mail,
    daemon,
    auth,
    syslog,
    lpr,
    news,
    uucp,
    clock,
    authpriv,
    ftp,
    ntp,
    logaudit,
    logalert,
    cron,
    local0,
    local1,
    local2,
    local3,
    local4,
    local5,
    local6,
    local7,
}
"@


Add-Type -TypeDefinition @"
public enum Syslog_Severity
{
    Emergency,
    Alert,
    Critical,
    Error,
    Warning,
    Notice,
    Informational,
    Debug
}
"@


Function Send-SyslogMessage
{
    <#
            .SYNOPSIS
            Sends a SYSLOG message to a server running the SYSLOG daemon
 
            .DESCRIPTION
            Sends a message to a SYSLOG server as defined in RFC 5424 and RFC 3164.
 
            .INPUTS
            TODO: Need to update this
 
            .OUTPUTS
            Nothing is output
 
            .EXAMPLE
            Send-SyslogMessage -Server mySyslogserver -Message 'The server is down!' -Severity Emergency -Facility Mail
            Sends a syslog message to mysyslogserver, saying "server is down", severity emergency and facility is mail
 
            .EXAMPLE
            Send-SyslogMessage -Server mySyslogserver -Message 'The server is up' -Severity Informational -Facility Mail -Transport TCP
            Sends a syslog message to mysyslogserver, using TCP, saying "server is up", severity informational and facility is mail
 
            .NOTES
            NAME: Send-SyslogMessage
            AUTHOR: Kieran Jacobsen (kjacobsen)
                    Jared Poeppelman (powershellshock)
                    Ronald Rink (dfch)
                    Xtrahost
                    Fredruk Furtenbach (flic)
 
            .LINK
            https://github.com/poshsecurity/Posh-Syslog
 
            .LINK
            https://poshsecurity.com
 
    #>

    
    [CMDLetBinding(DefaultParameterSetName = 'RFC5424')]
    Param
    (
        #Destination SYSLOG server that message is to be sent to.
        [Parameter( Mandatory = $true,
                    ValueFromPipelineByPropertyName  = $false,
                    HelpMessage = 'Server to send message to')]
        [ValidateNotNullOrEmpty()]
        [String] 
        $Server,
    
        #Our message or content that we want to send to the server. This is option in RFC 5424, the CMDLet still has this as a madatory parameter, to send no message, simply specifiy '-' (as per RFC).
        [Parameter( Mandatory = $true,
                    ValueFromPipelineByPropertyName  = $true,
                    HelpMessage = 'Message to send')]
        [ValidateNotNullOrEmpty()]
        [String]
        $Message,
    
        #Severity level as defined in SYSLOG specification, must be of ENUM type Syslog_Severity
        [Parameter( Mandatory = $true,
                    ValueFromPipelineByPropertyName  = $true,
                    HelpMessage = 'Messsage severity level')]
        [ValidateNotNullOrEmpty()]
        [Syslog_Severity]
        $Severity,
    
        #Facility of message as defined in SYSLOG specification, must be of ENUM type Syslog_Facility
        [Parameter( Mandatory = $true,
                    ValueFromPipelineByPropertyName  = $true,
                    HelpMessage = 'Facility sending message')]
        [ValidateNotNullOrEmpty()]
        [Syslog_Facility] 
        $Facility,
    
        #Hostname of machine the message is about, if not specified, RFC 5425 selection rules will be followed.
        [Parameter( Mandatory = $false,
                    ValueFromPipelineByPropertyName  = $false)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Hostname,
    
        #Specify the name of the application or script that is sending the mesage. If not specified, will select the ScriptName, or if empty, powershell.exe will be sent. To send Null, specify '-' to meet RFC 5424.
        [Parameter( Mandatory = $false,
                    ValueFromPipelineByPropertyName  = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $ApplicationName,
    
        #ProcessID or PID of generator of message. Will automatically use $PID global variable. If you want to override this and send null, specify '-' to meet RFC 5424 rquirements. This is only sent for RFC 5424 messages.
        [Parameter( Mandatory = $false,
                    ValueFromPipelineByPropertyName  = $true,
                    ParameterSetName = 'RFC5424')]
        [ValidateNotNullOrEmpty()]
        [String]
        $ProcessID = $PID,
    
        #Error message or troubleshooting number associated with the message being sent. If you want to override this and send null, specify '-' to meet RFC 5424 rquirements. This is only sent for RFC 5424 messages.
        [Parameter( Mandatory = $false,
                    ValueFromPipelineByPropertyName  = $true,
                    ParameterSetName = 'RFC5424')]
        [ValidateNotNullOrEmpty()]
        [String]
        $MessageID = '-',
    
        #Key Pairs of structured data as a string as defined in RFC5424. Default will be '-' which means null. This is only sent for RFC 5424 messages.
        [Parameter( Mandatory = $false,
                    ValueFromPipelineByPropertyName  = $true,
                    ParameterSetName = 'RFC5424')]
        [ValidateNotNullOrEmpty()]
        [String]
        $StructuredData = '-',
    
        #Time and date of the message, must be of type DateTime. Correct format will be selected depending on RFC requested. If not specified, will call get-date to get appropriate date time.
        [Parameter( Mandatory = $false,
                    ValueFromPipelineByPropertyName  = $true)]
        [ValidateNotNullOrEmpty()]
        [DateTime] 
        $Timestamp = (Get-Date),
    
        #SYSLOG UDP (or TCP) port to which to send the message. Defaults to 514, if not specified.
        [Parameter( Mandatory = $false,
                    ValueFromPipelineByPropertyName  = $false)]
        [ValidateNotNullOrEmpty()]
        [ValidateRange(1, 65535)]
        [Alias('UDPPort','TCPPort')]
        [UInt16]
        $Port = 514,

        # Transport protocol (TCP or UDP) over which the message will be sent. Default is UDP.
        [Parameter( Mandatory = $false,
                    ValueFromPipelineByPropertyName  = $false)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('UDP','TCP')]
        [String]
        $Transport = 'UDP',

        # Framing method used for the message, default is 'Octet-Counting' (see RFC6587 section 3.4). This only applies when TCP is used for transport (no effect on UDP messages).
        [Parameter( Mandatory = $false,
                    ValueFromPipelineByPropertyName  = $false)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Octet-Counting','Non-Transparent-Framing','None')]
        [String]
        $FramingMethod = 'Octet-Counting',
    
        #Send an RFC3164 fomatted message instead of RFC5424.
        [Parameter( Mandatory = $false,
                    ParameterSetName = 'RFC3164')]
        [switch]
        $RFC3164
    )
    
    Begin 
    {
        Write-Debug -Message 'Starting the BEGIN block...'

        # Create an ASCII Encoding object
        $Encoding = [Text.Encoding]::ASCII

        # Initiate the required network objects
        Switch ($Transport)
        {        
            'UDP' 
            {
                try 
                {
                    $NetworkClient = Connect-UDPClient -Server $Server -Port $Port
                }
                catch
                {
                    throw $_
                }
            }

            'TCP'
            {
                try 
                {
                    $NetworkClient = Connect-TCPClient -Server $Server -Port $Port
                    $TcpWriter = Get-TCPWriter -TcpClient $NetworkClient
                }
                catch
                {
                    throw $_
                }
                
            }
        }
        
        # If the hostname parameter is not specified, then we need to determine the correct value to be sent.
        if (-not $PSBoundParameters.ContainsKey('Hostname'))
        {
            Write-Verbose -Message 'No Hostname value provided, Detecting correct HOSTNAME value...'
            $Hostname = Get-SyslogHostname -Socket $NetworkClient.Client
        }

        # Get the calling script name, if there is one
        if (($null -ne $myInvocation.ScriptName) -and ($myInvocation.ScriptName -ne ''))
        {
            $Caller = Split-Path -Leaf -Path $myInvocation.ScriptName
        }
        else
        {
            $Caller = 'PowerShell'
        }
        
        Write-Debug -Message 'Finished the BEGIN block'     
    }
    
    Process 
    {
        Write-Debug -Message 'Starting the PROCESS block...'

        # Evaluate the facility and severity based on the enum types
        $Facility_Number = $Facility.value__
        $Severity_Number = $Severity.value__
        Write-Verbose -Message ('Syslog Facility value is {0}, Severity value is {1}' -f $Facility_Number, $Severity_Number)

        # Calculate the PRI
        $Priority = ($Facility_Number * 8) + $Severity_Number
        Write-Verbose -Message ('Priority (PRI) is {0}' -f $Priority)

        # Set the APP-NAME
        if (-not $PSBoundParameters.ContainsKey('ApplicationName'))
        {
            Write-Verbose -Message ('No APP-NAME value was provided by caller, using previously detected value: {0}'-f $ApplicationName)
            $ApplicationName = $Caller
        }

        Switch ($PSCmdlet.ParameterSetName)
        {
            'RFC3164' 
            {
                Write-Verbose -Message 'Using RFC 3164 message format. Maxmimum length of 1024 bytes (section 4.1)'

                #Get the timestamp
                <#
                    The TIMESTAMP field is the local time and is in the format of "Mmm dd
                    hh:mm:ss" (without the quote marks) where:
                    ...
                    dd is the day of the month. If the day of the month is less
                    than 10, then it MUST be represented as a space and then the
                    number. For example, the 7th day of August would be
                    represented as "Aug 7", with two spaces between the "g" and
                    the "7".
                #>

                
                if ($Timestamp.Day.tostring().length -eq 1) {
                    $FormattedTimestamp = (Get-Culture).TextInfo.ToTitleCase($Timestamp.ToString('MMM d HH:mm:ss'))
                }
                else
                {
                    $FormattedTimestamp = (Get-Culture).TextInfo.ToTitleCase($Timestamp.ToString('MMM dd HH:mm:ss'))
                }

                # Assemble the full syslog formatted Message
                $FullSyslogMessage = '<{0}>{1} {2} {3} {4}' -f $Priority, $FormattedTimestamp, $Hostname, $ApplicationName, $Message
                
                # Set the max message length per RFC 3164 section 4.1
                $MaxLength = 1024
            }

            'RFC5424'
            {
                Write-Verbose -Message 'Using RFC 5424 message format. Maxmimum length of 2048 bytes.'
            
                #Get the timestamp
                $FormattedTimestamp = $Timestamp.ToString('yyyy-MM-ddTHH:mm:ss.ffffffzzz')
                
                # Assemble the full syslog formatted Message
                $FullSyslogMessage = '<{0}>1 {1} {2} {3} {4} {5} {6} {7}' -f $Priority, $FormattedTimestamp, $Hostname, $ApplicationName, $ProcessID, $MessageID, $StructuredData, $Message
                
                # Set the max message length per RFC 5424 section 6.1
                $MaxLength = 2048
            }
        }
        
        Write-Verbose -Message ('Message being attempted is: {0}' -f $FullSyslogMessage)
        
        # Ensure that the message is not too long. We could just compare the strings length, however using the encoding is the more appropriate way of confirming the length in bytes.
        if ($Encoding.GetByteCount($FullSyslogMessage) -gt $MaxLength)
        {
            $FullSyslogMessage = $FullSyslogMessage.Substring(0,$MaxLength)
            Write-Verbose -Message ('Message was too long and was shortened to {0} characters' -f $MaxLength)
            Write-Verbose -Message ('Shortened message is: {0}' -f $FullSyslogMessage)
        }

        Switch ($Transport)
        {
            'UDP' 
            {
                # Convert into byte array representation
                $ByteSyslogMessage = $Encoding.GetBytes($FullSyslogMessage)

                # Send the Message
                Try 
                {
                    Send-UDPMessage -UdpClient $NetworkClient -Datagram $ByteSyslogMessage
                }
                Catch 
                {
                    If ($null -ne $NetworkClient.client)
                    {
                        Write-Verbose -Message 'Cleaning up the UDP client object'
                        Disconnect-UDPClient -UdpClient $NetworkClient
                    }
                    throw $_
                }
            }

            'TCP'
            {
                Write-Verbose -Message ('Framing method is: {0}' -f $FramingMethod)
                Switch ($FramingMethod) 
                {  
                    'Octet-Counting' 
                    { 
                        $OctetCount = ($Encoding.GetBytes($FullSyslogMessage)).Length
                        $FramedSyslogMessage = '{0} {1}' -f $OctetCount, $FullSyslogMessage
                        Write-Verbose -Message ('Octet-Counting - Framed message is: {0}' -f $FullSyslogMessage)
                    }

                    'Non-Transparent-Framing' 
                    {
                        $FramedSyslogMessage = '{0}{1}' -f $FullSyslogMessage, "`n"
                        Write-Verbose -Message ('Non-Transparent-Framing - Framed message is: {0}' -f $FullSyslogMessage)
                    }
                    
                    'None' 
                    {
                        $FramedSyslogMessage = $FullSyslogMessage
                        Write-Verbose -Message "No change to the message for framing type 'none'"  
                    }
                }
                
                # Convert into byte array representation
                $ByteSyslogMessage = $Encoding.GetBytes($FramedSyslogMessage)

                # Send the Message
                Try 
                {
                    Send-TCPMessage -TCPWriter $TcpWriter -Datagram $ByteSyslogMessage
                }
                Catch 
                {
                    If ($null -ne $TcpStream)
                    {
                        Write-Verbose -Message 'Cleaning up the TCP writer object'
                        Disconnect-TCPWriter -TCPWriter $TcpWriter
                    }

                    If ($null -ne $NetworkClient.client)
                    {
                        Write-Verbose -Message 'Cleaning up the TCP client object'
                        Disconnect-TCPClient -TcpClient $NetworkClient
                    }

                    throw $_
                }
            }
        }

        Write-Debug -Message 'Finished the PROCESS block' 
    }
    
    End 
    {
        Write-Debug -Message 'Starting the END block...'
        
        # Clean up our network objects
        Switch ($Transport)
        {
            'UDP' 
            {
                If ($null -ne $NetworkClient.client)
                {
                     Write-Verbose -Message 'Cleaning up the UDP client object'
                    Disconnect-UDPClient -UdpClient $NetworkClient
                }
            }          
            'TCP'
            {               
                If ($null -ne $TcpWriter)
                {
                    Write-Verbose -Message 'Cleaning up the TCP writer object'
                    Disconnect-TCPWriter -TCPWriter $TcpWriter
                }

                If ($null -ne $NetworkClient.client)
                {
                    Write-Verbose -Message 'Cleaning up the TCP client object'
                    Disconnect-TCPClient -TcpClient $NetworkClient
                }
            }
        }
        Write-Debug -Message 'Finished the END block' 
    }
}