-
Notifications
You must be signed in to change notification settings - Fork 1
/
harden_07_rhel5_cis-v1.1.1.sh
329 lines (224 loc) · 10.7 KB
/
harden_07_rhel5_cis-v1.1.1.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/bin/sh
#
# harden_07_rhel5_cis-v1.1.1.sh
##########################################################################
# AUTHOR: Jack Maynard #
# Created: May 10, 2010 - version 1.0 #
# #
# RHEL5 Hardening Script Section 7.0 - File/Directory Permissions/Access #
# Scripted to Center for Internet Security (CIS) RHEL5 Benchmark v1.1.1 #
# www.cisecurity.org #
##########################################################################
# Variable Declarations
# Redirect stdout and stderr to /dev/null, this helps reduce screen noise.
# Change /dev/null to a real file for debugging purposes.
STDOUT_STDERR="/dev/null"
exec &>${STDOUT_STDERR}
##########################################################################
# #
# RHEL5 Benchmark Section 7.0 #
# File/Directory Permissions/Access #
# #
##########################################################################
##########################################################################
# 7.1 - Add 'nodev' Option To Appropriate Partitions In /etc/fstab #
##########################################################################
add_nodev_partitions(){
awk '( $3 ~ /^ext[23]$/ && $2 != "/" ) { $4 = $4 ",nodev" }; \
{ printf "%-26s %-22s %-8s %-16s %-1s %-1s\n",$1,$2,$3,$4,$5,$6 }' \
/etc/fstab > /tmp/fstab.tmp
cp -f /tmp/fstab.tmp /etc/fstab
rm -f /tmp/fstab.tmp
chown -f root:root /etc/fstab
chmod -f 0644 /etc/fstab
}
##########################################################################
# 7.2 - Add 'nosuid' 'nodev' Option For Removable Media In /etc/fstab #
##########################################################################
add_nosuid_nodev_removable_media(){
# Part 1
# Additional devices this section 'might' consider could be a DVD or cd recorder
awk '( $2 ~ /^\/m.*\/(floppy|cdrom|corder)$/ ) && ( $4 !~ /,nodev,nosuid/ ) \
{ $4 = $4 ",nodev,nosuid" }; \
{ printf "%-26s%-22s%-8s%-16s %-1s %-1s\n",$1,$2,$3,$4,$5,$6 }' \
/etc/fstab > /tmp/fstab.tmp
cp -f /tmp/fstab.tmp /etc/fstab
chown -f root:root /etc/fstab
chmod -f 0644 /etc/fstab
# Part 2
fdiPATH='unknown'
if [ -e /usr/share/hal/fdi/95userpolicy ]; then
# Apply this to RHEL AS4 system
fdiPATH="/usr/share/hal/fdi/95userpolicy"
else
if [ -e /usr/share/hal/fdi/policy/20thirdparty ]; then
# apply this for RHEL5
fdiPATH="/usr/share/hal/fdi/policy/20thirdparty"
fi
fi
if [ "$fdiPATH" == 'unknown' ]; then
echo > /dev/null
else
cat <<END_SCRIPT >> $fdiPATH/floppycdrom.fdi
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- -*- SGML -*- -->
<deviceinfo version="0.2">
<!-- Default policies merged onto computer root object -->
<device>
<match key="info.udi" string="/org/freedesktop/Hal/devices/computer">
<merge key="storage.policy.default.mount_option.nodev" type="bool">true</merge>
<merge key="storage.policy.default.mount_option.nosuid" type="bool">true</merge>
</match>
</device>
</deviceinfo>
END_SCRIPT
chown -f root:root $fdiPATH/floppycdrom.fdi
chmod -f 0640 $fdiPATH/floppycdrom.fdi
fi
}
##########################################################################
# 7.3 - Disable User-Mounted Removable File Systems #
##########################################################################
disable_user_mount_removable_filesys(){
CONS_PERM_FILE="/etc/security/console.perms"
DEF_FILE="/etc/security/console.perms.d/50-default.perms"
# If the test below passes, the 2nd file is changed, not the first.
# Need to protect both.
test -f ${DEF_FILE} && CONS_PERM_FILE=${DEF_FILE}
# Each entry listed below will NOT be commented out in the
# "console.perms" file. The remaining entries in that file WILL
# be commented out and thus disabled post-reboot. Further, "memstick"
# and "diskonkey" were not part of the original CIS specification to be
# left alone, but have been included to tailor the hardened build for
# usage of normal system USB requirements (such as keyboards and mice).
cp ${CONS_PERM_FILE} ${CONS_PERM_FILE}.tmp
awk '( $1 == "<console>" ) && \
( $3 !~ /sound|fb|kbd|joystick|v4l|mainboard|gpm|scanner|memstick|diskonkey/ ) \
{ $1 = "#<console>" }; { print }' ${CONS_PERM_FILE}.tmp > ${CONS_PERM_FILE}
chown -f root:root ${CONS_PERM_FILE}
chmod -f 0600 ${CONS_PERM_FILE}
rm ${CONS_PERM_FILE}.tmp
}
##########################################################################
# 7.4 - Verify passwd, shadow, and group File Permissions #
##########################################################################
verify_passwd_shadow_group_perms(){
for FILE in /etc/group /etc/gshadow /etc/passwd /etc/shadow;
do
if [ -e $FILE ]; then
chown root:root ${FILE}
fi
done
for FILE in /etc/group /etc/passwd;
do
if [ -e $FILE ]; then
chmod 0644 ${FILE}
ls -l ${FILE}
fi
done
for FILE in /etc/gshadow /etc/shadow;
do
if [ -e $FILE ]; then
chmod 0400 ${FILE}
ls -l ${FILE}
fi
done
}
##########################################################################
# 7.5 - Ensure World-Writable Directories Have Their Sticky Bit Set #
##########################################################################
find_world_write_sticky(){
for PART in `awk '($3 ~ "ext2|ext3") { print $2 }' /etc/fstab`; do
find $PART -xdev -type d -perm -0002 -a ! -perm -1000 -ls \
| while read DIR; do
chmod o+t $DIR
echo $DIR
done
done
}
##########################################################################
# 7.6 - Find Unauthorized World-Writable Files #
##########################################################################
find_unauth_world_write(){
for PART in $( grep -v '^#' /etc/fstab | awk '( $3 ~ "ext[23]" ) \
{ print $2 }' ); do
find $PART -xdev -type f -perm -0002 -a ! -perm -1000 \
-print | while read FILE; do
chmod o-w $FILE
done
done
}
##########################################################################
# 7.7 - Find Unauthorized SUID/SGID System Executables #
##########################################################################
find_unauth_suid_sgid(){
for PART in $( grep -v '^#' /etc/fstab | \
awk '($3 ~ "ext[23]" ) { print $2 }' ); do
find $PART -xdev -perm -04000 -o -perm -02000 -print | sort \
| while read FILE; do
if [[ ! $(grep $FILE ./include/authorized-suid-sgid.txt) ]]; then
echo $FILE
fi
done
done
}
##########################################################################
# 7.8 - Find All Unowned Directories and Files #
##########################################################################
find_unowned_files(){
# Refer to CIS-CAT Report for a list of unowned directories / files.
echo "" > /dev/null
}
##########################################################################
# 7.9 - Disable USB Devices #
##########################################################################
disable_usb(){
DEF_KERN=$( grubby --default-kernel)
grubby --update-kernel=$DEF_KERN --args="nousb"
}
#---------- BEGIN CONFIGURATION ----------#
# TO CHANGE SCRIPT BEHAVIOR ONLY EDIT THE SECTIONS BELOW THIS LINE !!!
##########################################################################
# #
# CONFIGURATION #
# Section 7.0 - File/Directory Permissions/Access #
# #
##########################################################################
##########################################################################
# 7.1 - Add 'nodev' Option To Appropriate Partitions In /etc/fstab #
##########################################################################
add_nodev_partitions
##########################################################################
# 7.2 - Add 'nosuid' 'nodev' Option For Removable Media In /etc/fstab #
##########################################################################
add_nosuid_nodev_removable_media
##########################################################################
# 7.3 - Disable User-Mounted Removable File Systems #
##########################################################################
disable_user_mount_removable_filesys
##########################################################################
# 7.4 - Verify passwd, shadow, and group File Permissions #
##########################################################################
verify_passwd_shadow_group_perms
##########################################################################
# 7.5 - Ensure World-Writable Directories Have Their Sticky Bit Set #
##########################################################################
find_world_write_sticky
##########################################################################
# 7.6 - Find Unauthorized World-writable Files #
##########################################################################
find_unauth_world_write
##########################################################################
# 7.7 - Find Unauthorized SUID/SGID System Executables #
##########################################################################
find_unauth_suid_sgid
##########################################################################
# 7.8 - Find All Unowned Directories and Files #
##########################################################################
find_unowned_files
##########################################################################
# 7.9 - Disable USB Devices #
##########################################################################
#disable_usb
#---------- END CONFIGURATION ----------#
#---------- END SECTION 7 ----------#
#END