防火墙脚本设计

该博客介绍了实现案例的步骤,包括编写典型的Linux网关防火墙脚本,如编写脚本文件/opt/ipfw-gw.sh并设置开机自动运行,其网络型脚本以filter表的FORWRD链为主,还提及使用IP地址黑/白名单,定义白名单可让特定地址数据包无条件放行。

 步骤

实现此案例需要按照如下步骤进行。
步骤一:编写典型的Linux网关防火墙脚本

1)编写脚本文件/opt/ipfw-gw.sh

[root@gw1 ~]# vim /opt/ipfw-gw.sh
#!/bin/bash
## 2015.05.20 TsengYia.
#### 1. 定义方便移植的环境变量 ####
INET_IF="eth1"
INET_IP="174.16.16.1"
LAN_NET="192.168.4.0/24"
LAN_WWW_IP="192.168.4.5"
IPT="/sbin/iptables"
#### 2. 内核参数、相关模块调整 ####
/sbin/modprobe nf_nat_ftp
/sbin/sysctl -w net.ipv4.ip_forward=1
/sbin/sysctl -w net.ipv4.ip_default_ttl=128
/sbin/sysctl -w net.ipv4.icmp_echo_ignore_all=1
/sbin/sysctl -w net.ipv4.icmp_echo_ignore_broadcasts
/sbin/sysctl -w net.ipv4.tcp_syncookies=1
/sbin/sysctl -w net.ipv4.tcp_syn_retries=3
/sbin/sysctl -w net.ipv4.tcp_synack_retries=3
/sbin/sysctl -w net.ipv4.tcp_fin_timeout=60
/sbin/sysctl -w net.ipv4.tcp_max_syn_backlog=3200
#### 3. 清空旧规则,并设置各链的默认规则 ####
#/etc/init.d/iptables stop
$IPT -t filter -X
$IPT -t nat -X
$IPT -t mangle -X
$IPT -t raw -X
$IPT -t filter -F
$IPT -t nat -F
$IPT -t mangle -F
$IPT -t raw -F
$IPT -P INPUT DROP
$IPT -P FORWARD DROP
$IPT -P OUTPUT ACCEPT
#### 4. 自定义规则 .. ####
#### 4.1 nat表的详细策略
$IPT -t nat -A POSTROUTING -s $LAN_NET -o $INET_IF -j SNAT --to-source $INET_IP
$IPT -t nat -A PREROUTING -i $INET_IF -d $INET_IP -p tcp --dport 80 -j DNAT --t
o-destination $LAN_WWW_IP
#### 4.2 filter表的详细策略
$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A INPUT -p tcp --dport 22 -j ACCEPT
$IPT -A FORWARD -s $LAN_NET -o $INET_IF -p udp --dport 53 -j ACCEPT
$IPT -A FORWARD -s $LAN_NET -o $INET_IF -p tcp -m multiport --dport 20:22,25,80
,110,143,443,993,995 -j ACCEPT
$IPT -A FORWARD -d $LAN_NET -i $INET_IF -m state --state ESTABLISHED,RELATED -j
 ACCEPT
$IPT -A FORWARD -d $LAN_WWW_IP -p tcp --dport 80 -j ACCEPT
$IPT -A FORWARD -s $LAN_WWW_IP -p tcp --sport 80 -j ACCEPT

[root@gw1 ~]# chmod  +x /opt/ipfw-gw.sh

2)根据需要将ipfw-gw.sh脚本设置为开机自动运行

[root@gw1 ~]# vim /etc/rc.local 
#!/bin/sh
.. ..
touch /var/lock/subsys/local
/opt/ipfw-gw.sh
步骤二:编写网络型、主机型防护规则
1)主机型脚本
控制的数据包侧重于本机与其他主机之间的访问,因此iptables防火墙规则以 filter 表的 INPUT 链为主,OUTPUT 链其次。
比如为网站服务器svr5编写防火墙脚本:
[root@svr5 ~]# vim /opt/ipfw-host.sh
#!/bin/bash
## 2015.05.20 TsengYia.
#### 1. 定义方便移植的环境变量 ####
INET_IF="eth0"
INET_IP="192.168.4.5"
IPT="/sbin/iptables"
#### 2. 内核参数、相关模块调整 ####
/sbin/sysctl -w net.ipv4.ip_forward=0
/sbin/sysctl -w net.ipv4.ip_default_ttl=128
/sbin/sysctl -w net.ipv4.icmp_echo_ignore_all=1
/sbin/sysctl -w net.ipv4.icmp_echo_ignore_broadcasts
/sbin/sysctl -w net.ipv4.tcp_syncookies=1
/sbin/sysctl -w net.ipv4.tcp_syn_retries=3
/sbin/sysctl -w net.ipv4.tcp_synack_retries=3
/sbin/sysctl -w net.ipv4.tcp_fin_timeout=60
/sbin/sysctl -w net.ipv4.tcp_max_syn_backlog=3200
#### 3. 清空旧规则,并设置各链的默认规则 ####
#/etc/init.d/iptables stop
$IPT -t filter -X
$IPT -t nat -X
$IPT -t mangle -X
$IPT -t raw -X
$IPT -t filter -F
$IPT -t nat -F
$IPT -t mangle -F
$IPT -t raw -F
$IPT -P INPUT DROP
$IPT -P FORWARD DROP
$IPT -P OUTPUT ACCEPT
#### 4. 自定义规则 .. ####
$IPT -A INPUT -p tcp -m multiport --dport 22,25,80,110,143,443,993,995,2150:2750 -j ACCEPT
$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

[root@svr5 ~]# chmod +x /opt/ipfw-host.sh 				//添加执行权限
[root@svr5 ~]# vim /etc/rc.local  						//设置开机自运行
#!/bin/sh
.. ..
touch /var/lock/subsys/local
/opt/ipfw-host.sh

2)网络型脚本
控制的数据包侧重于内网、外网之间的访问,因此iptables防火墙规则以 filter 表的 FORWRD 链为主,需要地址转换时还会用到nat表。
比如步骤一中的网关防火墙脚本ipfw-gw.sh

[root@gw1 ~]# cat /opt/ipfw-gw.sh
.. ..
$IPT  -t nat  -A  POSTROUTING  -s  $LAN_NET  -o  $INET_IF  -j  SNAT  --to-source  $INET_IP
$IPT  -t nat  -A  PREROUTING  -i  $INET_IF  -d  $INET_IP  -p tcp  --dport  80  -j DNAT  --to-destination  $LAN_WWW_IP
.. ..
$IPT  -A  FORWARD  -d  $LAN_NET  -i  $INET_IF  -m state  --state  ESTABLISHED,RELATED   -j  ACCEPT
.. ..

步骤三:使用IP地址黑/白名单
1)编写IP地址黑名单、白名单
定义白名单,来自这些地址(比如远程管理机)的数据包将会无条件放行:

[root@gw1 ~]# vim /opt/ipfw.wlist
## the SSH-Station for administrators
192.168.4.110
220.121.72.85
定义黑名单,来自这些地址的数据包将会无条件丢弃:
[root@gw1 ~]# cat  /opt/ipfw.blist
61.45.135.29
121.113.79.81
2)修改ipfw-gw.sh网关防火墙脚本,启用黑、白名单
[root@gw1 ~]# vim /opt/ipfw-gw.sh
.. ..

#### 5. White & Black List .. ####
WHITE_LIST="/opt/ipfw.wlist"
for  i  in  $(grep -v "^#" $WHITE_LIST)  			//遍历设置白名单规则
do
    $IPT  -I  INPUT  -s $i  -j ACCEPT
    $IPT  -I  OUTPUT  -d $i  -j ACCEPT
    $IPT  -I  FORWARD  -s $i  -j ACCEPT
    $IPT  -I  FORWARD  -d $i  -j ACCEPT
done
BLACK_LIST="/opt/ipfw.blist"  
for  i  in  $(grep -v "^#" $BLACK_LIST)  			//遍历设置黑名单规则
do
    $IPT  -I  INPUT  -s $i  -j DROP
    $IPT  -I  OUTPUT  -d $i  -j DROP
    $IPT  -I  FORWARD  -s $i  -j DROP
    $IPT  -I  FORWARD  -d $i  -j DROP
done
3)执行ipfw-gw.sh脚本,确认防火墙规则
[root@gw1 ~]# iptables -nL
Chain INPUT (policy DROP)
target     prot opt source               destination         
DROP       all  --  218.29.30.131        0.0.0.0/0           
DROP       all  --  121.113.79.81        0.0.0.0/0           
DROP       all  --  61.45.135.29         0.0.0.0/0           
ACCEPT     all  --  220.121.72.85        0.0.0.0/0           
ACCEPT     all  --  192.168.4.110        0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 

Chain FORWARD (policy DROP)
target     prot opt source               destination         
DROP       all  --  0.0.0.0/0            218.29.30.131       
DROP       all  --  218.29.30.131        0.0.0.0/0           
DROP       all  --  0.0.0.0/0            121.113.79.81       
DROP       all  --  121.113.79.81        0.0.0.0/0           
DROP       all  --  0.0.0.0/0            61.45.135.29        
DROP       all  --  61.45.135.29         0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            220.121.72.85       
ACCEPT     all  --  220.121.72.85        0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            192.168.4.110       
ACCEPT     all  --  192.168.4.110        0.0.0.0/0           
ACCEPT     udp  --  192.168.4.0/24       0.0.0.0/0           udp dpt:53 
ACCEPT     tcp  --  192.168.4.0/24       0.0.0.0/0           multiport dports 20:22,25,80,110,143,443,993,995 
ACCEPT     all  --  0.0.0.0/0            192.168.4.0/24      state RELATED,ESTABLISHED 
ACCEPT     tcp  --  0.0.0.0/0            192.168.4.5         tcp dpt:80 
ACCEPT     tcp  --  192.168.4.5          0.0.0.0/0           tcp spt:80 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       all  --  0.0.0.0/0            218.29.30.131       
DROP       all  --  0.0.0.0/0            121.113.79.81       
DROP       all  --  0.0.0.0/0            61.45.135.29        
ACCEPT     all  --  0.0.0.0/0            220.121.72.85       
ACCEPT     all  --  0.0.0.0/0            192.168.4.110


// fire.cpp : Defines the class behaviors for the application. // #include "StarWarsCtrl.h" // Added by ClassView #include "stdafx.h" #include "fire.h" #include "MainFrm.h" #include "fireDoc.h" #include "fireView.h" #include <afxsock.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CFireApp BEGIN_MESSAGE_MAP(CFireApp, CWinApp) //{{AFX_MSG_MAP(CFireApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP // Standard file based document commands ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CFireApp construction CFireApp::CFireApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CFireApp object CFireApp theApp; ///////////////////////////////////////////////////////////////////////////// // CFireApp initialization BOOL CFireApp::InitInstance() { // CG: The following block was added by the Windows Sockets component. { if (!AfxSocketInit()) { AfxMessageBox(CG_IDS_SOCKETS_INIT_FAILED); return FALSE; } } AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CFireDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CFireView)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; // The one and only window has been initialized, so show and update it. HICON hIcon = ::LoadIcon (AfxGetResourceHandle (), MAKEINTRESOURCE(IDI_MAINFRAME)); m_pMainWnd->SetIcon(hIcon,FALSE); m_pMainWnd->SetWindowText("NetDefender"); m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; CStarWarsCtrl m_StarWarsCtrl; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) // No message handlers //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) // DDX_Control(pDX, IDC_STARWARS, m_StarWarsCtrl); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() // App command to run the dialog void CFireApp::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); } ///////////////////////////////////////////////////////////////////////////// // CFireApp message handlers
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值