{"id":163,"date":"2012-06-11T04:21:30","date_gmt":"2012-06-11T04:21:30","guid":{"rendered":"http:\/\/rmohan.com\/?p=163"},"modified":"2012-07-29T07:24:30","modified_gmt":"2012-07-28T23:24:30","slug":"mod-security","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=163","title":{"rendered":"Mod Security"},"content":{"rendered":"<p><strong>Mod Security <\/strong><\/p>\n<p>Mod security has a default configuration file, and comes with a core rule set. The configuration works with include files which work for the modsecurity part like this:<\/p>\n<p>httpd.conf<br \/>\n|<br \/>\n|&#8211; default-server.conf . . . . . . . . . set up the default server that replies to non-virtual-host requests<br \/>\n| `&#8211;conf.d\/mod_security2.conf . . . . enable mod-security default configuration<br \/>\n|<br \/>\n`&#8211;conf.d\/modsecurity\/*.conf . . . . . . add the core rule set<\/p>\n<p>Since this include structure is not enabled by default (because the core rule set is not enabled by default) we have to include the core rule set manually.<\/p>\n<p>Create the correct directories and copy the core rule set config files to this directory:<\/p>\n<p>reverseproxy:\/usr\/share\/doc\/packages\/apache2-mod_security2\/rules # mkdir \/etc\/apache2\/conf.d\/modsecurity<br \/>\nreverseproxy:\/usr\/share\/doc\/packages\/apache2-mod_security2\/rules # cp *.conf \/etc\/apache2\/conf.d\/modsecurity<br \/>\nreverseproxy:\/usr\/share\/doc\/packages\/apache2-mod_security2\/rules # cd \/etc\/apache2\/conf.d\/modsecurity<\/p>\n<p>reverseproxy:\/etc\/apache2\/conf.d\/modsecurity # ll<br \/>\n-rw-r&#8211;r&#8211; 1 root root 12325 Jan 31 14:03 modsecurity_crs_10_config.conf<br \/>\n-rw-r&#8211;r&#8211; 1 root root 5164 Jan 31 14:03 modsecurity_crs_20_protocol_violations.conf<br \/>\n-rw-r&#8211;r&#8211; 1 root root 3538 Jan 31 14:03 modsecurity_crs_21_protocol_anomalies.conf<br \/>\n-rw-r&#8211;r&#8211; 1 root root 2496 Jan 31 14:03 modsecurity_crs_23_request_limits.conf<br \/>\n-rw-r&#8211;r&#8211; 1 root root 6399 Jan 31 14:03 modsecurity_crs_30_http_policy.conf<br \/>\n-rw-r&#8211;r&#8211; 1 root root 2720 Jan 31 14:03 modsecurity_crs_35_bad_robots.conf<br \/>\n-rw-r&#8211;r&#8211; 1 root root 28726 Jan 31 14:03 modsecurity_crs_40_generic_attacks.conf<br \/>\n-rw-r&#8211;r&#8211; 1 root root 2463 Jan 31 14:03 modsecurity_crs_45_trojans.conf<br \/>\n-rw-r&#8211;r&#8211; 1 root root 8268 Jan 31 14:03 modsecurity_crs_50_outbound.conf<\/p>\n<p>Add the include line for the core rule set in the httpd.conf:<\/p>\n<p># Include Mod Security Core Rule Set<br \/>\nInclude \/etc\/apache2\/conf.d\/modsecurity\/*.conf<\/p>\n<p>Now we will configure the config files themselves to run modsecurity first in DetectionOnly<br \/>\nmode to prevent the risk for false positives. We also set the logfiles correctly:<\/p>\n<p>vi \/etc\/apache2\/conf.d\/mod_security2.conf:<br \/>\n# Basic configuration options<br \/>\n#SecRuleEngine On<br \/>\nSecRuleEngine DetectionOnly<\/p>\n<p>vi \/etc\/apache2\/conf.d\/modsecurity\/modsecurity_crs_10_config.conf:<br \/>\nSecRuleEngine DetectionOnly<br \/>\nSecAuditLog \/var\/log\/apache2\/modsec_audit.log<br \/>\nSecDebugLog \/var\/log\/apache2\/modsec_debug.log<br \/>\nSecDebugLogLevel 3<\/p>\n<p>Now restart apache:<\/p>\n<p>reverseproxy:\/var\/log\/apache2 # \/etc\/init.d\/apache2 start<br \/>\nStarting httpd2 (prefork) [Mon Jan 31 14:30:35 2011] [warn] worker http:\/\/10.10.12.20\/start already used by another worker<br \/>\n[Mon Jan 31 14:30:35 2011] [warn] worker http:\/\/10.10.12.20\/start already used by another worker<\/p>\n<p>Documentation Core Rule Set<\/p>\n<p>Core Rule Set Structure &amp; Usage<br \/>\n====================================<\/p>\n<p>To activate the rules for your web server installation:<\/p>\n<p>1) You may want to edit and customize modsecurity_crs_10_config.conf.<br \/>\nAdditionally you may want to edit modsecurity_crs_30_http_policy.conf<br \/>\nwhich enforces an application specific HTTP protocol usage.<\/p>\n<p>2) Add the following line to your httpd.conf (assuming<br \/>\nyou&#8217;ve placed the rule files into conf\/modsecurity\/):<\/p>\n<p>Include conf\/modsecurity\/*.conf<\/p>\n<p>3) Restart web server.<\/p>\n<p>4) Make sure your web sites are still running fine.<\/p>\n<p>Core Rule Set Content<br \/>\n=========================<\/p>\n<p>In order to provide generic web applications protection, the Core Rule Set<br \/>\nuses the following techniques:<\/p>\n<p>1. HTTP protection &#8211; detecting violations of the HTTP protocol and a locally<br \/>\ndefined usage policy.<\/p>\n<p>2. Common Web Attacks Protection &#8211; detecting common web application security<br \/>\nattack.<\/p>\n<p>3. Automation detection &#8211; Detecting bots, crawlers, scanners and other surface<br \/>\nmalicious activity.<\/p>\n<p>4. Trojan Protection &#8211; Detecting access to Trojans horses.<\/p>\n<p>5. Errors Hiding &#8211; Disguising error messages sent by the server<\/p>\n<p>In addition the rule set also hints at the power of ModSecurity beyond<br \/>\nproviding security by reporting access from the major search engines to your<br \/>\nsite.<\/p>\n<p>HTTP Protection &#8211; This first line of protection ensures that all abnormal HTTP<br \/>\nrequests are detected. This line of defense eliminates a large number of<br \/>\nautomated and non targeted attacks as well as protects the web server itself.<br \/>\nCommon Web Attacks Protection Rules on the second level address the common web<br \/>\napplication security attack methods. These are the issues that can appear in<br \/>\nany web application. Some of the issues addressed are:<\/p>\n<p>&#8211; SQL Injection<br \/>\n&#8211; Cross-Site Scripting (XSS)<br \/>\n&#8211; OS Command execution<br \/>\n&#8211; Remote code inclusion<br \/>\n&#8211; LDAP Injection<br \/>\n&#8211; SSI Injection<br \/>\n&#8211; Information leak<br \/>\n&#8211; Buffer overflows<br \/>\n&#8211; File disclosure<\/p>\n<p>Automation Detection &#8211; Automated clients are both a security risk and a<br \/>\ncommercial risk. Automated crawlers collect information from your site, consume<br \/>\nbandwidth and might also search for vulnerabilities on the web site. Automation<br \/>\ndetection is especially useful for generic detection of comments spam.<\/p>\n<p>Trojan Protection &#8211; ModSecurity Core Rule Set detects access to back doors<br \/>\ninstalled on a web server. This feature is very important in a hosting<br \/>\nenvironment when some of this backdoors may be uploaded in a legitimate way and<br \/>\nused maliciously. In addition the Core Rule Set includes a hook for adding<br \/>\nan Anti-Virus program such as ClamAV for checking file uploads.<\/p>\n<p>Errors Hiding &#8211; If all fails, the Core Rule Set will detect errors sent by<br \/>\nthe web server. Detecting and blocking errors prevents attackers from<br \/>\ncollecting reconnaissance information about the web application and also server<br \/>\nas a last line of defense in case an attack was not detected eariler.<\/p>\n<p>Few Word of Caution<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>As with every new technology, using the ModSecurity Core Rule Set requires some caution:<\/p>\n<p>&#8211; Every Rule Set can have false positive in new environments and any new<br \/>\ninstallation should initially use the log only Rule Set version or if no such<br \/>\nversion is available, set ModSecurity to Detection only using the SecRuleEngine<br \/>\nDetectionOnly command.<\/p>\n<p>After running ModSecurity in a detection only mode for a while review the evens<br \/>\ngenerated and decide if any modification to the rule set should be made before<br \/>\nmoving to protection mode.<\/p>\n<p>From the mod security manual:<\/p>\n<p>SecRuleEngine<\/p>\n<p>Description: Configures the rules engine.<br \/>\nSyntax: SecRuleEngine On|Off|DetectionOnly<br \/>\nExample Usage: SecRuleEngine On<br \/>\nProcessing Phase: Any<br \/>\nScope: Any<br \/>\nVersion: 2.0.0<br \/>\nDependencies\/Notes: This directive can also be controlled by the ctl action (ctl:ruleEngine=off) for per rule processing.<br \/>\nPossible values are:<br \/>\n* On &#8211; process rules.<br \/>\n* Off &#8211; do not process rules.<br \/>\n* DetectionOnly &#8211; process rules but never intercept transactions, even when rules are configured to do so.<\/p>\n<p>Mod Security Handling False Positives<br \/>\nMod security is now configured as detection only. For now, we keep it like this, closely monitoring the mod security logfiles for false positives. When we are sure there are no more false positives (or at least nothing our customers will notice) we can set the SecRuleEngine to On.<\/p>\n<p>This blog also explains how to deal with false positives: Handling False Positives<br \/>\nMod Security Troubleshooting<\/p>\n<p>Starting httpd2 (prefork) [Mon Jan 31 14:20:51 2011] [warn] worker http:\/\/10.10.12.20\/start already used by another worker<br \/>\n[Mon Jan 31 14:20:51 2011] [warn] worker http:\/\/10.10.12.20\/start already used by another worker<br \/>\nSyntax error on line 53 of \/etc\/apache2\/conf.d\/modsecurity\/modsecurity_crs_10_config.conf:<br \/>\nInvalid command &#8216;SecRuleEngine&#8217;, perhaps misspelled or defined by a module not included in the server configuration<\/p>\n<p>The command line was:<br \/>\n\/usr\/sbin\/httpd2-prefork -f \/etc\/apache2\/httpd.conf -DSSL<\/p>\n<p>&#8216;Solution:&#8217; The module mod_security is not enabled. Check for the module with the command &#8216;httpd2 -M&#8217;. If the module is really not there, add the module in \/etc\/sysconfig\/apache2.<\/p>\n<p>reverseproxy:\/var\/log\/apache2 # \/etc\/init.d\/apache2 restart<br \/>\n[Mon Jan 31 14:29:23 2011] [warn] worker http:\/\/10.10.12.20\/start already used by another worker<br \/>\n[Mon Jan 31 14:29:23 2011] [warn] worker http:\/\/10.10.12.20\/start already used by another worker<br \/>\nSyntax error on line 191 of \/etc\/apache2\/conf.d\/modsecurity\/modsecurity_crs_10_config.conf:<br \/>\nModSecurity: Failed to open the audit log file: \/srv\/www\/logs\/modsec_audit.log<\/p>\n<p>&#8216;Solution:&#8217; The directory specified for the logs does not exist. Create the directory with this command:<\/p>\n<p>reverseproxy:\/var\/log\/apache2 # mkdir -p \/srv\/www\/logs\/<\/p>\n<p>or change the location to \/var\/log\/apache2. Of course, the same message can be displayed for \/srv\/www\/logs\/modsec_debug.log.<br \/>\nTesting Mod Security<br \/>\nYou can test if mod security is running correctly by going to the index file of your website by ip-address and adding &#8216;?file=\/etc\/passwd&#8217; to the url:<\/p>\n<p>https:\/\/10.10.10.20\/start\/index.html?file=\/etc\/passwd<\/p>\n<p>This will be noticed, and displayed in the log (not stopped, remember, we&#8217;re running in DetectionOnly mode):<\/p>\n<p>less modsec_debug.log<\/p>\n<p>[31\/Jan\/2011:15:46:31 +0100] [10.10.10.20\/sid#7f0c98cffdc8][rid#7f0c98feb488][\/start\/0100_NavigationPublic.html][2] Warning. Pattern match &#8220;^[\\d\\.]+$&#8221; at REQUEST_HEADERS:Host. [<br \/>\nfile &#8220;\/etc\/apache2\/conf.d\/modsecurity\/modsecurity_crs_21_protocol_anomalies.conf&#8221;] [line &#8220;60&#8221;] [id &#8220;960017&#8221;] [msg &#8220;Host header is a numeric IP address&#8221;] [severity &#8220;CRITICAL&#8221;] [ta<br \/>\ng &#8220;PROTOCOL_VIOLATION\/IP_HOST&#8221;]<br \/>\n[31\/Jan\/2011:15:46:42 +0100] [10.10.10.20\/sid#7f0c98cffdc8][rid#7f0c98fe2908][\/start\/index.html][2] Warning. Pattern match &#8220;^[\\d\\.]+$&#8221; at REQUEST_HEADERS:Host. [file &#8220;\/etc\/apach<br \/>\ne2\/conf.d\/modsecurity\/modsecurity_crs_21_protocol_anomalies.conf&#8221;] [line &#8220;60&#8221;] [id &#8220;960017&#8221;] [msg &#8220;Host header is a numeric IP address&#8221;] [severity &#8220;CRITICAL&#8221;] [tag &#8220;PROTOCOL_VIOL<br \/>\nATION\/IP_HOST&#8221;]<br \/>\n[31\/Jan\/2011:15:46:42 +0100] [10.10.10.20\/sid#7f0c98cffdc8][rid#7f0c98fe2908][\/start\/index.html][2] Warning. Pattern match &#8220;(?:\\b(?:\\.(?:ht(?:access|passwd|group)|www_?acl)|glob<br \/>\nal\\.asa|httpd\\.conf|boot\\.ini)\\b|\\\/etc\\\/)&#8221; at ARGS:file. [file &#8220;\/etc\/apache2\/conf.d\/modsecurity\/modsecurity_crs_40_generic_attacks.conf&#8221;] [line &#8220;114&#8221;] [id &#8220;950005&#8221;] [msg &#8220;Remote<br \/>\nFile Access Attempt&#8221;] [data &#8220;\/etc\/&#8221;] [severity &#8220;CRITICAL&#8221;] [tag &#8220;WEB_ATTACK\/FILE_INJECTION&#8221;]<br \/>\n[31\/Jan\/2011:15:46:42 +0100] [10.10.10.20\/sid#7f0c98cffdc8][rid#7f0c98fe2908][\/start\/index.html][2] Warning. Pattern match &#8220;(?:\\b(?:(?:n(?:et(?:\\b\\W+?\\blocalgroup|\\.exe)|(?:map|<br \/>\nc)\\.exe)|t(?:racer(?:oute|t)|elnet\\.exe|clsh8?|ftp)|(?:w(?:guest|sh)|rcmd|ftp)\\.exe|echo\\b\\W*?\\by+)\\b|c(?:md(?:(?:32)?\\.exe\\b|\\b\\W*?\\\/c)|d(?:\\b\\W*?[\\\\\/]|\\W*?\\.\\.)|hmod.{0,40}?\\+.<br \/>\n{0,3}x))|[\\;\\|\\`]\\W*? &#8230;&#8221; at ARGS:file. [file &#8220;\/etc\/apache2\/conf.d\/modsecurity\/modsecurity_crs_40_generic_attacks.conf&#8221;] [line &#8220;133&#8221;] [id &#8220;950006&#8221;] [msg &#8220;System Command Injectio<br \/>\nn&#8221;] [data &#8220;\/passwd&#8221;] [severity &#8220;CRITICAL&#8221;] [tag &#8220;WEB_ATTACK\/COMMAND_INJECTION&#8221;]<\/p>\n<p>less modsec_audit.log:<\/p>\n<p>Message: Warning. Pattern match &#8220;^[\\d\\.]+$&#8221; at REQUEST_HEADERS:Host. [file &#8220;\/etc\/apache2\/conf.d\/modsecurity\/modsecurity_crs_21_protocol_anomalies.conf&#8221;] [line &#8220;60&#8221;] [id &#8220;960017&#8221;]<br \/>\n[msg &#8220;Host header is a numeric IP address&#8221;] [severity &#8220;CRITICAL&#8221;] [tag &#8220;PROTOCOL_VIOLATION\/IP_HOST&#8221;]<br \/>\nApache-Handler: proxy-server<br \/>\nStopwatch: 1296487473036980 19376 (997 2882 -)<br \/>\nProducer: ModSecurity for Apache\/2.5.6 (http:\/\/www.modsecurity.org\/); core ruleset\/1.6.1.<br \/>\nServer: Apache\/2.2.10 (Linux\/SUSE)<\/p>\n<p><strong>Mod Security Resources<\/strong><\/p>\n<p>http:\/\/www.modsecurity.org\/<br \/>\nhttp:\/\/www.modsecurity.org\/documentation\/faq.html<br \/>\nhttp:\/\/www.modsecurity.org\/documentation\/modsecurity-apache\/2.5.12\/html-multipage\/introduction.html<br \/>\nInstall Modsecurity<br \/>\nInstall core rule set<\/p>\n<p>&nbsp;<\/p>\n<p>MY SET OF RULES TO DEFEND THE WEB SERVER<br \/>\nSecFilterEngine On<\/p>\n<p># Make sure that URL encoding is valid<br \/>\nSecFilterCheckURLEncoding On<\/p>\n<p># Unicode encoding check<br \/>\nSecFilterCheckUnicodeEncoding On<\/p>\n<p># Only allow bytes from this range<br \/>\nSecFilterForceByteRange 0 255<\/p>\n<p># Only log actionable requests<br \/>\nSecAuditEngine RelevantOnly<\/p>\n<p># The name of the audit log file<br \/>\nSecAuditLog \/var\/log\/apache2\/audit_log<\/p>\n<p># Debug level set to a minimum<br \/>\nSecFilterDebugLog \/var\/log\/apache2\/modsec_debug_log<br \/>\nSecFilterDebugLevel 2<\/p>\n<p># Should mod_security inspect POST payloads<br \/>\nSecFilterScanPOST On<\/p>\n<p># By default log and deny suspicious requests<br \/>\n# with HTTP status 500<br \/>\nSecFilterDefaultAction &#8220;deny,log,status:500&#8221;<\/p>\n<p># Add custom secfilter rules here<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mod Security <\/p>\n<p>Mod security has a default configuration file, and comes with a core rule set. The configuration works with include files which work for the modsecurity part like this:<\/p>\n<p>httpd.conf | |&#8211; default-server.conf . . . . . . . . . set up the default server that replies to non-virtual-host requests | [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/163"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163"}],"version-history":[{"count":6,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":827,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions\/827"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}