-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cis_dil_benchmark_filesystem.py
636 lines (572 loc) · 18.4 KB
/
test_cis_dil_benchmark_filesystem.py
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import pytest
def assert_is_mounted(host, path):
mp = host.mount_point(path)
assert mp.exists, f"{path} should be mounted on its own partition"
def assert_is_mounted_with_argument(host, path, argument):
mp = host.mount_point(path)
if not mp.exists:
pytest.skip(f"{path} has no separated partition")
assert argument in mp.options, f"{path} should be mounted with `{argument}` option"
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_cramfs_module_loaded(record_property, host):
record_property("code", "1.1.1.1")
record_property("title", "CIS 1.1.1.1: Disable unused filesystem cramfs (Scored)")
record_property("impact", 1.0)
record_property(
"description",
"""
Ensure mounting of cramfs is disabled.
Solution:
- Add the line `install cramfs /bin/true` to the file `/etc/modprobe.d/cramfs.conf`.
""",
)
assert "" == host.run("lsmod | grep cramfs").stdout
# modprobe = host.run(f"modprobe -n -v cramfs")
# assert "install /bin/true" == modprobe.stdout
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_freevxfs_module_loaded(record_property, host):
record_property("code", "1.1.1.2")
record_property("title", "CIS 1.1.1.2: Disable unused filesystem freevxfs (Scored)")
record_property("impact", 1.0)
record_property(
"description",
"""
Ensure mounting of freevxfs is disabled.
Solution:
- Add the line `install freevxfs /bin/true` to the file `/etc/modprobe.d/freevxfs.conf`.
""",
)
assert "" == host.run("lsmod | grep freevxfs").stdout
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_iffs2_module_loaded(record_property, host):
record_property("code", "1.1.1.3")
record_property("title", "CIS 1.1.1.3: Disable unused filesystem iffs2 (Scored)")
record_property("impact", 1.0)
record_property(
"description",
"""
Ensure mounting of iffs2 is disabled.
Solution:
- Add the line `install iffs2 /bin/true` to the file `/etc/modprobe.d/iffs2.conf`.
""",
)
assert "" == host.run("lsmod | grep iffs2").stdout
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_hfs_module_loaded(record_property, host):
record_property("code", "1.1.1.4")
record_property("title", "CIS 1.1.1.4: Disable unused filesystem hfs (Scored)")
record_property("impact", 1.0)
record_property(
"description",
"""
Ensure mounting of hfs is disabled.
Solution:
- Add the line `install hfs /bin/true` to the file `/etc/modprobe.d/hfs.conf`.
""",
)
assert "" == host.run("lsmod | grep hfs").stdout
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_hfsplus_module_loaded(record_property, host):
record_property("code", "1.1.1.5")
record_property("title", "CIS 1.1.1.5: Disable unused filesystem hfsplus (Scored)")
record_property("impact", 1.0)
record_property(
"description",
"""
Ensure mounting of hfsplus is disabled.
Solution:
- Add the line `install hfsplus /bin/true` to the file `/etc/modprobe.d/hfsplus.conf`.
""",
)
assert "" == host.run("lsmod | grep hfsplus").stdout
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_squashfs_module_loaded(record_property, host):
record_property("code", "1.1.1.6")
record_property("title", "CIS 1.1.1.6: Disable unused filesystem squashfs (Scored)")
record_property("impact", 1.0)
record_property(
"description",
"""
Ensure mounting of squashfs is disabled.
Solution:
- Add the line `install squashfs /bin/true` to the file
`/etc/modprobe.d/squashfs.conf`.
""",
)
assert "" == host.run("lsmod | grep squashfs").stdout
# modprobe = host.run(f"modprobe -n -v squashfs")
# assert "install /bin/true" == modprobe.stdout
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_udf_module_loaded(record_property, host):
record_property("code", "1.1.1.7")
record_property("title", "CIS 1.1.1.7: Disable unused filesystem udf (Scored)")
record_property("impact", 1.0)
record_property(
"description",
"""
Ensure mounting of udf is disabled.
Solution:
- Add the line `install udf /bin/true` to the file
`/etc/modprobe.d/udf.conf`.
""",
)
assert "" == host.run("lsmod | grep udf").stdout
@pytest.mark.workstation2
@pytest.mark.server2
@pytest.mark.cis
def test_fat_module_loaded(record_property, host):
record_property("code", "1.1.1.8")
record_property("title", "CIS 1.1.1.8: Disable unused filesystem fat (Not Scored)")
record_property("impact", 0)
record_property(
"description",
"""
Ensure mounting of fat is disabled.
Solution:
- Add the line `install fat /bin/true` to the file
`/etc/modprobe.d/fat.conf`.
""",
)
assert "" == host.run("lsmod | grep fat").stdout
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_tmp_configured(record_property, host):
record_property("code", "1.1.2")
record_property("title", "CIS 1.1.2: Ensure /tmp is configured (Scored)")
record_property("impact", 1.0)
record_property(
"description",
"""
The /tmp directory is a world-writable directory used for temporary storage by
all users and some applications.
Solution:
Configure /etc/fstab as appropriate.
example:
```
tmpfs /tmp tmpfs defaults,rw,nosuid,nodev,noexec,relatime 0 0
```
""",
)
assert_is_mounted(host, "/tmp")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_tmp_with_nodev(record_property, host):
record_property("code", "1.1.3")
record_property(
"title", "CIS 1.1.3: Ensure nodev option set on /tmp partition (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The nodev mount option specifies that the filesystem cannot contain special devices.
Solution:
Edit the /etc/fstab file and add nodev to the fourth field (mounting options) for the /tmp partition.
""",
)
assert_is_mounted_with_argument(host, "/tmp", "nodev")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_tmp_with_nosuid(record_property, host):
record_property("code", "1.1.4")
record_property(
"title", "CIS 1.1.4: Ensure nosuid option set on /tmp partition (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The nosuid mount option specifies that the filesystem cannot contain setuid
files.
Solution:
Edit the /etc/fstab file and add nosuid to the fourth field (mounting options) for the /tmp partition.
""",
)
assert_is_mounted_with_argument(host, "/tmp", "nosuid")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_tmp_with_noexec(record_property, host):
record_property("code", "1.1.5")
record_property(
"title", "CIS 1.1.5: Ensure noexec option set on /tmp partition (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The noexec mount option specifies that the filesystem cannot contain executable binaries.
Solution:
Edit the /etc/fstab file and add noexec to the fourth field (mounting options) for the /tmp partition.
""",
)
assert_is_mounted_with_argument(host, "/tmp", "noexec")
@pytest.mark.workstation2
@pytest.mark.server2
@pytest.mark.cis
def test_separate_partition_for_var(record_property, host):
record_property("code", "1.1.6")
record_property(
"title", "CIS 1.1.6: Ensure separate partition exists for /var (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The /var directory is used by daemons and other system services to temporarily store
dynamic data. Some directories created by these processes may be world-writable.
Solution:
Configure /etc/fstab as appropriate.
""",
)
assert_is_mounted(host, "/var")
@pytest.mark.workstation2
@pytest.mark.server2
@pytest.mark.cis
def test_separate_partition_for_var_tmp(record_property, host):
record_property("code", "1.1.7")
record_property(
"title", "CIS 1.1.7: Ensure separate partition exists for /var/tmp (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The /var/tmp directory is a world-writable directory used for temporary storage by all
users and some applications.
Solution:
Configure /etc/fstab as appropriate.
""",
)
assert_is_mounted(host, "/var/tmp")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_nodev_on_var_tmp_if_exists(record_property, host):
record_property("code", "1.1.8")
record_property(
"title", "CIS 1.1.8: Ensure nodev option set on /var/tmp partition (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The nodev mount option specifies that the filesystem cannot contain special devices.
Solution:
Edit the /etc/fstab file and add nodev to the fourth field (mounting options) for the
/var/tmp partition
""",
)
assert_is_mounted_with_argument(host, "/var/tmp", "nodev")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_nosuid_on_var_tmp_if_exists(record_property, host):
record_property("code", "1.1.9")
record_property(
"title", "CIS 1.1.9: Ensure nosuid option set on /var/tmp partition (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The nosuid mount option specifies that the filesystem cannot contain setuid files.
Solution:
Edit the /etc/fstab file and add nosuid to the fourth field (mounting options) for the
/var/tmp partition
""",
)
assert_is_mounted_with_argument(host, "/var/tmp", "nosuid")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_noexec_on_var_tmp_if_exists(record_property, host):
record_property("code", "1.1.10")
record_property(
"title", "CIS 1.1.10: Ensure noexec option set on /var/tmp partition (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The noexec mount option specifies that the filesystem cannot contain executable binaries.
Solution:
Edit the /etc/fstab file and add noexec to the fourth field (mounting options) for the
/var/tmp partition
""",
)
assert_is_mounted_with_argument(host, "/var/tmp", "noexec")
@pytest.mark.workstation2
@pytest.mark.server2
@pytest.mark.cis
def test_separate_partition_for_var_log(record_property, host):
record_property("code", "1.1.11")
record_property(
"title", "CIS 1.1.11: Ensure separate partition exists for /var/log (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The /var/log directory is used by system services to store log data.
Solution:
Configure /etc/fstab as appropriate.
""",
)
assert_is_mounted(host, "/var/log")
@pytest.mark.workstation2
@pytest.mark.server2
@pytest.mark.cis
def test_separate_partition_for_var_log_audit(record_property, host):
record_property("code", "1.1.12")
record_property(
"title",
"CIS 1.1.12: Ensure separate partition exists for /var/log/audit (Scored)",
)
record_property("impact", 1.0)
record_property(
"description",
"""
The auditing daemon, auditd , stores log data in the /var/log/audit directory.
Solution:
Configure /etc/fstab as appropriate.
""",
)
assert_is_mounted(host, "/var/log/audit")
@pytest.mark.workstation2
@pytest.mark.server2
@pytest.mark.cis
def test_separate_partition_for_home(record_property, host):
record_property("code", "1.1.13")
record_property(
"title", "CIS 1.1.13: Ensure separate partition exists for /home (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The /home directory is used to support disk storage needs of local users.
Solution:
Configure /etc/fstab as appropriate.
""",
)
assert_is_mounted(host, "/home")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_home_with_nodev(record_property, host):
record_property("code", "1.1.14")
record_property(
"title", "CIS 1.1.14: Ensure nodev option set on /home partition (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The nodev mount option specifies that the filesystem cannot contain special devices.
Solution:
Edit the /etc/fstab file and add nodev to the fourth field (mounting options) for the /home partition.
""",
)
assert_is_mounted_with_argument(host, "/home", "nodev")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_dev_shm_with_nodev(record_property, host):
record_property("code", "1.1.15")
record_property(
"title", "CIS 1.1.15: Ensure nodev option set on /dev/shm partition (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The nodev mount option specifies that the filesystem cannot contain special devices.
Solution:
Edit the /etc/fstab file and add nodev to the fourth field (mounting options) for the /dev/shm partition.
""",
)
assert_is_mounted_with_argument(host, "/dev/shm", "nodev")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_dev_shm_with_nosuid(record_property, host):
record_property("code", "1.1.16")
record_property(
"title", "CIS 1.1.16: Ensure nosuid option set on /dev/shm partition (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The nosuid mount option specifies that the filesystem cannot contain setuid files.
Solution:
Edit the /etc/fstab file and add nosuid to the fourth field (mounting options) for the /dev/shm partition.
""",
)
assert_is_mounted_with_argument(host, "/dev/shm", "nosuid")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_dev_shm_with_noexec(record_property, host):
record_property("code", "1.1.17")
record_property(
"title", "CIS 1.1.17: Ensure noexec option set on /dev/shm partition (Scored)"
)
record_property("impact", 1.0)
record_property(
"description",
"""
The noexec mount option specifies that the filesystem cannot contain executable binaries.
Solution:
Edit the /etc/fstab file and add nosuid to the fourth field (mounting options) for the /dev/shm partition.
""",
)
assert_is_mounted_with_argument(host, "/dev/shm", "noexec")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_all_removable_media_partitions_with_nodev(record_property, host):
record_property("code", "1.1.18")
record_property(
"title",
"CIS 1.1.18: Ensure nodev option set on removable media partitions(Not Scored)",
)
record_property("impact", 0)
record_property(
"description",
"""
The nodev mount option specifies that the filesystem cannot contain special devices.
""",
)
pytest.skip("Not implemented yet")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_all_removable_media_partitions_with_nosuid(record_property, host):
record_property("code", "1.1.19")
record_property(
"title",
"CIS 1.1.19: Ensure nosuid option set on removable media partitions (Not Scored)",
)
record_property("impact", 0)
record_property(
"description",
"""
The nosuid mount option specifies that the filesystem cannot contain special devices.
""",
)
pytest.skip("Not implemented yet")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
def test_all_removable_media_partitions_with_noexec(record_property, host):
record_property("code", "1.1.20")
record_property(
"title",
"CIS 1.1.20: Ensure noexec option set on removable media partitions (Not Scored)",
)
record_property("impact", 0)
record_property(
"description",
"""
The noexecmount option specifies that the filesystem cannot contain special devices.
""",
)
pytest.skip("Not implemented yet")
@pytest.mark.workstation1
@pytest.mark.server1
@pytest.mark.cis
@pytest.mark.slow
def test_sticky_for_world_writable_dirs(record_property, host):
record_property("code", "1.1.21")
record_property(
"title",
"CIS 1.1.21: Ensure sticky bit is set on all world-writable directories (Scored)",
)
record_property("impact", 1.0)
record_property(
"description",
"""
Setting the sticky bit on world writable directories prevents users from deleting or
renaming files in that directory that are not owned by them.
Solution:
Run the next command:
df --local -P | awk '{if (NR!=1) print $6}' \\
| xargs -I '{}' find '{}' -xdev -type d \\( -perm -0002 -a ! -perm -1000 \\) 2>/dev/null \\
| xargs -I '{}' chmod a+t '{}'
""",
)
search = host.run(
"df --local --portability "
" | awk '{if (NR!=1) print $6}' "
" | sort " # added to increase speed
r" | xargs -I '{}' find '{}' -xdev -type d \( -perm -0002 -a ! -perm -1000 \) 2>/dev/null"
" | head -n1" # added to increase speed
)
assert (
search.stdout == ""
), "sticky bit for all world-writable directories should be set."
@pytest.mark.server1
@pytest.mark.workstation2
@pytest.mark.cis
def test_disable_automounting(record_property, host):
record_property("code", "1.1.22")
record_property(
"title",
"CIS 1.1.22: Disable Automounting (Scored)",
)
record_property("impact", 1.0)
record_property(
"description",
"""
autofs allows automatic mounting of devices, typically including CD/DVDs and USB drives.
Solution:
Any command of:
# chkconfig autofs off
# systemctl disable autofs
# update-rc.d autofs disable
""",
)
autofs = host.service("autofs")
assert (
not autofs.is_running and not autofs.is_enabled
), "autofs should not be installed"
@pytest.mark.server1
@pytest.mark.workstation2
@pytest.mark.cis
def test_disable_usb_storage(record_property, host):
record_property("code", "1.1.23")
record_property(
"title",
"CIS 1.1.23: Disable USB Storage(Scored)",
)
record_property("impact", 1.0)
record_property(
"description",
"""
USB popularity and utility has led to USB-based malware being a simple and
common means for network infiltration and a first step to establishing a
persistent threat within a networked environment.
""",
)
assert (
"" == host.run("lsmod | grep usb-storage").stdout
), "usb-storage module should not be enabled."