Skip to content

Commit 59d5441

Browse files
committed
feat(router): adding modsecurity module with OWASP core rule set as sec option
1 parent 7c8636d commit 59d5441

5 files changed

Lines changed: 40 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ _Note that Kubernetes annotation maps are all of Go type `map[string]string`. A
250250
| <a name="enable-regex-domains"></a>deis-router | deployment | [router.deis.io/nginx.enableRegexDomains](#enable-regex-domains) | `"false"` | Whether to _enable_ application-level regex domain that can be explicitly defined for specific applications. With this option enabled, each app can have its own regex domain in server_name blocks of the nginx config. This allows for useful domains like `store-number-\d*.example.com`. |
251251
| <a name="load-tcell-module"></a>deis-router | deployment | [router.deis.io/nginx.loadTcellModule](#load-tcell-module) | `"false"` | Whether to _enable_ the dynamic security nginx module provided by [Tcell](https://tcell.io/) as a [WAF](https://en.wikipedia.org/wiki/Web_application_firewall) on the router. Note this requires that you purchase a Tcell account and have application configured in the Tcell UI. The tcell_agent.config is passed through a configMap object through k8s. Example of the configMap can be found in this [gist](https://gist.github.com/Cryptophobia/648b23f234eeb9538c87b478de401a53). The open source alternative for this is Modsecurity dynamic module. |
252252
| <a name="global-tcell-app-id"></a>deis-router | deployment | [router.deis.io/nginx.globalTcellAppID](#global-tcell-app-id) | N/A | This is the global app id to insert for the Tcell module in the top-most http{} block of the nginx config. To configure an individual app id for each application, you need to use the application annotation below. The application specific app id will take precedence over the global one. |
253+
| <a name="load-modsecurity-module"></a>deis-router | deployment | [router.deis.io/nginx.loadModsecurityModule](#load-modsecurity-module) | `"false"` | Whether to _enable_ the open source dynamic security nginx module [Modsecurity](https://github.com/SpiderLabs/ModSecurity/tree/v3/master) globally for all apps as a [WAF](https://en.wikipedia.org/wiki/Web_application_firewall) on the router. The rule set that Modsecurity will use by default is the [OWASP ModSecurity Core Rule Set (CRS)](https://github.com/SpiderLabs/owasp-modsecurity-crs) and Modsecurity will be turned on to block malicious traffic on all apps if this annotation is enabled. This core rule set can be overwritten by configMap like in the example above for the Tcell module. |
253254
| <a name="default-whitelist"></a>deis-router | deployment | [router.deis.io/nginx.defaultWhitelist](#default-whitelist) | N/A | A default (router-wide) whitelist expressed as a comma-delimited list of addresses (using IP or CIDR notation). Application-specific whitelists can either extend or override this default. |
254255
| <a name="whitelist-mode"></a>deis-router | deployment | [router.deis.io/nginx.whitelistMode](#whitelist-mode) | `"extend"` | Whether application-specific whitelists should extend or override the router-wide default whitelist (if defined). Valid values are `"extend"` and `"override"`. |
255256
| <a name="default-service-enabled"></a>deis-router | deployment | [router.deis.io/nginx.defaultServiceEnabled](#default-service-enabled) | `"false"` | Enables default back-end service for traffic hitting /. In order to work correctly both `defaultServiceIP` and `DefaultAppName` MUST also be set. |

model/model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type RouterConfig struct {
5959
EnableRegexDomains bool `key:"enableRegexDomains" constraint:"(?i)^(true|false)$"`
6060
LoadTcellModule bool `key:"loadTcellModule" constraint:"(?i)^(true|false)$"`
6161
GlobalTcellAppID string `key:"globalTcellAppID" constraint:"(?i)^([a-z0-9]+(-[a-z0-9]+)*)+[a-z0-9]+$"`
62+
LoadModsecurityModule bool `key:"loadModsecurityModule" constraint:"(?i)^(true|false)$"`
6263
DefaultServiceIP string `key:"defaultServiceIP"`
6364
DefaultAppName string `key:"defaultAppName"`
6465
DefaultServiceEnabled bool `key:"defaultServiceEnabled" constraint:"(?i)^(true|false)$"`
@@ -96,6 +97,7 @@ func newRouterConfig() (*RouterConfig, error) {
9697
WhitelistMode: "extend",
9798
EnableRegexDomains: false,
9899
LoadTcellModule: false,
100+
LoadModsecurityModule: false,
99101
RequestIDs: false,
100102
SSLConfig: newSSLConfig(),
101103
DefaultServiceEnabled: false,

nginx/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ worker_processes {{ $routerConfig.WorkerProcesses }};
2121
load_module modules/ngx_http_tcell_agent_module.so;
2222
{{- end }}
2323
24+
{{ if $routerConfig.LoadModsecurityModule -}}
25+
# Loading the Modsecurity connector nginx dynamic module
26+
load_module modules/ngx_http_modsecurity_module.so;
27+
{{- end }}
28+
2429
events {
2530
worker_connections {{ $routerConfig.MaxWorkerConnections }};
2631
# multi_accept on;
@@ -237,6 +242,12 @@ http {
237242
tcell_app_id {{ $appConfig.TcellAppID }};
238243
{{- end }}
239244
245+
{{ if $routerConfig.LoadModsecurityModule -}}
246+
# Turning on modsecurity if modsecurity module loaded
247+
modsecurity on;
248+
modsecurity_rules_file /opt/router/conf/modsecurity.conf;
249+
{{- end }}
250+
240251
{{ if index $appConfig.Certificates $domain }}
241252
listen 6443 ssl {{ if $routerConfig.HTTP2Enabled }}http2{{ end }} {{ if $routerConfig.UseProxyProtocol }}proxy_protocol{{ end }};
242253
ssl_protocols {{ $sslConfig.Protocols }};

rootfs/Dockerfile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ RUN adduser --system \
99

1010
COPY /bin /bin
1111

12-
RUN buildDeps='gcc make libgeoip-dev libssl-dev libpcre3-dev'; \
12+
RUN buildDeps='gcc make apt-utils libgeoip-dev libssl-dev libpcre3-dev'; \
1313
apt-get update && \
1414
apt-get install -y --no-install-recommends \
1515
$buildDeps \
@@ -51,6 +51,22 @@ RUN buildDeps='gcc make libgeoip-dev libssl-dev libpcre3-dev'; \
5151
get_src 8c535a2e526a9684afca6c227dc2115eb06681c48103541b97c73716da9f0cf5 "https://s3.amazonaws.com/hephy-artifacts/hephy-router/nginx_tcellagent-1.0.7-agentonly-zuora-linux-x86_64.tar.gz" && \
5252
mv "$PREFIX/modules/nginx_tcellagent-1.0.7-agentonly-zuora-linux-x86_64/ubuntu/xenial/nginx-1.13.7-custom_flags_ssl_1.0.2g/ngx_http_tcell_agent_module.so" . && \
5353
rm -rf "$PREFIX/modules/nginx_tcellagent-1.0.7-agentonly-zuora-linux-x86_64" && \
54+
# include libmodsecurity3 and modsecurity connector dynamic module
55+
modsecurityDeps='apt-utils git libcurl4-openssl-dev libyajl-dev libxml2 libxml2-dev' && \
56+
apt-get install -y --no-install-recommends \
57+
$modsecurityDeps && \
58+
cd "$PREFIX/modules" && \
59+
get_src 2dd0d6dc258da729a42a94ea5118a3b16b2f3f7f91c198342a67f19378656366 "https://s3.amazonaws.com/hephy-artifacts/hephy-router/modsecurity-v3-ubuntu-16.04.tar.gz" && \
60+
mv usr/local/modsecurity /usr/local/modsecurity && \
61+
rm -rf usr && \
62+
get_src_file c9fd4ddb69ba1ce0a3118e529c43f87c3ab216e20900e25863e58537399d2d19 "https://s3.amazonaws.com/hephy-artifacts/hephy-router/ngx_http_modsecurity_module.so" && \
63+
# setup the modsecurity config and OWASP rules
64+
cd "$PREFIX/conf" && \
65+
get_src_file 905d47245003204b338eb1760933ab48a12c6b1a29c6f7080860b6c5d9ad337b "https://s3.amazonaws.com/hephy-artifacts/hephy-router/modsecurity.conf" && \
66+
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git && \
67+
cp -R owasp-modsecurity-crs/rules/ $PREFIX/conf/ && \
68+
cp $PREFIX/conf/owasp-modsecurity-crs/crs-setup.conf.example $PREFIX/conf/crs-setup.conf && \
69+
rm -rf owasp-modsecurity-crs && \
5470
# cleanup
5571
apt-get purge -y --auto-remove $buildDeps && \
5672
apt-get autoremove -y && \
@@ -76,7 +92,7 @@ RUN buildDeps='gcc make libgeoip-dev libssl-dev libpcre3-dev'; \
7692
COPY . /
7793

7894
# Fix some permissions since we'll be running as a non-root user
79-
RUN chown -R router:router /opt/router
95+
RUN chown -R router:router /opt/router /var/log
8096

8197
USER router
8298

rootfs/bin/get_src_file

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
hash="$1"
4+
url="$2"
5+
f=$(basename "$url")
6+
7+
curl -sSL "$url" -o "$f"
8+
echo "$hash $f" | sha256sum -c - || exit 10

0 commit comments

Comments
 (0)