Exploit databaseDownload repository ZIP
GH-5ade03a100aaac91604892e57a034fd9
freeguy-6/dmz-security-monitoring-hardening
Sources for CVE-2014-6271
128 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files16
freeguy-6/dmz-security-monitoring-hardeningRevision 563ea13b1703
- .gitignore
- CY376_DMZ_Report_Kennedy_Kumi_Holomah.docx
- CY376_DMZ_Report_Kennedy_Kumi_Holomah.pdf
- README.md
- dmz_web_hardening.sh
- figure01_lab_topology.png
- figure02_et_open_rule_categories.png
- figure03_suricata_inline_ips.png
- figure04_home_net_scoping.png
- figure05_suricata_alert_shellshock.png
- figure06_blocked_hosts.png
- figure07_hardening_script_summary.png
- figure08_post_hardening_ssh_status.png
- figure09_post_hardening_verification.png
- figure10_pfsense_status.png
- figure11_wazuh_dashboard.png
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
dmz_web_hardening.sh
Shell · 11,241 bytes#!/usr/bin/env bash
#
# dmz_web_hardening.sh
#
# Purpose : Harden the DMZ Ubuntu Server running Apache for the
# "DMZ Security Monitoring and Hardening" cybersecurity lab.
# Scope : System updates, SSH (if present), UFW firewall, Apache
# hardening (including a Shellshock/CGI mitigation to match
# the CVE-2014-6271 signature already validated by Suricata
# on the DMZ interface), fail2ban, and automatic security
# updates.
#
# Usage : sudo bash dmz_web_hardening.sh
#
# Notes : - Designed to be idempotent-ish: safe to re-run.
# - Every destructive/config-changing step backs up the
# original file first (suffix: .bak-YYYYmmdd-HHMMSS).
# - Review the CONFIG section below before running.
#
set -euo pipefail
IFS=$'\n\t'
# ---------------------------------------------------------------------------
# CONFIG - adjust to match your lab topology before running
# ---------------------------------------------------------------------------
LAN_SUBNET="192.168.20.0/24" # Only this subnet may SSH in
DMZ_HTTP_PORT="80"
DMZ_HTTPS_PORT="443"
TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
LOGFILE="/var/log/dmz_hardening_${TIMESTAMP}.log"
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
log() { echo -e "[*] $*" | tee -a "$LOGFILE"; }
ok() { echo -e "[OK] $*" | tee -a "$LOGFILE"; }
warn() { echo -e "[!!] $*" | tee -a "$LOGFILE"; }
backup_file() {
local f="$1"
if [[ -f "$f" ]]; then
cp -p "$f" "${f}.bak-${TIMESTAMP}"
log "Backed up $f -> ${f}.bak-${TIMESTAMP}"
fi
}
require_root() {
if [[ "$(id -u)" -ne 0 ]]; then
echo "This script must be run as root (sudo bash $0)" >&2
exit 1
fi
}
# ---------------------------------------------------------------------------
# 0. Preflight
# ---------------------------------------------------------------------------
require_root
touch "$LOGFILE"
log "=== DMZ Web Server Hardening started $(date) ==="
# ---------------------------------------------------------------------------
# 1. System updates
# ---------------------------------------------------------------------------
log "Updating package lists and upgrading installed packages..."
export DEBIAN_FRONTEND=noninteractive
apt-get update -y | tee -a "$LOGFILE"
apt-get upgrade -y | tee -a "$LOGFILE"
apt-get autoremove -y | tee -a "$LOGFILE"
ok "System packages updated."
# Explicitly ensure bash itself is current -- this is the actual fix for
# the Shellshock family of vulnerabilities (CVE-2014-6271 and related),
# which the lab's Suricata DMZ instance was observed detecting.
log "Ensuring bash is patched against Shellshock (CVE-2014-6271)..."
apt-get install --only-upgrade -y bash | tee -a "$LOGFILE"
ok "bash is up to date."
# ---------------------------------------------------------------------------
# 2. SSH hardening (only if SSH is installed / present)
# ---------------------------------------------------------------------------
SSHD_CONFIG="/etc/ssh/sshd_config"
if command -v sshd >/dev/null 2>&1 || dpkg -l | grep -q openssh-server; then
log "SSH server detected. Applying SSH hardening..."
backup_file "$SSHD_CONFIG"
# Helper to set (or add) a directive in sshd_config
set_sshd_option() {
local key="$1" val="$2"
if grep -qiE "^\s*${key}\s+" "$SSHD_CONFIG"; then
sed -i "s/^\s*#\?\s*${key}.*/${key} ${val}/I" "$SSHD_CONFIG"
else
echo "${key} ${val}" >> "$SSHD_CONFIG"
fi
}
set_sshd_option "PermitRootLogin" "no"
set_sshd_option "PasswordAuthentication" "no" # key-based auth only
set_sshd_option "PermitEmptyPasswords" "no"
set_sshd_option "X11Forwarding" "no"
set_sshd_option "MaxAuthTries" "3"
set_sshd_option "ClientAliveInterval" "300"
set_sshd_option "ClientAliveCountMax" "2"
set_sshd_option "Protocol" "2"
if sshd -t 2>>"$LOGFILE"; then
systemctl restart ssh || systemctl restart sshd || true
ok "SSH hardened and service restarted."
warn "PasswordAuthentication is now disabled -- confirm you have a working SSH key before closing this session!"
else
warn "sshd_config failed syntax check -- reverting to backup, SSH left unchanged."
cp -p "${SSHD_CONFIG}.bak-${TIMESTAMP}" "$SSHD_CONFIG"
fi
else
log "No SSH server detected on this host -- skipping SSH hardening section."
fi
# ---------------------------------------------------------------------------
# 3. Firewall (UFW)
# ---------------------------------------------------------------------------
log "Configuring UFW firewall..."
apt-get install -y ufw | tee -a "$LOGFILE"
ufw --force reset >>"$LOGFILE" 2>&1
ufw default deny incoming
ufw default allow outgoing
# Web traffic open to anyone (this is the DMZ's public-facing role)
ufw allow "${DMZ_HTTP_PORT}/tcp"
ufw allow "${DMZ_HTTPS_PORT}/tcp"
# SSH restricted to the LAN subnet only, if SSH is installed
if command -v sshd >/dev/null 2>&1 || dpkg -l | grep -q openssh-server; then
ufw allow from "$LAN_SUBNET" to any port 22 proto tcp
log "SSH restricted to ${LAN_SUBNET} only."
fi
ufw --force enable
ufw status verbose | tee -a "$LOGFILE"
ok "UFW firewall configured and enabled."
# ---------------------------------------------------------------------------
# 4. Apache hardening
# ---------------------------------------------------------------------------
if command -v apache2 >/dev/null 2>&1 || dpkg -l | grep -q apache2; then
log "Apache detected. Applying Apache hardening..."
APACHE_SECURITY_CONF="/etc/apache2/conf-available/security.conf"
backup_file "$APACHE_SECURITY_CONF"
# Hide Apache version / OS details in headers and error pages
if [[ -f "$APACHE_SECURITY_CONF" ]]; then
sed -i "s/^ServerTokens.*/ServerTokens Prod/" "$APACHE_SECURITY_CONF" || echo "ServerTokens Prod" >> "$APACHE_SECURITY_CONF"
sed -i "s/^ServerSignature.*/ServerSignature Off/" "$APACHE_SECURITY_CONF" || echo "ServerSignature Off" >> "$APACHE_SECURITY_CONF"
grep -q "^ServerTokens" "$APACHE_SECURITY_CONF" || echo "ServerTokens Prod" >> "$APACHE_SECURITY_CONF"
grep -q "^ServerSignature" "$APACHE_SECURITY_CONF" || echo "ServerSignature Off" >> "$APACHE_SECURITY_CONF"
fi
# Disable directory listing and .htaccess override globally, tighten
# the default <Directory /> block in apache2.conf
APACHE_CONF="/etc/apache2/apache2.conf"
backup_file "$APACHE_CONF"
if ! grep -q "Options -Indexes" "$APACHE_CONF"; then
cat >> "$APACHE_CONF" <<'EOF'
# --- Hardening additions (dmz_web_hardening.sh) ---
<Directory /var/www/>
Options -Indexes -FollowSymLinks
AllowOverride None
Require all granted
</Directory>
EOF
fi
# Disable mod_cgi / mod_cgid unless explicitly needed. Shellshock-class
# attacks (CVE-2014-6271) rely on CGI scripts passing attacker-controlled
# headers into a vulnerable bash environment -- if this server does not
# need CGI, removing it closes off that entire attack surface.
read -rp "Does this server require CGI scripts (mod_cgi/mod_cgid)? [y/N]: " NEED_CGI
NEED_CGI="${NEED_CGI:-N}"
if [[ ! "$NEED_CGI" =~ ^[Yy]$ ]]; then
a2dismod cgi 2>/dev/null || true
a2dismod cgid 2>/dev/null || true
log "mod_cgi/mod_cgid disabled (Shellshock CGI attack surface removed)."
else
warn "CGI left enabled per user choice. Ensure bash is fully patched (done in step 1) and CGI scripts are reviewed."
fi
# Disable other commonly-unneeded/risky modules if present
for mod in autoindex status info; do
a2dismod "$mod" 2>/dev/null || true
done
# Remove default "It works!" page if present (info disclosure / fingerprinting)
if [[ -f /var/www/html/index.html ]]; then
DEFAULT_HASH=$(md5sum /var/www/html/index.html 2>/dev/null | awk '{print $1}')
# Only remove if it looks like the untouched Apache default page
if grep -qi "Apache2 .*Default Page" /var/www/html/index.html 2>/dev/null; then
mv /var/www/html/index.html "/var/www/html/index.html.bak-${TIMESTAMP}"
log "Default Apache placeholder page moved aside."
fi
fi
# Lock down ownership/permissions on the web root
chown -R root:www-data /var/www/html
find /var/www/html -type d -exec chmod 750 {} \;
find /var/www/html -type f -exec chmod 640 {} \;
ok "Web root ownership/permissions locked down (root:www-data, 750/640)."
apache2ctl configtest 2>&1 | tee -a "$LOGFILE"
systemctl restart apache2
ok "Apache hardened and restarted."
else
warn "Apache not detected -- skipping Apache-specific hardening."
fi
# ---------------------------------------------------------------------------
# 5. fail2ban (brute-force protection)
# ---------------------------------------------------------------------------
log "Installing and configuring fail2ban..."
apt-get install -y fail2ban | tee -a "$LOGFILE"
cat > /etc/fail2ban/jail.local <<EOF
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
[sshd]
enabled = true
[apache-auth]
enabled = true
[apache-badbots]
enabled = true
[apache-noscript]
enabled = true
[apache-overflows]
enabled = true
EOF
systemctl enable fail2ban
systemctl restart fail2ban
ok "fail2ban installed and enabled."
# ---------------------------------------------------------------------------
# 6. Automatic security updates
# ---------------------------------------------------------------------------
log "Enabling unattended security upgrades..."
apt-get install -y unattended-upgrades | tee -a "$LOGFILE"
dpkg-reconfigure -f noninteractive unattended-upgrades
ok "Unattended security upgrades enabled."
# ---------------------------------------------------------------------------
# 7. Remove/disable unnecessary services
# ---------------------------------------------------------------------------
log "Disabling common unnecessary services if present (telnet, ftp, rsh)..."
for svc in telnet ftp rsh-server; do
if dpkg -l | grep -q "^ii\s*${svc}"; then
apt-get purge -y "$svc" | tee -a "$LOGFILE"
log "Purged $svc."
fi
done
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
log "=== Hardening complete: $(date) ==="
log "Full log saved to: $LOGFILE"
log "Backups of modified config files use the suffix .bak-${TIMESTAMP}"
echo
echo "Summary of changes:"
echo " - System packages updated, bash patched (Shellshock)"
echo " - SSH hardened (if present): no root login, key-only auth, restricted to ${LAN_SUBNET}"
echo " - UFW enabled: allow ${DMZ_HTTP_PORT}/${DMZ_HTTPS_PORT} from anywhere, SSH from LAN only"
echo " - Apache: version banners hidden, directory listing disabled, CGI handled per your choice, web root permissions locked down"
echo " - fail2ban installed and enabled for SSH + Apache"
echo " - Unattended security upgrades enabled"
echo " - Unnecessary legacy services purged if present"