歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Linux下 用 fail2ban 防止攻擊

fail2ban可以監視你的系統日志,然後匹配日志的錯誤信息(正則式匹配)執行相應的屏蔽動作(一般情況下是調用防火牆屏蔽),如:當有人在試探你的SSH、SMTP、FTP密碼,只要達到你預設的次數,fail2ban就會調用防火牆屏蔽這個IP,而且可以發送e-mail通知系統管理員

通過iptables, tcp-wrapper, shorewall等方式阻止此IP的訪問。

需要Pytion支持

python安裝

python安裝方法:

cd /tmp
wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar xzf Python-2.7.3.tgz
cd Python-2.7.3
./configure
make && make install
rm -rf /usr/bin/python
ln -s /tmp/Python-2.7.3/python /usr/bin/

fail2ban安裝

cd /tmp
wget https://github.com/downloads/fail2ban/fail2ban/fail2ban_0.8.6.orig.tar.gz
tar xzf fail2ban_0.8.6.orig.tar.gz
cd fail2ban-fail2ban-a20d1f8/
./setup.py install
cp files/RedHat-initd  /etc/init.d/fail2ban
chmod 755 /etc/init.d/fail2ban

配置fail2ban日志輪循:

vi /etc/logrotate.d/fail2ban

寫入:

/var/log/fail2ban.log {
    weekly
    rotate 7
    missingok
    compress
    postrotate
      /etc/init.d/fail2ban restart 1>/dev/null || true
    endscript
}

fail2ban使用方法
配置文件:

/etc/fail2ban/
├── action.d
│  ├── dummy.conf
│  ├── hostsdeny.conf
│  ├── iptables.conf
│  ├── mail-whois.conf
│  ├── mail.conf
│  └── shorewall.conf
├── fail2ban.conf
├── fail2ban.local
├── filter.d
│  ├── apache-auth.conf
│  ├── apache-noscript.conf
│  ├── couriersmtp.conf
│  ├── postfix.conf
│  ├── proftpd.conf
│  ├── qmail.conf
│  ├── sasl.conf
│  ├── sshd.conf
│  └── vsftpd.conf
├── jail.conf
└── jail.local

目錄action.d下的文件指定滿足條件時執行的一些動作,比如使用iptables禁止ip訪問。
目錄filter.d下的文件定義匹配日志的正則表達式。
fail2ban.conf文件是配置fail2ban-server程序啟動的一些參數
jail.conf文件包含filter及action的指定。
每個conf文件可被local文件覆蓋,conf文件第一個被讀取,接著是讀取local文件,所以local文件中定義的參數會覆蓋conf中的參數。所以我們不需要添加所有的內容到local文件,只需要添加conf文件中你想覆蓋的部分參數就好。

Copyright © Linux教程網 All Rights Reserved