forked from blake-fm/vcenter-log4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog4j-vcenter-6.5-7.0-workaround.sh
312 lines (295 loc) · 11.9 KB
/
log4j-vcenter-6.5-7.0-workaround.sh
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
# Applies recommendations in: https://kb.vmware.com/s/article/87081
# Should work for all versions, tested on 6.7 and 7.0 U3b
#
# Apply patch: cve-workaround
# Other documentation: https://github.com/blake-fm/vcenter-log4j
#
# Code style is awful; I'm aware. It was fast and my bash is rusty.
#
# - Blake Marlow
vCenterVersion=$(vpxd -v | sed -r 's/.+ ([0-9]\.[0-9]).*/\1/')
cve-workaround () {
if [ "$1" == "-rollback" ]; then
printf "Rollback - are you sure you want to do this? [y/n] "
read userInput
[ ${userInput::1} == "y" ] || [ ${userInput::1} == "Y" ] && internal-rollback
return 0
elif [ "$1" != "-v" ]; then
internal-apply-workaround "$1"
fi
internal-verify-workaround
}
internal-apply-workaround () {
# vMON Service
currService="vMON Service"
currFile="/usr/lib/vmware-vmon/java-wrapper-vmon"
if internal-target-file-exists; then
if [ $(grep '^log4j_arg="-Dlog4j2.formatMsgNoLookups=true"$' $currFile | wc -l) -ne 0 ]; then
logmsg -skip
elif internal-create-backup-and-verify; then
if grep '^exec $java_start_bin $jvm_dynargs $security_dynargs $original_args$' $currFile.bak > /dev/null; then
sed -e 's/^exec $java_start_bin $jvm_dynargs $security_dynargs $original_args$/# Workaround CVE-2021-44228\nlog4j_arg="-Dlog4j2.formatMsgNoLookups=true"\nexec \$java_start_bin \$jvm_dynargs \$log4j_arg \$security_dynargs \$original_args\n# END Workaround CVE-2021-44228/' $currFile.bak > $currFile
fi
if grep '^exec $java_start_bin $jvm_dynargs "$@"$' $currFile.bak > /dev/null; then
sed -e 's/^exec $java_start_bin $jvm_dynargs "$@"$/# Workaround CVE-2021-44228\nlog4j_arg="-Dlog4j2.formatMsgNoLookups=true"\nexec \$java_start_bin \$jvm_dynargs \$log4j_arg "\$@"\n# END Workaround\n/' $currFile.bak > $currFile
fi
logmsg -big "Stopping $currService - this will take a while..."
service-control --stop --all
logmsg -big "Services stopped, restarting - this will take even longer..."
service-control --start --all
fi
fi
# Update Manager Service
if [ $vCenterVersion == '7.0' ]; then
currService="Update Manager Service"
currFile="/usr/lib/vmware-updatemgr/bin/jetty/start.ini"
if internal-target-file-exists; then
if [ $(grep 'Dlog4j2\.formatMsgNoLookups=true' $currFile | wc -l) -ne 0 ]; then
logmsg -skip
else
internal-create-backup-and-verify && echo '# Workaround CVE-2021-44228
-Dlog4j2.formatMsgNoLookups=true
# END WORKAROUND' >> $currFile
internal-restart-service vmware-updatemgr
fi
fi
fi
# Analytics Service
currService="Analytics Service"
currFile="/usr/lib/vmware/common-jars/log4j-core-2.8.2.jar"
if internal-target-file-exists; then
if [ $(grep -i jndilookup $currFile | wc -l) -eq 0 ]; then
logmsg -skip
else
internal-create-backup-and-verify && zip -q -d $currFile org/apache/logging/log4j/core/lookup/JndiLookup.class
internal-restart-service vmware-analytics
fi
elif [ $vCenterVersion == '6.7' ]; then
logmsg -warn "6.7 detected: Certain versions of 6.7 do not require this workaround. VMware have changed their minds at least 3 times in 24hrs on this - check KB87081 for guidance. Sorry. =/"
fi
# DBCC Utility
if [ $vCenterVersion == '7.0' ]; then
currService="DBCC Utility"
currFile="/usr/lib/vmware-dbcc/lib/log4j-core-2.8.2.jar"
if internal-target-file-exists; then
if [ $(grep -i jndilookup $currFile | wc -l) -eq 0 ]; then
logmsg -skip
else
internal-create-backup-and-verify && zip -q -d $currFile org/apache/logging/log4j/core/lookup/JndiLookup.class
flog "$currService" "No restart required."
fi
fi
fi
# CM Service
if [ $vCenterVersion == '6.7' ] || [ $vCenterVersion == '6.5' ]; then
currService="CM service"
currFile="/usr/lib/vmware-cm/lib/log4j-core.jar"
if internal-target-file-exists; then
if [ $(grep -i jndilookup $currFile | wc -l) -eq 0 ]; then
logmsg -skip
else
internal-create-backup-and-verify && zip -q -d $currFile org/apache/logging/log4j/core/lookup/JndiLookup.class
internal-restart-service vmware-cm
fi
fi
fi
if [ $vCenterVersion == '6.7' ] || [ $vCenterVersion == '6.5' ]; then
# Secure Token Service
currService="Secure Token Service"
currFile="/etc/rc.d/init.d/vmware-stsd"
if internal-target-file-exists; then
if [ $(grep -E '^[ ]+-Dlog4j2.formatMsgNoLookups=true \\$' $currFile | wc -l) -gt 0 ]; then
logmsg -skip
else
internal-create-backup-and-verify && sed -r 's#([ ]+)(-Dauditlog.dir=/var/log/audit/sso-events \\)#\1\2\n\1-Dlog4j2.formatMsgNoLookups=true \\#' $currFile.bak > $currFile
internal-restart-service vmware-stsd
fi
fi
# Identity Management Service
currService="Identity Management Service"
currFile="/etc/rc.d/init.d/vmware-sts-idmd"
if internal-target-file-exists; then
if [ $(grep -E '^[ ]+-Dlog4j2.formatMsgNoLookups=true \\$' $currFile | wc -l) -gt 0 ]; then
logmsg -skip
else
internal-create-backup-and-verify && sed -r 's#([ ]+)(-Dlog4j.configurationFile=file://\$PREFIX/share/config/log4j2.xml \\)#\1\2\n\1-Dlog4j2.formatMsgNoLookups=true \\#' $currFile.bak > $currFile
internal-restart-service vmware-sts-idmd
fi
fi
fi
# PSC Client Service
if [ "$1" == "-sprayandpray65" ] && [ $vCenterVersion == '6.5' ]; then
currService="PSC Client Service"
currFile="/etc/rc.d/init.d/vmware-psc-client"
if internal-target-file-exists; then
if [ $(grep -E '^[ ]+-Dlog4j2.formatMsgNoLookups=true \\$' $currFile | wc -l) -gt 0 ]; then
logmsg -skip
else
internal-create-backup-and-verify && sed -r 's#([ ]+)(-Djava.io.tmpdir="\$CATALINA_BASE/temp" \\)#\1\2\n\1-Dlog4j2.formatMsgNoLookups=true \\#' $currFile.bak > $currFile
internal-restart-service vmware-psc-client
fi
fi
fi
}
internal-target-file-exists () {
if [ -f $currFile ]; then
flog "$currService" "Detected"
return 0
else
logmsg -error "Could not detect $currService"
return 1
fi
}
internal-create-backup-and-verify () {
flog "$currService" "Applying workaround..."
cp -aiv $currFile{,.bak}
if [ -f $currFile.bak ]; then
return 0
else
logmsg -error "$currService: Backup file not detected - skipping this workaround."
return 1
fi
}
internal-verify-workaround () {
# vMON
rawProcCount=$(($(ps auxww | grep '/usr/java/jre-vmware' | wc -l) - 1))
trueProcCount=$(($(ps auxww | grep '\-Dlog4j2.formatMsgNoLookups=true' | wc -l) - 1))
logmsg -big "Verification:"
logmsg -big "Number of processes running formatMsgNoLookups=true: $trueProcCount"
if [ $rawProcCount -eq $trueProcCount ]; then
logmsg -confirm "vMON - All JRE processes are running workaround."
else
logmsg -error "Process count mismatch. Got $rawProcCount JRE processes, but confirmed $trueProcCount. Confirm using: ps auxww | grep formatMsgNoLookups"
fi
if [ $vCenterVersion == '7.0' ]; then
# Update Manager
cd /usr/lib/vmware-updatemgr/bin/jetty/
if [ $(java -jar start.jar --list-config 2>/dev/null | grep 'log4j2\.formatMsgNoLookups = true (' | wc -l) -ne 0 ]; then
logmsg -confirm "Update Manager workaround."
else
logmsg -error "Update Manager workaround may have failed. Manually confirm using: cd /usr/lib/vmware-updatemgr/bin/jetty/ && java -jar start.jar --list-config"
fi
cd - >/dev/null
# DBCC
if [ $(grep -i jndilookup /usr/lib/vmware-dbcc/lib/log4j-core-2.8.2.jar | wc -l) -eq 0 ]; then
logmsg -confirm "DBCC Utility workaround."
else
logmsg -error "DBCC Utility reports that JndiLookup has not been removed."
fi
fi
# Analytics
if [ $vCenterVersion == '6.7' ] || [ $vCenterVersion == '7.0' ]; then
if [ -f /usr/lib/vmware/common-jars/log4j-core-2.8.2.jar ]; then
analyticsCount=$(grep -i jndilookup /usr/lib/vmware/common-jars/log4j-core-2.8.2.jar | wc -l)
if [ $analyticsCount -eq 0 ]; then
logmsg -confirm "Analytics Service workaround."
else
logmsg -error "Analytics Service reports that JndiLookup has not been removed."
fi
else
logmsg -warn "Skipping Analytics check - Analytics Service was not detected - ignore if version is newer than 6.7 P05."
fi
fi
if [ $vCenterVersion == '6.7' ] || [ $vCenterVersion == '6.5' ]; then
# CM Service
cmCount=$(grep -i jndilookup /usr/lib/vmware-cm/lib/log4j-core.jar | wc -l)
if [ $cmCount -eq 0 ]; then
logmsg -confirm "CM Service workaround."
else
logmsg -error "CM Service reports that JndiLookup has not been removed."
fi
# Secure Token Service
currService="Secure Token Service"
internal-check-process "vmware-stsd"
# Identity Management Service
currService="Identity Management Service"
internal-check-process "vmware-sts-idmd"
# PSC Client Service
if [ $vCenterVersion == '6.5' ]; then
currService="PSC Client Service"
internal-check-process "vmware-psc-client"
fi
fi
}
internal-rollback () {
# vMON
currService="vMON Service"
currFile="/usr/lib/vmware-vmon/java-wrapper-vmon"
internal-restore-and-restart "--all"
# Update Manager
currService="Update Manager Service"
currFile="/usr/lib/vmware-updatemgr/bin/jetty/start.ini"
internal-restore-and-restart "vmware-updatemgr"
# Analytics
currService="Analytics Service"
currFile="/usr/lib/vmware/common-jars/log4j-core-2.8.2.jar"
internal-restore-and-restart "vmware-analytics"
# DBCC Utility
currService="DBCC Utility"
currFile="/usr/lib/vmware-dbcc/lib/log4j-core-2.8.2.jar"
internal-restore-and-restart -noservice
# CM Service
currService="CM service"
currFile="/usr/lib/vmware-cm/lib/log4j-core.jar"
internal-restore-and-restart "vmware-cm"
# Secure Token Service
currService="Secure Token Service"
currFile="/etc/rc.d/init.d/vmware-stsd"
internal-restore-and-restart "vmware-stsd"
# Identity Management Service
currService="Identity Management Service"
currFile="/etc/rc.d/init.d/vmware-sts-idmd"
internal-restore-and-restart "vmware-sts-idmd"
# PSC Client Service
currService="PSC Client Service"
currFile="/etc/rc.d/init.d/vmware-psc-client"
internal-restore-and-restart "vmware-psc-client"
}
internal-restore-and-restart () {
if [ -f $currFile.bak ]; then
flog "$currService" "Rolling back..."
cp -av $currFile{.bak,}
if [ $1 != "-noservice" ]; then
internal-restart-service "$1"
else
flog "$currService" "No restart required."
fi
else
flog "$currService" "Nothing to roll back."
fi
}
internal-restart-service () {
flog "$currService" "Restarting service..."
service-control --stop $1
service-control --start $1
}
internal-check-process () {
rawProcCount=$(($(ps auxww | grep " $1 " | wc -l) - 1))
trueProcCount=$(($(ps auxww | grep " $1 .*-Dlog4j2.formatMsgNoLookups=true " | wc -l) - 1))
if [ $trueProcCount -gt 0 ]; then
if [ $rawProcCount -eq $trueProcCount ]; then
logmsg -confirm "$currService - all $trueProcCount processes are running workaround."
else
logmsg -error "Process count mismatch. Got $rawProcCount $currService processes, but confirmed $trueProcCount. Confirm using: ps auxww | grep formatMsgNoLookups"
fi
else
logmsg -warn "$currService is not running."
fi
}
# Logging system
logmsg () {
case $1 in
-confirm)
flog "Confirmed" "$2" ;;
-error)
flog "ERROR" "$2" ;;
-warn)
flog "WARNING" "$2" ;;
-skip)
flog "SKIPPED" "Workaround already applied to $currFile" ;;
-big)
printf "\n%s\n\n" "$2" ;;
esac
}
# Formatted logger
flog () { printf "%-30s %s\n" "$1" "$2"; }