This repository has been archived by the owner on Jan 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcheckhack-zimbra-preferences
436 lines (373 loc) · 12.3 KB
/
checkhack-zimbra-preferences
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
#!/bin/ksh
#
# Written by David A. Halsema <[email protected]>
# 2010-10-07
# Edited by Keith McDermott <[email protected]>
# 2015-06-05
#
# checkhack-zimbra-preferences
#
# Try to detect "spammy" changes to a user's Zimbra preferences and
# automatically clear the password of hacked accounts. Uses a point
# scoring system.
#
# Each of the following conditions scores points:
#
# Default: Accumulate 8 points in a calendar day, and it will automatically
# clear the person's password. Designed so that there must be at least
# two occurrences of something happening before clearing the password.
#
# * (1 pt.) Preference change occurs
# * (3 pt.) Signature contains any of the special words in CHECK_KEYS
# * (3 pt.) Any plain text signature is larger in size than CHECK_BYTES
# * (3 pt.) Reply-To: changes to a non @domain.com address
# * (3 pt.) Reply-To: contains special words in CHECK_REPLY
# * (1 pt.) Forwarding to a non @domain.com address
# * (3 pt.) Local mail delivery is disabled
# * (1 pt.) Saving messages to Sent folder is disabled
#
# CUSTOMIZATIONS!!
# Change the following lines/sections to fit your needs:
# Line 300: Add your domain in place of domain.com
# Line 358: Add your domain in place of domain.com
# Line 415: Add in your own custom code for locking accounts, be it a Zimbra command or something else
# This script does not shell escape properly see: https://github.com/Zimbra-Community/zimbra-tools/blob/master/checkhack-zimbra-preferences.md
umask 077
MAILTO="[email protected]"
PROG=`basename $0`
PREV=/var/tmp/zimbra-preferences-prev
CURR=/var/tmp/zimbra-preferences-curr
OFF=/tmp/$PROG-OFF
TMPFILE=/tmp/$PROG-signatures
day=`date "+%Y-%m-%d"`
SCORE_DBDIR=/tmp/zimbra-preferences-scores/$day
DELIM="===================================================================="
cleanup() {
rm -f $TMPFILE $TMPFILE-2
}
trap 'cleanup' 0
trap 'cleanup; exit 1' 1 2 3 15
#
# If the OFF file exists exit without doing anything. Also remove the
# $CURR and $PREV files so it will start from scratch once the OFF file
# has been removed.
#
# This was used as an aid during Zimbra migrations to lessen performance
# impact and false positives on new people configuring their signatures.
#
if [ -f $OFF ]; then
rm -f $PREV $CURR
rm -rf $SCORE_DBDIR
exit 0
fi
#
# Number of accumulated points required in one calendar day required to
# clear a password. This must be an integer.
#
POINTS_KILL=7
#
# Number of points awarded for a person's preferences changing. This must
# be an integer.
#
SCORE_PREF_CHANGE=1
#
# Keywords to scan for, and the number of points awarded if found. The
# point value must be an integer.
#
CHECK_KEYS="(centurylink|microsoft|inheritance|update your account|quota limit|verification code|online draw|loan|support|lottery|nigeria|chevron|barrister|dear winner|error code 334409|account update|password|payment|bank account|lotto|congrat)"
SCORE_KEYS=3
#
# The allowed size of any signature. Any size greater than or equal to
# CHECK_BYTES will result in points being awarded. The point value must
# be an integer.
#
CHECK_BYTES=1024
SCORE_BYTES=3
#
# The score to assign reply_to changes that do not end with our domain.
#
SCORE_REPLYTO=3
#
# The score to assign mail forwarding changes that do not end with our domain.
#
SCORE_EXTERNAL_MAIL_FORWARDING=1
#
# The score to assign when local mail delivery is disabled
#
SCORE_LOCAL_DELIVERY_DISABLED=3
#
# The score to assign when you disable saving of mail to Sent folder
#
SCORE_SAVE_TO_SENT_DISABLED=1
#
# The score to assign reply_to changes that have these keywords.
#
CHECK_REPLY="(@qq\.com|@aol\.co\.uk|@live\.com|@live\.co\.uk|@hotmail\.co\.uk|@ymail\.com|loginteam|loginupgrade|lottery|@yahoo\.com\.hk|@yahoo\.com\.cn|@yahoo\.cn|@yahoo\.hk|mr\.jack\.kenedy|@britishnatelot\.com|@safadi-saeedi\.com|@siamza\.com)"
SCORE_REPLYTO_CHECK=3
#
# The number of preference changes which are allowed to occur between
# the current run and the previous run. Should catch cases where there
# is an error dumping the Zimbra preferences and we end up with a zero
# length file.
#
NUM_ALLOWED_LINES=30
#
# Rotate old preferences file for a new dump to take place. Only rotate
# if the current file exists and has a size, otherwise an error may have
# occurred last dump.
#
if [ -s $CURR ]; then
mv $CURR $PREV
fi
#
# Dump signatures and identities for users and put all the data on one
# line for diff to detect changes. If a change is found, a tr command
# can be done to put the newlines back in the correct place.
#
hostname=`uname -n`
accounts=`su - zimbra -c "/opt/zimbra/bin/zmprov -l gaa -s $hostname"`
for account in $accounts
do
echo "ga -e $account uid zimbraPrefMailForwardingAddress zimbraPrefMailLocalDeliveryDisabled zimbraPrefSaveToSent"
echo "gsig $account zimbraSignatureName zimbraPrefMailSignature zimbraPrefMailSignatureHTML"
echo "gid $account zimbraPrefIdentityName zimbraPrefFromDisplay zimbraPrefFromAddress zimbraPrefReplyToDisplay zimbraPrefReplyToAddress"
done \
| su - zimbra -c '/opt/zimbra/bin/zmprov' \
> $TMPFILE
if [ $? -ne 0 ]; then
echo "Error occurred in zmprov dump of Zimbra preferences" 2>&1
exit 1
fi
sed -e 's/prov> //g' -e '/^#/d' < $TMPFILE > $TMPFILE-2
while read line
do
case "$line" in
uid:*)
if [ -n "$data" ]; then
data=`print -R "$data" \
| awk '{ gsub("\034$", ""); print }'`
print -R "$uid: $data"
fi
data=""
uid="$line"
;;
zimbraPrefMailForwardingAddress:*|zimbraPrefMailLocalDeliveryDisabled:*|zimbraPrefSaveToSent:*|zimbraSignatureName:*|zimbraPrefMailSignature:*|zimbraPrefMailSignatureHTML:*|zimbraPrefIdentityName:*|zimbraPrefFromDisplay:*|zimbraPrefFromAddress:*|zimbraPrefReplyToDisplay:*|zimbraPrefReplyToAddress:*)
if [ -n "$data" ]; then
data=`print -R "$data" \
| awk '{ gsub("\034$", ""); print }'`
print -R "$uid: $data"
fi
data="$line"
;;
*)
#
# This is a multi-lined attribute value and we want
# to put it on one line for easy diff'ing.
#
if [ -z "$data" ]; then
data="$line"
else
data="$data$(print '\034')$line"
fi
;;
esac
done < $TMPFILE-2 > $CURR
#
# Last line need to be printed?
#
if [ -n "$data" ]; then
print -R "$uid: $data" >> $CURR
fi
sort $CURR -o $CURR
#
# If no $PREV file is present, assume this is the first time we're running
# the script and initialize $PREV to be the same as $CURR
#
if [ ! -f $PREV ]; then
cp $CURR $PREV
fi
#
# Quick sanity check on the preference file dumps. If the number of
# lines change too much, then just exit assuming we got bad data due
# to an error of some sort in the dumping process.
#
oldcount=`awk 'END {print NR}' $PREV`
newcount=`awk 'END {print NR}' $CURR`
if [ $oldcount -ge $newcount ]; then
diffcount=$(($oldcount - $newcount))
else
diffcount=$(($newcount - $oldcount))
fi
if [ $diffcount -ge $NUM_ALLOWED_LINES ]; then
mail -s "Zimbra Preference Sanity Check" $MAILTO << EOF
The checkhack script is exiting without doing anything.
The number of lines in the Zimbra preference data dumps has changed by
more than allowed by the threshold ($NUM_ALLOWED_LINES).
Old Line Count: $oldcount ($PREV)
New Line Count: $newcount ($CURR)
This is most likely caused by an error during the last Zimbra preference
dump. It should automatically sort itself out once there are two dump
files which are within the threshold again.
(This script is $0 on `hostname`)
EOF
exit 1
fi
#
# Keep scores for logins in /tmp by date. Eventually they will get removed
# by the watchtmp scripts.
#
if [ ! -d $SCORE_DBDIR ]; then
mkdir -p $SCORE_DBDIR
fi
#
# Get the list of new/changed preferences
#
changed=`diff $PREV $CURR | grep '^> '`
typeset -A score
typeset -A scorestring
typeset -A changedlines
typeset -A ignore
while read -r junk junk login field value
do
login=`echo "$login" | sed -e 's/:$//' | tr "A-Z" "a-z"`
if [ -z "$login" ]; then
continue
fi
#
# If the zimbraPrefIdentityName changed to DEFAULT, this is
# a new account (either through service setting or migration)
# and should not be scored. Place on the ignore list.
#
if [ "$field" = "zimbraPrefIdentityName:" -a "$value" = "DEFAULT" ]
then
ignore[$login]="YES"
continue
fi
#
# Something changed with the login, assign initial score
#
if [ -z ${score[$login]} ]; then
score[$login]="$SCORE_PREF_CHANGE"
scorestring[$login]="CHANGED($SCORE_PREF_CHANGE)"
fi
fieldlen=${#field}
fielddelim=$(print -R $DELIM | cut -c1-${fieldlen})
changedlines[$login]="${changedlines[$login]}$(print '\034')$field$(print '\034')$fielddelim$(print '\034')$value$(print '\034')"
case "$field" in
zimbraPrefMailForwardingAddress:)
print -R "$value" | egrep -qi '(^[^@]*$|@.*domain.com$)'
if [ $? -eq 0 ]; then
# OK, no @ signs, or it is our domain
:
else
# No domain addy found
score[$login]=$(expr ${score[$login]} + $SCORE_EXTERNAL_MAIL_FORWARDING)
scorestring[$login]="${scorestring[$login]} + EXTERNAL_MAIL_FORWARDING($SCORE_EXTERNAL_MAIL_FORWARDING)"
fi
;;
zimbraPrefMailLocalDeliveryDisabled:)
if [ "$value" = "TRUE" ]; then
score[$login]=$(expr ${score[$login]} + $SCORE_LOCAL_DELIVERY_DISABLED)
scorestring[$login]="${scorestring[$login]} + LOCAL_DELIVERY_DISABLED($SCORE_LOCAL_DELIVERY_DISABLED)"
fi
;;
zimbraPrefSaveToSent:)
if [ "$value" = "FALSE" ]; then
score[$login]=$(expr ${score[$login]} + $SCORE_SAVE_TO_SENT_DISABLED)
scorestring[$login]="${scorestring[$login]} + SAVE_TO_SENT_DISABLED($SCORE_SAVE_TO_SENT_DISABLED)"
fi
;;
zimbraPrefMailSignature:|zimbraPrefMailSignatureHTML:)
#
# Size calculation (Only check plain text signatures)
#
if [ "$field" = "zimbraPrefMailSignature:" ]; then
size=$(print -R "$value" | wc -c)
if [ $size -ge $CHECK_BYTES ]; then
score[$login]=$(expr ${score[$login]} + $SCORE_BYTES)
scorestring[$login]="${scorestring[$login]} + LENGTH($SCORE_BYTES)"
fi
fi
#
# Keyword check
#
print -R "$value" | egrep -qi "$CHECK_KEYS"
if [ $? -eq 0 ]; then
score[$login]=$(expr ${score[$login]} + $SCORE_KEYS)
scorestring[$login]="${scorestring[$login]} + SIG_KEYWORD($SCORE_KEYS)"
fi
;;
zimbraPrefReplyToAddress:)
#
# Reply-To headers check, score $SCORE_REPLYTO points for the
# following conditions:
#
# 1) reply_to changed
# 2) reply_to has an @ sign
# 3) reply_to does not end with "domain.com"
#
print -R "$value" | egrep -qi '(^[^@]*$|@.*domain.com$)'
if [ $? -eq 0 ]; then
# OK, no @ signs, or it is our domain address
:
else
# No return addy found on our domain
score[$login]=$(expr ${score[$login]} + $SCORE_REPLYTO)
scorestring[$login]="${scorestring[$login]} + REPLYTO_NOTOURDOMAIN($SCORE_REPLYTO)"
fi
#
# In addition, score SCORE_REPLYTO_CHECK more points if
# the replyto contains any keywords in CHECK_REPLY
#
print -R "$value" | egrep -qi "$CHECK_REPLY"
if [ $? -eq 0 ]; then
score[$login]=$(expr ${score[$login]} + $SCORE_REPLYTO_CHECK)
scorestring[$login]="${score_string[$login]} + REPLYTO_KEYWORD($SCORE_REPLYTO_CHECK)"
fi
;;
*)
;;
esac
done <<EOF
$changed
EOF
for key in ${!score[*]}
do
if [ "${ignore[$key]}" = "YES" ]; then
continue
fi
#
# Read previous score and add previous change value
#
if [ -f $SCORE_DBDIR/$key ]; then
prev=`cat $SCORE_DBDIR/$key`
score[$key]=$(expr $prev + ${score[$key]})
scorestring[$key]="PREVIOUS($prev) + ${scorestring[$key]}"
fi
fmt_score_string=`echo " ${scorestring[$key]}" | fmt -w 78`
if [ ${score[$key]} -ge $POINTS_KILL ]; then
tr "\034" "\n" << EOF | mail -s "Zimbra Hacked Account Disabled" $MAILTO
User "$key" had account disabled due to hack heuristics. [score=${score[$key]}]
$fmt_score_string
Add descriptive text here on your unlocking procedures.
${changedlines[$key]}
(This script is $0 on `hostname`)
EOF
#Add code here to lock the account. Possibly just a zmprov to lock the account, or something more if you need to tie into your own accounting system.
rm -f $SCORE_DBDIR/$key
else
print -R "${score[$key]}" > $SCORE_DBDIR/$key
#
# Send e-mail to admins for all preferences scoring over
# a two. Can serve as an early warning sign of trouble,
# or even false positives.
#
if [ ${score[$key]} -gt 2 ]; then
(print -R "login=$key, score=${score[$key]}"; \
print -R "${changedlines[$key]}"; \
print -R "${scorestring[$key]}") \
| tr "\034" "\n" \
| mail -s "Zimbra Preference Change (login=$key, score=${score[$key]})" $MAILTO
fi
fi
done