Security
ModSecurity WAF
ModSecurity is a popular, open-source Web Application Firewall. In this section, you will learn how to manage ModSecurity in HAProxy Enterprise Kubernetes Ingress Controller to protect your container-based apps.
Enable ModSecurity for all Ingress routes Jump to heading
To enable ModSecurity with the Core Rule Set for all routes handled by the ingress controller:
- 
Download the latest version of the source code from the ModSecurity Core Rule Set GitHub page. The Core Rule Set contains a broad set of rules for detecting suspicious HTTP requests and is a good starting point for populating ModSecurity with sensible defaults. Later, you can customize these rules. The source code comes archived as a zip file or a tar, gzipped file. After downloading the archive, extract the files. 
- 
Inside the extracted folder, rename the file crs-setup.conf.exampletocrs-setup.conf.
- 
Download two files from the OWASP ModSecurity GitHub page: - unicode.mapping
- modsecurity.conf-recommended(rename it to- modsecurity.conf)
 You can use the following wgetcommands:nixwget https://raw.githubusercontent.com/owasp-modsecurity/ModSecurity/v3/master/unicode.mapping \-o unicode.mappingwget https://raw.githubusercontent.com/owasp-modsecurity/ModSecurity/v3/master/modsecurity.conf-recommended \-o modsecurity.confnixwget https://raw.githubusercontent.com/owasp-modsecurity/ModSecurity/v3/master/unicode.mapping \-o unicode.mappingwget https://raw.githubusercontent.com/owasp-modsecurity/ModSecurity/v3/master/modsecurity.conf-recommended \-o modsecurity.conf
- 
Copy crs-setup.conf,unicode.mapping, andmodsecurity.confto therulesfolder inside the Core Rule Set folder.
- 
From inside the rulesfolder, use the commandkubectl create secret genericto add all of the files in the folder to your Kubernetes cluster as a Secret object. The Secret will be named modsecuritycrs.nixkubectl create secret generic modsecuritycrs --namespace haproxy-controller --from-file .nixkubectl create secret generic modsecuritycrs --namespace haproxy-controller --from-file .
- 
Edit the ingress controller’s ConfigMap: nixkubectl edit configmap haproxy-kubernetes-ingress --namespace haproxy-controllernixkubectl edit configmap haproxy-kubernetes-ingress --namespace haproxy-controllerAdd the modsecuritykey under thedatasection, setting it to the namespace and name of the secret you just created. After making your changes, save and close the file.haproxy-kubernetes-ingress.yamlyamlapiVersion: v1kind: ConfigMapmetadata:annotations:...data:modsecurity: haproxy-controller/modsecuritycrshaproxy-kubernetes-ingress.yamlyamlapiVersion: v1kind: ConfigMapmetadata:annotations:...data:modsecurity: haproxy-controller/modsecuritycrsThis will enable ModSecurity for all applications behind the ingress controller. By default, ModSecurity is in detection-only mode and will not block threats. See the section Enable blocking mode. 
Enable ModSecurity only for a specific Ingress Jump to heading
Available since
version 1.7
To enable ModSecurity for specific Ingress rules instead of for all routes, follow these steps:
- 
Download the latest version of the source code from the ModSecurity Core Rule Set GitHub page. The Core Rule Set contains a broad set of rules for detecting suspicious HTTP requests and is a good starting point for populating ModSecurity with sensible defaults. Later, you can customize these rules. The source code comes archived as a zip file or a tar, gzipped file. After downloading the archive, extract the files. 
- 
Inside the extracted folder, rename the file crs-setup.conf.exampletocrs-setup.conf.
- 
Download two files from the OWASP ModSecurity GitHub page: - unicode.mapping
- modsecurity.conf-recommended(rename it to- modsecurity.conf)
 You can use the following wgetcommands:nixwget https://raw.githubusercontent.com/owasp-modsecurity/ModSecurity/v3/master/unicode.mapping \-o unicode.mappingwget https://raw.githubusercontent.com/owasp-modsecurity/ModSecurity/v3/master/modsecurity.conf-recommended \-o modsecurity.confnixwget https://raw.githubusercontent.com/owasp-modsecurity/ModSecurity/v3/master/unicode.mapping \-o unicode.mappingwget https://raw.githubusercontent.com/owasp-modsecurity/ModSecurity/v3/master/modsecurity.conf-recommended \-o modsecurity.conf
- 
Copy crs-setup.conf,unicode.mapping, andmodsecurity.confto therulesfolder inside the Core Rule Set folder.
- 
From inside the rulesfolder, use the commandkubectl create secret genericto add all of the files in the folder to your Kubernetes cluster as a Secret object. The Secret will be named modsecuritycrs.nixkubectl create secret generic modsecuritycrs --namespace haproxy-controller --from-file .nixkubectl create secret generic modsecuritycrs --namespace haproxy-controller --from-file .
- 
Edit your Ingress definition and add the modsecurityannotation.example-ingress.yamlyamlapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: example-ingressannotations:haproxy.org/modsecurity: haproxy-controller/modsecuritycrsspec:ingressClassName: haproxyrules:- host: "example.com"http:paths:- path: /pathType: Prefixbackend:service:name: example-serviceport:number: 8080example-ingress.yamlyamlapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: example-ingressannotations:haproxy.org/modsecurity: haproxy-controller/modsecuritycrsspec:ingressClassName: haproxyrules:- host: "example.com"http:paths:- path: /pathType: Prefixbackend:service:name: example-serviceport:number: 8080
- 
Apply the change using kubectl apply.nixkubectl apply -f example-ingress.yamlnixkubectl apply -f example-ingress.yamlNote that this technique allows you to set different rule sets for each Ingress resource to tailor the rules for each application. By default, ModSecurity is in detection-only mode and will not block threats. See the section Enable blocking mode. 
Enable blocking mode Jump to heading
By default, ModSecurity is in detection-only mode and will log, but not block, suspicious requests. To enable blocking of suspicious requests:
- 
Edit the file modsecurity.conf.
- 
Change the line SecRuleEngine DetectionOnlytoSecRuleEngine On.
- 
Delete and recreate the modsecuritycrs Secret. nixkubectl delete secret modsecuritycrs --namespace haproxy-controllerkubectl create secret generic modsecuritycrs --namespace haproxy-controller --from-file .nixkubectl delete secret modsecuritycrs --namespace haproxy-controllerkubectl create secret generic modsecuritycrs --namespace haproxy-controller --from-file .
- 
Test to see if the rules are working. Make a request that violates a rule and verify that the response is Forbidden. Note that in this case, the NodePort for the ingress controller service is 30000. nixcurl http://localhost:30000/?load=../../../../etc/passwdnixcurl http://localhost:30000/?load=../../../../etc/passwd
Do you have any suggestions on how we can improve the content of this page?