CVE-2024–40725 5.3 Medium Severity
- #!/bin/bash
- # Disable AddType and ForceType directives in Apache config files
- disable_directives() {
- config_files=(“/etc/apache2/apache2.conf” “/etc/apache2/conf-available/*.conf” “/etc/apache2/sites-available/*.conf” “/etc/httpd/conf/httpd.conf”)
- for config_file in ${config_files[@]}; do
- if [[ -f $config_file ]]; then
- echo “Processing $config_file…”
- # Comment out AddType and ForceType lines
- sudo sed -i ‘s/^\s*\(AddType\|ForceType\)/# \1/g’ $config_file
- fi
- done
- echo “Disabled AddType and ForceType directives.”
- }
- # Set proper permissions for PHP and other sensitive files
- set_file_permissions() {
- sensitive_dirs=(“/var/www/html” “/your/custom/path”)
- for dir in ${sensitive_dirs[@]}; do
- if [[ -d $dir ]]; then
- echo “Setting file permissions in $dir…”
- # Set permission to 640 for PHP and other sensitive files
- sudo find $dir -type f \( -name “*.php” -o -name “*.conf” \) -exec chmod 640 {} \;
- fi
- done
- echo “File permissions have been set.”
- }
- # Create .htaccess to deny access to sensitive files
- deny_access_to_sensitive_files() {
- sensitive_dirs=(“/var/www/html” “/your/custom/path”)
- for dir in ${sensitive_dirs[@]}; do
- if [[ -d $dir ]]; then
- echo “Creating .htaccess in $dir…”
- # Add .htaccess to deny direct access to sensitive files
- echo ‘<FilesMatch “\.php$”>
- Require all denied
- </FilesMatch>’ | sudo tee “$dir/.htaccess”
- fi
- done
- echo “.htaccess files created to block direct access to sensitive files.”
- }
- # Disable directory listings in Apache
- disable_directory_listings() {
- apache_conf=”/etc/apache2/apache2.conf” # CHANGE IF NEEDED
- if [[ ! -f “$apache_conf” ]]; then
- apache_conf=”/etc/httpd/conf/httpd.conf”
- fi
- if [[ -f “$apache_conf” ]]; then
- echo “Disabling directory listings in Apache…”
- sudo sed -i ‘s/Options Indexes/Options -Indexes/g’ “$apache_conf”
- echo “Directory listings disabled.”
- else
- echo “Apache configuration file not found. Please verify the path.”
- fi
- }
- # Restart Apache
- restart_apache() {
- if systemctl status apache2.service > /dev/null 2>&1; then
- echo “Restarting Apache (apache2.service)…”
- sudo systemctl restart apache2
- elif systemctl status httpd.service > /dev/null 2>&1; then
- echo “Restarting Apache (httpd.service)…”
- sudo systemctl restart httpd
- else
- echo “Apache service not found. Please verify the service name.”
- fi
- }
- disable_directive
- set_file_permissions
- deny_access_to_sensitive_files
- disable_directory_listings
- restart_apache
- echo “All mitigations for CVE-2024–40725 have been applied.”
Description
🔒 Mitigating CVE-2024–40725: Securing Apache from Source Code Disclosure
This Bash script is designed to help protect your Apache HTTP Server from CVE-2024–40725, a vulnerability that can expose sensitive source code like PHP scripts. This issue stems from improper handling of the AddType
directive in Apache configurations.
🔍 How it works:
The script performs multiple steps to secure your Apache setup:
- Disables risky
AddType
andForceType
directives in Apache configurations, preventing raw code from being served instead of interpreted. - Sets secure file permissions on sensitive files (like
.php
), ensuring that unauthorized users cannot read them. - Creates
.htaccess
rules in critical directories to block access to files containing sensitive code. - Disables directory listings to prevent potential attackers from exploring your directory structure.
- Restarts Apache to apply these changes effectively.
⚠️ Why it matters:
CVE-2024–40725 exploits misconfigurations in older Apache versions (pre-2.4.62), allowing attackers to disclose local source code, which could lead to further exploitation. If you’re running an affected Apache version, applying these mitigations can significantly reduce the risk of exposure while you plan your update to the patched version.