Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ### BEGIN INIT INFO
- systemctl set-default multi-user.target
- apt remove --purge rpcbind exim4 exim4-config exim4-base isc-dhcp-client isc-dhcp-common xinetd openbsd-inetd -y
- apt update && apt install tcpdump libpam-cracklib fail2ban ntp pigz haveged arpwatch apt-show-versions auditd locate open-vm-tools iptables-persistent needrestart libpam-tmpdir debian-goodies debsecan debsums apt-listbugs net-tools -y && updatedb
- cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
- echo "[sshd-ddos]" > /etc/fail2ban/jail.d/defaults-debian.conf
- echo "enabled = true" >> /etc/fail2ban/jail.d/defaults-debian.conf
- echo "#################"
- echo "# CIS Hardening #"
- echo "#################"
- echo 'install usb-storage /bin/true' >> /etc/modprobe.d/disable-usb-storage.conf
- echo 'install tipc /bin/true' >> /etc/modprobe.d/disable-tipc.conf
- echo "blacklist firewire-core" >> /etc/modprobe.d/blacklist-firewire.conf
- echo "blacklist usb-storage" >> /etc/modprobe.d/blacklist.conf
- echo "install cramfs /bin/false" > /etc/modprobe.d/CIS.conf
- echo "install freevxfs /bin/false" >> /etc/modprobe.d/CIS.conf
- echo "install jffs2 /bin/false" >> /etc/modprobe.d/CIS.conf
- echo "install hfs /bin/false" >> /etc/modprobe.d/CIS.conf
- echo "install hfsplus /bin/false" >> /etc/modprobe.d/CIS.conf
- echo "install squashfs /bin/false" >> /etc/modprobe.d/CIS.conf
- echo "install udf /bin/false" >> /etc/modprobe.d/CIS.conf
- echo "Authorized uses only. All activity may be monitored and reported." > /etc/issue
- echo "Authorized uses only. All activity may be monitored and reported." > /etc/issue.net
- sed 's/^PASS_MAX_DAYS.*99999/PASS_MAX_DAYS 365/' -i /etc/login.defs
- sed 's/^PASS_MIN_DAYS.*0/PASS_MIN_DAYS 7/' -i /etc/login.defs
- sed 's/^PASS_MIN_LEN.*5/PASS_MIN_LEN 8/' -i /etc/login.defs
- sed 's/^PASS_WARN_AGE.*7/PASS_WARN_AGE 14/' -i /etc/login.defs
- cat /dev/null > /etc/securetty
- echo "tty1" > /etc/securetty
- echo "tty2" >> /etc/securetty
- echo "tty3" >> /etc/securetty
- chmod 0600 /etc/securetty
- ### NTP
- cat /dev/null > /etc/ntp.conf
- touch /var/log/ntpstats/ntpd.log
- echo "server pool.ntp.org" > /etc/ntp.conf
- echo "driftfile /var/lib/ntp/drift" >> /etc/ntp.conf
- echo "logfile /var/log/ntpstats/ntpd.log" >> /etc/ntp.conf
- echo "restrict default nomodify notrap nopeer noquery" >> /etc/ntp.conf
- echo "restrict 127.0.0.1" >> /etc/ntp.conf
- echo "includefile /etc/ntp/crypto/pw" >> /etc/ntp.conf
- echo "keys /etc/ntp/keys" >> /etc/ntp.conf
- echo "#################"
- echo "# SSH Hardening #"
- echo "#################"
- # Details : AllowTcpForwarding (YES --> NO)
- sed 's/#AllowTcpForwarding yes/AllowTcpForwarding no/g' -i /etc/ssh/sshd_config
- # ClientAliveCountMax (3 --> 0)
- sed 's/#ClientAliveCountMax 3/ClientAliveCountMax 0/g' -i /etc/ssh/sshd_config
- # Compression (YES --> (DELAYED|NO))
- sed 's/#Compression delayed/Compression no/g' -i /etc/ssh/sshd_config
- # LogLevel (INFO --> VERBOSE)
- sed 's/#LogLevel INFO/LogLevel VERBOSE/g' -i /etc/ssh/sshd_config
- # MaxAuthTries (6 --> 2)
- sed 's/#MaxAuthTries 6/MaxAuthTries 1/g' -i /etc/ssh/sshd_config
- # MaxSessions (10 --> 2)
- sed 's/#MaxSessions 10/MaxSessions 2/g' -i /etc/ssh/sshd_config
- # PermitRootLogin (YES --> NO)
- sed 's/#PermitRootLogin WITHOUT-PASSWORD/PermitRootLogin NO/g' -i /etc/ssh/sshd_config
- # TCPKeepAlive (YES --> NO)
- sed 's/#TCPKeepAlive yes/TCPKeepAlive no/g' -i /etc/ssh/sshd_config
- # UseDNS (YES --> NO)
- sed 's/#UseDNS yes/UseDNS no/g' -i /etc/ssh/sshd_config
- # X11Forwarding (YES --> NO)
- sed 's/X11Forwarding yes/X11Forwarding no/g' -i /etc/ssh/sshd_config
- # AllowAgentForwarding (YES --> NO)
- sed 's/#AllowAgentForwarding yes/AllowAgentForwarding no/g' -i /etc/ssh/sshd_config
- # IgnoreRhosts (YES)
- sed 's/#IgnoreRhosts yes/IgnoreRhosts yes/g' -i /etc/ssh/sshd_config
- # PermitEmptyPasswords (NO)
- sed 's/#PermitEmptyPasswords no/PermitEmptyPasswords no/g' -i /etc/ssh/sshd_config
- # PermitUserEnvironment (NO)
- sed 's/#PermitUserEnvironment no/PermitUserEnvironment no/g' -i /etc/ssh/sshd_config
- # PermitRootLogin (NO)
- sed 's/#PermitRootLogin WITHOUT-PASSWORD/PermitRootLogin no/g' -i /etc/ssh/sshd_config
- echo "MACs [email protected],[email protected],[email protected],hmac-sha2-512,hmac-sha2-256,[email protected]" >> /etc/ssh/sshd_config
- echo "Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr" >> /etc/ssh/sshd_config
- echo "KexAlgorithms [email protected],ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256" >> /etc/ssh/sshd_config
- #echo "ClientAliveInterval 300" >> /etc/ssh/sshd_config
- echo "LoginGraceTime 60" >> /etc/ssh/sshd_config
- echo "Protocol 2" >> /etc/ssh/sshd_config
- echo "UseDNS no" >> /etc/ssh/sshd_config
- rm /etc/ssh/ssh_host_rsa_key* -f && ssh-keygen -q -f /etc/ssh/ssh_host_rsa_key -b 4096 -N ""
- rm /etc/ssh/ssh_host_ecdsa_key* -f && ssh-keygen -q -f /etc/ssh/ssh_host_ecdsa_key -b 4096 -N ""
- rm /etc/ssh/ssh_host_ed25519_key -f && ssh-keygen -q -f /etc/ssh/ssh_host_ed25519_key -N ""
- chown root:root /etc/motd && chmod 644 /etc/motd
- chown root:root /etc/issue && chmod 644 /etc/issue
- chown root:root /etc/issue.net && chmod 644 /etc/issue.net
- rm -f /root/.ssh/id*
- ssh-keygen -b 4096 -t rsa -N "" -f /root/.ssh/id_rsa && ssh-keygen -t ed25519 -N "" -f /root/.ssh/id_ed25519 && ssh-keygen -b 521 -t ecdsa -N "" -f /root/.ssh/id_ecdsa
- chmod 700 /root/.ssh
- chmod 0644 /etc/hosts.deny && chmod 644 /etc/hosts.allow
- chown root:root /etc/ssh/sshd_config && chmod 600 /etc/ssh/sshd_config
- chmod 0640 /etc/shadow && chmod 644 /etc/passwd && chmod 644 /etc/group && chown root:root /etc/passwd && chown root:root /etc/group
- chmod 0644 /etc/passwd && chown root /etc/passwd && chgrp root /etc/passwd
- chown root:root /etc/crontab
- chmod og-rwx /etc/crontab
- chown root:root /etc/cron.hourly
- chmod og-rwx /etc/cron.hourly
- chown root:root /etc/cron.daily
- chmod og-rwx /etc/cron.daily
- chown root:root /etc/cron.weekly
- chmod og-rwx /etc/cron.weekly
- chown root:root /etc/cron.monthly
- chmod og-rwx /etc/cron.monthly
- chown root:root /etc/cron.d
- chmod og-rwx /etc/cron.d
- rm /etc/cron.deny
- rm /etc/at.deny
- touch /etc/cron.allow
- touch /etc/at.allow
- chmod og-rwx /etc/cron.allow
- chmod og-rwx /etc/at.allow
- chown root:root /etc/cron.allow
- chown root:root /etc/at.allow
- echo " " >> /etc/crontab
- echo "# Borrado de logs .GZ cada Domingo a las 3am" >> /etc/crontab
- echo "0 3 * * sun root rm -f /var/log/*.gz" >> /etc/crontab
- ### Audit configuration
- sed 's/^max_log_file.*/max_log_file = 25/' -i /etc/audit/auditd.conf
- sed 's/^num_logs.*/num_logs = 4/' -i /etc/audit/auditd.conf
- sed 's/^space_left_action.*/space_left_action = email/' -i /etc/audit/auditd.conf
- sed 's/^action_mail_acct.*/action_mail_acct = root/' -i /etc/audit/auditd.conf
- sed 's/^admin_space_left*/admin_space_left = 10/' -i /etc/audit/auditd.conf
- sed 's/^admin_space_left_action.*/admin_space_left_action = suspend/' -i /etc/audit/auditd.conf
- sed 's/^max_log_file_action.*/max_log_file_action = keep_logs/' -i /etc/audit/auditd.conf
- chown root:root /etc/audit/rules.d/audit.rules
- chmod 0640 /etc/audit/rules.d/audit.rules
- chmod -R g-wx,o-rwx /var/log/*
- ### Disable Ctrl-Alt-Del Reboot Activation
- systemctl mask ctrl-alt-del.target
- ### Disable accounts as soon as the password has expired
- sed -i 's/^INACTIVE.*/INACTIVE=0/' /etc/default/useradd
- ### Remember in the command history to 5000
- sed -i 's/HISTSIZE=.*/HISTSIZE=5000/g' /etc/profile
- echo ""
- echo "###############"
- echo "# GRUB Config #"
- echo "###############"
- sed 's/GRUB_TIMEOUT=5/GRUB_TIMEOUT=2/g' -i /etc/default/grub
- sed -e '/GRUB_CMDLINE_LINUX/s/"$/audit=1 ipv6.disable=1"/' -i /etc/default/grub
- update-grub && chown root:root /boot/grub/grub.cfg && chmod og-rwx /boot/grub/grub.cfg && chmod 600 /boot/grub/grub.cfg
- mkdir -p /mnt/cdrom
- echo " " >> /etc/fstab
- echo "###################" >> /etc/fstab
- echo "# FSTAB Hardening #" >> /etc/fstab
- echo "###################" >> /etc/fstab
- echo " " >> /etc/fstab
- echo "/tmp /var/tmp tmpfs rw,nosuid,nodev,noexec 0 0" >> /etc/fstab
- echo "tmpfs /dev/shm tmpfs rw,nosuid,nodev,noexec 0 0" >> /etc/fstab
- echo "proc /proc proc defaults,hidepid=2 0 0" >> /etc/fstab
- mount -av
- touch /etc/rsyslog.d/10-iptables.conf
- echo ":msg, contains, "iptables: " -/var/log/iptables.log" >> /etc/rsyslog.d/10-iptables.conf
- service rsyslog restart
Advertisement
Add Comment
Please, Sign In to add comment