Skip to content

Commit c307c0a

Browse files
Merge pull request #27 from StrangeRanger/dev
Enhance nginx WAF setup with availability and build dependencies
2 parents fd181c8 + abf7f80 commit c307c0a

2 files changed

Lines changed: 79 additions & 16 deletions

File tree

hardening/Nginx WAF/CHANGELOG.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66

77
## [Unreleased]
88

9-
### Added
10-
11-
- Initial changelog tracking for the Nginx WAF hardening tool.
12-
139
## [1.0.0-beta] - 2026-05-17
1410

1511
### Added
1612

17-
- Initial beta release of the Nginx WAF hardening tool.
18-
- Baseline documentation and configuration for deploying and maintaining the WAF setup.
19-
- Initial hardening-focused rules and project structure for protecting Nginx-based deployments.
13+
- Added Nginx WAF hardening tool for installing and configuring ModSecurity with Nginx.
14+
- Added automatic installation of required build dependencies for ModSecurity and Nginx dynamic module compilation.
15+
- Added ModSecurity v3 source build and installation workflow.
16+
- Added ModSecurity-nginx dynamic module build using the installed Nginx version and configure arguments.
17+
- Added Nginx module loading configuration through `modules-available` and `modules-enabled`.
18+
- Added OWASP Core Rule Set installation and ModSecurity main configuration generation.
19+
- Added Nginx configuration validation and restart after setup.
20+
21+
### Fixed
22+
23+
- Added missing build dependencies required by Nginx SSL, XSLT, image filter, Perl, gzip, and ModSecurity modules.
24+
- Removed redundant or unused dependency entries from the required package list.
25+
- Limited Nginx module-specific build dependencies to systems whose installed Nginx was built with those modules.

hardening/Nginx WAF/nginx-waf.bash

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,30 @@ readonly C_MODULES_ENABLED="/etc/nginx/modules-enabled"
3232
readonly C_MODSEC_PATH="/etc/nginx/modsec"
3333
readonly C_MODSEC_CONF_PATH="$C_MODSEC_PATH/modsecurity.conf"
3434
readonly C_MAIN_CONF_PATH="$C_MODSEC_PATH/main.conf"
35-
36-
# TODO: ERROR CATCHING ELSE WILL JUST FAIL SILENTLY IF NGINX IS NOT INSTALLED OR NOT IN PATH
37-
C_NGINX_VERSION="$(nginx -V 2>&1 | sed -n 's/^nginx version: nginx\/\([0-9.]\+\).*/\1/p')"
38-
C_NGINX_CONFIG_ARGS="$(nginx -V 2>&1 | awk -F': ' '/configure arguments/ {print $2}')"
39-
C_MODULES_PATH="$(sed -n 's/.*--modules-path=\([^ ]*\).*/\1/p' <<<"$C_NGINX_CONFIG_ARGS" | head -n 1)"
40-
readonly C_NGINX_VERSION C_NGINX_CONFIG_ARGS C_MODULES_PATH
35+
readonly C_REQUIRED_PKGS=(
36+
git
37+
autoconf
38+
automake
39+
build-essential
40+
libcurl4-openssl-dev
41+
libgeoip-dev
42+
libpcre2-dev
43+
libtool
44+
libxml2-dev
45+
libyajl-dev
46+
pkgconf
47+
wget
48+
zlib1g-dev
49+
)
50+
51+
C_NGINX_VERSION=""
52+
C_NGINX_CONFIG_ARGS=""
53+
C_MODULES_PATH=""
4154

4255
modsecurity_clone_exists=false
4356
coreruleset_clone_exists=false
57+
required_pkgs=("${C_REQUIRED_PKGS[@]}")
58+
missing_pkgs=()
4459

4560

4661
####[Functions]#############################################################################
@@ -66,15 +81,57 @@ require_non_empty() {
6681
[[ -n "$var_value" ]] || error_exit "Required value '${var_name}' is empty"
6782
}
6883

84+
require_pkg() {
85+
local required_pkg="$1"
86+
87+
for pkg in "${required_pkgs[@]}"; do
88+
[[ $pkg == "$required_pkg" ]] && return 0
89+
done
90+
91+
required_pkgs+=("$required_pkg")
92+
}
93+
6994

7095
####[ Trapping & Initial Checks ]###########################################################
7196

7297

7398
trap on_err ERR
7499

75-
require_non_empty "C_NGINX_VERSION" "$C_NGINX_VERSION"
76-
require_non_empty "C_NGINX_CONFIG_ARGS" "$C_NGINX_CONFIG_ARGS"
77-
require_non_empty "C_MODULES_PATH" "$C_MODULES_PATH"
100+
101+
####[ Initial Checks ]######################################################################
102+
103+
104+
if (( EUID != 0 )); then
105+
error_exit "This script must be run with root privileges"
106+
fi
107+
108+
if command -v nginx &>/dev/null; then
109+
C_NGINX_VERSION="$(nginx -V 2>&1 | sed -n 's/^nginx version: nginx\/\([0-9.]\+\).*/\1/p')"
110+
C_NGINX_CONFIG_ARGS="$(nginx -V 2>&1 | awk -F': ' '/configure arguments/ {print $2}')"
111+
C_MODULES_PATH="$(sed -n 's/.*--modules-path=\([^ ]*\).*/\1/p' <<<"$C_NGINX_CONFIG_ARGS" | head -n 1)"
112+
require_non_empty "C_NGINX_VERSION" "$C_NGINX_VERSION"
113+
require_non_empty "C_NGINX_CONFIG_ARGS" "$C_NGINX_CONFIG_ARGS"
114+
require_non_empty "C_MODULES_PATH" "$C_MODULES_PATH"
115+
else
116+
error_exit "Nginx is not installed or not in PATH"
117+
fi
118+
119+
[[ $C_NGINX_CONFIG_ARGS == *--with-http_image_filter_module* ]] && require_pkg "libgd-dev"
120+
[[ $C_NGINX_CONFIG_ARGS == *--with-http_perl_module* ]] && require_pkg "libperl-dev"
121+
[[ $C_NGINX_CONFIG_ARGS == *--with-http_xslt_module* ]] && require_pkg "libxslt1-dev"
122+
[[ $C_NGINX_CONFIG_ARGS == *ssl* ]] && require_pkg "libssl-dev"
123+
124+
for pkg in "${required_pkgs[@]}"; do
125+
if ! dpkg -s "$pkg" &>/dev/null; then
126+
missing_pkgs+=("$pkg")
127+
fi
128+
done
129+
130+
if (( ${#missing_pkgs[@]} > 0 )); then
131+
echo "${C_INFO}Installing missing packages: ${missing_pkgs[*]}"
132+
apt-get update
133+
apt-get install -y "${missing_pkgs[@]}"
134+
fi
78135

79136

80137
####[ Main ]################################################################################

0 commit comments

Comments
 (0)