-
Notifications
You must be signed in to change notification settings - Fork 75
/
Dockerfile
244 lines (218 loc) · 9.61 KB
/
Dockerfile
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
ARG NGINX_VERSION="n/a"
FROM nginxinc/nginx-unprivileged:${NGINX_VERSION} AS build
ARG MODSEC3_VERSION="n/a"
ARG LMDB_VERSION="n/a"
ARG LUA_VERSION="n/a"
USER root
# Note: libpcre3-dev (PCRE 1) is required by the build description,
# even though the build will use PCRE2.
RUN set -eux; \
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections; \
apt-get update -qq; \
LD_LIBRARY_PATH="" apt-get install -y -qq --no-install-recommends --no-install-suggests \
automake \
cmake \
doxygen \
g++ \
git \
libcurl4-gnutls-dev \
libfuzzy-dev \
liblua${LUA_VERSION}-dev \
libpcre3-dev \
libpcre2-dev \
libtool \
libxml2-dev \
libmaxminddb-dev \
libyajl-dev \
make \
patch \
pkg-config \
ruby \
zlib1g-dev; \
apt-get clean; \
rm -rf /var/lib/apt/lists/*
WORKDIR /sources
RUN set -eux; \
git clone https://github.com/LMDB/lmdb --branch LMDB_${LMDB_VERSION} --depth 1; \
make -C lmdb/libraries/liblmdb install; \
strip /usr/local/lib/liblmdb*.so*
RUN set -eux; \
git clone https://github.com/owasp-modsecurity/ModSecurity --branch "v${MODSEC3_VERSION}" --depth 1 --recursive; \
cd ModSecurity; \
ARCH=$(gcc -print-multiarch); \
sed -ie "s/i386-linux-gnu/${ARCH}/g" build/ssdeep.m4; \
sed -ie "s/i386-linux-gnu/${ARCH}/g" build/pcre2.m4; \
./build.sh; \
./configure --with-yajl --with-ssdeep --with-pcre2 --with-maxmind --enable-silent-rules; \
make install; \
strip /usr/local/modsecurity/lib/lib*.so*
# We use master
RUN set -eux; \
git clone -b master --depth 1 https://github.com/owasp-modsecurity/ModSecurity-nginx.git; \
curl -sSL https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz -o nginx-${NGINX_VERSION}.tar.gz; \
tar -xzf nginx-${NGINX_VERSION}.tar.gz; \
cd ./nginx-${NGINX_VERSION}; \
./configure --with-compat --add-dynamic-module=../ModSecurity-nginx; \
make modules; \
strip objs/ngx_http_modsecurity_module.so; \
cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules/; \
mkdir /etc/modsecurity.d; \
curl -sSL https://raw.githubusercontent.com/owasp-modsecurity/ModSecurity/v3/master/unicode.mapping \
-o /etc/modsecurity.d/unicode.mapping
# Generate/Download Diffie-Hellman parameter files
RUN set -eux; \
mkdir -p /usr/share/TLS; \
curl -sSL https://ssl-config.mozilla.org/ffdhe2048.txt -o /usr/share/TLS/dhparam-2048.pem; \
curl -sSL https://ssl-config.mozilla.org/ffdhe4096.txt -o /usr/share/TLS/dhparam-4096.pem
FROM nginxinc/nginx-unprivileged:${NGINX_VERSION} AS crs_release
ARG CRS_RELEASE
USER root
# hadolint ignore=DL3008,SC2016
RUN set -eux; \
apt-get update; \
apt-get -y install --no-install-recommends \
ca-certificates \
curl \
gnupg; \
mkdir /opt/owasp-crs; \
chown nginx:nginx /opt/owasp-crs
USER nginx
WORKDIR /sources
RUN curl -sSL https://github.com/coreruleset/coreruleset/releases/download/v${CRS_RELEASE}/coreruleset-${CRS_RELEASE}-minimal.tar.gz -o v${CRS_RELEASE}-minimal.tar.gz; \
curl -sSL https://github.com/coreruleset/coreruleset/releases/download/v${CRS_RELEASE}/coreruleset-${CRS_RELEASE}-minimal.tar.gz.asc -o coreruleset-${CRS_RELEASE}-minimal.tar.gz.asc; \
gpg --fetch-key https://coreruleset.org/security.asc; \
gpg --verify coreruleset-${CRS_RELEASE}-minimal.tar.gz.asc v${CRS_RELEASE}-minimal.tar.gz; \
tar -zxf v${CRS_RELEASE}-minimal.tar.gz --strip-components=1 -C /opt/owasp-crs; \
rm -f v${CRS_RELEASE}-minimal.tar.gz coreruleset-${CRS_RELEASE}-minimal.tar.gz.asc; \
mv -v /opt/owasp-crs/crs-setup.conf.example /opt/owasp-crs/crs-setup.conf
FROM nginxinc/nginx-unprivileged:${NGINX_VERSION}
ARG MODSEC3_VERSION
ARG LMDB_VERSION
ARG LUA_VERSION
ARG LUA_MODULES
LABEL maintainer="Felipe Zipitria <[email protected]>"
ENV \
ACCESSLOG=/var/log/nginx/access.log \
BACKEND=http://localhost:80 \
DNS_SERVER= \
ERRORLOG=/var/log/nginx/error.log \
KEEPALIVE_TIMEOUT=60s \
LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib \
LOGLEVEL=warn \
METRICS_ALLOW_FROM='127.0.0.0/24' \
METRICS_DENY_FROM='all' \
METRICSLOG=/dev/null \
MODSEC_ARGUMENT_SEPARATOR="&" \
MODSEC_ARGUMENTS_LIMIT=1000 \
MODSEC_AUDIT_ENGINE="RelevantOnly" \
MODSEC_AUDIT_LOG=/dev/stdout \
MODSEC_AUDIT_LOG_FORMAT=JSON \
MODSEC_AUDIT_LOG_PARTS='ABIJDEFHZ' \
MODSEC_AUDIT_LOG_RELEVANT_STATUS="^(?:5|4(?!04))" \
MODSEC_AUDIT_LOG_TYPE=Serial \
MODSEC_COOKIE_FORMAT=0 \
MODSEC_AUDIT_STORAGE_DIR=/var/log/modsecurity/audit/ \
MODSEC_DATA_DIR=/tmp/modsecurity/data \
MODSEC_DEBUG_LOG=/dev/null \
MODSEC_DEBUG_LOGLEVEL=0 \
MODSEC_DEFAULT_PHASE1_ACTION="phase:1,pass,log,tag:'\${MODSEC_TAG}'" \
MODSEC_DEFAULT_PHASE2_ACTION="phase:2,pass,log,tag:'\${MODSEC_TAG}'" \
MODSEC_DISABLE_BACKEND_COMPRESSION="Off" \
MODSEC_PCRE_MATCH_LIMIT=100000 \
MODSEC_PCRE_MATCH_LIMIT_RECURSION=100000 \
MODSEC_REQ_BODY_ACCESS=on \
MODSEC_REQ_BODY_JSON_DEPTH_LIMIT=512 \
MODSEC_REQ_BODY_LIMIT=13107200 \
MODSEC_REQ_BODY_LIMIT_ACTION="Reject" \
MODSEC_REQ_BODY_NOFILES_LIMIT=131072 \
MODSEC_RESP_BODY_ACCESS=on \
MODSEC_RESP_BODY_LIMIT=1048576 \
MODSEC_RESP_BODY_LIMIT_ACTION="ProcessPartial" \
MODSEC_RESP_BODY_MIMETYPE="text/plain text/html text/xml" \
MODSEC_RULE_ENGINE=on \
MODSEC_STATUS_ENGINE="Off" \
MODSEC_TAG=modsecurity \
MODSEC_TMP_DIR=/tmp/modsecurity/tmp \
MODSEC_TMP_SAVE_UPLOADED_FILES="on" \
MODSEC_UNICODE_MAPPING=20127 \
MODSEC_UPLOAD_DIR=/tmp/modsecurity/upload \
MODSEC_UPLOAD_FILE_MODE=0600 \
MODSEC_UPLOAD_KEEP_FILES=Off \
NGINX_ALWAYS_TLS_REDIRECT=off \
NGINX_ENVSUBST_OUTPUT_DIR=/etc/nginx \
PORT=8080 \
PROXY_SSL_CERT=/etc/nginx/conf/proxy.crt \
PROXY_SSL_CERT_KEY=/etc/nginx/conf/proxy.key \
PROXY_SSL_CIPHERS="ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384" \
PROXY_SSL=off \
PROXY_SSL_PROTOCOLS="TLSv1.2 TLSv1.3" \
PROXY_SSL_VERIFY_DEPTH=1 \
PROXY_SSL_VERIFY=off \
PROXY_TIMEOUT=60s \
REAL_IP_HEADER="X-REAL-IP" \
REAL_IP_PROXY_HEADER="X-REAL-IP" \
REAL_IP_RECURSIVE="on" \
SERVER_NAME=localhost \
SERVER_TOKENS=off \
SET_REAL_IP_FROM="127.0.0.1" \
SSL_CERT=/etc/nginx/conf/server.crt \
SSL_CERT_KEY=/etc/nginx/conf/server.key \
SSL_CIPHERS="ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384" \
SSL_DH_BITS=2048 \
SSL_OCSP_STAPLING=on \
SSL_PORT=8443 \
SSL_PREFER_CIPHERS=off \
SSL_PROTOCOLS="TLSv1.2 TLSv1.3" \
SSL_VERIFY_DEPTH=1 \
SSL_VERIFY=off \
WORKER_CONNECTIONS=1024 \
# CRS specific variables
PARANOIA=1 \
ANOMALY_INBOUND=5 \
ANOMALY_OUTBOUND=4 \
BLOCKING_PARANOIA=1
COPY --from=build /usr/local/modsecurity/lib/libmodsecurity.so.${MODSEC3_VERSION} /usr/local/modsecurity/lib/
COPY --from=build /etc/nginx/modules/ngx_http_modsecurity_module.so /etc/nginx/modules/ngx_http_modsecurity_module.so
COPY --from=build /usr/local/lib/liblmdb.so /usr/local/lib/
COPY --from=build /usr/share/TLS/dhparam-* /etc/ssl/certs/
COPY --from=build /etc/modsecurity.d/unicode.mapping /etc/modsecurity.d/unicode.mapping
COPY --from=crs_release /opt/owasp-crs /opt/owasp-crs
COPY src/etc/modsecurity.d/modsecurity.conf /etc/nginx/templates/modsecurity.d/modsecurity.conf.template
COPY src/etc/modsecurity.d/modsecurity-override.conf /etc/nginx/templates/modsecurity.d/modsecurity-override.conf.template
COPY src/etc/modsecurity.d/setup.conf /etc/nginx/templates/modsecurity.d/setup.conf.template
COPY nginx/docker-entrypoint.d/*.sh /docker-entrypoint.d/
COPY src/opt/modsecurity/activate-plugins.sh /docker-entrypoint.d/94-activate-plugins.sh
COPY src/opt/modsecurity/activate-rules.sh /docker-entrypoint.d/95-activate-rules.sh
# We use the templating mechanism from the nginx image here.
COPY nginx/templates /etc/nginx/templates/
COPY src/bin/* /usr/local/bin/
USER root
RUN set -eux; \
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections; \
apt-get update -qq; \
LD_LIBRARY_PATH="" apt-get install -y -qq --no-install-recommends --no-install-suggests \
ca-certificates \
curl \
libcurl4-gnutls-dev \
libfuzzy2 \
liblua${LUA_VERSION} \
${LUA_MODULES} \
libxml2 \
libyajl2 \
libmaxminddb-dev \
moreutils; \
rm -rf /var/lib/apt/lists/*; \
apt-get clean; \
mkdir /etc/nginx/ssl; \
# Comment out the SecDisableBackendCompression option since it is not supported in V3
sed -i 's/^\(SecDisableBackendCompression .*\)/# \1/' /etc/nginx/templates/modsecurity.d/modsecurity-override.conf.template; \
ln -s /usr/local/modsecurity/lib/libmodsecurity.so.${MODSEC3_VERSION} /usr/local/modsecurity/lib/libmodsecurity.so.3.0; \
ln -s /usr/local/modsecurity/lib/libmodsecurity.so.${MODSEC3_VERSION} /usr/local/modsecurity/lib/libmodsecurity.so.3; \
ln -s /usr/local/modsecurity/lib/libmodsecurity.so.${MODSEC3_VERSION} /usr/local/modsecurity/lib/libmodsecurity.so; \
ln -sv /opt/owasp-crs /etc/modsecurity.d/; \
chown nginx:nginx /opt/owasp-crs /etc/modsecurity.d
USER nginx
RUN mkdir -p /tmp/modsecurity/data; \
mkdir -p /tmp/modsecurity/upload; \
mkdir -p /tmp/modsecurity/tmp
HEALTHCHECK CMD /usr/local/bin/healthcheck