发新话题
打印

[安全技巧] 简易防火墙脚本的编写

简易防火墙脚本的编写

简易防火墙脚本的编写
由于对iptables还是最近才学习,可能存在很多问题,如果有什么不对的地方的话,请大家多多指教!
简易防火墙脚本的编写
纲要:
1.        关于Linux防火墙
2.        Iptables简介及应用基本规则
3.        简易防火墙脚本编写实战
4.        其它相关
一,关于Linux防火墙
当今世界黑客攻击的踪影无处不在,经常关注安全信息的朋友就知道,经常有一些大型的站点被黑客攻击,这是因为服务器一般是运行在安全性不高的Internet环境下,这就需要为服务器提供可靠的安全保证,Linux操作系统本身就技持包过滤规则,从2.4的内核开始,开始通常使用命令iptables,经过简单的配置就能配置成功能强大的防火墙.

二,Iptables的简介及基本应用规则
1.        相关术语简介
链:链是iptables中规则的列表,系统默认有三个链,INPUT(输入),OUTPUT(输出),FORWARD(转发),系统允行用户自定议链。
规则:规则是组成链的基本单位,-A(追加),-D(删除),-R(替换),I(插入)一条规则,-L有来显示当前所有规则。
协议:协议是计算机和计算机之间通信过程中遵循的方法和标准,包括(tcp,upd,icmp)
目标:目标(TARGETS),是对数据包进行处理的方法,这些操作通常包括,ACCEPT(通过),DROP(删除),REJECT(拒绝),QUEUE(队列),RETURN(返回)。
2.Iptables启动相关命令简介
# chkconfig ipchains off
# chkconfig iptables on
要启动iptables,必须先停止ipchains,现在重新启动系统,可以停止或卸载ipchains模块。
# /etc/init.d/ipchains stop
# modprobe –r ipchains
如果在做NAT或IP伪装时,那就要打开IP包转发,。
#vi /etc/sysct1.conf
net.ipv4.ip_forward=1
重新启动网络接口,以使IP包转发生效
/etc/init.d/network restart
/etc/init.d/iptables start
用lsmod命令查看iptalbes所用的模块是否已经被加载。
#lsmod | grep ip

三,简易防火墙脚本编写实战
要求:
1.        WWW 服务(web服务器)
2.        DNS 服务
3.        邮件服务(SMTP,POP3)
4.        FTP服务,且允许被动连接。
5.        允许从远程主机接入MSSQL服务
6.        TELNET服务,如安装telnet BBS (FireBird)
7.        指定只有192.168.0.1这个主机能够使用SSH服务登陆Linux主机
8.        禁止其它主机ping本机,但允许本机ping其它外部主机.
9.        信任回环lo
10.丢失所有不匹配的数据包


具体操作:
#vi firewall  在里面添加如下内容,
#! /bin/bash
# Establish Linux Easy FireWall,Edit by NetSeek@2005
#set default PATH
export PATH=/sbin:/usr/sbin:/local/usr/sbin:/usr/bin:/bin

# Clean old iptables
iptables -F
iptables -X
iptables -Z

# www(http)
# allow all http/https incoming/return connections
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# DNS
#  NameServer and allow UDP packets in for DNS client from nameservers
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -m state ! --state NEW -j ACCEPT

# Mail
# SMTP
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --sport 25 ! --syn -j ACCEPT
# POP3
iptables -A INPUT -p tcp --dport 110 -j ACCEPT
iptables -A INPUT -p tcp --sport 110 ! --syn -j ACCEPT

# FTP
# allow all ftpd incoming connections
iptables -A INPUT -p tcp --dport 5020 -j ACCEPT
iptables -A INPUT -p tcp --dport 5021 -j ACCEPT
# Enable passive ftp transfers
iptables -A INPUT -p tcp --destination-port 10000:10010 -j ACCPET

# MsSQL
iptables -A INPUT -p tcp --sport 1433 ! --syn -j ACCEPT

# Telnet
# Allow all telnet incoming connections
iptables -A INPUT -s 192.168.0.1 -p tcp --dport 23 -j ACCEPT

# SSH
# Only allow sshd incoming connections from 192.168.0.1
iptables -A INPUT -s 192.168.0.1 -p tcp --dport 22 -j ACCEPT

# Allow ICMP in if it is related to other connections
iptables -A INPUT -p icmp -m state ! --state NEW -j ACCEPT

# credit circle
iptables -A INPUT -i lo -j ACCEPT

# Drop not matching data
iptables -A INPUT -p all -j DROP

保存退出.
# chmod 755 firewall
# mv firewall /usr/sbin/firewall

让其随着系统开机启动
#vi /etc/rc.d/rc.local ,添加如下内容
/usr/sbin/firewall

四,其它相关
由于iptables是一个功能相当强大,且相当复杂的东西,要了解更多的信息就用”man iptables”查看相关命令的用法,由于本人水平有限,学习也需要一个过程,本人只是针对前面所介绍的文章,进行简单的配置,下面给大家介绍一些比较好的自动生成墙脚本的站点,以有利有大家更加深入的学习。
以下资料转自情长在线(感谢火焰兄弟转来的精彩资料)
在线生成firewall脚本(iptables)--向导模式
Bifrost - GUI firewall management interface to iptables
http://bifrost.heimdalls.com

LinWiz - Linux configuration file and scripting Wizards
http://www.lowth.com/LinWiz/

GIPTables Firewall - IPTABLES Rules Generator
http://www.giptables.org/

Easy Firewall Generator for IPTables(在线生成复杂的防火墙脚本*****)
http://morizot.net/firewall/gen/

PFG for IPTables 在线生成简单的防火墙脚本***
http://www.thegate.nu/pfg/

Firewall Builder - GUI Firewall Frontend(功能强大的防火墙构建工具*****)
http://www.fwbuilder.org/index.html

______________________________________________________
Dnsmasq - caching DNS forwarder
http://thekelleys.org.uk/dnsmasq/doc.html

FireHOL, the iptables stateful packet filtering firewall builder
http://firehol.sourceforge.net/

BullDog - A comprehensive and progressive firewall
http://tanaya.net/BullDog/
WallFire: wflogs - firewall log analysis tool
http://www.wallfire.org/wflogs/

Ulog-php - a php analyser for netfilter U-log
http://home.regit.org/ulogd-php.html

Firewall Tester
http://ftester.sourceforge.net/

YAFT's Another Firewall Tool
http://sourceforge.net/projects/yaft

Turtle Firewall Project
http://turtlefirewall.sourceforge.net/

TuxFrw - Firewall Automation Tool
http://tuxfrw.sourceforge.net/index.html

Shoreline Firewall
http://www.shorewall.net/
______________________________________
perl firewall在线生成
levy - Perl Firewall Generater
http://muse.linuxmafia.org/levy/
____________________________________
gSshield - BASH Shell Script Configurator
http://muse.linuxmafia.org/gshield.html
_________________________________
流量分析
Mason - Builds from system traffic
http://www.stearns.org/mason/
________________________________________
firewall log分析软件
adcfw-log - firewall logs analyzer/summarizer
http://adcfw-log.sourceforge.net/

IPTables log analyzer
http://www.gege.org/iptables/
_______________________

IPMENU - Curses Firewall Frontend
http://users.pandora.be/stes/ipmenu.html
________________________________
Firelogd - Firewall Log Daemon
http://www.speakeasy.org/~roux/dmn/
firewall log分析软件主页(fireparse)
Fireparse - Firewall Log Parser
http://aaron.marasco.com/linux.html
__________________________________________
SAINT - Assess the Security of Computer Networks
http://www.saintcorporation.com/saint/
saint在linux著名的扫描软件下面那个是下载地址
SATAN - Port Scanner with a Web Interface
http://www.ibiblio.org/pub/packages...atan-for-Linux/
_____________________________________
Abacus - Intrusion Prevention System
http://www.psionic.com/abacus/

Firewall Generator(在线生成简单的防火墙脚本***)
http://www.citadec.com/FirewallGenerator.html

[ 本帖最后由 NetSeek 于 2006-11-28 14:55 编辑 ]
积极!努力!低调!百折不挠!IT运维专家网[LinuxTone]
分享才能进步!帮助他人就是在帮助自己! 共同学习探讨Linux!
  Linux= howto+man ! ^_^

TOP

TOP

脚本有很多问题,NetSeek 应该好好研究一下 state 模块的 ESTABLISHED 和 RELATED 状态,以及 ip_conntrack_ftp 模块的使用,多看些例子
置顶的 PDF 文档里就有详细的介绍

btw:那个贴子还是你移动处理过的咧
一个人活得精彩
封存于 0612141938(133)

TOP

還是8错的

TOP

很不错哦
好好学习

TOP

不错,学习了
love linux, love life ,love travelling

TOP

发新话题