4

apache的安全增强配置(使用mod_chroot,mod_security)

 2 years ago
source link: https://byronhe.com/post/2010/08/17/apache-security-enhancement-using-mod_chroot-mod_security/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

apache的安全增强配置(使用mod_chroot,mod_security)

LAMP环境的一般入侵,大致经过sql注入,上传webshell,本地提权至root,安装rootkit等步骤。

采用如下的配置,mod_chroot和单独分区挂载可以让本地提权变得极为困难,而mod-security可以封堵一般的sql注入等应用层攻击。

以下内容是在ubuntu10.04上实践以后总结出来的,直接上命令,就不做过多解释了,水平有限,错误在所难免,欢迎指正。

首先确保apache,php,mysql已经正常工作,出现问题的话,查看 /var/log/apache2, /var/log/syslog, /var/log/mysql/error.log 尤其是apparmor导致的权限错误,不易发现。

1.mod_chroot的安装,配置

目标是chroot到/var/www。

(1) 安装:

sudo service apache2 stop
sudo apt-get libapache2-mod-chroot
sudo vi /etc/apache2/mod-available/mod_chroot.conf
<IfModulemod_chroot.c>
  LoadFile /lib/libgcc_s.so.1
  ChrootDir /var/www
</IfModule>
sudo a2enmodmod_chroot

/etc/apache2/site-enabled/000-default 中 DocumentRoot 改为 /

sudo ln -s /var/www/var/run/apache2.pid /var/run/apache2.pid
ln -s /var/www/var/run/apache2.pid /var/run/apache2.pid

加到/etc/rc.local中

sudo mkdir /var/www/tmp #放session
sudo chmod 1777tmp
sudo mkdir -p /var/www/var/run/mysqld
sudo ln -s / /var/www/var/www

安装好apache,静态页面和php正常后,弄mysql

  1. sudo service mysql stop.
  2. 改/etc/apparmor.d/usr.sbin.mysqld,         把"/var/run/mysqld.sock w,“那一行复制并改为        “/var/www/var/run/mysqld.sock w,”
  3. 改/etc/mysql/my.cnf         在[client],[mysqld_safe],[mysqld]每一节里socket路径改为        “socket = /var/www/var/run/mysqld/mysqld.sock”
  4. sudo servicemysql start.

一些其他问题

date()不正常,解决办法:

sudo mkdir -p /var/www/usr/share /var/www/etc
sudo cp -rp /usr/share/zoneinfo /var/www/usr/share/
sudo cp /etc/localtime/var/www/etc/

DNS可能出问题(没试过)

sudo cp /etc/resolv.conf/var/www/etc/resolv.conf

找不到错误页面

sudo cp -rp /usr/share/apache2//var/www/usr/share/

2.mod_security的安装,配置

sudo aptitude install libapache2-mod-security2
sudo cp/usr/share/doc/mod-security-common/examples/rules/modsecurity_crs_10_config.conf  /etc/apache2/mods-enabled/mod-security.conf
sudo a2enmod mod-security
sudo apache2ctl stop

修改 /etc/apache2/mods-available/mod-security.conf 中的 debug_log 和 audit_log 路径到合适的位置,并添加如下两行

Include/usr/share/doc/mod-security-common/examples/rules/modsecurity_crs_10_global_config.conf
Include/usr/share/doc/mod-security-common/examples/rules/base_rules/*.conf
sudo apache2ctl start

 #/usr/share/doc/mod-security-common/examples/rules/目录下还有一些规则可用

参照gentoo的ebuild中的如下内容(http://gentoo-portage.com/www-apache/mod_security/ChangeLog)

if ! use vanilla; then 
         mv "${D}"${APACHE_MODULES_CONFDIR}/mod_security/modsecurity_*{41_phpids,50_outbound}*/
                 "${D}"${APACHE_MODULES_CONFDIR}/mod_security/optional_rules || die
fi

modsecurity_*{41_phpids,50_outbound}*的这几个规则还是不要用比较好。

另外,http://www.gotroot.com/tiki-index.php?page=mod_security+rules 提供一些规则

3.另外,可以把/var/www/放在一个单独的分区上,用noexec,nosuid,nodev参数挂载,打开mysql的apparmor,可以极大增强安全性。

参考资料: /usr/share/doc/mod-chroot-common/

http://core.segfault.pl/~hobbit/mod_chroot/index.html

http://server.it168.com/a2010/0714/1077/000001077357.shtml

http://www.howtoforge.com/chrooting-apache2-mod-chroot-debian-etch

/usr/share/doc/mod-security-common

后期补充一些:

chroot的关键是确保/var/www/ ,/var/www/var/www都存在,并且/var/www/var/www 是指向/的符号连接

对apache chroot的过程,我的理解(如有不对,欢迎指正)是

  1. 先初始化其他模块
  2. chroot到/var/www
  3. 改变当前目录到/var/www(此时实际上是改变到/var/www/var/www目录下)
  4. 当http请求到来时,比如http://127.0.0.1/index.php,根据vhost的配置(如果documentroot是/,网页实际放在/var/www/下的话),apache寻找/index.php(实际上是/var/www/index.php)

另外,还有个诊断方法是查看/proc//root和/proc//cwd,一个是apache的root路径,一个是apache的当前目录(是apache的pid ),再看看apache的访问日志


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK