-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSend-SYSLOG.ps1
214 lines (186 loc) · 8.37 KB
/
Send-SYSLOG.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
##### Send-SYSLOG (v6) [Function Req: OutCLI function] - Based on https://github.com/poshsecurity/Posh-Syslog
#requires -Version 2 #-Modules NetTCPIP
function Send-SYSLOG
{
<#
Send-SYSLOG -MessageID "SUBJECT" `
-Message "TEST" `
-Severity 'Informational' -Facility 'logaudit' `
-ApplicationName "TestScript" -Server "409.49.4.148" -Hostname "IPOFHOSTRETRIVEDINFOFROM"
#>
[CMDLetBinding(DefaultParameterSetName = 'RFC5424')]
Param
(
#Destination SYSLOG server that message is to be sent to.
[Parameter(mandatory = $true, 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, HelpMessage = 'Message to send')]
[ValidateNotNullOrEmpty()]
[String]$Message,
#Severity level as defined in SYSLOG specification, must be of ENUM type Syslog_Severity
[Parameter(mandatory = $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, HelpMessage = 'Facility sending message')]
[ValidateNotNullOrEmpty()]
[Syslog_Facility]$Facility,
#Hostname of machine the mssage is about, if not specified, RFC 5425 selection rules will be followed.
[Parameter(mandatory = $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)]
[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, 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, 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, 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)]
[ValidateNotNullOrEmpty()]
[DateTime]$Timestamp = (Get-Date),
#SYSLOG UDP port to send message to. Defaults to 514 if not specified.
[Parameter(mandatory = $false)]
[ValidateNotNullOrEmpty()] [ValidateRange(1,65535)]
[UInt16]$UDPPort = 514,
#Send an RFC3164 fomatted message instead of RFC5424.
[Parameter(mandatory = $True, ParameterSetName = 'RFC3164')]
[switch]$RFC3164
)
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
}
enum Syslog_Severity{
Emergency
Alert
Critical
Error
Warning
Notice
Informational
Debug
}
# Evaluate the facility and severity based on the enum types
$Facility_Number = $Facility.value__
$Severity_Number = $Severity.value__
OutCLI -f_mTxt "Syslog Facility, $Facility_Number, Severity is $Severity_Number" -f_mSubj "Send-SYSLOG" -f_Verbose
# Calculate the priority
$Priority = ($Facility_Number * 8) + $Severity_Number
OutCLI -f_mTxt "Priority is $Priority" -f_mSubj "Send-SYSLOG" -f_Verbose
<#
Application name or process name, simply find out if a script is calling the CMDLet, else use PowerShell
#>
if ($ApplicationName -eq '')
{
if (($null -ne $myInvocation.ScriptName) -and ($myInvocation.ScriptName -ne ''))
{
$ApplicationName = Split-Path -Leaf -Path $myInvocation.ScriptName
}
else
{
$ApplicationName = 'PowerShell'
}
}
<#
According to RFC 5424, we need to send our hostname as one of these 5 (in order of priority)
1. FQDN
2. Static IP address
3. Hostname - Windows always has one of these, so this is our last resort
4. Dynamic IP address - We will never get to this one
5. the NILVALUE - or this one
Windows should always, in the worst case, have a result at 3, the hostname or computer name of the system this command is run from.
Taking this into account, then our logic, if no hostname is provided should be:
1. Are we domain joined, if so, we have a FQDN
2. Do we have any static ip allocations and can we ping or at least try to ping the SYSLOG server from an interface with a static ip address
The elseif statement first determines if there are any interfaces with a static address, if there are, it does the ping, otherwise it returns false immediately.
This is due to how PowerShell processes -and/-or/etc logic.
3. The name of the computer
#>
if ($Hostname -eq '')
{
if ($null -ne $ENV:userdnsdomain)
{
# Option 1, FQDN
$Hostname = $ENV:Computername + '.' + $ENV:userdnsdomain
}
elseif (($null -ne (Get-NetIPAddress -PrefixOrigin Manual -SuffixOrigin Manual -ErrorAction SilentlyContinue)) -and ((Test-NetConnection -ComputerName $Server -ErrorAction SilentlyContinue).SourceAddress.PrefixOrigin -eq 'Manual'))
{
# Option 2, Static IP address
# ** Changes here as suggested by Brtlvrs **
$Hostname = (Test-NetConnection -ComputerName $Server -ErrorAction SilentlyContinue).SourceAddress.IPAddress
}
else
{
# Option 3
$Hostname = $ENV:Computernames
}
}
if ($PSCmdlet.ParameterSetName -eq 'RFC3164')
{
OutCLI -f_mTxt 'Using RFC 3164 UNIX/BSD message format' -f_mSubj "Send-SYSLOG" -f_Verbose
#Get the timestamp
$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
}
else
{
OutCLI -f_mTxt 'Using RFC 5424 IETF message format' -f_mSubj "Send-SYSLOG" -f_Verbose
#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
}
OutCLI -f_mTxt "SYSLOG Message = '$FullSyslogMessage'" -f_mSubj "Send-SYSLOG" -f_Verbose
# create an ASCII Encoding object
$Encoding = [System.Text.Encoding]::ASCII
# Convert into byte array representation
$ByteSyslogMessage = $Encoding.GetBytes($FullSyslogMessage)
# If the message is too long, shorten it
if ($ByteSyslogMessage.Length -gt 1024)
{
$ByteSyslogMessage = $Encoding.GetBytes($FullSyslogMessage.SubString(0, 1024))
}
# Create a UDP Client Object
$UDPCLient = New-Object -TypeName System.Net.Sockets.UdpClient
$UDPCLient.Connect($Server, $UDPPort)
# Send the Message
$null = $UDPCLient.Send($ByteSyslogMessage, $ByteSyslogMessage.Length)
#Close the connection
$UDPCLient.Close()
}