17 tháng 1 2013

Install Firewall in Linux Centos

1. Install APF Firewall in Linux 

Tutorial on installing APF firewall (IP tables based) in Linux. 

When it comes to security, there is no such thing without firewall. when you buy a new dedicated or vps server, atleast make sure you install a firewall to protect your server from various intrusion attacks. Within minutes you can set up your firewall up and running. APF firewall is one of the popular and easy to setup IP tables based firewall for Linux servers. It also has additional features like prevention from DoS attacks.



Just follow the steps to setup and configure the firewall.

CAUTION: Be careful on setting up the firewall on what ports to open and what not. If you arent sure, you may lock yourself out

> wget http://www.rfxnetworks.com/downloads/apf-current.tar.gz
> tar -zxf apf-0.9.3.tar.gz

Now run the install shell script. Make sure you have iptables installed before you install apf firewall.

> cd apf-0.9.3
> ./install.sh


After installation, you have to manually configure your firewall settings on what ports to open and what to block. You can edit the configuration file located in /etc/apf/conf.apf

Installed paths
Configuration File: /etc/apf/conf.apf
Binary: /usr/local/sbin/apf
Start/Stop: /etc/init/d/apf (start|stop|restart)
Log: /var/log/apf_log
Antidos conf file: /etc/apf/ad/config.antidos


You can also use commands in the command line

apf -s (start)
apf -r (to restart)
apf -f (to stop)


Firewall Configuration
Now open /etc/apf/conf.apf configuration file to manually make settings.

> nano /etc/apf/conf.apf

Step 1:

Locate EGF="0" and change to EGF="1"
Locate and set USE_AD="1"


Step 2:

Locate DEVEL_MODE and its value must be DEVEL_MODE="1"


(Once you make sure that firewall is working fine, you can set this value to "0" later)

Step 3:

Here is the most important part on setting up ports. Make sure you dont lock yourself out by misquoting the port number. If you are using cpanel make sure you open 2082 and 2087 as otherwise you will not be able to login into cpanel.

For Cpanel:

Make sure you locate the following lines and change the values there.

# Common ingress (inbound) TCP ports
IG_TCP_CPORTS=" 20,21,22,25,26,53,80,110,143,443,465,993,995,2077, 2078,2082,
2083,2086,2087,2095,2096,3306,6666"

# Common ingress (inbound) UDP ports
IG_UDP_CPORTS="21,53,465,873,2077,2078"

# Common ICMP (inbound) types
# 'internals/icmp.types' for type definition; 'all' is wildcard for any
IG_ICMP_TYPES="3,5,11,0,30,8"

# Common egress (outbound) TCP ports
EG_TCP_CPORTS="21,22,25,26,37,43,53,80,110,113,443 ,465,873,2089,3306"

# Common egress (outbound) UDP ports
EG_UDP_CPORTS="20,21,53,465,873"

# Common ICMP (outbound) types
# 'internals/icmp.types' for type definition; 'all' is wildcard for any
EG_ICMP_TYPES="all"

For Plesk:

# Common ingress (inbound) TCP ports
IG_TCP_CPORTS="20,21,22,25,53,80,110,143,443,465,9 93,995,8443"

# Common ingress (inbound) UDP ports
IG_UDP_CPORTS="37,53,873"

# Common ICMP (inbound) types
# 'internals/icmp.types' for type definition; 'all' is wildcard for any
IG_ICMP_TYPES="3,5,11,0,30,8"

# Common egress (outbound) TCP ports
EG_TCP_CPORTS="20,21,22,25,53,37,43,80,113,443,465 ,873,5224"

# Common egress (outbound) UDP ports
EG_UDP_CPORTS="53,873"

Step 5:

Finally set DEVEL_MODE='0'
APF Firewall for VPS Servers:
If you are running apf firewall in VPS, there is a possibility that you will get "eth0: Device not found". Instead you have to make settings in the configuration file.

Find the following lines and change the values like below.

IFACE_IN = "venet0"
IFACE_OUT = "venet0"
DEVEL_MODE="1"
SET_MONOKERN="1" 




SET_MONOKERN="1" 
(The above line is important to have "1" if you are in VPS server as otherwise you will get unable to load ip tables module")

Removing APF firewall
First stop the apf

/etc/init.d/apf stop

Then, flush ip tables and remove all existing apf files even in cron job

iptables -F
rm -Rf /etc/apf
rm -Rf /usr/local/sbin/apf 
rm -Rf /etc/rc.d/init.d/apf
rm -Rf /var/log/apf_log 
rm -Rf /var/log/apfados_log


/sbin/chkconfig --level 345 apf off

Now remove this cron job

nano /etc/cron.daily/fw

Find and Remove this line:
/etc/rc.d/init.d/apf restart >> /dev/null 2>&1


2. DoS-Deflate chống DDOS cho server Apache
D) DoS-Deflate là một mã nguồn mở miễn phí Unix / Linux script by MediaLayer tự động giảm nhẹ (D) DoS tấn công. Được xem là phần mềm tốt nhất, miễn phí, mã nguồn mở giải pháp để bảo vệ máy chủ chống lại hầu hết các cuộc tấn công DDoS. Dos Deflate cơ bản giám sát và theo dõi các địa chỉ IP đang gửi và thiết lập số lượng lớn các kết nối mạng TCP như gửi email tin đại chúng, DoS ping, các yêu cầu HTTP) bằng cách sử dụng "netstat" command, đó là triệu chứng của một cuộc tấn công từ chối dịch vụ. Khi phát hiện số lượng kết nối từ một nút duy nhất vượt quá giới hạn nhất định cài sẵn, kịch bản sẽ tự động sử dụng APF hoặc iptables cấm và chặn IP. Tùy thuộc vào cấu hình, các địa chỉ IP bị cấm sử dụng sẽ được unbanned APF hoặc iptables (chỉ hoạt động trên APF v 0,96 hoặc tốt hơn).

Cài đặt và thiết lập của (D) DOS-Deflate trên máy chủ là vô cùng dễ dàng. Đơn giản chỉ cần đăng nhập như là người chủ bằng cách mở truy cập SSH vào máy chủ, và chạy các lệnh sau:

wget http://www.inetbase.com/scripts/ddos/install.sh
chmod 0700 install.sh
./install.sh
Để gỡ bỏ cài đặt D () DOS-Deflate, chạy các lệnh sau:

wget http://www.inetbase.com/scripts/ddos/uninstall.ddos
chmod 0700 uninstall.ddos
./uninstall.ddos
Các tập tin cấu hình cho (D) DOS-Deflate là ddos.conf, và theo mặc định nó sẽ có giá trị sau:

FREQ=1 FREQ = 1
NO_OF_CONNECTIONS=50 NO_OF_CONNECTIONS = 50
APF_BAN=1 APF_BAN = 1
KILL=1 KILL = 1
EMAIL_TO=”root” EMAIL_TO = "root"
BAN_PERIOD=600 BAN_PERIOD = 600
Người dùng có thể thay đổi bất kỳ các thiết lập này cho phù hợp với nhu cầu khác nhau hoặc mẫu sử dụng các máy chủ khác nhau. Cũng có thể vào danh sách trắng và bỏ cấm vĩnh viễn (không bao giờ ban) danh sách các địa chỉ IP của chúng trong thư mục / usr / local / DDoS / ignore.ip.list tập tin. Nếu bạn có kế hoạch thực hiện và chạy kịch bản tương tác, người dùng có thể thiết lập KILL = 0 sao cho bất cứ IP xấu phát hiện là không cấm

3. BFD -- Brute Force Detection
BFD is a shell script which parses security logs and detects authentication failures. It is a brute force implementation without much complexity, and it works in conjunction with a APF (Advanced Policy-based Firewall).
## Get the latest source and untar.
# cd /usr/src/utils
# wget http://rfxnetworks.com/downloads/bfd-current.tar.gz
# tar xfz bfd-current.tar.gz
# cd bfd-*
# ./install.sh
Read the README file, and edit the configuration file located in /usr/local/bfd/conf.bfd.
Find ALERT="0" and replace it with ALERT="1"
Find EMAIL_USR="root" and replace it with EMAIL_USR="username@yourdomain.com"
Edit /usr/local/bfd/ignore.hosts file, and add your own trusted IPs. BFD uses APF and hence it orverrides allow_hosts.rules, so it is important that you add trusted IP addresses to prevent yourself from being locked out.
## Start the program.
#  /usr/local/sbin/bfd -s


4.RootKit -- Spyware and Junkware detection and removal tool 
Go to Rootkit Hunter homepage, and download the latest release.
## Get the latest source and untar
# cd /usr/src/utils
# wget http://downloads.rootkit.nl/rkhunter-.tar.gz
# tar xfz rkhunter-*.gz
# cd rkhunter
# ./installer.sh
## run rkhunter
# rkhunter -c

5.Setup automatic protection on System Reboot

## Edit /etc/rc.d/rc.local 
##      (or similar file depending on Linux version)
## Add the following lines at the bottom of the file

/usr/local/sbin/apf --start
/usr/local/ddos/ddos.sh -c
Note:
The SYN Floods and ICMP DDoS may also be prevented by utilizing the Linux traffic control utility (tc). To view setup instructions, please see relevant sections of Linux Advanced Routing & Traffic Control HOWTO.

Notes from the users:

Some of the users experienced following errors while starting APF.
bash# apf --start

Unable to load iptables module (ip_tables), aborting.
According to Burst and Ryan of r-fx.org, changing the SET_MONOKERN variable in /etc/apf/conf.apf to "1" will correct the problem.

1 nhận xét:

APF không hoạt động tốt lắm với CloudFlare. Mọi người có thử cài vDDoS Protection Reverse Proxy ở https://sourceforge.net/p/vddos-protection chưa? Nó chống ddos ở Layer 7 lọc gói tin DOS, DDOS, SYN Floods, HTTP Floods

Đăng nhận xét