-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhel8_cis.py
1327 lines (1218 loc) · 71.7 KB
/
rhel8_cis.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import subprocess
import re
import sys
import datetime
def main():
step1()
step2()
step3()
step4()
step5()
step6()
def step1():
with open("rhel8.txt", "a+" , buffering=1) as step1:
step1.write("#\n# Begin chapter 1 ")
step1.write("#\n Ensure Red Hat Subscription Manager connection is configured")
with subprocess.Popen(['subscription-manager' , 'register'], stdout = step1 ) as tmp1:
stdout,stderr = tmp1.communicate()
step1.write("#\n Disable the rhnsd Daemon ")
with subprocess.Popen(['systemctl' , '--now' , 'disable' , 'rhnsd'], stdout = step1 ) as tmp2:
stdout,stderr = tmp2.communicate()
step1.write("#\n Ensure gpgcheck is globally activated\n")
step1.write("# Ensure /etc/yum.conf gpgcheck set to 1\n")
cmd = "grep ^gpgcheck /etc/yum.conf"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout=step1) as tmp3:
stdout,stderr = tmp3.communicate()
step1.write ("# Ensure /etc/yum.repos.d/ all file enable gpgcheck set to 1\n")
with subprocess.Popen(['grep' , '^gpgcheck' , '/etc/yum.repos.d/*'] , stdout=step1) as tmp4:
stdout,stderr = tmp4.communicate()
step1.write("#\n Ensure sudo is installed\n")
with subprocess.Popen(['dnf' , 'install' , 'sudo'] , stdout=step1) as tmp5:
stdout,stderr = tmp5.communicate()
step1.write("#\n Ensure sudo commands use pty\n")
with open("/etc/sudoers" , "a+" , buffering = 1) as tmp:
tmp.write("Defaults use_pty")
tmp.close()
step1.write("#\nEnsure sudo log file exists\n")
with open("/etc/sudoers" , "a+" , buffering = 1) as tmp:
tmp.write('Defaults logfile="/var/log/sudo.log"')
tmp.close()
step1.write ("# Ensure AIDE is installed \n")
cmd = "dnf install -y aide"
cmd = cmd.split()
cmd2 = "aide --init"
cmd2 = cmd2.split()
cmd3 = "mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz"
cmd3 = cmd3.split()
with subprocess.Popen(cmd, stdout=step1) as tmp6:
stderr,stdout = tmp6.communicate()
step1.write ("# Establishing Now Init For Aide\n")
with subprocess.Popen(cmd2, stdout=step1) as tmp7:
stderr,stdout = tmp7.communicate()
step1.write ("# Moved The New Database of Ide\n")
with subprocess.Popen(cmd3, stdout=step1) as tmp8:
stderr,stdout = tmp8.communicate()
step1.write ("# Ensure filesystem integrity is regularly checked\n")
cmd = "echo '0 5 * * * /usr/sbin/aide --check' > bash.aide.tmp"
cmd2 = "crontab -u root bash.aide.tmp"
cmd2 = cmd2.split()
cmd3 = "rm -rf bash.aide.tmp"
cmd3 = cmd3.split()
with subprocess.Popen(['/bin/bash', '-c' , cmd], stdout=step1) as tmp9:
stdout,stderr = tmp9.communicate()
with subprocess.Popen(cmd2, stdout=step1) as tmp10:
stdout,stderr = tmp10.communicate()
with subprocess.Popen(cmd3, stdout=step1) as tmp11:
stdout,stderr = tmp11.communicate()
step1.write("# Ensure permissions on bootloader config are configured \n")
cmd = "chown root:root /boot/grub2/grub.cfg"
cmd = cmd.split()
cmd2 = "chmod og-rwx /boot/grub2/grub.cfg"
cmd2 = cmd2.split()
cmd3 = "chown root:root /boot/grub2/grubenv"
cmd3 = cmd3.split()
cmd4 = "chmod og-rwx /boot/grub2/grubenv"
cmd4 = cmd4.split()
with subprocess.Popen(cmd, stdout = step1) as tmp12:
stdout,stderr = tmp12.communicate()
with subprocess.Popen(cmd2, stdout = step1) as tmp13:
stdout,stderr = tmp13.communicate()
with subprocess.Popen(cmd3, stdout = step1) as tmp14:
stdout,stderr = tmp14.communicate()
with subprocess.Popen(cmd4, stdout = step1) as tmp15:
stdout,stderr = tmp15.communicate()
step1.write (" Success Only root can read and modify grub config\n")
step1.write (" Ensure authentication required for single user mode\n")
with open("/usr/lib/systemd/system/rescue.service" , "a+" , buffering = 1)as tmp:
tmp.write("ExecStart=-/usr/lib/systemd/systemd-sulogin-shell rescue")
tmp.close()
with open("/usr/lib/systemd/system/emergency.service" , "a+" , buffering = 1)as tmp:
tmp.write("ExecStart=-/usr/lib/systemd/systemd-sulogin-shell emergency")
tmp.close()
step1.write("# 1.6.1 Ensure core dumps are restricted\n")
with open ("/etc/security/limits.conf" , "rt" , buffering = 1 ) as tmp:
result_tmp = tmp.read()
tmp.close()
with open ("/etc/security/limits.conf" , "wt" , buffering = 1 ) as tmp:
tmp.write(result_tmp)
tmp.write("\n* hard core 0\n")
tmp.close()
with open ("/etc/sysctl.conf" , "rt" , buffering = 1 ) as tmp:
result_tmp = tmp.read()
tmp.close()
with open ("/etc/sysctl.conf" , "wt" , buffering = 1 ) as tmp:
tmp.write(result_tmp)
tmp.write("\nfs.suid_dumpable = 0\n")
tmp.close()
with subprocess.Popen(["sysctl" , "-w" , "fs.suid_dumpable=0"], stdout = step1) as tmp16:
stdout,stderr = tmp16.communicate()
with open ("/etc/systemd/coredump.conf" , "rt" , buffering = 1 ) as tmp:
result_tmp = tmp.read()
tmp.close()
with open ("/etc/systemd/coredump.conf" , "wt" , buffering = 1 ) as tmp:
tmp.write(result_tmp)
tmp.write("\nStorage=none\n")
tmp.write("ProcessSizeMax=0\n")
tmp.close()
with subprocess.Popen(["systemctl" , "daemon-reload"], stdout = step1) as tmp17:
stdout,stderr = tmp17.communicate()
step1.write("1.6.2 Ensure address space layout randomization (ASLR) is enabled \n")
with open ("/etc/sysctl.conf" , "rt" , buffering = 1 ) as tmp:
result_tmp = tmp.read()
tmp.close()
with open ("/etc/sysctl.conf" , "wt" , buffering = 1 ) as tmp:
tmp.write(result_tmp)
tmp.write("\nkernel.randomize_va_space = 2\n")
tmp.close()
with subprocess.Popen(["sysctl" , "-w" , "kernel.randomize_va_space=2"], stdout = step1) as tmp18:
stdout,stderr = tmp18.communicate()
step1.write("# Ensure SELinux is installed\n")
cmd = ("dnf install libselinux")
cmd = cmd.split()
with subprocess.Popen(cmd , stdout = step1) as tmp20:
stdout,stderr = tmp20.communicate()
step1.write("# Ensure SELinux policy is configured\n")
cmd = "sestatus | grep Loaded"
with subprocess.Popen(['/bin/sh', '-c' , cmd], stdout = step1) as tmp21:
stdout,stderr = tmp21.communicate()
step1.write("#Ensure the SELinux state is enforcing\n")
cmd = "grep -E '^\s*SELINUX=enforcing' /etc/selinux/config"
with subprocess.Popen(['/bin/sh' , '-c' , cmd], stdout = step1) as tmp22:
stdout,stderr = tmp22.communicate()
step1.write("# Ensure SETroubleshoot is not installed\n")
cmd = "dnf -y remove setroubleshoot"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step1) as tmp23:
stdout,stderr = tmp23.communicate()
step1.write("# Ensure the MCS Translation Service (mcstrans) is not installed\n")
cmd = "dnf -y remove mcstrans"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout=step1) as tmp24:
stdout,stderr = tmp24.communicate()
step1.write("# Ensure message of the day is configured properly\n")
cmd = "echo 'Authorized uses only. All activity may be monitored and reported.' > /etc/motd"
with subprocess.Popen (['/bin/sh' , '-c' , cmd] , stdout = step1) as tmp25:
stdout,stderr = tmp25.communicate()
step1.write("# Ensure local login warning banner is configured properly\n")
cmd = "echo 'Authorized uses only. All activity may be monitored and reported.' > /etc/issue"
with subprocess.Popen (['/bin/sh' , '-c' , cmd] , stdout = step1) as tmp26:
stdout,stderr = tmp26.communicate()
step1.write("# Ensure remote login warning banner is configured properly\n")
cmd = "echo 'Authorized uses only. All activity may be monitored and reported.' > /etc/issue.net"
with subprocess.Popen (['/bin/sh' , '-c' , cmd] , stdout = step1) as tmp27:
stdout,stderr = tmp27.communicate()
step1.write("# Ensure permissions on /etc/motd are configured\n")
cmd = "chown root:root /etc/motd"
cmd = cmd.split()
cmd2 = "chmod u-x,go-wx /etc/motd"
with subprocess.Popen (cmd , stdout = step1) as tmp28:
stdout,stderr = tmp28.communicate()
with subprocess.Popen (['/bin/sh' , '-c' , cmd2] , stdout = step1) as tmp29:
stdout,stderr = tmp29.communicate()
step1.write("# Ensure permissions on /etc/issue are configured\n")
cmd = "chown root:root /etc/issue"
cmd = cmd.split()
cmd2 = "chmod u-x,go-wx /etc/issue"
with subprocess.Popen (cmd , stdout = step1) as tmp30:
stdout,stderr = tmp30.communicate()
with subprocess.Popen (['/bin/sh' , '-c' , cmd2] , stdout = step1) as tmp31:
stdout,stderr = tmp31.communicate()
step1.write("# Ensure permissions on /etc/issue.net are configured\n")
cmd = "chown root:root /etc/issue.net"
cmd = cmd.split()
cmd2 = "chmod u-x,go-wx /etc/issue.net"
with subprocess.Popen (cmd , stdout = step1) as tmp32:
stdout,stderr = tmp32.communicate()
with subprocess.Popen (['/bin/sh' , '-c' , cmd2] , stdout = step1) as tmp33:
stdout,stderr = tmp33.communicate()
step1.write("Ensure GDM login banner is configured")
with open("/etc/gdm3/greeter.dconf-defaults" , "a+" , buffering = 1)as tmp:
tmp.write("[org/gnome/login-screen]\n")
tmp.write("banner-message-enable=true \n")
tmp.write("banner-message-text='Authorized uses only. All activity may be monitored and reported.'")
tmp.close()
step1.write("# Ensure system-wide crypto policy is not legacy\n")
cmd = "update-crypto-policies --set FUTURE"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step1) as tmp34:
stdout,stderr = tmp34.communicate()
step1.close()
def step2():
with open("rhel8.txt", "a+" , buffering=1) as step2:
step2.write("Begin chapter 2")
step2.write("# Ensure xinetd is not installed\n")
cmd = "dnf -y remove xinetd"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2 ) as tmp1:
stdout,stderr = tmp1.communicate()
step2.write("# Ensure time synchronization is in use\n")
cmd = "dnf -y install chrony"
cmd = cmd.split()
with subprocess.Popen(cmd , stdout = step2) as tmp2:
stdout,stderr = tmp2.communicate()
step2.write("# Ensure chrony is configured \n")
with subprocess.Popen(['/bin/sh' , '-c' , 'grep -E "^(server|pool)" /etc/chrony.conf'], stdout = step2) as tmp3:
stdout,stderr = tmp3.communicate()
step2.flush()
step2.write("# 2.2.2 Ensure X Window System is not installed\n")
cmd = "apt-get remove -y xserver-xorg*"
cmd = cmd.split()
with subprocess.Popen(cmd,stdout = step2) as tmp4:
stdout,stderr = tmp4.communicate()
step2.write("# Ensure Avahi Server is not enabled\n")
cmd = "systemctl disable avahi-daemon"
cmd = cmd.split()
with subprocess.Popen(cmd , stdout = step2 ) as tmp7:
stdout,stderr = tmp7.communicate()
step2.write("# Ensure SNMP Server is not enabled\n")
cmd = "systemctl --now disable snmpd"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp8:
stderr,stdout = tmp8.communicate()
step2.write("# Ensure HTTP Proxy Server is not enabled\n")
cmd = "systemctl --now disable squid"
cmd = cmd.split()
with subprocess.Popen(cmd , stdout = step2 ) as tmp9:
stdout,stderr = tmp9.communicate()
step2.write("# 2.2.7 Ensure Samba is not enabled \n")
cmd = "systemctl --now disable smb"
cmd = cmd.split()
with subprocess.Popen(cmd , stdout = step2) as tmp10:
stderr,stdout = tmp10.communicate()
step2.write("# Ensure IMAP and POP3 server is not enabled\n")
cmd = "systemctl --now disable dovecot"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp11:
stdout,stderr = tmp11.communicate()
step2.write("# Ensure FTP Server is not enabled\n")
cmd = "systemctl --now disable vsftpd"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp13:
stdout,stderr = tmp13.communicate()
step2.write("# Ensure DNS Server is not enabled\n")
cmd = "systemctl --now disable named"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp14:
stdout,stderr = tmp14.communicate()
step2.write("# Ensure NFS is not enabled\n")
cmd = "systemctl --now disable nfs"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp15:
stdout,stderr = tmp15.communicate()
step2.write("# Ensure RPC is not enabled\n")
cmd = "systemctl disable rpcbind"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp16:
stdout,stderr = tmp16.communicate()
step2.write("# Ensure LDAP server is not enabled\n")
cmd = "systemctl disable slapd"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp17:
stdout,stderr = tmp17.communicate()
step2.write("# Ensure DHCP Server is not enabled\n")
cmd = "systemctl disable isc-dhcp-server"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp19:
stdout,stderr = tmp19.communicate()
os.system("systemctl disable isc-dhcp-server6")
step2.write("# Ensure CUPS is not enabled\n")
cmd = "systemctl disable cups"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp20:
stdout,stderr = tmp20.communicate()
step2.write("# Ensure NIS Server is not enabled\n")
cmd = "systemctl --now disable ypserv"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp21:
stdout,stderr = tmp21.communicate()
step2.write("# Ensure NIS Client is not installed \n")
cmd = "dnf -y remove ypbind"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp22:
stdout,stderr = tmp22.communicate()
step2.write("# Ensure telnet client is not installed \n")
cmd = "dnf -y remove telnet"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp23:
stdout,stderr = tmp23.communicate()
step2.write("# Ensure LDAP client is not installed \n")
cmd = "dnf -y remove openldap-clients"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step2) as tmp24:
stdout,stderr = tmp24.communicate()
step2.close()
def step3():
with open("rhel8.txt", "a+" , buffering=1) as step3:
step3.write("# 3.1.1 Ensure IP forwarding is disabled\n")
with open("/etc/sysctl.conf" , "a+" , buffering = 1) as kernel0:
kernel0.write("net.ipv4.ip_forward = 0\n")
kernel0.write("net.ipv6.conf.all.forwarding = 0\n")
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.ip_forward=0'] , stdout = step3) as tmp01:
stdout,stderr = tmp01.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv6.conf.all.forwarding=0'] , stdout = step3) as tmp02:
stdout,stderr = tmp02.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.route.flush=1'] , stdout = step3) as tmp03:
stdout,stderr = tmp03.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv6.route.flush=1'] , stdout = step3) as tmp04:
stdout,stderr = tmp04.communicate()
step3.write("# Ensure packet redirect sending is disabled\n")
with open("/etc/sysctl.conf" , "a+" , buffering = 1) as kernel:
kernel.write("net.ipv4.conf.all.send_redirects = 0\n")
kernel.write("net.ipv4.conf.default.send_redirects = 0\n")
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.conf.all.send_redirects=0'] , stdout = step3 ) as tmp3:
stdout,stderr = tmp3.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.conf.default.send_redirects=0'] , stdout = step3 ) as tmp4:
stdout,stderr = tmp4.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.route.flush=1'] , stdout = step3 ) as tmp5:
stdout,stderr = tmp5.communicate()
step3.write("# Ensure source routed packets are not accepted\n")
with open("/etc/sysctl.conf" , "a+" , buffering = 1) as kernel1:
kernel1.write("net.ipv4.conf.all.accept_source_route = 0\n")
kernel1.write("net.ipv4.conf.default.accept_source_route = 0\n")
kernel1.write("net.ipv6.conf.all.accept_source_route = 0\n")
kernel1.write("net.ipv6.conf.default.accept_source_route = 0\n")
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.conf.all.accept_source_route=0'], stdout = step3 ) as tmp6:
stdout,stderr = tmp6.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.conf.default.accept_source_route=0'], stdout = step3 ) as tmp7:
stdout,stderr = tmp7.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv6.conf.all.accept_source_route=0'], stdout = step3 ) as tmp8:
stdout,stderr = tmp8.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv6.conf.default.accept_source_route=0'], stdout = step3 ) as tmp9:
stdout,stderr = tmp9.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.route.flush=1'], stdout = step3 ) as tmp10:
stdout,stderr = tmp10.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv6.route.flush=1'], stdout = step3 ) as tmp11:
stdout,stderr = tmp11.communicate()
step3.write("# Ensure ICMP redirects are not accepted\n")
with open ("/etc/sysctl.conf" , "a+" , buffering = 1) as kernel2:
kernel2.write("net.ipv4.conf.all.accept_redirects = 0\n")
kernel2.write("net.ipv4.conf.default.accept_redirects = 0\n")
kernel2.write("net.ipv6.conf.all.accept_redirects = 0\n")
kernel2.write("net.ipv6.conf.default.accept_redirects = 0\n")
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.conf.all.accept_redirects=0'], stdout = step3) as tmp12:
stdout,stderr = tmp12.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.conf.default.accept_redirects=0'], stdout = step3) as tmp13:
stdout,stderr = tmp13.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv6.conf.all.accept_redirects=0'], stdout = step3) as tmp14:
stdout,stderr = tmp14.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv6.conf.default.accept_redirects=0'], stdout = step3) as tmp15:
stdout,stderr = tmp15.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.route.flush=1'], stdout = step3) as tmp16:
stdout,stderr = tmp16.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv6.route.flush=1'], stdout = step3) as tmp17:
stdout,stderr = tmp17.communicate()
step3.write("Ensure secure ICMP redirects are not accepted\n")
with open ("/etc/sysctl.conf" , "a+" , buffering = 1) as kernel3:
kernel3.write("net.ipv4.conf.all.secure_redirects = 0\n")
kernel3.write("net.ipv4.conf.default.secure_redirects = 0\n")
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.conf.all.secure_redirects=0'], stdout = step3) as tmp18:
stdout,stderr = tmp18.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.conf.default.secure_redirects=0'], stdout = step3) as tmp19:
stdout,stderr = tmp19.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.route.flush=1'], stdout = step3) as tmp20:
stdout,stderr = tmp20.communicate()
step3.write("# Ensure suspicious packets are logged\n")
with open ("/etc/sysctl.conf" , "a" , buffering = 1) as kernel4:
kernel4.write("net.ipv4.conf.all.log_martians = 1\n")
kernel4.write("net.ipv4.conf.default.log_martians = 1\n")
with subprocess.Popen(['sysctl' , '-w' , 'sysctl -w net.ipv4.conf.all.log_martians=1'], stdout = step3) as tmp21:
stdout,stderr = tmp21.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'sysctl -w net.ipv4.conf.default.log_martians=1'], stdout = step3) as tmp22:
stdout,stderr = tmp22.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'sysctl -w net.ipv4.route.flush=1'], stdout = step3) as tmp23:
stdout,stderr = tmp23.communicate()
step3.write("# Ensure broadcast ICMP requests are ignored\n")
with open ("/etc/sysctl.conf" , "a" , buffering = 1) as kernel5:
kernel5.write("net.ipv4.icmp_echo_ignore_broadcasts = 1\n")
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.icmp_echo_ignore_broadcasts=1'] , stdout = step3) as tmp24:
stdout,stderr = tmp24.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.route.flush=1'] , stdout = step3) as tmp25:
stdout,stderr = tmp25.communicate()
step3.write("# Ensure bogus ICMP responses are ignored\n")
with open ("/etc/sysctl.conf" , "a+" , buffering = 1) as kernel6:
kernel6.write("net.ipv4.icmp_ignore_bogus_error_responses = 1\n")
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.icmp_ignore_bogus_error_responses=1'] , stdout = step3) as tmp26:
stdout,stderr = tmp26.communicate()
with subprocess.Popen(['sysctl' , '-c' , 'net.ipv4.route.flush=1'] , stdout = step3) as tmp27:
stdout,stderr = tmp27.communicate()
step3.write("# Ensure Reverse Path Filtering is enabled\n")
with open ("/etc/sysctl.conf" , "a" , buffering = 1) as kernel7:
kernel7.write("net.ipv4.conf.default.rp_filter=1\n")
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.conf.default.rp_filter=1'] , stdout = step3) as tmp28:
stdout,stderr = tmp28.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.route.flush=1'] , stdout = step3) as tmp29:
stdout,stderr = tmp29.communicate()
step3.write("# Ensure TCP SYN Cookies is enabled\n")
with open ("/etc/sysctl.conf" , "a" , buffering = 1) as kernel8:
kernel8.write("net.ipv4.tcp_syncookies = 1\n")
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.tcp_syncookies=1'] , stdout = step3) as tmp30:
stdout,stderr = tmp30.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv4.route.flush=1'] , stdout = step3) as tmp31:
stdout,stderr = tmp31.communicate()
step3.write("# Ensure IPv6 router advertisements are not accepted\n")
with open ("/etc/sysctl.conf" , "a" , buffering = 1) as kernel9:
kernel9.write("net.ipv6.conf.all.accept_ra = 0\n")
kernel9.write("net.ipv6.conf.default.accept_ra = 0\n")
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv6.conf.all.accept_ra=0'] , stdout = step3) as tmp32:
stdout,stderr = tmp32.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv6.conf.default.accept_ra=0'] , stdout = step3) as tmp33:
stdout,stderr = tmp33.communicate()
with subprocess.Popen(['sysctl' , '-w' , 'net.ipv6.route.flush=1=0'] , stdout = step3) as tmp34:
stdout,stderr = tmp34.communicate()
step3.write("# Ensure DCCP is disabled\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'modprobe -r -n dccp'] , stdout = step3) as tmp35:
stdout,stderr = tmp35.communicate()
step3.write("# Ensure SCTP is disabled\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'modprobe -r -n sctp'] , stdout = step3) as tmp36:
stdout,stderr = tmp36.communicate()
step3.write("# Ensure RDS is disabled\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'modprobe -r -n rds'] , stdout = step3) as tmp37:
stdout,stderr = tmp37.communicate()
step3.write("# Ensure TIPC is disabled\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'modprobe -r -n tipc'] , stdout = step3) as tmp38:
stdout,stderr = tmp38.communicate()
step3.write("# Ensure a Firewall package is installed\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'dnf -y install firewalld'] , stdout = step3) as tmp39:
stdout,stderr = tmp39.communicate()
step3.write("# Ensure firewalld service is enabled and running\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'systemctl --now enable firewalld'] , stdout = step3) as tmp40:
stdout,stderr = tmp40.communicate()
step3.write("# Ensure nftables is not enabled\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'systemctl --now mask nftables'] , stdout = step3) as tmp41:
stdout,stderr = tmp41.communicate()
step3.write("# Ensure default zone is set\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'firewall-cmd --set-default-zone=public'] , stdout = step3) as tmp42:
stdout,stderr = tmp42.communicate()
step3.write("# Ensure iptables is not enabled\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'systemctl --now mask iptables'] , stdout = step3) as tmp43:
stdout,stderr = tmp43.communicate()
step3.write("# Ensure iptables are flushed\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'ip6tables -F'] , stdout = step3) as tmp44:
stdout,stderr = tmp44.communicate()
with subprocess.Popen(['/bin/sh' , '-c' , 'iptables -F'] , stdout = step3) as tmp45:
stdout,stderr = tmp45.communicate()
step3.write("# Ensure wireless interfaces are disabled\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'nmcli radio all off'] , stdout = step3) as tmp46:
stdout,stderr = tmp46.communicate()
step3.close()
def step4():
with open("rhel8.txt", "a+" , buffering=1) as step4:
step4.write("# Ensure auditd is installed\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'dnf -y install audit audit-libs'] , stdout = step4) as tmp1:
stdout,stderr = tmp1.communicate()
step4.write("# Ensure auditd service is enabled\n")
with subprocess.Popen(['/bin/sh' , '-c' , 'systemctl --now enable auditd'] , stdout = step4) as tmp2:
stdout,stderr = tmp2.communicate()
step4.write("# Ensure auditing for processes that start prior to auditd is enabled\n")
with open("/etc/default/grub" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
result_tmp = result_tmp.rstrip()
result_tmp = re.sub('GRUB_CMDLINE.*' , 'GRUB_CMDLINE_LINUX="audit=1 crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet"' , result_tmp )
tmp.close()
with open("/etc/default/grub" , "wt" , buffering = 1) as tmp:
tmp.write(result_tmp)
tmp.close()
with subprocess.Popen(['/bin/sh' , '-c' , 'grub2-mkconfig -o /boot/grub2/grub.cfg'] , stdout = step4) as tmp3:
stdout,stderr = tmp3.communicate()
step4.write("# Ensure audit_backlog_limit is sufficient\n")
with open("/etc/default/grub" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
result_tmp = result_tmp.rstrip()
result_tmp = re.sub('GRUB_CMDLINE.*' , 'GRUB_CMDLINE_LINUX="audit_backlog_limit=8192 audit=1 crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet"' , result_tmp )
tmp.close()
with open("/etc/default/grub" , "wt" , buffering = 1) as tmp:
tmp.write(result_tmp)
tmp.close()
with subprocess.Popen(['/bin/sh' , '-c' , 'grub2-mkconfig -o /boot/grub2/grub.cfg'] , stdout = step4) as tmp4:
stdout,stderr = tmp4.communicate()
step4.write("# Ensure audit log storage size is configured to 20 MB\n")
with open("/etc/audit/auditd.conf" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
result_tmp = result_tmp.rstrip()
result_tmp = re.sub('max_log_file =.*' , 'max_log_file = 20' , result_tmp )
tmp.close()
with open("/etc/audit/auditd.conf" , "wt" , buffering = 1) as tmp:
tmp.write(result_tmp)
tmp.close()
step4.write("# Ensure audit logs are not automatically deleted\n")
with open("/etc/audit/auditd.conf" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
result_tmp = result_tmp.rstrip()
result_tmp = re.sub('max_log_file_action.*' , 'max_log_file_action = keep_logs' , result_tmp )
tmp.close()
with open("/etc/audit/auditd.conf" , "wt" , buffering = 1) as tmp:
tmp.write(result_tmp)
tmp.close()
step4.write("# Ensure system is disabled when audit logs are full\n")
with open("/etc/audit/auditd.conf" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
result_tmp = result_tmp.rstrip()
result_tmp = re.sub('space_left_action.*' , 'space_left_action = email' , result_tmp )
result_tmp = re.sub('action_mail_acct.*' , 'action_mail_acct = root' , result_tmp )
result_tmp = re.sub('admin_space_left_action.*' , 'admin_space_left_action = halt' , result_tmp )
tmp.close()
with open("/etc/audit/auditd.conf" , "wt" , buffering = 1) as tmp:
tmp.write(result_tmp)
tmp.close()
step4.write("# Ensure changes to system administration scope (sudoers) is collected \n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("## Ensure Sudoers is Collected\n")
tmp.write ("-w /etc/sudoers -p wa -k scope\n")
tmp.write ("-w /etc/sudoers.d/ -p wa -k scope\n")
tmp.close()
step4.write("# Ensure login and logout events are collected\n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure Login and Logout Events Are Collected\n")
tmp.write ("-w /var/log/faillog -p wa -k logins\n")
tmp.write ("-w /var/log/lastlog -p wa -k logins\n")
tmp.close()
step4.write("# Ensure session initiation information is collected\n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure session initiation Collected\n")
tmp.write ("-w /var/run/utmp -p wa -k session\n")
tmp.write ("-w /var/log/wtmp -p wa -k logins\n")
tmp.write ("-w /var/log/btmp -p wa -k logins\n")
tmp.close()
step4.write("# Ensure events that modify date and time information are collected\n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure session initiation Collected\n")
tmp.write ("-a always,exit -F arch=b64 -S adjtimex -S settimeofday -k time-change\n")
tmp.write ("-a always,exit -F arch=b32 -S adjtimex -S settimeofday -S stime -k timechange\n")
tmp.write ("-a always,exit -F arch=b64 -S clock_settime -k time-change\n")
tmp.write ("-a always,exit -F arch=b32 -S clock_settime -k time-change\n")
tmp.write ("-w /etc/localtime -p wa -k time-change\n")
tmp.close()
step4.write("# Ensure events that modify the system's Mandatory Access Controls are collected\n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure events that modify the system's Mandatory Access Controls are collected\n")
tmp.write ("-w /etc/selinux/ -p wa -k MAC-policy\n")
tmp.write ("-w /usr/share/selinux/ -p wa -k MAC-policy\n")
tmp.close()
step4.write("# Ensure events that modify the system's network environment are collected\n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure events that modify the system's network environment are collected\n")
tmp.write ("-a always,exit -F arch=b64 -S sethostname -S setdomainname -k system-locale\n")
tmp.write ("-a always,exit -F arch=b32 -S sethostname -S setdomainname -k system-locale\n")
tmp.write ("-w /etc/issue -p wa -k system-locale\n")
tmp.write ("-w /etc/issue.net -p wa -k system-locale\n")
tmp.write ("-w /etc/hosts -p wa -k system-locale\n")
tmp.write ("-w /etc/sysconfig/network -p wa -k system-locale\n")
tmp.close()
step4.write("# Ensure discretionary access control permission modification events are collected \n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure discretionary access control permission modification events are collected\n")
tmp.write ("-a always,exit -F arch=b64 -S chmod -S fchmod -S fchmodat -F auid>=1000 -F auid!=4294967295 -k perm_mod\n")
tmp.write ("-a always,exit -F arch=b32 -S chmod -S fchmod -S fchmodat -F auid>=1000 -F auid!=4294967295 -k perm_mod\n")
tmp.write ("-a always,exit -F arch=b64 -S chown -S fchown -S fchownat -S lchown -F auid>=1000 -F auid!=4294967295 -k perm_mod\n")
tmp.write ("-a always,exit -F arch=b32 -S chown -S fchown -S fchownat -S lchown -F auid>=1000 -F auid!=4294967295 -k perm_mod\n")
tmp.write ("-a always,exit -F arch=b64 -S setxattr -S lsetxattr -S fsetxattr -S removexattr -S lremovexattr -S fremovexattr -F auid>=1000 -F auid!=4294967295 -k perm_mod\n")
tmp.write ("-a always,exit -F arch=b32 -S setxattr -S lsetxattr -S fsetxattr -S removexattr -S lremovexattr -S fremovexattr -F auid>=1000 -F auid!=4294967295 -k perm_mod\n")
tmp.close()
step4.write("# Ensure unsuccessful unauthorized file access attempts are collected \n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure unsuccessful unauthorized file access attempts are collected\n")
tmp.write ("-a always,exit -F arch=b64 -S creat -S open -S openat -S truncate -S ftruncate -F exit=-EACCES -F auid>=1000 -F auid!=4294967295 -k access\n")
tmp.write ("-a always,exit -F arch=b32 -S creat -S open -S openat -S truncate -S ftruncate -F exit=-EACCES -F auid>=1000 -F auid!=4294967295 -k access\n")
tmp.write ("-a always,exit -F arch=b64 -S creat -S open -S openat -S truncate -S ftruncate -F exit=-EPERM -F auid>=1000 -F auid!=4294967295 -k access\n")
tmp.write ("-a always,exit -F arch=b32 -S creat -S open -S openat -S truncate -S ftruncate -F exit=-EPERM -F auid>=1000 -F auid!=4294967295 -k access\n")
tmp.close()
step4.write("# Ensure events that modify user/group information are collected\n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure events that modify user/group information are collected\n")
tmp.write ("-w /etc/group -p wa -k identity\n")
tmp.write ("-w /etc/passwd -p wa -k identity\n")
tmp.write ("-w /etc/gshadow -p wa -k identity\n")
tmp.write ("-w /etc/shadow -p wa -k identity\n")
tmp.write ("-w /etc/security/opasswd -p wa -k identity\n")
tmp.close()
step4.write("# Ensure successful file system mounts are collected \n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure successful file system mounts are collected\n")
tmp.write ("-a always,exit -F arch=b64 -S mount -F auid>=1000 -F auid!=4294967295 -k mounts\n")
tmp.write ("-a always,exit -F arch=b32 -S mount -F auid>=1000 -F auid!=4294967295 -k mounts\n")
tmp.close()
step4.write("# Ensure use of privileged commands is collected \n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure use of privileged commands is collected\n")
cmd = """ find / -xdev \( -perm -4000 -o -perm -2000 \) -type f | awk '{print "-a always,exit -F path=" $1 " -F perm=x -F auid>='"$(awk '/^\s*UID_MIN/{print $2}' /etc/login.defs)"' -F auid!=4294967295 -k privileged" }' >> /etc/audit/rules.d/audit.rules """
with subprocess.Popen(['/bin/sh' , '-c' , cmd], stdout = step4 ) as tmp01:
stdout,stderr = tmp01.communicate()
tmp.close()
step4.write("# Ensure file deletion events by users are collected\n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure file deletion events by users are collected\n")
tmp.write ("-a always,exit -F arch=b64 -S unlink -S unlinkat -S rename -S renameat -F auid>=1000 -F auid!=4294967295 -k delete\n")
tmp.write ("-a always,exit -F arch=b32 -S unlink -S unlinkat -S rename -S renameat -F auid>=1000 -F auid!=4294967295 -k delete\n")
tmp.close()
step4.write("# Ensure kernel module loading and unloading is collected\n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure kernel module loading and unloading is collected\n")
tmp.write ("-w /sbin/insmod -p x -k modules\n")
tmp.write ("-w /sbin/rmmod -p x -k modules\n")
tmp.write ("-w /sbin/modprobe -p x -k modules\n")
tmp.write ("-a always,exit -F arch=b64 -S init_module -S delete_module -k modules\n")
tmp.close()
step4.write("# Ensure system administrator actions (sudolog) are collected\n")
with open("/etc/audit/rules.d/audit.rules" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/audit/rules.d/audit.rules" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure system administrator actions (sudolog) are collected\n")
tmp.write ("-w /var/log/sudo.log -p wa -k actions\n")
tmp.close()
step4.write("# Ensure the audit configuration is immutable\n")
with open("/etc/audit/rules.d/99-finalize.rules" , "wt" , buffering = 1) as tmp:
tmp.write ("\n## Ensure the audit configuration is immutable\n")
tmp.write ("-e 2\n")
tmp.close()
step4.write("# Ensure rsyslog is installed\n")
cmd = "dnf -y install rsyslog"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step4) as tmp04:
stderr,stdout = tmp04.communicate()
step4.write("# Ensure rsyslog Service is enabled\n")
cmd = "systemctl --now enable rsyslog"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step4) as tmp05:
stderr,stdout = tmp05.communicate()
step4.write("# Ensure rsyslog default file permissions configured\n")
with open("/etc/rsyslog.conf" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/rsyslog.conf" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure rsyslog default file permissions configured\n")
tmp.write ("$FileCreateMode 0640\n")
tmp.close()
cmd = "systemctl restart rsyslog"
cmd = cmd.split()
with subprocess.Popen(cmd, stdout = step4) as tmp06:
stderr,stdout = tmp06.communicate()
step4.write("# Ensure journald is configured to send logs to rsyslog\n")
with open ("/etc/systemd/journald.conf" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/systemd/journald.conf" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure journald is configured to send logs to rsyslog\n")
tmp.write ("ForwardToSyslog=yes\n")
tmp.close()
step4.write("# Ensure journald is configured to compress large log files\n")
with open ("/etc/systemd/journald.conf" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/systemd/journald.conf" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure journald is configured to compress large log files\n")
tmp.write ("Compress=yes\n")
tmp.close()
step4.write("# Ensure journald is configured to write logfiles to persistent disk \n")
with open ("/etc/systemd/journald.conf" , "rt" , buffering = 1) as tmp:
result_tmp = tmp.read()
tmp.close()
with open("/etc/systemd/journald.conf" , "wt" , buffering = 1) as tmp:
tmp.write (result_tmp)
tmp.write ("\n## Ensure journald is configured to write logfiles to persistent disk\n")
tmp.write ("Storage=persistent\n")
tmp.close()
step4.close()
def step5():
with open("rhel8.txt", "a+" , buffering=1) as step5:
step5.write("# Ensure cron daemon is enabled\n")
cmd = "systemctl --now enable crond"
cmd = cmd.split()
with subprocess.Popen (cmd , stdout = step5) as tmp01:
stdout,stderr = tmp01.communicate()
step5.write("# Ensure permissions on /etc/crontab are configured\n")
cmd = "chown root:root /etc/crontab"
cmd = cmd.split()
cmd2 = "chmod og-rwx /etc/crontab"
cmd2 = cmd2.split()
with subprocess.Popen (cmd , stdout = step5) as tmp02:
stdout,stderr = tmp02.communicate()
with subprocess.Popen (cmd2 , stdout = step5) as tmp03:
stdout,stderr = tmp03.communicate()
step5.write("# Ensure permissions on /etc/cron.hourly are configured\n")
cmd = "chown root:root /etc/cron.hourly"
cmd = cmd.split()
cmd2 = "chmod og-rwx /etc/cron.hourly"
cmd2 = cmd2.split()
with subprocess.Popen (cmd , stdout = step5) as tmp04:
stdout,stderr = tmp04.communicate()
with subprocess.Popen (cmd2 , stdout = step5) as tmp05:
stdout,stderr = tmp05.communicate()
step5.write("# Ensure permissions on /etc/cron.daily are configured\n")
cmd = "chown root:root /etc/cron.daily"
cmd = cmd.split()
cmd2 = "chmod og-rwx /etc/cron.daily"
cmd2 = cmd2.split()
with subprocess.Popen (cmd , stdout = step5) as tmp06:
stdout,stderr = tmp06.communicate()
with subprocess.Popen (cmd2 , stdout = step5) as tmp07:
stdout,stderr = tmp07.communicate()
step5.write("# Ensure permissions on /etc/cron.weekly are configured\n")
cmd = "chown root:root /etc/cron.weekly"
cmd = cmd.split()
cmd2 = "chmod og-rwx /etc/cron.weekly"
cmd2 = cmd2.split()
with subprocess.Popen (cmd , stdout = step5) as tmp08:
stdout,stderr = tmp08.communicate()
with subprocess.Popen (cmd2 , stdout = step5) as tmp09:
stdout,stderr = tmp09.communicate()
step5.write("# Ensure permissions on /etc/cron.monthly are configured\n")
cmd = "chown root:root /etc/cron.monthly"
cmd = cmd.split()
cmd2 = "chmod og-rwx /etc/cron.monthly"
cmd2 = cmd2.split()
with subprocess.Popen (cmd , stdout = step5) as tmp10:
stdout,stderr = tmp10.communicate()
with subprocess.Popen (cmd2 , stdout = step5) as tmp11:
stdout,stderr = tmp11.communicate()
step5.write("# Ensure permissions on /etc/cron.d are configured\n")
cmd = "chown root:root /etc/cron.d"
cmd = cmd.split()
cmd2 = "chmod og-rwx /etc/cron.d"
cmd2 = cmd2.split()
with subprocess.Popen (cmd , stdout = step5) as tmp12:
stdout,stderr = tmp12.communicate()
with subprocess.Popen (cmd2 , stdout = step5) as tmp13:
stdout,stderr = tmp13.communicate()
step5.write("# Ensure at/cron is restricted to authorized users\n")
cmd = "rm -rf /etc/cron.deny"
cmd = cmd.split()
cmd2 = "rm -rf /etc/at.deny"
cmd2 = cmd2.split()
cmd3 = "touch /etc/cron.allow"
cmd3 = cmd3.split()
cmd4 = "touch /etc/at.allow"
cmd4 = cmd4.split()
cmd5 = "chmod og-rwx /etc/cron.allow"
cmd5 = cmd5.split()
cmd6 = "chmod og-rwx /etc/at.allow"
cmd6 = cmd6.split()
cmd7 = "chown root:root /etc/cron.allow"
cmd7 = cmd7.split()
cmd8 = "chown root:root /etc/at.allow"
cmd8 = cmd8.split()
with subprocess.Popen (cmd , stdout = step5) as tmp14:
stdout,stderr = tmp14.communicate()
with subprocess.Popen (cmd2 , stdout = step5) as tmp15:
stdout,stderr = tmp15.communicate()
with subprocess.Popen (cmd3 , stdout = step5) as tmp16:
stdout,stderr = tmp16.communicate()
with subprocess.Popen (cmd4 , stdout = step5) as tmp17:
stdout,stderr = tmp17.communicate()
with subprocess.Popen (cmd5 , stdout = step5) as tmp18:
stdout,stderr = tmp18.communicate()
with subprocess.Popen (cmd6 , stdout = step5) as tmp19:
stdout,stderr = tmp19.communicate()
with subprocess.Popen (cmd7 , stdout = step5) as tmp20:
stdout,stderr = tmp20.communicate()
with subprocess.Popen (cmd8 , stdout = step5) as tmp21:
stdout,stderr = tmp21.communicate()
step5.write("# Ensure permissions on /etc/ssh/sshd_config are configured\n")
cmd = "chown root:root /etc/ssh/sshd_config"
cmd = cmd.split()
cmd2 = "chmod og-rwx /etc/ssh/sshd_config"
cmd2 = cmd2.split()
with subprocess.Popen (cmd , stdout = step5) as tmp22:
stdout,stderr = tmp22.communicate()
with subprocess.Popen (cmd2 , stdout = step5) as tmp23:
stdout,stderr = tmp23.communicate()
step5.write("# Ensure SSH access is limited\n")
step5.write("### Check Document On How to Access it\n")
step5.write("# Ensure permissions on SSH private host key files are configured\n")
with subprocess.Popen (['/bin/sh' , '-c' , "find /etc/ssh -xdev -type f -name 'ssh_host_*_key' -exec chown root:root {} \;"] , stdout = step5) as tmp24:
stdout,stderr = tmp24.communicate()
with subprocess.Popen (['/bin/sh' , '-c' , "find /etc/ssh -xdev -type f -name 'ssh_host_*_key' -exec chmod 0600 {} \;"] , stdout = step5) as tmp25:
stdout,stderr = tmp25.communicate()
step5.write("# Ensure permissions on SSH public host key files are configured \n")
with subprocess.Popen (['/bin/sh' , '-c' , "find /etc/ssh -xdev -type f -name 'ssh_host_*_key.pub' -exec chmod 0644 {} \;"] , stdout = step5) as tmp26:
stdout,stderr = tmp26.communicate()
with subprocess.Popen (['/bin/sh' , '-c' , "find /etc/ssh -xdev -type f -name 'ssh_host_*_key.pub' -exec chown root:root {} \;"] , stdout = step5) as tmp27:
stdout,stderr = tmp27.communicate()
step5.write("# Ensure SSH LogLevel to verbose\n")
with open ("/etc/ssh/sshd_config" , "rt" , buffering = 1 ) as sshd:
result_tmp = sshd.read()
sshd.close()
with open ("/etc/ssh/sshd_config" , "wt" , buffering = 1 ) as sshd:
result_tmp = re.sub(".*LogLevel.*" , "LogLevel VERBOSE" , result_tmp)
sshd.write(result_tmp)
sshd.close()
step5.write("# Ensure SSH X11 forwarding is disabled\n")
with open ("/etc/ssh/sshd_config" , "rt" , buffering = 1 ) as sshd:
result_tmp = sshd.read()
sshd.close()
with open ("/etc/ssh/sshd_config" , "wt" , buffering = 1 ) as sshd:
result_tmp = re.sub(".*X11Forwarding.*" , "X11Forwarding no" , result_tmp)
sshd.write(result_tmp)
sshd.close()
step5.write("# Ensure SSH MaxAuthTries is set to 4 or less\n")
with open ("/etc/ssh/sshd_config" , "rt" , buffering = 1 ) as sshd:
result_tmp = sshd.read()
sshd.close()
with open ("/etc/ssh/sshd_config" , "wt" , buffering = 1 ) as sshd:
result_tmp = re.sub(".*MaxAuthTries.*" , "MaxAuthTries 4" , result_tmp)
sshd.write(result_tmp)
sshd.close()
step5.write("# Ensure SSH IgnoreRhosts is enabled\n")
with open ("/etc/ssh/sshd_config" , "rt" , buffering = 1 ) as sshd:
result_tmp = sshd.read()
sshd.close()
with open ("/etc/ssh/sshd_config" , "wt" , buffering = 1 ) as sshd:
result_tmp = re.sub(".*IgnoreRhosts.*" , "IgnoreRhosts yes" , result_tmp)
sshd.write(result_tmp)
sshd.close()
step5.write("# Ensure SSH HostbasedAuthentication is disabled\n")
with open ("/etc/ssh/sshd_config" , "rt" , buffering = 1 ) as sshd:
result_tmp = sshd.read()
sshd.close()
with open ("/etc/ssh/sshd_config" , "wt" , buffering = 1 ) as sshd:
result_tmp = re.sub(".*HostbasedAuthentication.*" , "HostbasedAuthentication no" , result_tmp)
sshd.write(result_tmp)
sshd.close()
step5.write("# Ensure SSH root login is disabled\n")
with open ("/etc/ssh/sshd_config" , "rt" , buffering = 1 ) as sshd:
result_tmp = sshd.read()
sshd.close()
with open ("/etc/ssh/sshd_config" , "wt" , buffering = 1 ) as sshd:
result_tmp = re.sub(".*PermitRootLogin.*" , "PermitRootLogin no" , result_tmp)
sshd.write(result_tmp)
sshd.close()
step5.write("# Ensure SSH PermitEmptyPasswords is disabled\n")
with open ("/etc/ssh/sshd_config" , "rt" , buffering = 1 ) as sshd:
result_tmp = sshd.read()
sshd.close()
with open ("/etc/ssh/sshd_config" , "wt" , buffering = 1 ) as sshd:
result_tmp = re.sub(".*PermitEmptyPasswords.*" , "PermitEmptyPasswords no" , result_tmp)
sshd.write(result_tmp)
sshd.close()
step5.write("# Ensure SSH PermitUserEnvironment is disabled\n")
with open ("/etc/ssh/sshd_config" , "rt" , buffering = 1 ) as sshd:
result_tmp = sshd.read()
sshd.close()
with open ("/etc/ssh/sshd_config" , "wt" , buffering = 1 ) as sshd:
result_tmp = re.sub(".*PermitUserEnvironment.*" , "PermitUserEnvironment no" , result_tmp)
sshd.write(result_tmp)
sshd.close()
step5.write("# Ensure SSH Idle Timeout Interval is configured\n")
with open ("/etc/ssh/sshd_config" , "rt" , buffering = 1 ) as sshd:
result_tmp = sshd.read()
sshd.close()
with open ("/etc/ssh/sshd_config" , "wt" , buffering = 1 ) as sshd:
result_tmp = re.sub(".*ClientAliveInterval.*" , "ClientAliveInterval 300" , result_tmp)
result_tmp = re.sub(".*ClientAliveCountMax.*" , "ClientAliveCountMax 0" , result_tmp)
sshd.write(result_tmp)
sshd.close()
step5.write("# Ensure SSH LoginGraceTime is set to one minute or less\n")
with open ("/etc/ssh/sshd_config" , "rt" , buffering = 1 ) as sshd:
result_tmp = sshd.read()
sshd.close()
with open ("/etc/ssh/sshd_config" , "wt" , buffering = 1 ) as sshd:
result_tmp = re.sub(".*LoginGraceTime.*" , "LoginGraceTime 60" , result_tmp)
sshd.write(result_tmp)
sshd.close()
step5.write("# Ensure SSH warning banner is configured\n")
with open ("/etc/ssh/sshd_config" , "rt" , buffering = 1 ) as sshd:
result_tmp = sshd.read()
sshd.close()