Hallo,
da ich demnächst ein paar mehr oder weniger gleich konfigurierte Server aufsetzen will, habe ich alle meine dokumentierten Schritte mal zu einem Setup/ Security Script zusammengebaut.
Gleich vorweg: Ich bin kein Programmierer. Manches mag elganter oder kürzer gehen. Aber es funktioniert fast alles.
Was haltet Ihr davon? Irgendwas, was schlecht ist? Fehlt was? Würdet ihr noch was hinzufügen? Aktuell alls Dummies und Platzhalterwerte.
Es fehlen aktuell noch diverse Werte für die sshd_config.
Was aktuell noch niccht funktioniert ist die Benahrichtigung bei SSH Login. Weiß jemand was da falsch ist bzw. fehlt?
Mailbenachrichtigungen an sich funktionieren für Logwatch (allerdings nur im Textformat und nicht wie gewünscht als html Mail), Apticron, unattended upgraddes etc.
Gerne Anmerkungen, Kommentare etc.
Und hier jetzt das Skript:
#!/bin/bash
# Define applications to install
APPS=(
"ufw"
"curl"
"wget"
"git"
"mc"
"unattended-upgrades"
"apt-transport-https"
"ca-certificates"
"software-properties-common"
"mailutils" # For sending email notifications
"logwatch" # For log monitoring and email notifications
"auditd"
"audispd-plugins"
"openssh-server" # Ensure SSH server is installed
)
# Define email notification settings for unattended upgrades
UNATTENDED_UPGRADES_EMAIL="your-email@example.com" # Replace with your email address
# Define email notification settings for Logwatch
LOGWATCH_EMAIL="your-email@example.com" # Replace with your email address
# Define new SSH user settings
NEW_USER="youruser" # Replace with the desired username
NEW_USER_PASSWORD="yourpassword" # Replace with the desired password
# Define Ubuntu Pro credentials
UBUNTU_PRO_TOKEN="your_ubuntu_pro_token" # Replace with your Ubuntu Pro token
# Define the SSH port
SSH_PORT=2222 # Replace with your desired SSH port
# Define your SSH keys in an array
SSH_KEYS=(
"ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArX1... user1@domain.com"
"ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsd2... user2@domain.com"
"ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAwq3... user3@domain.com"
)
# Define the second static IP and VLAN configuration for internal use
VLAN_INTERFACE="eth0.100" # Replace with the desired interface name
VLAN_IP="192.168.100.10/24" # Replace with the desired static IP and subnet
# Define auditd monitoring script settings
AUDITD_MONITOR_SCRIPT="/usr/local/bin/auditd_monitor.sh"
AUDITD_EMAIL_RECIPIENT="your-email@example.com" # Replace with your email address
AUDITD_LAST_LINE_FILE="/tmp/auditd_last_line"
# Function to install applications
install_apps() {
echo "Installing applications..."
apt update -y
apt install -y "${APPS[@]}"
}
# Function to configure email notifications for SSH logins
configure_ssh_notifications() {
echo "Configuring SSH login notifications..."
# Install mailutils if not already installed
apt install -y postfix mailutils
# Create or update the SSH login script
SSH_LOGIN_SCRIPT="/etc/profile.d/ssh_login_notify.sh"
cat <<EOF > $SSH_LOGIN_SCRIPT
#!/bin/bash
if [ "\$USER" != "root" ]; then
SUBJECT="SSH Login Alert"
MESSAGE="User \${USER} has logged in from \${SSH_CLIENT} on \$(date)"
echo "\$MESSAGE" | mail -s "\$SUBJECT" "$UNATTENDED_UPGRADES_EMAIL"
fi
EOF
chmod +x $SSH_LOGIN_SCRIPT
}
# Function to create a new user and disable root access
setup_user_and_disable_root() {
echo "Creating new user and disabling root access..."
# Create the new user
useradd -m -s /bin/bash "$NEW_USER"
echo "$NEW_USER:$NEW_USER_PASSWORD" | chpasswd
# Add the new user to the sudo group
usermod -aG sudo "$NEW_USER"
# Lock the root account
passwd -l root
# Set up the .ssh directory and authorized_keys file
mkdir -p /home/$NEW_USER/.ssh
chmod 700 /home/$NEW_USER/.ssh
touch /home/$NEW_USER/.ssh/authorized_keys
chmod 600 /home/$NEW_USER/.ssh/authorized_keys
# Add each SSH key to the authorized_keys file
for KEY in "${SSH_KEYS[@]}"; do
echo "$KEY" >> /home/$NEW_USER/.ssh/authorized_keys
done
# Ensure the correct ownership of the .ssh directory and its contents
chown -R $NEW_USER:$NEW_USER /home/$NEW_USER/.ssh
}
# Function to configure SSH with a custom port
configure_ssh_port() {
echo "Configuring SSH with a custom port..."
# Use the SSH_PORT variable defined earlier in the script
# If not defined, it defaults to 2222
SSH_PORT="${SSH_PORT:-2222}"
# Create a systemd override directory for SSH
mkdir -p /etc/systemd/system/ssh.service.d
# Create a drop-in configuration file to change the SSH port
cat <<EOF > /etc/systemd/system/ssh.service.d/override.conf
[Service]
ExecStart=
ExecStart=/usr/sbin/sshd -D -p $SSH_PORT
EOF
# Reload the systemd daemon and restart SSH to apply changes
systemctl daemon-reload
systemctl restart ssh
echo "SSH is now configured to use port $SSH_PORT."
}
# Function to enable Ubuntu Pro updates
enable_ubuntu_pro() {
echo "Enabling Ubuntu Pro updates..."
# Install the Ubuntu Pro client
apt update -y
apt install -y ubuntu-advantage-tools
# Attach the Ubuntu Pro token
ua attach "$UBUNTU_PRO_TOKEN"
# Enable the relevant Ubuntu Pro services
ua enable livepatch
ua enable esm-infra
ua enable esm-apps
# Update package lists and upgrade to apply Ubuntu Pro updates
apt update -y
apt upgrade -y
}
# Function to apply custom SSHD configuration settings
customize_sshd_config() {
echo "Applying custom SSHD configuration..."
# Backup current sshd_config
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# Update or append the SSHD configuration settings
sed -i "s/^#*Port.*/Port $SSH_PORT/" /etc/ssh/sshd_config
sed -i "s/^#*PermitRootLogin.*/PermitRootLogin no/" /etc/ssh/sshd_config
sed -i "s/^#*PasswordAuthentication.*/PasswordAuthentication no/" /etc/ssh/sshd_config
sed -i "s/^#*ChallengeResponseAuthentication.*/ChallengeResponseAuthentication no/" /etc/ssh/sshd_config
sed -i "s/^#*UsePAM.*/UsePAM yes/" /etc/ssh/sshd_config
# Allow the new user to connect via SSH
if grep -q "^AllowUsers" /etc/ssh/sshd_config; then
sed -i "s/^AllowUsers.*/AllowUsers $NEW_USER/" /etc/ssh/sshd_config
else
echo "AllowUsers $NEW_USER" >> /etc/ssh/sshd_config
fi
# Restart SSH service to apply changes
systemctl restart ssh
}
# Function to configure UFW with rate limiting
configure_firewall() {
echo "Configuring UFW with rate limiting..."
# Basic UFW setup
ufw default deny incoming
ufw default allow outgoing
# Allow SSH on the defined port
ufw allow $SSH_PORT/tcp
# Enable rate limiting for SSH
ufw limit $SSH_PORT/tcp
# Enable the firewall
ufw enable
}
# Function to configure timesyncd with custom NTP servers
configure_timesyncd() {
echo "Configuring systemd-timesyncd with custom NTP servers..."
# Set NTP servers
sed -i 's/^#NTP=.*/NTP=ntp1.dismail.de ntp2.dismail.de/' /etc/systemd/timesyncd.conf
# Restart the timesyncd service
systemctl restart systemd-timesyncd
}
# Function to configure unattended upgrades with email notifications
configure_unattended_upgrades() {
echo "Configuring unattended upgrades with email notifications..."
# Configure email notifications for unattended upgrades
dpkg-reconfigure -plow unattended-upgrades
# Set email for notifications
sed -i "s|^//Unattended-Upgrade::Mail.*|Unattended-Upgrade::Mail \"$UNATTENDED_UPGRADES_EMAIL\";|" /etc/apt/apt.conf.d/50unattended-upgrades
# Enable automatic upgrades
echo 'APT::Periodic::Update-Package-Lists "1";' >> /etc/apt/apt.conf.d/10periodic
echo 'APT::Periodic::Unattended-Upgrade "1";' >> /etc/apt/apt.conf.d/10periodic
systemctl restart unattended-upgrades
}
# Function to configure Logwatch with email notifications
configure_logwatch() {
echo "Configuring Logwatch with email notifications..."
# Set email recipient for Logwatch
sed -i "s/^MailTo =.*/MailTo = $LOGWATCH_EMAIL/" /usr/share/logwatch/default.conf/logwatch.conf
sed -i "s/Detail =.*/Detail = Med/" /usr/share/logwatch/default.conf/logwatch.conf
# Run Logwatch daily and send email
echo "0 0 * * * /usr/sbin/logwatch --output mail --format html --mailto $LOGWATCH_EMAIL --detail high" > /etc/cron.d/logwatch
}
# Function to configure a second static IP for VLAN (internal use)
configure_vlan_ip() {
echo "Configuring a second static IP for VLAN (internal use)..."
# Create Netplan configuration for the VLAN
VLAN_CONFIG_FILE="/etc/netplan/02-vlan-config.yaml"
cat <<EOF > $VLAN_CONFIG_FILE
network:
version: 2
ethernets:
$VLAN_INTERFACE:
dhcp4: no
addresses:
- $VLAN_IP
EOF
# Apply the Netplan configuration
netplan apply
}
Alles anzeigen