網路伺服器應用 linux server
DESCRIPTION
網路伺服器應用 Linux Server. Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology [email protected] http://www.cse.dlit.edu.tw/~andres. Chapter 11 簡易 Firewall 架設. 11.1 Firewall( 防火牆 ). Proxy IP Filter 2.0.xx: ipfwadm 2.2.xx : ipchains - PowerPoint PPT PresentationTRANSCRIPT
網路伺服器應用Linux Server
Andres, Wen-Yuan Liao
Department of Computer Science and EngineeringDe Lin Institute of Technology
[email protected]://www.cse.dlit.edu.tw/~andres
Chapter 11 簡易 Firewall 架設
11.1 Firewall( 防火牆 )
Proxy IP Filter
– 2.0.xx: ipfwadm– 2.2.xx : ipchains– 2.4.xx : iptables
TCP Wrappers
抵擋的方法
拒絕讓封包進入主機的某些 port 拒絕讓某些來源 IP 的封包進入 拒絕讓帶有某些特殊旗標 ( flag ) 的封包進入
– SYN 的主動連線的旗標 防火牆的規則
– 一行一行依序來檢查的,若符合任何一條規則,則予以動作 ( 接受或丟棄 ) ,否則繼續往下檢查到最後一條
11.2 Linux Firewall
Iptables module– lsmod– rmmod ipchains– modprobe ip_tables– /sbin/iptables
tables
至少有兩個 table– Filter table
預設 管理主機的安全
– nat table 處理 NAT 的功能
清除規則
/sbin/iptables [-FXZ] -F :清除所有的已訂定的規則 -X :殺掉所有使用者建立的 tables-Z :將所有 tables 的計數與流量統計歸零
在遠端連線時,這三個指令必須要用 scripts 來連續執行
example : /sbin/iptables -F /sbin/iptables -X /sbin/iptables -Z
定義政策 ( Policy )
/sbin/iptables [-t tables] [-P] [INPUT,OUTPUT,FORWARD| PREROUTING,OUTPUT,POSTROUTING] [ACCEPT,DROP] 參數說明: -t :定義 table tables : table 的名稱,例如 nat -P :定義政策 ( Policy ) INPUT :封包為輸入主機的方向 OUTPUT :封包為輸出主機的方向 FORWARD :封包為不進入主機而向外再傳輸出去的方向 PREROUTING :在進入路由之前進行的工作 OUTPUT :封包為輸出主機的方向 POSTROUTING :在進入路由之後進行的工作
Example
/sbin/iptables -P INPUT ACCEPT /sbin/iptables -P OUTPUT ACCEPT /sbin/iptables -P FORWARD ACCEPT /sbin/iptables -t nat -P PREROUTING ACCEPT /sbin/iptables -t nat -P OUTPUT ACCEPT /sbin/iptables -t nat -P POSTROUTING ACCEPT
增加、插入規則 /sbin/iptables [-AI] [INPUT,OUTPUT,FORWARD] [-i
o interface] [-p TCP,UDP] [-s IP/network] [--sport ports] [-d IP/network] [--dport ports] -j [ACCEPT,DROP]
-A :新增加一條規則在最後面一行 -I :在第一條規則加入 INPUT :封包為輸入主機的方向 OUTPUT :封包為輸出主機的方向 FORWARD :封包為不進入主機而向外再傳輸出去的方向
增加、插入規則 -i :流入的網卡介面
-o :流出的網卡介面 interface :網路卡介面,例如 ppp0, eth0, eth1.... -p :封包的協定TCP :封包為 TCP 協定的封包; UDP :封包為 UDP 協定的封包; -s :來源封包的 IP 或者是 Network ( 網域 ) ; --sport :來源封包的 port 號碼; -d :目標主機的 IP 或者是 Network ( 網域 ) ; --dport :目標主機的 port 號碼; -j :動作,可以接底下的動作; ACCEPT :接受該封包 DROP :丟棄封包
Example
/sbin/iptables -A INPUT -i lo -j ACCEPT – 所有的來自 lo 這個介面的封包,都予以接受
/sbin/iptables -A INPUT -i eth0 -p TCP -s 192.168.0.1 -j ACCEPT – 來自 192.168.0.1 這個 IP 的封包都予以接受
/sbin/iptables -A INPUT -i eth0 -p TCP -s 192.168.1.0/24 -j ACCEPT – 來自 192.168.1.0 這個 C Class 的網域的任何一部電腦,就予以接受
/sbin/iptables -A INPUT -i eth0 -p TCP -s 192.168.1.25 -j DROP – 來自 192.168.1.25 的 IP 的封包,就直接全部給他丟棄
/sbin/iptables -A INPUT -i eth0 -p TCP --dport 21 -j DROP – 只要想要進來 21 這個 port 的封包,就把他丟棄
/sbin/iptables -A INPUT -i eth0 -p TCP -s 192.168.0.24 --dport 22 -j ACCEPT
– 來自 192.168.0.24 的主機,想要到我的 port 22 時,就予以接受
規則模式– 關閉所有的,開放特定的
政策上 ( Policy )– 先選擇 ACCEPT – 然後在最後一行才以 NEW, INVALID 的狀態來關
閉所有的服務之 port
11.3 Firewall example
/usr/local/virus/iptables/– iptables.rule
設定規則的檔案,包括清除防火牆規則、載入模組、設定一些服務的登入與否等等
– iptables.deny – iptables.allow
iptables.rule
EXTIF="ppp0" INIF="eth0" INNET="192.168.1.0/24
1. Testing your Kernel version and remove the ipchains module
kver=`uname -r | cut -c 1-3` if [ "$kver" != "2.4" ] && [ "$kver" != "2.5" ]; then echo "Your Linux Kernel Version may not be suported by this script!" echo "This scripts will not be runing" exit fi
ipchains=`lsmod | grep ipchains` if [ "$ipchains" != "" ]; then echo "unload ipchains in your system" rmmod ipchains 2> /dev/null fi
2. Loading some modules
PATH=/sbin:/bin:/usr/sbin:/usr/bin export PATH EXTIF INIF INNET modprobe ip_tables > /dev/null 2>&1 modprobe iptable_nat > /dev/null 2>&1 modprobe ip_nat_ftp > /dev/null 2>&1 modprobe ip_conntrack > /dev/null 2>&1 modprobe ip_conntrack_ftp > /dev/null 2>&1 modprobe ip_conntrack_irc > /dev/null 2>&1
3. Clear the iptables's rules
/sbin/iptables -F /sbin/iptables -X /sbin/iptables -Z /sbin/iptables -F -t nat /sbin/iptables -X -t nat /sbin/iptables -Z -t nat /sbin/iptables -P INPUT ACCEPT /sbin/iptables -P OUTPUT ACCEPT /sbin/iptables -P FORWARD ACCEPT /sbin/iptables -t nat -P PREROUTING ACCEPT /sbin/iptables -t nat -P POSTROUTING ACCEPT /sbin/iptables -t nat -P OUTPUT ACCEPT
4. Allow the lo and inside interface to access the server and internet
/sbin/iptables -A INPUT -i lo -j ACCEPT
if [ "$INIF" != "" ]; then
/sbin/iptables -A INPUT -i $INIF -j ACCEPT
echo "1" > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -t nat -A POSTROUTING -s $INNET -o
$EXTIF -j MASQUERADE
fi
5. Deny and Allow IP or host or network from files
if [ -f /usr/local/virus/iptables/iptables.deny ]; then /usr/local/virus/iptables/iptables.deny fi if [ -f /usr/local/virus/iptables/iptables.allow ]; then /usr/local/virus/iptables/iptables.allow fi
6. Allow ICMP come in
AICMP="0 3 3/4 4 11 12 14 16 18" for tyicmp in $AICMP do /sbin/iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT done
7. Allow services
/sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 22 -j ACCEPT /sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 25 -j ACCEPT /sbin/iptables -A INPUT -p UDP -i $EXTIF --dport 53 -j ACCEPT /sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 53 -j ACCEPT /sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 80 -j ACCEPT /sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 110 -j ACCEPT
8. Allow RELATED and ESTABLISHED
/sbin/iptables -A INPUT -m state --state ESTABLISHED,
RELATED -j ACCEPT
9. deny all /sbin/iptables -A INPUT -m state --state NEW,INVALID
-j DROP
iptables.allow
/sbin/iptables -A INPUT -i $EXTIF -s 140.116.44.0/24 -j ACCEPT
iptables.deny
/sbin/iptables -A INPUT -i $EXTIF -s 140.115.236.8 -j DROP /sbin/iptables -A INPUT -i $EXTIF -s 140.120.13.237 -j DROP
/etc/rc.d/rc.local
/usr/local/virus/iptables/iptables.rule
11.4 TCP_Wrappers
/usr/sbin/tcpd – 進行 TCP 的檢驗工作
檢驗流程設定檔– /etc/hosts.allow– /etc/hosts.deny– 一設定完畢立刻就生效
< 服務名稱 > : <IP/network> : <action>
network 可使用 192.168.0.0/255.255.255.0 network 不可使用 192.168.0.0/24 /etc/hosts.allow
in.telnetd: 127.0.0.1 in.ftpd: 127.0.0.1
/etc/hosts.deny in.telnetd: 192.168.2.3
Example
/etc/hosts.allow in.telnetd: 192.168.1.2, 192.168.1.10, 192.168.1.20in.ftpd: 192.168.1.2, 192.168.1.10, 102.168.1.20sshd: 192.168.1.0/255.255.255.0, xxx.yyy.zzz.qqq
/etc/hosts.deny in.telnetd: ALL in.ftpd: ALL sshd: ALL