Project

General

Profile

Support #561

Updated by Daniel Curtis over 9 years ago

{{>toc}} 

 This is a simple guide for installing and configuring mod_security for Apache 2.4 on FreeBSD 9.2. 

 * Update the system and ports tree: 
 <pre> 
 pkg update && pkg upgrade 
 portsnap fetch extract 
 </pre> 

 * Install portmaster: 
 <pre> 
 cd /usr/ports/ports-mgmt/portmaster 
 make install clean 
 pkg2ng 
 </pre> 

 * Install git: 
 <pre> 
 portmaster devel/git 
 </pre> 

 * Install sudo: 
 <pre> 
 portmaster security/sudo 
 </pre> 

 --- 

 h1. Install mod_security 

 * Install mod_security 
 <pre> 
 portmaster www/mod_security 
 </pre> 

 h2. Configure mod_security 

 * ModSecurity requires firewall rule definitions. Most people use the OWASP ModSecurity Core Rule Set (CRS). The easiest way to track the OWASP CRS repository right now is to use Git. Let's make a directory for all our ModSecurity related stuff, and clone the CRS repository under it 
 <pre> 
 mkdir -p /usr/local/etc/modsecurity && cd /usr/local/etc/modsecurity 
 git clone https://github.com/SpiderLabs/owasp-modsecurity-crs crs 
 </pre> 

 * Copy the default ModSecurity config file: 
 <pre> 
 cp /usr/local/etc/modsecurity.conf-example /usr/local/etc/modsecurity.conf 
 </pre> 

 * And fetch a necessary file which is currently not included in the port: 
 <pre> 
 cd /usr/local/etc 
 fetch https://raw.github.com/SpiderLabs/ModSecurity/master/unicode.mapping 
 </pre> 

 * Copy the default ModSecurity CRS config file: 
 <pre> 
 cd /usr/local/etc/modsecurity 
 cp crs/modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf 
 </pre> 

 * Now create an Apache configuration snippet that loads the ModSecurity module and includes the configurations and CRS: 
 <pre> 
 vi /usr/local/etc/apache24/modules.d/020_mod_security.conf 
 </pre> 
 #* And add/modify the following 
 <pre> 
 LoadModule security2_module libexec/apache24/mod_security2.so 

 <IfModule security2_module> 
     # Include ModSecurity configuration 
     Include /usr/local/etc/modsecurity.conf 

     # Include OWASP Core Rule Set (CRS) configuration and base rules 
     Include /usr/local/etc/modsecurity/modsecurity_crs_10_setup.conf 
     Include /usr/local/etc/modsecurity/crs/base_rules/*.conf 

     # Add custom configuration and CRS exceptions here. Example: 
     # SecRuleRemoveById 960015 
 </IfModule> 
 </pre> 

 * When the configuration is all set, simply restart Apache:  
 <pre> 
 service apache24 restart 
 </pre> 
 #* Confirm that ModSecurity is loaded by checking Apache's log file: 
 <pre> 
 tail /var/log/httpd-error.log 
 </pre> 
 #* _Example output_: 
 <pre> 
 ModSecurity for Apache/2.7.7 (http://www.modsecurity.org/) configured. 
 ModSecurity: APR compiled version="1.4.8"; loaded version="1.4.8" 
 ModSecurity: PCRE compiled version="8.34 "; loaded version="8.34 2013-12-15" 
 ModSecurity: LIBXML compiled version="2.8.0" 
 </pre> 

 * Also check with the apachectl command: 
 <pre> 
 apachectl -M 
 </pre> 
 #* _ Truncated output:_ 
 <pre> 
 Loaded Modules: 
  ...  
  security2_module (shared) 
 </pre> 

 h3. Enable blocking mode 

 * Blocking mode can be enabled by editing @modsecurity.conf@ and changing the following line: 
 <pre> 
 SecRuleEngine On 
 </pre> 
 #* And restart apache to apply it: 
 <pre> 
 service apache24 restart 
 </pre> 

 h3. Update Core Rule Set 

 * Keep the CRS updated from time to time: 
 <pre> 
 cd /usr/local/etc/modsecurity/crs 
 git pull 
 </pre> 

 h2. Install WeBekci 

 * Download and extract WeBekci: 
 <pre> 
 cd ~ 
 wget http://downloads.sourceforge.net/project/webekci/webekci/OWASP-WeBekci-1.0/webekci-1.0.tar.gz 
 tar xzf webekci-1.0.tar.gz 
 </pre> 

 * Move and change into the WeBekci directory: 
 <pre> 
 mv webekci /usr/local/www/apache24/data 
 cd /usr/local/www/apache24/data/webekci 
 </pre> 

 * Edit .htaccess file: 
 <pre> 
 vi .htaccess 
 </pre>  
 #* And modify the correct path for the .htpasswd file in the AuthUserFile line: 
 <pre> 
 AuthUserFile /usr/home/bunyamin/.htpasswd 
 AuthType Basic 
 AuthName "Owasp-WeBekci Screet Area" 
 <LIMIT GET POST> 
  require valid-user 
 </LIMIT> 
 </pre> 

 * Now create a new .htpasswd file for user bob with password SuperSecretPassword: 
 <pre> 
 htpasswd -bc /usr/local/www/apache24/data/webekci bob SuperSecretPassword 
 </pre> 

 * Now, you need define new Directory in the httpd.conf file. 
 <pre> 
 vi /usr/local/etc/apache24/httpd.conf 
 </pre> 
 #* And add the following: 
 Alias /webekci/ "/usr/local/www/webekci/" 
 <Directory "/usr/local/www/webekci/"> 
   Options None 
   AllowOverride All 
   Order Allow,Deny 
   Allow from 127.0.0.1 
 </Directory> 
 *NOTE*: If you are using mod_rewrite, then enter “AllowOverride All” so that .htaccess file can be read. Otherwise enter “AllowOverride None”. 

 * Make necessary modifications in config.php file.  
 <pre> 
 vi config.php 
 </pre> 
 #* Add the following line: 
 <pre> 
 $config['modsecurity_conf']='/usr/local/etc/apache24/modules.d/020_mod_security.conf'; 
 </pre> 

 * To give the www user read and write permissions: 
 <pre> 
 chown www /usr/local/etc/apache24/modules.d/020_mod_security.conf 
 </pre> 

 * The www user is the user that apache runs as. Make sure the following entries are in httpd.conf: 
 <pre> 
 User www 
 Group www 
 </pre> 

 * After configuring WeBekci you need to restart apache: 
 <pre> 
 service apache restart 
 </pre> 

 * Edit the sudoers file: 
 <pre> 
 visudo 
 </pre> 
 #* And add these lines to allow apache to run configtest and restart on itself: 
 <pre> 
 www ALL=NOPASSWD:/usr/local/sbin/httpd -k restart 
 www ALL=NOPASSWD:/usr/local/sbin/httpd -t 
 </pre> 
 Now www user can do configtest and restart operations without having to enter a password. 

 * Do this with these sudo configurations:  
 <pre> 
 $config['apache_config_test'] = '/usr/local/bin/sudo /usr/local/sbin/httpd -t'; 
 $config['apache_restart']='/usr/local/bin/sudo /usr/local/sbin/httpd -k restart'; 
 </pre> 

 * Edit the sudoers file: 
 <pre> 
 visudo 
 </pre> 
 #* And add these lines to allow apache to run configtest and restart on itself: 
 <pre> 
 www ALL=NOPASSWD:/usr/local/sbin/httpd -k restart 
 www ALL=NOPASSWD:/usr/local/sbin/httpd -t 
 </pre> 
 Now www user can do “config test” and “restart” operations restart apache without having to enter password. 

 * Make sure the entered MySQL related changes are in the config.php file: 
 <pre> 
 vi config.php 
 </pre> 
 #* And modify the following to the database created earlier: 

 * Now browse your site and run the install.php file: 
 http://www.example.com/webekci/install.php 

 * Do not forget to delete install.php when the install has finished: 
 <pre> 
 rm /usr/local/www/apache24/data/webekci/install.php 
 </pre> 

 h2. Resources 

 * https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual 
 * https://www.owasp.org/index.php/Category:OWASP_WeBekci_Project

Back