手机版
你好,游客 登录 注册
背景:
阅读新闻

认识Linux防火墙Iptables

[日期:2009-03-16] 来源:Linux公社  作者:Linux [字体:大 中 小]

iptables是一款状态防火墙几乎集成到所有Linux发行版中了,这就意味着你可以使用它基于ip地址的规则来控制远程机器访问你的服务器,以及连接请求的类型。(旧的无状态的防火墙让你只能根据数据包的内容来做出判断,因此你被端口号限制,不能跟踪会话的存在,如FTP数据流),Debian用户可以通过apt- get install iptables conntrack来获取它。

当你从终端登陆到机器上时请完成你的初始化测试,用一个错误的规则将你自己锁在外面,然后你亲自恢复,当所有数据包被允许通过,因此这如果不被接受,观察iptables-save和iptables- restore命令,这里有一个加上注释的例子脚本,它非常基础,但是基本上它可以告诉你iptables是如何工作的:

#!/bin/bash
# example iptables script
# flush the old rules
iptables -F
# set the default policy of the chain to accept
iptables -P INPUT ACCEPT
# create a new table for logging and discarding
# unwanted packets
iptables -N LOGDROP
# use rate limiting on the logging, and
# add a prefix of ’filter: ’
iptables -A LOGDROP -m limit -j LOG
↪–log-prefix "filter: "
# drop unwanted TCP connections with a
# TCP ReSeT packet
iptables -A LOGDROP -p tcp -j REJECT
↪–reject-with tcp-reset
# drop other packets by sending an ICMP
# port unreachable in response
iptables -A LOGDROP -j REJECT
↪–reject-with icmp-port-unreachable
# now drop the packet
iptables -A LOGDROP -j DROP
#allow anything on the local interface
iptables -A INPUT -i lo -j RETURN
# allow packets that are related to
# an on-going conversation
iptables -A INPUT -p tcp -m conntrack
↪–ctstate RELATED,ESTABLISHED -j
RETURN
iptables -A INPUT -p udp -m conntrack
↪–ctstate RELATED,ESTABLISHED -j
RETURN
# allow SSH traffic
iptables -A INPUT -p tcp -m tcp
↪–dport 22 -j RETURN
# allow HTTP and HTTPS traffic
iptables -A INPUT -p tcp -m tcp
↪–dport 443 -j RETURN
iptables -A INPUT -p tcp -m tcp
↪–dport 80 -j RETURN
# accept the following ICMP types -
# echo, echo reply, source quench,
ttl exceeded,
# destination unreachable - and drop the rest
iptables -A INPUT -p icmp -m icmp
↪–icmp-type 0 -j RETURN
iptables -A INPUT -p icmp -m icmp
↪–icmp-type 3 -j RETURN
iptables -A INPUT -p icmp -m icmp
↪–icmp-type 4 -j RETURN
iptables -A INPUT -p icmp -m icmp
↪–icmp-type 8 -j RETURN
iptables -A INPUT -p icmp -m icmp
↪–icmp-type 11 -j RETURN
# if we haven’t accepted it, drop and log it.
iptables -A INPUT -j LOGDROP

如果你正在疑惑日志在哪里,象什么样子,这里有一个例子,来自系统日志/var/log/messages,一个来自192.168.50.40的UDP数据包,源端口30766,目标服务器是192.168.0.8,目标端口是1026,这个数据包被丢掉了,它可能是windows机器上了信使服务发出的垃圾信息:

Nov 22 06:24:00 localhost kernel: filter: IN=eth0
OUT=MAC=00:0a:e6:4e:6d:49:00:14:6c:67:cc:9a:08:00
SRC=192.168.50.40 DST=192.168.0.8 LEN=402
TOS=0×00 PREC=0×00 TTL=47 ID=3345 PROTO=UDP
SPT=30766 DPT=1026 LEN=382

日志输出看起来有点恐怖,但是你能容易地拣出你想要的信息,首先,你看到的是日期和主机名,IN和OUT描述了数据包是来自哪个端口和通过哪个端口出去的,MAC给出了源和目标MAC地址,SRC和DST是源和目标ip地址,PROTO是UDP,TCP,ICMP等等,SPT和DPT是源和目标端口号,例如:大部分到服务的连接如ssh都有一个临时的源端口,如35214,和一个目标端口22,那么象前面说道的windows机器的信使服务数据包,你可以安全地忽略它。

当你安装号防火墙后,务必再从另外一台机器上运行一次nmap,来检查是否只打开了正确的端口。

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款