-
TCP Wrappers
TCP Wrappers 是通过两个文件来管理的一个类似防火墙的机制
/etc/hosts.allow
/etc/hosts.deny
并非所有的软件都可以通过这两个文件来管理,只有下面这两个软件才可以通过这两个文件来管理防火墙规则:
有 super daemon(xinetd) 所管理的服务
支持 libwrap.so 模块的服务# 查出系统有没有安装xinetd ,若没有请安装,安装完毕后,查询 xinitd 管理的服务有哪些。 [root@CentOS ~]# yum install xinetd [root@CentOS ~]# chkconfig xinetd on [root@CentOS ~]# chkconfig --list chargen-stream: off daytime-dgram: off daytime-stream: off discard-dgram: off discard-stream: off echo-dgram: off echo-stream: off rsync: off tcpmux-server: off time-dgram: off #上述都可以由 TCP Wrappers 来简易设置防火墙规则 # 请问,rsyslogd、sshd、xinetd、httpd(若服务不存在,请自行安装),这四个程序有没有支持 TCP Wrapers 的阻挡功能 [root@CentOS ~]# ldd $(which rsyslogd sshd inetd httpd) #输出结果比较复杂,需要自己来看 [root@CentOS ~]# for name in rsyslogd sshd xinetd httpd; do echo $name; ldd $(which $name) | grep libwrap; done rsyslogd sshd libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f6204e8b000) xinetd libwrap.so.0 => /lib64/libwrap.so.0 (0x00007fc8102cf000) httpd #如果出现 libwrap 文件名的话,代表找到了该函数库,并支持 TCP Wrappers。所以,sshd、xinetd 支持,但是rsyslogd、httpd在这两个程序不支持。也就是说,httpd与rsyslogd 不能够使用 /etc/hosts/{allow|dent} 来进行防火墙机制的控制
-
/etc/hosts.{allow|dent} 的设置方式
#语法 <service(program_name)> : <IP, domain, hostname> <服务 (也就是程序名称)> : (IP 或 域 或主机名> # 上面的><符号不存在与配置文件中
防火墙规则的顺序:
1 . 先以 /etc/hosts.allow 进行优化比对,该规则符合就给予以放行
2 . 再以 /etc/hosts.deny 比对,规则符合就予以抵挡
3 . 若不在这两个文件内,亦即规则都不符合,最终则予以放行# 先放开本机的 127.0.0.1 可以进行任何本机的服务,然后,让局域网(192.168.1.0/24) 可以使用rsysc,同事10.0.0.100 页能够使用rsysc,但其他来源不允许使用rsysc。 [root@CentOS ~]# cat /etc/xinetd.d/rsync # default: off # description: The rsync server is a good addition to an ftp server, as it \ # allows crc checksumming etc. service rsync { disable = yes flags = IPv6 socket_type = stream wait = no user = root server = /usr/bin/rsync #rsysc 的服务启动的文件名。文件名为rsysc server_args = --daemon log_on_failure += USERID } # 因为 rsysc 的服务启动的文件名为rsysc。所以应该这样设置: [root@CentOS ~]# vim /etc/hosts.allow ALL: 127.0.0.1 rsysc: 192.168.1.0/255.255.255.0 10.0.0.100 #不支持192.168.1.0/24这种写法 [root@CentOS ~]# vim /etc/hosts.deny rsysc: ALL
-
iptables:linux 的数据包过滤软件
根据数据包的分析资料“对比”预先定义的规则内容,若数据包数据与规则内容相同则进行动作,否则就继续下一条规则的比对
如果规则都不符合,则执行默认策略
默认情况下。linux的iptables至少就有3个表格,包括管理本机进出的Filter、管理后端主机(防火墙内部的其他计算机)的NAT、管理特殊标志使用的,angle(较少使用)。- Filter(过滤器):主要跟进入Linux本机的数据包有关,是默认的 table
INPUT:主要与想要进入linux本机的数据包有关
OUTPUT:主要与linux本机发炒股的数据包有关
FORWARF:与linux本机没有关系,与NAT的 table 的相关性较高 - NAT(地址转换):主要用来进行来源于目的地的IP或port的转换,与linux本机无关,主要与linux主机后的局域网内计算机相关
PREROUTING:在进行路由判断之前所要进行的规则(DNAT/REDIRECT)
POSTROUTING:在进行路由判断之前所要进行的规则(SNAT/MASQUERADE)
OUTPUT:与发送出去的数据包有关 - Mangle(破坏者):主要是与特殊的数据包烦人路由标志有关。
- Filter(过滤器):主要跟进入Linux本机的数据包有关,是默认的 table
-
规则的查看与清除
1 . 查看规则
[root@CentOS ~]# iptables [-t tables] [-L] [-nv] -L:列出目前的规则 -n:不进行 IP 与 HOSTNAME 的反查,显示信息的速度会快很多 -v:列出更多信息
# 列出 filter table 的规则 [root@CentOS ~]# iptables -L -n Chain INPUT (policy ACCEPT) #INPUT链 target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) #FORWARD链 target prot opt source destination REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) #OUTPUT链 target prot opt source destination # 列出 nat table 的规则 [root@CentOS ~]# iptables -t nat -L -n Chain PREROUTING (policy ACCEPT) target prot opt source destination Chain POSTROUTING (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
target:代表进行的操作
prot:代表使用的数据包协议
opt:额外的说明
source:代表此规则是针对哪个来源IP进行限制
destination:代表此规则是针对哪个目标IP进行限制# 查看完整的防火墙规则 [root@CentOS ~]# iptables-save [-t table] [root@CentOS ~]# iptables-save # Generated by iptables-save v1.4.7 on Wed Apr 5 08:30:35 2017 *nat :PREROUTING ACCEPT [17:1637] :POSTROUTING ACCEPT [1:60] :OUTPUT ACCEPT [1:60] COMMIT # Completed on Wed Apr 5 08:30:35 2017 # Generated by iptables-save v1.4.7 on Wed Apr 5 08:30:35 2017 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [1909:187403] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT # Completed on Wed Apr 5 08:30:35 2017
2 . 清除规则
[root@CentOS ~]# iptables [-t tables] [-FXZ] -F:清除所有的已制订的规则 -X:除掉所有用户自动以的链 -Z:将所有的链的计数与流量统计都归零
# 清除本机防火墙的所有规则 [root@CentOS ~]# iptables -F [root@CentOS ~]# iptables -X [root@CentOS ~]# iptables -Z # 会清除所有规则,但不会改变默认策略
-
定义默认策略
当数据包不在规则之内时,则该数据包的通过与否,是以默认策略为准[root@CentOS ~]# iptables [-t nat] [INPUT,OUTPUT,FORWARD] [ACCEPT,DROP] -P:定义默认策略 ACCEPT:数据包可接受 DROP:该数据包直接丢弃 # 将本机的 INPUT 设置为 DROP, 其他设置为 ACCEPT [root@CentOS Desktop]# iptables -P INPUT DROP [root@CentOS Desktop]# iptables -P OUTPUT ACCEPT [root@CentOS Desktop]# iptables -P FORWARD ACCEPT [root@CentOS Desktop]# iptables-save # Generated by iptables-save v1.4.7 on Wed Apr 5 08:54:50 2017 *nat :PREROUTING ACCEPT [70:6483] :POSTROUTING ACCEPT [4:682] :OUTPUT ACCEPT [4:682] COMMIT # Completed on Wed Apr 5 08:54:50 2017 # Generated by iptables-save v1.4.7 on Wed Apr 5 08:54:50 2017 *filter :INPUT DROP [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] COMMIT # Completed on Wed Apr 5 08:54:50 2017
-
数据包的基础对比:IP、网络及接口设备
[root@CentOS ~]# iptables [AI 链名] [-io 网络接口] [-p 协议] [-s 来源 IP/网络] [-d 目标IP/网络] -j [ACCEPT|DROP|REJECT|LOG] -AI:准对某条链进行规则的插入或累加 -io:网络接口,进或出 -P 协议:设置此规则适用于哪种数据包格式 -s 来源:来源IP/网络 -d:目标 IP/网络 -j:后面接操作
# 设置 lo 成为受信任的设备,亦即进出 lo 的数据包都予以接受 [root@CentOS ~]# iptables -A INPUT -i lo -j ACCEPT # 只要是来自内网的192.168.100.0/24 的数据包统统接受 [root@CentOS ~]# iptables -A INPUT -i eth1 -s 192.168.100.0/24 -j ACCEPT # 只要是来自192.168.100.10 就接受,但来自192.168.100.230这个来源的就丢弃 [root@CentOS ~]# iptables -A INPUT -i eth1 -s 192.168.100.10 -j ACCEPT [root@CentOS ~]# iptables -A INPUT -i eth1 -s 192.168.100.130 -j DROP # 查看详细规则 [root@CentOS ~]# iptables-save # Generated by iptables-save v1.4.7 on Wed Apr 5 09:20:57 2017 *nat :PREROUTING ACCEPT [132:12351] :POSTROUTING ACCEPT [25:2813] :OUTPUT ACCEPT [25:2813] COMMIT # Completed on Wed Apr 5 09:20:57 2017 # Generated by iptables-save v1.4.7 on Wed Apr 5 09:20:57 2017 *filter :INPUT DROP [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -i lo -j ACCEPT -A INPUT -s 192.168.100.0/24 -i eth1 -j ACCEPT -A INPUT -s 192.168.100.10/32 -i eth1 -j ACCEPT -A INPUT -s 192.168.100.203/32 -i eth1 -j DROP COMMIT # Completed on Wed Apr 5 09:20:57 2017
# 记录想要记录的某个规则的记录(写入内核日志文件) [root@CentOS ~]# iptables -A INPUT -s 192.168.2.200 -j LOG [root@CentOS ~]# iptables -L -n
-
TCP、UDP 的规则比对:针对端口设置
[root@CentOS ~]# iptables [-AI 链] [-io 网络接口] [-P tcp,udp] [-s 来源IP/网络] [--sport 端口范围] [-d 目标IP/网络] [--dport 端口范围] -j [ACCEPT|DROP|REJECT] --sport 端口范围:限制来源端口号码,端口号码可以是连续的,例如 1024:65535
# 因为仅有TCP与UDP数据包具有端口号,因此要想使用--dport和--sport时,需要加上-P tcp 或-P udp 的参数才会成功。 # 想要连接进入本机 port 21 的数据包都阻挡掉 [root@CentOS Desktop]# iptables -A INPUT -i eth0 -p tcp --dport 21 -j DROP # 相连到本机的网上邻居(udp port 137,138 tcp port 139,445)就放行 [root@CentOS ~]# iptables -A INPUT -i eth0 -p udp --dport 137:138 -j ACCEPT [root@CentOS ~]# iptables -A INPUT -i eth0 -p tcp --dport 139 -j ACCEPT [root@CentOS ~]# iptables -A INPUT -i eth0 -p tcp --dport 445 -j ACCEPT # 只要来自 192.168.1.0/24 的1024:65535 端口的数据包,且想要连接到本机的ssh port 就给予阻挡 [root@CentOS ~]# iptables -A INPUT -i eth0 -p tcp -s 192.168.1.0/24 --sport 1024:65535 --dport ssh -j DROP # 将来自任何地方来源 port 1:1023 的主动连接到本机的1:1023连接丢弃 [root@CentOS ~]# iptables -A INPUT -i eth0 -p tcp --sport 1:1023 --dport 1:1023 --syn -j DROP #
-
iptables外挂模块:mac与state
[root@CentOS ~]# iptables -A INPUT [-m state] [[state 状态] -m:一些iptables 的外挂模块,state或mac --state:一些数据包状态 INVALID:无效的数据包 ESTABLISHED:已经成功连接的状态 NEW:想要新建立的数据包状态 RELATED:表示数据包与主机发送出去的数据有关 ``` ``` # 只要已建立连接或已发出请求的数据包就予以通过,不合法数据包就丢弃 [root@CentOS ~]# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT [root@CentOS ~]# iptables -A INPUT -m state --state INVALID -j ACCEPT # 针对局域网的 aa:bb:cc:dd:ee:ff 主机开放其连接 [root@CentOS ~]# iptables -A INPUT -m mac --mac-source aa:bb:cc:dd:ee:ff -j ACCEPT
-
ICMP 数据包规则的比对:准对是否响应ping来设计
[root@CentOS ~]# iptables -A INPUT [-p icmp] [–icmp-type 类型] [-j ACCEPT
–icmp-typpe:后面必须接ICMP 的数据包类型,也可以使用代号,8是回显请求让0,3,4,11,12,14,16,18 的 ICMP type 可以进入本机
[root@CentOS ~]# vim somefile
#!/bin/bash
icmp_type=“0 3 4 11 12 14 16 18”
for typeicmp in $icmp_type
do
iptables -A INPUT -i eth0 -p icmp --icmp-type $typeicmp -j ACCEPT
done
[root@CentOS ~]# chmod +x somefile
[root@CentOS ~]# sh somefile -
超简单的客户端防火墙设计与防火墙规则存储
如果将Linix主机作为客户端且不提供网络服务时,应该图和设计防火墙:
1 . 规则清零:iptables -F
2 . 默认策略,将INPUT设为DROP,其他ACCEPT
3 . 信任本机,lo必须设置为信任设备
4 . 回应数据包让本机通过主动向外发出请求而相应的数据可以进入本机(ESTABLISHED,RELATED)
5 . 信任用户# 使用脚本来设置 [root@CentOS ~]# vim firewall.sh #!/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin; export PATH # 1. 清除规则 iptables -F iptables -X iptables -Z # 2. 设置策略 iptables -P INPUT DROP iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT # 3~5 制订各项规则 iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT #iptables -A INPUT -i eth0 -s 192.168.1..0/24 -j ACCEPT # 6. 写入防火墙规则配置文件 /etc/init.d/iptables save [root@CentOS ~]# ./firewall.sh iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] #chkconfig --listiptables。如果不写脚本直接修改/etc/sysconfig/iptables也可以 #若要让这次修改的各种设置在下次开机时还保存,那就需要对/etc/init.d/iptables save这个命令加参数
-
IPv4 的内核管理功能:/etc/sys/net/ipv4/*
1 . /proc/sys/net/ipv4/tcp_syncookies:预防SYN泛洪攻击,可以启用内核SYN Cookie模块。
当启动SYN Cookie 时,主机在发送SYN/ACK确认包前,会要求Client 端在短时间内回复一个序号买这个序号包含许多原SYN数据包的信息,包括IP ‘port等。多Client端可以回复一个正确的序号,俺么主机就确定该数据包为可信的,因此会发送SYN/ACK数据包,否则就不理会此数据包# 启动SYN Cookie模块(会造成某些服务延迟)。 [root@CentOS ~]# echo "1" > /proc/sys/net/ipv4/tcp_syncookies
2 . /peoc/sys/net/icmp_echo_ignore_broadcasts:预防ping of death 攻击,可以取消ICMP类型8的回显就是了。
某些局域网内的常见的服务(例如DHCP)会使用ping的方式来侦测是否有重复的IP,所以最好不要取消所有的ping响应 /proc/sys/net/ipv4内的icmp_echo_ignore_broadcasts:仅有ping broadcast 地址时才取消ping的回应 /proc/sys/net/ipv4内的icmp_echo_all:全部的ping都不回应
[root@CentOS ~]# echo "1" > /proc/sys/net/ipv4icmp_echo_ignore_broadcasts
3 . /proc/sys/net/ipv4/conf/网络接口/*
rp_filter:逆向路径过滤
log_martians:可以用来启动记录不合法的IP来源的功能
accept_source_route
accept_redirects
send_redirects:发送一个ICMP redirect数据包# 可以使用echo 1 > /proc/sys/net/ipv4/conf/网络接口/rp.filter # 建议修改配置文件 [root@CentOS ~]# vim /etc/sysctl.conf net.ipv4.tcp_syncookies = 1 net.ipv4.icmp_echo_ignore_broadcast = 1 net.ipv4.conf.all.rp_filter = 1 net.ipv4.cong .default.rp_filter = 1 net.ipv4.conf.eth0.rp_filter = 1 net.ipv4.conf.lo.rp_filter = 1 [root@CentOS ~]# sysctl -p
-
设置单机防火墙的一个实例
规则草拟
外网网卡eth0
内网网卡eth1,切内部使用192.168.100.0/24网段
主机,默认开放的服务有WWW,SSH,HTTPS
filter table 默认策略:INPUT DROP,OUPUT ACCEPT, FORWARD ACCEPT# 将整个脚本分为3部分: # iptable.rule:设置最基本的规则 # iptables.deny:设置阻挡 # iptables.allow:设置放行
[root@CentOS ~]# mkdir -p /usr/local/virus/iptables [root@CentOS ~]# cd /usr/local/virus/iptables [root@CentOS iptables]# vim iptables.rule #!/bin/bash # 请先输入相关参数,不要输入错误 EXTIF="eth0" # 这个是可以来你上public ip 的网络接口 INIF="eth1" #内部LAN 的网络连接,若无则写成 INIF="" INNET="192.168.100.0/24" # 若无内网络接口,请填写 INNET="" export EXTIF INIF INNEF # 第一部分,针对本机的防火墙设置 # 1 . 先设置好内核的网络功能 echo "1" > /proc/sys/net/ipv4/tcp_syncookies echo "1" > /proc/sys/net/ipv4icmp_echo_ignore_broadcasts for i in /proc/sys/net/ipv4/conf/*/{rp_filter,log_martians}; do echo "1" > $i done for i in /proc/sys/net/ipv4/conf/*/{accept_source_route,accept_redirects,send_redirects}; do echo "0" > $i done # 2 . 清除规则、设置默认策略及开放 lo 与相关的设置值 PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin; export PATH iptables -F iptables -X iptables -Z iptables -P INPUT DROP iptables -P OUTPUT ACCEPT iptables -P FPRWARD ACCEPT iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -nm state -state RELATED,ESTABLISHED -j ACCEPT # 3 . 启动额外的防火墙script模块 if [ -f /usr/local/virus/iptables/iptables.deny ]; then sh /usr/local/virus/iptables/iptables.deny fi if [ -f /usr/local/virus/iptables/iptables.allow ]; then sh /usr/local/virus/iptables/iptables.allow fi if [ -f /usr/local/virus/iptables/iptables.http ]; then sh /usr/local/virus/iptables/iptables.http fi # 4 . 允许某些类型的 ICMP 数据包进入 AICMP ="0 3 3/4 11 12 14 16 18" for tyicmp in $AICMP do iptables -A INPUT -i #EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT done # 5 . 允许某些服务的进入,,请依照自己的环境开启 # iptables -A INPUT -p TCP -i $EXTIF --dport 21 --sport 1024:65535 -j ACCEPT # FTP # iptables -A INPUT -p TCP -i $EXTIF --dport 22 --sport 1024:65535 -j ACCEPT # SSH # iptables -A INPUT -p TCP -i $EXTIF --dport 25 --sport 1024:65535 -j ACCEPT # SMTP # iptables -A INPUT -p TCP -i $EXTIF --dport 53 --sport 1024:65535 -j ACCEPT # DNS # iptables -A INPUT -p TCP -i $EXTIF --dport 53 --sport 1024:65535 -j ACCEPT # DNS # iptables -A INPUT -p TCP -i $EXTIF --dport 80 --sport 1024:65535 -j ACCEPT # WWW # iptables -A INPUT -p TCP -i $EXTIF --dport 110 --sport 1024:65535 -j ACCEPT # POP3 # iptables -A INPUT -p TCP -i $EXTIF --dport 443 --sport 1024:65535 -j ACCEPT # HTTPS # 第二部分,针对后端主机的防火墙设置 # 1 . 先加载一些有用的模块 modules="ip_tables iptables_nat ip_nat_ftp ip_nat-irc ip_conntrack ip_conntrack_ftp ip_conntrack_irc" for mod in $modules do testmod=`lsmod | grep "^${mod} " | awk '{print $1}'` if [ "$testmod" == "" ]; then modprobe $mod fi # 2 . 清除 NAT table 的规则 iptables -F -t nat iptables -X -t nat iptables -Z -t nat iptables -t nat -P PREROUTING ACCEPT iptables -t nat -P POSTROUTING ACCEPT iptables -t nat -P OUTPUT ACCEPT # 3 . 若有内部接口的存在(双网卡)开放成为路由器,且为IP分享器 if [ "$INIF" != "" ]; then iptables -A INPUT -i $INIF -j ACCEPT echo "1" > /proc/sys/net/ipv4/ip_forward if [ "$INIET" != "" ]; then for innet in $INNET do iptables -r nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE done fi fi # 如果你的 MSN 一直无法连接,或者是某些网站 OK 某些网站不 OK ,可能是 MTU 的问题,那可以将下面这行取消批注,来启动 MTU 的范围 iptables -A FORWARD -p tcp -m tcp --tcp-flags STN,RST SYN -m tcpmss --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu # 4 . NAT 服务器后端的 LAN 内对外值服务器设置 iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 -j DNAT --tp-description 192.168.1.210:80 #WWW # 5 . 特殊的功能,包括 windows 远程桌面产生的规则,假设桌面主机为 1.2.3.4 # iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4 --dport 6000 -j DNAT --to-description 192.168.100.10 # iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4 --sport 3389 -j DNAT --to-description 192.168.100.20 # 6 . 最后将这些功能存储下来 /etc/init.d/iptables save
假设我要让140.116.44.0/24 这个网络的所有主机可以进入本机主机的话
[root@CentOS iptables]# vim iptables.allow #!/bin/bash # 下面则填写允许进入本机的其他网络的主机或网络 iptables -A INPUT -i $EXTIF -s 140.116.44.0/24 -j ACCEPT # 下面则是关于阻挡的文件设置法 [root@CentOS iptables]# vim iptables.deny #!/bin/bash # 下面填写的是你要阻挡的那个东西 iptables -A INPUT -i $EXTIF -s 140.116.44.254 -j DEOP [root@CentOS iptables]# chmod 700 iptables.* # 设置开机启动 [root@CentOS iptables]# vim /etc/re.d/rc.local # 1 . Firewall /usr/local/virus/iptables/iptables.rule
鸟哥的服务器10 防火墙
最新推荐文章于 2022-10-16 16:45:07 发布