iptables nat 技术笔记(1)


用了老久了的iptables,拿出来点心得写出iptables nat笔记供大家参考!

1.Java环境建立

尽量使用/etc/profile做全局配置

2.前置机中导入iptables时,注意iptables版本问题。

导入iptables后可以用service iptables save将配置自动保存。

修改/etc/sysctl.conf可以使ip_forward功能自动打开。

以上设置后,使用chkconfig将iptables服务选上,可以在默认运行级别下保证iptables的自动运行。

3.使用redhat 8版本的ifup(即服务器切割脚本中的ifrouteup)可以在调整网卡时自动修改默认路由

4.在IDC做服务器切割时,尽量使用mac地址模拟

操作命令ifconfig eth0 hw ether aa:bb:cc:dd:ee:ff

5.静态路由添加方式

/etc/sysconfig/network-scripts/routes-eth0

192.168.0.0/16 via 10.10.0.22

/etc/sysconfig/static-routes

eth0 net 192.168.0.0 netmask 255.255.0.0 gw 10.10.0.22

6.iptables做端口映射时,如果映射的服务器默认网关并非是前置机本身,则postrouting中SNAT必须指定为前置机内网地址

以电信机房为例,防火墙eth0 10.10.0.1 eth1 218.78.212.84

Client A :10.10.0.33 默认路由:10.10.0.1

-A PREROUTING -d 218.78.212.84 -p tcp -m tcp –dport 22 -j DNAT –to-destination 10.10.0.33

结果:除10.10.0.33不能连通外,都可以ssh,日志记录为正确来源IP

-A PREROUTING -d 218.78.212.84 -p tcp -m tcp –dport 22 -j DNAT –to-destination 10.10.0.33

-A POSTROUTING -d 10.10.0.33 -p tcp -m tcp –dport 22 -j SNAT –to-source 218.78.212.84

结果:包括10.10.0.33都可以ssh,但日志记录为防火墙外网IP 218.78.212.84

-A PREROUTING -d 218.78.212.84 -p tcp -m tcp –dport 22 -j DNAT –to-destination 10.10.0.33

-A POSTROUTING -s 10.10.0.33 -p tcp -m tcp –dport 22 -j SNAT –to-source 218.78.212.84

结果:包括10.10.0.33都可以ssh,且日志记录为正确来源IP

Clent B :10.10.0.138 默认路由:10.10.0.241

-A PREROUTING -d 218.78.212.84 -p tcp -m tcp –dport 22 -j DNAT –to-destination 10.10.0.33

-A POSTROUTING -d 10.10.0.33 -p tcp -m tcp –dport 22 -j SNAT –to-source 10.10.0.1

结果:只有此方式包括10.10.0.138可以ssh,但日志记录为防火墙内网IP 10.10.0.1

7.修改/etc/inittab,将Ctrl+Alt+Del的组合键屏蔽,防止误操作及杜绝安全隐患

8.SSH只使用key验证方式

9.使用/etc/hosts.allow及/etc/hosts.deny做IP地址访问过滤

以下脚本是通过log日志将非法连接的IP列入/etc/hosts.deny的黑名单

# !/bin/bash

# 为防止自身地址被列入黑名单,可以先将自身IP加入/etc/hosts.allow

# 查找最后20条日志中非法连接和尝试连接失败的IP地址

tail -20 /var/log/secure|awk '$0~/Illegal|Failed/'|awk -Ffrom '{print $2}'|awk '{print $1}' >/tmp/badip

# 读取/etc/hosts.deny中有关sshd的地址定义

list=`grep sshd /etc/hosts.deny`

# 判断日志中非法连接IP的连接数超过5时列入黑名单

if [ "`wc -l /tmp/badip|awk '{print $1}'`" > "5" -a "`sort -u /tmp/badip|wc -l|awk '{print $1}'`" = "1" ]

then

blockip=`head -1 /tmp/badip`

if [ `grep $blockip /etc/hosts.deny|wc -l|awk '{print $1}'` != 1 ]

then

echo "$list $blockip" >/etc/hosts.deny

fi

fi


相关内容

    暂无相关文章