-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathubuntu_hardening.sh
74 lines (58 loc) · 2.2 KB
/
ubuntu_hardening.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
#!/bin/bash
# Ubuntu 22.04 LTS Hardening Script
# Check if script is run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
# 1. System Updates
apt update
apt upgrade -y
apt install unattended-upgrades -y
dpkg-reconfigure -plow unattended-upgrades
# 2. User Management
sed -i 's/PASS_MAX_DAYS\t99999/PASS_MAX_DAYS\t90/' /etc/login.defs
sed -i 's/PASS_MIN_DAYS\t0/PASS_MIN_DAYS\t10/' /etc/login.defs
sed -i 's/PASS_WARN_AGE\t7/PASS_WARN_AGE\t7/' /etc/login.defs
apt install libpam-pwquality -y
sed -i '1s/^/password requisite pam_pwquality.so retry=3 minlen=14 dcredit=-1 ucredit=-1 ocredit=-1 lcredit=-1\n/' /etc/pam.d/common-password
# 3. Network Security
apt install ufw -y
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw --force enable
systemctl disable avahi-daemon
systemctl disable cups
systemctl disable rpcbind
# 4. File System Security
chmod 700 /boot /etc/cron.monthly /etc/cron.weekly /etc/cron.daily /etc/cron.hourly
chmod 600 /etc/crontab /etc/ssh/sshd_config
chmod 644 /etc/passwd
chmod 640 /etc/shadow
chmod 644 /etc/group
chmod 640 /etc/gshadow
echo "tmpfs /run/shm tmpfs defaults,noexec,nosuid 0 0" >> /etc/fstab
# 5. Secure SSH
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sed -i 's/X11Forwarding yes/X11Forwarding no/' /etc/ssh/sshd_config
systemctl restart ssh
# 6. Install and Configure fail2ban
apt install fail2ban -y
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sed -i 's/bantime = 10m/bantime = 1h/' /etc/fail2ban/jail.local
sed -i 's/maxretry = 5/maxretry = 3/' /etc/fail2ban/jail.local
systemctl enable fail2ban
systemctl start fail2ban
# 7. Remove Unnecessary Packages
apt remove telnet rsh-client rsh-redone-client -y
# 8. Enable and Configure auditd
apt install auditd -y
systemctl enable auditd
systemctl start auditd
# 9. Disable USB Storage
echo "install usb-storage /bin/true" >> /etc/modprobe.d/disable-usb-storage.conf
# 10. Secure Shared Memory
echo "tmpfs /dev/shm tmpfs defaults,noexec,nosuid,nodev 0 0" >> /etc/fstab
echo "Hardening complete. Please review changes and reboot the system."