This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathShield.sh
1310 lines (1088 loc) · 44.8 KB
/
Shield.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
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
#!/bin/bash
auditd_configuration() {
# Installs auditd
apt install auditd
# Configures auditd
echo "
# Removes any existing auditd rules
-D
# Sets the buffer size, which may need to be increased, depending on the load of your system.
-b 8192
# Sets the failure mode to Failure Mode 1, which prints a failure message.
-f 2
# Audits the audit logs.
-w /var/log/audit/ -k auditlog
# Modifies the audit configuration,that occurs during the audit.
-w /etc/libaudit.conf -p wa -k auditconfig
-w /etc/audit/ -p wa -k auditconfig
-w /etc/audisp/ -p wa -k audispconfig
# Audits, user, group, and password databases
-w /etc/group -p wa -k etcgroup
-w /etc/passwd -p wa -k etcpasswd
-w /etc/gshadow -k etcgroup
-w /etc/shadow -k etcpasswd
-w /etc/security/opasswd -k opasswd
# Tools to change group identifiers
-w /usr/sbin/usermod -p x -k user_modification
-w /usr/sbin/addgroup -p x -k group_modification
-w /usr/sbin/adduser -p x -k user_modification
-w /usr/sbin/groupadd -p x -k group_modification
-w /usr/sbin/useradd -p x -k user_modification
-w /usr/sbin/groupmod -p x -k group_modification
# Monitors usage of passwd command
-w /usr/bin/passwd -p x -k passwd_modification
# Schedules cronjobs
-w /etc/cron.monthly/ -p wa -k cron
-w /etc/cron.hourly/ -p wa -k cron
-w /etc/crontab -p wa -k cron
-w /etc/cron.weekly/ -p wa -k cron
-w /etc/cron.d/ -p wa -k cron
-w /etc/cron.deny -p wa -k cron
-w /etc/cron.daily/ -p wa -k cron
-w /etc/cron.allow -p wa -k cron
-w /var/spool/cron/crontabs/ -k cron
# Login configuration and information
-w /etc/login.defs -p wa -k login
-w /etc/securetty -p wa -k login
-w /var/log/faillog -p wa -k login
-w /var/log/lastlog -p wa -k login
-w /var/log/tallylog -p wa -k login
# Network Environment
-w /etc/network/ -p wa -k network
-w /etc/hosts -p wa -k hosts
# Library search paths
-w /etc/ld.so.conf -p wa -k libpath
# Kernel parameters and module loading and unloading
-w /etc/sysctl.conf -p wa -k sysctl
-a always,exit -F perm=x -F auid!=-1 -F path=/sbin/insmod -k modules
-a always,exit -F perm=x -F auid!=-1 -F path=/sbin/modprobe -k modules
-a always,exit -F perm=x -F auid!=-1 -F path=/sbin/rmmod -k modules
-a always,exit -F arch=b64 -S finit_module -S init_module -S delete_module -F auid!=-1 -k modules
-a always,exit -F arch=b32 -S finit_module -S init_module -S delete_module -F auid!=-1 -k modules
# System startup scripts
-w /etc/init.d/ -p wa -k init
-w /etc/init/ -p wa -k init
-w /etc/inittab -p wa -k init
# SSH configuration
-w /etc/ssh/sshd_config -k sshd
# Changes to hostname
-a exit,always -F arch=b32 -S sethostname -k hostname
-a exit,always -F arch=b64 -S sethostname -k hostname
-a always,exit -F arch=b32 -S sethostname -S setdomainname -k network_modifications
-a always,exit -F arch=b64 -S sethostname -S setdomainname -k network_modifications
# Captures all failures to access on critical elements
-a exit,always -F arch=b64 -S open -F dir=/usr/local/bin -F success=0 -k unauthedfileacess
-a exit,always -F arch=b64 -S open -F dir=/bin -F success=0 -k unauthedfileacess
-a exit,always -F arch=b64 -S open -F dir=/var -F success=0 -k unauthedfileacess
-a exit,always -F arch=b64 -S open -F dir=/sbin -F success=0 -k unauthedfileacess
-a exit,always -F arch=b64 -S open -F dir=/home -F success=0 -k unauthedfileacess
-a exit,always -F arch=b64 -S open -F dir=/usr/sbin -F success=0 -k unauthedfileacess
-a exit,always -F arch=b64 -S open -F dir=/srv -F success=0 -k unauthedfileacess
-a exit,always -F arch=b64 -S open -F dir=/usr/bin -F success=0 -k unauthedfileacess
-a exit,always -F arch=b64 -S open -F dir=/etc -F success=0 -k unauthedfileacess
# Logs all commands executed by root
-a exit,always -F arch=b64 -F euid=0 -S execve -k rootcmd
-a exit,always -F arch=b32 -F euid=0 -S execve -k rootcmd
# Su and Sudo
-w /etc/sudoers -p rw -k priv_esc
-w /usr/bin/sudo -p x -k priv_esc
-w /bin/su -p x -k priv_esc
# Poweroffs and reboots tools
-w /sbin/shutdown -p x -k power
-w /sbin/halt -p x -k power
-w /sbin/poweroff -p x -k power
-w /sbin/reboot -p x -k power
# Monitors for use of audit management tools
-w /sbin/auditd -p x -k audittools
-w /sbin/auditctl -p x -k audittools
# Prevents chrony from overwhelming the logs
-a never,exit -F arch=b64 -S adjtimex -F auid=unset -F uid=chrony -F subj_type=chronyd_t
# Ignore End of Event records
-a always,exclude -F msgtype=EOE
# Ignore current working directory records
-a always,exclude -F msgtype=CWD
# VMWare tools
-a exit,never -F arch=b32 -S fork -F success=0 -F path=/usr/lib/vmware-tools -F subj_type=initrc_t -F exit=-2
-a exit,never -F arch=b64 -S fork -F success=0 -F path=/usr/lib/vmware-tools -F subj_type=initrc_t -F exit=-2
# High Volume Event Filter
-a exit,never -F arch=b32 -F dir=/dev/shm -k sharedmemaccess
-a exit,never -F arch=b64 -F dir=/dev/shm -k sharedmemaccess
-a exit,never -F arch=b32 -F dir=/var/lock/lvm -k locklvm
-a exit,never -F arch=b64 -F dir=/var/lock/lvm -k locklvm
# Modprobe configuration
-w /etc/modprobe.conf -p wa -k modprobe
# KExec usage
-a always,exit -F arch=b32 -S sys_kexec_load -k KEXEC
-a always,exit -F arch=b64 -S kexec_load -k KEXEC
# Special files
-a exit,always -F arch=b32 -S mknod -S mknodat -k specialfiles
-a exit,always -F arch=b64 -S mknod -S mknodat -k specialfiles
# Mount operations
-a always,exit -F arch=b64 -S mount -S umount2 -F auid!=-1 -k mount
-a always,exit -F arch=b32 -S mount -S umount -S umount2 -F auid!=-1 -k mount
# Local time zone
-w /etc/localtime -p wa -k localtime
# Stunnel
-w /usr/sbin/stunnel -p x -k stunnel
# Sudoers file changes
-w /etc/sudoers -p wa -k actions
# Changes to other files
-a always,exit -F dir=/etc/NetworkManager/ -F perm=wa -k network_modifications
-w /etc/sysconfig/network -p wa -k network_modifications
-w /etc/hosts -p wa -k network_modifications
-w /etc/sysconfig/network -p wa -k network_modifications
-w /etc/network/ -p wa -k network
# Time
-a exit,always -F arch=b32 -S adjtimex -S settimeofday -S clock_settime -k time
-a exit,always -F arch=b64 -S adjtimex -S settimeofday -S clock_settime -k time
# Changes to /etc/issue
-w /etc/issue -p wa -k etcissue
-w /etc/issue.net -p wa -k etcissu
# Change swap
-a always,exit -F arch=b64 -S swapon -S swapoff -F auid!=-1 -k swap
-a always,exit -F arch=b32 -S swapon -S swapoff -F auid!=-1 -k swap
# Pam configuration
-w /etc/security/namespace.conf -p wa -k pam
-w /etc/security/namespace.init -p wa -k pam
-w /etc/security/limits.conf -p wa -k pam
-w /etc/pam.d/ -p wa -k pam
-w /etc/security/pam_env.conf -p wa -k pam
# Systemd
-w /bin/systemctl -p x -k systemd
-w /etc/systemd/ -p wa -k systemd
# Process ID change applications
-w /bin/su -p x -k priv_esc
-w /usr/bin/sudo -p x -k priv_esc
-w /etc/sudoers -p rw -k priv_esc
# Session initiation information
-w /var/run/utmp -p wa -k session
-w /var/log/btmp -p wa -k session
-w /var/log/wtmp -p wa -k session
# Discretionary Access Control (DAC) modifications
-a always,exit -F arch=b32 -S chmod -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S chown -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S fchmod -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S fchmodat -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S fchown -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S fchownat -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S fremovexattr -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S fsetxattr -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S lchown -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S lremovexattr -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S lsetxattr -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S removexattr -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b32 -S setxattr -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S chmod -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S chown -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S fchmod -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S fchmodat -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S fchown -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S fchownat -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S fremovexattr -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S fsetxattr -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S lchown -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S lremovexattr -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S lsetxattr -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S removexattr -F auid>=500 -F auid!=4294967295 -k perm_mod
-a always,exit -F arch=b64 -S setxattr -F auid>=500 -F auid!=4294967295 -k perm_mod
# Suspicious activities
-w /usr/bin/wireshark -p x -k susp_activity
-w /usr/bin/ssh -p x -k susp_activity
-w /usr/bin/curl -p x -k susp_activity
-w /usr/bin/ncat -p x -k susp_activity
-w /usr/bin/base64 -p x -k susp_activity
-w /usr/bin/wget -p x -k susp_activity
-w /usr/bin/rawshark -p x -k susp_activity
-w /bin/nc -p x -k susp_activity
-w /usr/bin/rdesktop -p x -k sbin_susp
-w /bin/netcat -p x -k susp_activity
-w /usr/bin/socat -p x -k susp_activity
# Sbin suspicious activity
-w /usr/sbin/traceroute -p x -k sbin_susp
-w /sbin/ifconfig -p x -k sbin_susp
-w /usr/sbin/tcpdump -p x -k sbin_susp
-w /sbin/iptables -p x -k sbin_susp
# Reconnaissance and information gathering
-w /usr/bin/whoami -p x -k recon
-w /etc/hostname -p r -k recon
-w /etc/issue -p r -k recon
# Injection, these rules watch for code injection by the ptrace facility
# This could indicate someone trying to do something malicious or just debugging
-a always,exit -F arch=b32 -S ptrace -k tracing
-a always,exit -F arch=b64 -S ptrace -k tracing
-a always,exit -F arch=b32 -S ptrace -F a0=0x4 -k code_injection
-a always,exit -F arch=b64 -S ptrace -F a0=0x4 -k code_injection
-a always,exit -F arch=b32 -S ptrace -F a0=0x5 -k data_injection
-a always,exit -F arch=b64 -S ptrace -F a0=0x5 -k data_injection
-a always,exit -F arch=b32 -S ptrace -F a0=0x6 -k register_injection
-a always,exit -F arch=b64 -S ptrace -F a0=0x6 -k register_injection
# Privilege Abuse, the purpose of this rule is to detect when an admin may be abusing power by looking in user's home directory.
-a always,exit -F dir=/home -F uid=0 -F auid>=1000 -F auid!=4294967295 -C auid!=obj_uid -k power_abuse
# Apt-get and dpkg
-w /usr/bin/apt-get -p x -k software_mgmt
-w /usr/bin/apt-add-repository -p x -k software_mgmt
-w /usr/bin/aptitude -p x -k software_mgmt
-w /usr/bin/dpkg -p x -k software_mgmt
# CHEF
-w /etc/chef -p wa -k soft_chef
# GDS specific secrets
-w /etc/puppet/ssl -p wa -k puppet_ssl
#IBM Bigfix BESClient
-a exit,always -F arch=b64 -S open -F dir=/opt/BESClient -F success=0 -k soft_besclient
-w /var/opt/BESClient/ -p wa -k soft_besclient
# Root command executions
-a exit,always -F arch=b64 -F euid=0 -S execve -k rootcmd
-a exit,always -F arch=b32 -F euid=0 -S execve -k rootcmd
# Unauthorized file Access
-a always,exit -F arch=b32 -S creat -S open -S openat -S open_by_handle_at -S truncate -S ftruncate -F exit=-EACCES -F auid>=500 -F auid!=4294967295 -k file_access
-a always,exit -F arch=b32 -S creat -S open -S openat -S open_by_handle_at -S truncate -S ftruncate -F exit=-EPERM -F auid>=500 -F auid!=4294967295 -k file_access
-a always,exit -F arch=b64 -S creat -S open -S openat -S open_by_handle_at -S truncate -S ftruncate -F exit=-EACCES -F auid>=500 -F auid!=4294967295 -k file_access
-a always,exit -F arch=b64 -S creat -S open -S openat -S open_by_handle_at -S truncate -S ftruncate -F exit=-EPERM -F auid>=500 -F auid!=4294967295 -k file_access
# File Deletion Events by User
-a always,exit -F arch=b32 -S rmdir -S unlink -S unlinkat -S rename -S renameat -F auid>=500 -F auid!=4294967295 -k delete
-a always,exit -F arch=b64 -S rmdir -S unlink -S unlinkat -S rename -S renameat -F auid>=500 -F auid!=4294967295 -k delete
# Unsuccessful Creation
-a always,exit -F arch=b32 -S creat,link,mknod,mkdir,symlink,mknodat,linkat,symlinkat -F exit=-EACCES -k file_creation
-a always,exit -F arch=b64 -S mkdir,creat,link,symlink,mknod,mknodat,linkat,symlinkat -F exit=-EACCES -k file_creation
-a always,exit -F arch=b32 -S link,mkdir,symlink,mkdirat -F exit=-EPERM -k file_creation
-a always,exit -F arch=b64 -S mkdir,link,symlink,mkdirat -F exit=-EPERM -k file_creation
# Unsuccessful Modification
-a always,exit -F arch=b32 -S rename -S renameat -S truncate -S chmod -S setxattr -S lsetxattr -S removexattr -S lremovexattr -F exit=-EACCES -k file_modification
-a always,exit -F arch=b64 -S rename -S renameat -S truncate -S chmod -S setxattr -S lsetxattr -S removexattr -S lremovexattr -F exit=-EACCES -k file_modification
-a always,exit -F arch=b32 -S rename -S renameat -S truncate -S chmod -S setxattr -S lsetxattr -S removexattr -S lremovexattr -F exit=-EPERM -k file_modification
-a always,exit -F arch=b64 -S rename -S renameat -S truncate -S chmod -S setxattr -S lsetxattr -S removexattr -S lremovexattr -F exit=-EPERM -k file_modification
# Makes the configuration immutable
-e 2
" > /etc/audit/rules.d/audit.rules
systemctl enable auditd.service
service auditd restart
}
automatic_updates() {
# Enables automatic updates
apt-get install unattended-upgrades
dpkg-reconfigure -plow unattended-upgrades
}
disable_core_dumps() {
# Disables core dumps
echo "* hard core 0" >> /etc/security/limits.conf
echo "ulimit -c 0" >> /etc/profile
echo "ProcessSizeMax=0
Storage=none" >> /etc/systemd/coredump.conf
}
disable_firewire() {
# Disables firewire
echo "install udf /bin/true
blacklist firewire-core
blacklist firewire-ohci
blacklist firewire-sbp2" >> /etc/modprobe.d/blacklist.conf
}
disable_usb() {
# Disables usb
echo "blacklist usb-storage" >> /etc/modprobe.d/blacklist.conf
}
disable_uncommon_filesystems() {
# Disables uncommon filesystems
echo "install cramfs /bin/true
install freevxfs /bin/true
install hfs /bin/true
install hfsplus /bin/true
install jffs2 /bin/true
install squashfs /bin/true" >> /etc/modprobe.d/filesystems.conf
}
disable_uncommon_network_protocols() {
# Disables uncommon network protocols
echo "install dccp /bin/true
install sctp /bin/true
install tipc /bin/true
install rds /bin/true" >> /etc/modprobe.d/protocols.conf
apt-get --purge remove xinetd nis yp-tools tftpd atftpd tftpd-hpa telnetd rsh-server rsh-redone-server
}
setup_fail2ban() {
# Cleans out the local repository of retrieved package files that are left in /var/cache
apt clean
# Installs fail2ban
apt install fail2ban
# Enable fail2ban
systemctl start fail2ban && systemctl enable fail2ban
# Sets up fail2ban
echo -n "Would you like to change the ssh port on your system [y/N]?"
read -r qw
if [ "$qw" == "y" ]
then
echo -n "Please enter the port you like to change the ssh port to: "
read -r ty
echo"
bantime = 86400
findtime = 300
maxretry = 3
backend = systemd
banaction = iptables-multiport
banaction_allports = iptables-allports
[sshd]
enabled = true
[ssh]
enabled = true
port = $ty
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
" >> nano /etc/fail2ban/jail.local
fi
if [ "$qw" == "N" ]
then
echo "
bantime = 86400
findtime = 300
maxretry = 3
backend = systemd
banaction = iptables-multiport
banaction_allports = iptables-allports
[sshd]
enabled = true
[ssh]
enabled = true
port = 22
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
" >> nano /etc/fail2ban/jail.local
fi
# Restarts fail2ban
service fail2ban restart
}
iptable_configuration() {
# Installs Iptables
apt install iptables-persistent
# Flushes existing iptable rules
iptables -F
# Logs and drops packets
iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j LOG --log-prefix "IP_SPOOF A: "
iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP
# Maintains a list of recent connections to match against
iptables -A FORWARD -m recent --name portscan --rcheck --seconds 100 -j DROP
iptables -A FORWARD -p tcp -i eth0 --dport 443 -m recent --name portscan --set -j DROP
# Matching against a string in a packet's data payload
iptables -A FORWARD -m string --string '.com' -j DROP
iptables -A FORWARD -m string --string '.exe' -j DROP
# Time-based rules
iptables -A FORWARD -p tcp -m multiport --dport http,https -o eth0 -i eth1 -m time --timestart 21:30 --timestop 22:30 --days Mon,Tue,Wed,Thu,Fri -j ACCEPT
# Packet matching based on TTL values
iptables -A INPUT -s 1.2.3.4 -m ttl --ttl-lt 40 -j REJECT
# Configures Iptable Defaults
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
# Drops Spoofing attacks
iptables -A INPUT -s 10.0.0.0/8 -j DROP
iptables -A INPUT -s 169.254.0.0/16 -j DROP
iptables -A INPUT -s 172.16.0.0/12 -j DROP
iptables -A INPUT -s 127.0.0.0/8 -j DROP
iptables -A INPUT -s 192.168.0.0/24 -j DROP
iptables -A INPUT -s 224.0.0.0/4 -j DROP
iptables -A INPUT -d 224.0.0.0/4 -j DROP
iptables -A INPUT -s 240.0.0.0/5 -j DROP
iptables -A INPUT -d 240.0.0.0/5 -j DROP
iptables -A INPUT -s 0.0.0.0/8 -j DROP
iptables -A INPUT -d 0.0.0.0/8 -j DROP
iptables -A INPUT -d 239.255.255.0/24 -j DROP
iptables -A INPUT -d 255.255.255.255 -j DROP
# Accepts loopback input
iptables -A INPUT -i lo -p all -j ACCEPT
# Allows a three-way Handshake
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Drops packets with excessive RST to avoid Masked attacks
iptables -A INPUT -p tcp -m tcp --tcp-flags RST RST -m limit --limit 2/second --limit-burst 2 -j ACCEPT
# Stops Masked Attacks
iptables -A INPUT -p icmp --icmp-type 13 -j DROP
iptables -A INPUT -p icmp --icmp-type 14 -j DROP
iptables -A INPUT -p icmp --icmp-type 17 -j DROP
iptables -A INPUT -p icmp -m limit --limit 1/second -j ACCEPT
# Discards Invalid Packets
iptables -A INPUT -m state --state INVALID -j DROP
iptables -A FORWARD -m state --state INVALID -j DROP
iptables -A OUTPUT -m state --state INVALID -j DROP
# Allows ssh
iptables -A INPUT -p tcp -m tcp --dport 652 -j ACCEPT
# Allow one ssh connection at a time
iptables -A INPUT -p tcp --syn --dport 652 -m connlimit --connlimit-above 2 -j REJECT
# Allows Ping
iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
# Protection against port scanning
iptables -N port-scanning
iptables -A port-scanning -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s --limit-burst 2 -j RETURN
iptables -A port-scanning -j DROP
# SSH brute-force protection
iptables -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -m recent --set
iptables -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 10 -j DROP
# Syn-flood protection
iptables -N syn_flood
iptables -A INPUT -p tcp --syn -j syn_flood
iptables -A syn_flood -m limit --limit 1/s --limit-burst 3 -j RETURN
iptables -A syn_flood -j DROP
iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 1 -j ACCEPT
iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 1 -j LOG --log-prefix PING-DROP:
iptables -A INPUT -p icmp -j DROP
iptables -A OUTPUT -p icmp -j ACCEPT
# Mitigating SYN floods with SYNPROXY
iptables -t raw -A PREROUTING -p tcp -m tcp --syn -j CT --notrack
iptables -A INPUT -p tcp -m tcp -m conntrack --ctstate INVALID,UNTRACKED -j SYNPROXY --sack-perm --timestamp --wscale 7 --mss 1460
iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
# Block new packets that aren't SYN packets
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
# Force fragments packets check
iptables -A INPUT -f -j DROP
# XMAS packets
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
# Drop all null packets
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
# Block uncommon MSS values
iptables -t mangle -A PREROUTING -p tcp -m conntrack --ctstate NEW -m tcpmss ! --mss 536:65535 -j DROP
# Block packets with bogus TCP flags
iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,ACK FIN -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,URG URG -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,FIN FIN -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,PSH PSH -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL ALL -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL NONE -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
# Drops connections if the ip address makes more than 5 connection attempts to port 80 within 50 seconds
$IPT -I INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --set
$IPT -I INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --update --seconds 50 --hitcount 5 -j DROP
# Saves iptables rules
iptables-save > /etc/iptables/rules.v4
ip6tables-save > /etc/iptables/rules.v6
# Starts iptables
sudo ufw enable
}
kernel_configuration() {
# Configures the kernel
echo "net.ipv4.tcp_syncookies: 1
net.ipv4.tcp_synack_retries = 5
net.ipv4.conf.default.accept_source_route: 0
kernel.core_uses_pid: 1
net.ipv4.conf.default.rp_filter: 1
net.ipv4.conf.all.log_martians: 1
kernel.kptr_restrict: 2
net.ipv4.conf.default.secure_redirects: 1
net.ipv4.conf.default.accept_redirects: 0
kernel.sysrq: 0
net.ipv4.icmp_echo_ignore_all: 0
net.ipv4.ip_forward: 0
fs.protected_symlinks: 1
net.ipv4.tcp_rfc1337: 1
net.ipv4.icmp_echo_ignore_broadcasts: 1
net.ipv4.conf.all.rp_filter: 1
net.ipv4.conf.all.send_redirects: 0
net.ipv6.conf.all.forwarding: 0
net.ipv4.conf.all.accept_source_route: 0
net.ipv6.conf.default.accept_source_route: 0
net.ipv4.conf.default.log_martians: 1
net.ipv6.conf.all.accept_source_route: 0
net.ipv4.conf.all.secure_redirects: 1
fs.protected_hardlinks: 1
net.ipv4.conf.all.accept_redirects: 0
kernel.perf_event_paranoid: 2
net.ipv4.conf.default.send_redirects: 0
kernel.randomize_va_space: 2
net.ipv6.conf.all.accept_redirects: 0
net.ipv6.conf.default.accept_redirects: 0
net.ipv4.icmp_ignore_bogus_error_responses: 1
net.ipv4.conf.all.promote_secondaries : 1
fs.file-max : 65535
net.ipv4.ip_local_port_range : 2000 65000
net.core.rmem : 8388608
net.core.wmem_max : 8388608
net.core.netdev_max_backlog : 5000
net.ipv4.tcp_window_scaling : 1
kernel.exec-shield : 2
kernel.randomize_va_space : 2
net.ipv4.tcp_rmem : 4096 87380 8388608
net.ipv4.tcp_wmem : 4096 87380 8388608
net.ipv4.tcp_rfc1337 : 1
net.ipv6.conf.default.max_addresses : 1
net.ipv6.conf.default.dad_transmits : 0
net.ipv6.conf.default.autoconf : 0
net.ipv6.conf.default.accept_ra_defrtr : 0
net.ipv6.conf.default.accept_ra_pinfo : 0
net.ipv6.conf.default.router_solicitations : 0
kernel.panic : 10
kernel.randomize_va_space : 2
fs.protected_hardlinks : 1
fs.protected_symlinks : 1
kernel.yama.ptrace_scope: 1" > /etc/sysctl.d/80-lockdown.conf
sysctl --system
}
legal_banner() {
# Adds a legal banner to /etc/motd, /etc/issue and /etc/issue.net
echo "
Unauthorized access to this server is prohibited.
All connections are monitored and recorded.
Legal action will be taken. Please disconnect now.
" > /etc/motd
echo "
Unauthorized access to this server is prohibited.
All connections are monitored and recorded.
Legal action will be taken. Please disconnect now.
" > /etc/issue
echo "
Unauthorized access to this server is prohibited.
All connections are monitored and recorded.
Legal action will be taken. Please disconnect now.
" > /etc/issue.net
}
install_lynis_recommended_packages() {
# Cleans out the local repository of retrieved package files that are left in /var/cache
apt clean
# Installs lynis recommended packages
apt install apt-listchanges needrestart debsecan debsums libpam-cracklib aide usbguard acct
}
move_/tmp_to_/tmpfs() {
# Moves /tmp to /tmpfs
echo "tmpfs /tmp tmpfs rw,nosuid,nodev" >> /etc/fstab
}
purge_old_removed_packages() {
# Purges old and removed packages
apt autoremove
apt purge "$(dpkg -l | grep '^rc' | awk '{print $2}')"
}
remount_directories_with_restrictions() {
# Mounts /proc with hidepid=2
mount -o remount,rw,hidepid=2 /proc
# Mounts /tmp with noexec
mount -o remount,noexec /tmp
# Mount /dev with noexec
mount -o remount,noexec /dev
# Mounts /run as nodev
mount -o remount,nodev /run
# Keeps /boot as read only
echo "LABEL=/boot /boot ext2 defaults,ro 1 2" >> /etc/fstab
}
restrict_access_to_compilers() {
# Restricts access to compilers
v=$(which clang)
d=$(which gcc)
f=$(which c++)
h=$(which clang++)
r=$(which g++)
length=${#v}
lengt=${#d}
leng=${#f}
len=${#h}
le=${#r}
if [ "$length" -gt 0 ]
then
chmod o-x "$v"
chmod o-r "$v"
chmod o-w "$v"
fi
if [ "$lengt" -gt 0 ]
then
chmod o-x "$d"
chmod o-r "$d"
chmod o-w "$d"
fi
if [ "$leng" -gt 0 ]
then
chmod o-x "$f"
chmod o-r "$f"
chmod o-w "$f"
fi
if [ "$len" -gt 0 ]
then
chmod o-x "$h"
chmod o-r "$h"
chmod o-w "$h"
fi
if [ "$le" -gt 0 ]
then
chmod o-x "$r"
chmod o-r "$r"
chmod o-w "$r"
fi
if [ -d "/usr/bin/as" ]
then
chmod o-x /usr/bin/as
chmod o-r /usr/bin/as
chmod o-w /usr/bin/as
fi
}
restrict_logins() {
# Restricts logins by configuring login.defs
sed -i s/PASS_MIN_DAYS.*/PASS_MIN_DAYS\ 7/ /etc/login.defs
sed -i s/UMASK.*/UMASK\ 027/ /etc/login.defs
sed -i s/PASS_MAX_DAYS.*/PASS_MAX_DAYS\ 30/ /etc/login.defs
echo "SHA_CRYPT_MIN_ROUNDS 1000000
SHA_CRYPT_MAX_ROUNDS 100000000" >> /etc/login.defs
# Locks all empty password accounts
v=$(awk -F: '($2 == "") {print}' /etc/shadow)
d=length=${#v}
if [ "$d" -gt 0 ]
then
for i in $v
do
passwd -l "$i"
done
fi
}
secure_ssh() {
# Secures ssh
if [ "$qw" == "y" ]
then
echo "
ClientAliveCountMax 2
ClientAliveInterval 300
Compression no
LogLevel VERBOSE
MaxAuthTries 3
MaxSessions 2
TCPKeepAlive no
AllowAgentForwarding no
AllowTcpForwarding no
Port $ty
PasswordAuthentication no
AuthenticationMethods publickey
PubkeyAuthentication yes
ChallengeResponseAuthentication no
PermitEmptyPasswords no
HostbasedAuthentication no
IgnoreRhosts yes
Protocol 2
Ciphers aes128-ctr,aes192-ctr,aes256-ctr
X11Forwarding no
" >> /etc/ssh/sshd_config
sed -i s/^X11Forwarding.*/X11Forwarding\ no/ /etc/ssh/sshd_config
sed -i s/^UsePAM.*/UsePAM\ yes/ /etc/ssh/sshd_config
echo -n "Please enter the adminsters username:"
read -r e
echo "
AllowUsers $e
PermitRootLogin no
" >> /etc/ssh/sshd_config
fi
if [ "$qw" == "N" ]
then
echo "
ClientAliveCountMax 2
ClientAliveInterval 300
Compression no
LogLevel VERBOSE
MaxAuthTries 3
MaxSessions 2
TCPKeepAlive no
AllowAgentForwarding no
AllowTcpForwarding no
Port 22
PasswordAuthentication no
AuthenticationMethods publickey
PubkeyAuthentication yes
ChallengeResponseAuthentication no
PermitEmptyPasswords no
HostbasedAuthentication no
IgnoreRhosts yes
Protocol 2
Ciphers aes128-ctr,aes192-ctr,aes256-ctr
" >> /etc/ssh/sshd_config
sed -i s/^X11Forwarding.*/X11Forwarding\ no/ /etc/ssh/sshd_config
sed -i s/^UsePAM.*/UsePAM\ yes/ /etc/ssh/sshd_config
echo -n "Please enter the adminstrators username: "
read -r e
echo "
AllowUsers $e
PermitRootLogin no
" >> /etc/ssh/sshd_config
fi
}
setup_aide() {
# Cleans out the local repository of retrieved package files that are left in /var/cache
apt clean
# Installs and sets up aide
apt install aide
aideinit
cp /var/lib/aide/aide.db.new /var/lib/aide/aide.db
update-aide.conf
cp /var/lib/aide/aide.conf.autogenerated /etc/aide/aide.conf
}
update_upgrade() {
# Cleans out the local repository of retrieved package files that are left in /var/cache
apt clean
# Updates the package list
apt update
# Upgrades packages
apt upgrade
# Does a dist upgrade, which "intelligently" handles changing dependencies
apt dist-upgrade
}
setup_rkhunter_and_chkrootkit() {
# Cleans out the local repository of retrieved package files that are left in /var/cache
apt clean
# Installs chkrootkit
apt install chkrootkit
# Installs rkhunter
apt install rkhunter
# You need to specify where both softwares should look for the commands they require to run scans to detect rootkits
chkrootkit -p /mnt/safe
rkhunter --check --bindir /mnt/safe
}
disable_thunderbolt() {
# Disables Thunderbolt
echo "blacklist thunderbolt" >> /etc/modprobe.d/thunderbolt.conf
}
setup_psad() {
# Cleans out the local repository of retrieved package files that are left in /var/cache
apt clean
# Installs psad
apt install psad
# Sets up psad
echo -e "kern.info\t|/var/lib/psad/psadfifo" >> /etc/syslog.conf
/etc/init.d/sysklogd restart
/etc/init.d/klogd
echo -n "Please enter your email address to receive port scan detection messages:"
read -r la
echo -e "EMAIL_ADDRESSES $la;
ENABLE_AUTO_IDS Y;
IPTABLES_BLOCK_METHOD Y;
HOME_NET NOT_USED;
" >> /etc/psad/psad.conf
# Automatically removes all blocked ip addresses
psad -F
# Restarts psad to save configuaration information
/etc/init.d/psad restart
}
protect_physical_console_access() {
# Create a grub boot loader password
openssl passwd -1
echo "Please copy down this password"
echo -n "Please enter the hashed password that you were just given:"
read -r lye
echo "password --md5 $lye" >> /boot/grub/menu.lst
# Enables authentication for single-user mode
echo "~~:S:wait:/sbin/sulogin" >> /etc/inittab
# Disables interactive hotkey startup at boot
echo "PROMPT=no" >> /etc/sysconfig/init
# Automatically logsout Bash users
echo "
TMOUT=300
readonly TMOUT
export TMOUT" >> /etc/profile.d/autologout.sh
chmod +x /etc/profile.d/autologout.sh
# Disable Ctrl-Alt-Delete
sed -e '/ca::ctrlaltdel:/sbin/shutdown -t3 -r now/ s/^#*/#/' -i /etc/inittab
init q
sed -e '/exec /sbin/shutdown -r now "Control-Alt-Delete pressed"/ s/^#*/#/' -i /etc/event.d/control-alt-delete
# Configures root user timeout
echo "[ $UID -eq 0 ] && TMOUT=300" >> /etc/profiles
}
setup_lighttpd() {
# Cleans out the local repository of retrieved package files that are left in /var/cache
apt clean
# Installs lighttpd
apt install lighttpd
# Sets up lighttpd
echo "server.kbytes-per-second=1024" >> lighttpd.conf
service lighttpd reload
echo "connection.kbytes-per-second=64" >> lighttpd.conf
service lighttpd reload
}
setup_shorewall() {
# Cleans out the local repository of retrieved package files that are left in /var/cache
apt clean
# Installs shorewall
apt install shorewall shorewall-common shorewall-shell
# Sets up shorewall
echo "startup=1" >> /etc/default/shorewall
echo "#ZONE TYPE OPTIONS IN OUT
# OPTIONS OPTIONS
fw firewall
net ipv4" >> /etc/shorewall/zones
echo "#ZONE INTERFACE BROADCAST OPTIONS
net eth0 detect tcpflags,logmartians,nosmurfs
net eth1 detect dhcp
net ppp+ detect dhcp" >> /etc/shorewall/interfaces
echo "#SOURCE DEST POLICY LOG LEVEL LIMIT:
BURST
fw all ACCEPT
net all DROP info
all all REJECT info" >> /etc/shorewall/policy
}
install_logwatch() {
# Cleans out the local repository of retrieved package files that are left in /var/cache
apt clean
# Installs logcheck
apt install logwatch
}
setup_ufw() {
# Cleans out the local repository of retrieved package files that are left in /var/cache
apt clean
# Installs ufw
apt install ufw
# Sets up ufw
ufw default deny incoming
ufw default allow outgoing
ufw logging on
ufw deny 20/tcp
ufw deny 21/tcp
ufw deny 23/tcp
ufw deny 69/udp
ufw deny 110/tcp
ufw deny 143/tcp
ufw deny 161/udp