iptables练习

iptables实战

1.开启防火墙

systemctl start firewalld

2.清空所有的默认规则,定义自己的规则

iptables -F

查看此时的iptables
iptables -nL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 
略...... 

3.我们准备建立自己的规则

(1) 放行ssh (端口:22)

iptables -t filter -A INPUT  -s 0/0  -d 192.168.42.153  -p tcp --dport 22 -j ACCEPT 或者
iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT
iptables -t filter -A OUTPUT  -s 192.168.42.153  -d 0/0   -p tcp   --sport 22  -j ACCEPT 或者
iptables -A  OUTPUT -s  192.168.42.153  -p tcp  --sport  22 -j ACCEPT

(2)修改默认规则链(关闭所有端口)

iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

(3)放行web(80)端口 httpd nginx

iptables -I INPUT -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT 或者
iptables -t filter -I INPUT -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT

iptables -I OUTPUT -s 192.168.42.153 -p tcp --sport 80 -j ACCEPT 或者
iptables -t filter -I OUTPUT -s 192.168.42.153  -d 0/0 -p tcp --sport 80 -j ACCEPT

(4)修改默认规则链后,我们发现ping不通自己,也ping不通别的主机

iptables -t filter -I INPUT -s 127.0.0.1 -d 127.0.0.1 -i lo  -j ACCEPT 
iptables -t filter -I OUTPUT -s 127.0.0.1 -d 127.0.0.1 -o lo  -j ACCEPT

(5)允许自己ping别的主机

iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 8 -j ACCEPT
iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 0 -j ACCEPT

(6)允许任何人来ping本机

iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 8 -j ACCEPT
iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 0 -j ACCEPT

(7)同时开发多个端口(多端口匹配)

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp -m multiport --dports 22,80,3306 -j ACCEPT
iptables -I INPUT -d 0/0 -s 192.168.42.153 -p tcp -m multiport --sports 22,80,3306 -j ACCEPT

(8)iptables -vnL –line-numbers #显示数字

iptables  -vnL INPUT  --line-numbers 
Chain INPUT (policy DROP 1 packets, 229 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        8   576 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 8
2       12  1008 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 0
3       16  1226 ACCEPT     all  --  lo     *       127.0.0.1            127.0.0.1           
4       88  7565 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:80
5     2135  163K ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:22

(9) 源地址,目的地址范围匹配

iptables -I INPUT -d 192.168.42.153 -p tcp --dport 23 -m iprange --src-range 192.168.42.150-192.168.42.158 -j ACCEPT

iptables -I OUTPUT -s 192.168.42.153 -p tcp --dport 23 -m iprange --dst-range  192.168.42.150-192.168.42.158 -j ACCEPT

(10)禁止包含”old”字符的页面出来

iptables -I OUTPUT -s 192.168.42.153 -d 0/0 -p tcp --sport 80 -m string --algo bm --string "old" -j DROP

(11)基于时间限定,9点到19点,禁止访问80端口

iptables -I INPUT -s 0/0  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz  -j DROP

(12)周一到周五9点到19点禁止访问80端口

iptables -I INPUT  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz --weekdays 1,2,3,4,5  -j DROP

(13)端口大于2个并发连接(禁止)

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit --connlimit-above 2 -j DROP

(14)端口同一个客户端小于3个并发连接

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit ! --connlimit-above 3 -j DROP

4.新建自定义链 ,开放80

iptables -F
iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT
iptables -A  OUTPUT -s  192.168.42.153  -p tcp  --sport  22 -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -N webinput
iptables -N weboutput
iptables -I webinput -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT 
iptables -I weboutput -s 192.168.42.153 -p tcp --sport 80 -j ACCEPT
iptables -A INPUT -p tcp -j webinput 
iptables -A OUTPUT -p tcp -j weboutput

“`

5.NAT:(工作在网络和传输层) 过载技术

Basic NAT:静态NAT
    一个内部主机,分配一个外网地址
NAPT:动态NAT,网络地址端口转换;net会话表
    源地址转换:SNAT 用于内网主机访问互联网
    目标地址转换:DNAT 让互联网上主机访问本地内网上的某服务器上的服务(发布)


6.iptables基于SNAT和DNAT这两个目标(target)实现地址转换技术

-j SNT --to-source SIP
    规则添加:POSTROUTING链
-j MASQUERADE  动态获取(外网地址是动态的)
-j DNAT --to-destination DIP{:PORT}
    支持端口映射

iptables练习

iptables练习



7.源地址转换示例

iptables练习

// 此时 filter 表上的 FORWARD 链 要打开(会经过此表中的此链)
[root@nat ~]# iptables -t nat -F
[root@nat ~]# iptables -t nat -L -n
[root@nat ~]# iptables -t nat -A POSTROUTING -s 10.1.249.158 -j SNAT --to-source 192.168.2.3
[root@nat ~]# iptables -t nat -L -n
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  10.1.249.158         0.0.0.0/0           to:192.168.2.3 

内部主机访问外部主机
[root@nei ~]# curl http://192.168.2.4/index.html
hello 
查看外部主机中web的访问日志
[root@wai ~]# tail /var/log/httpd/access_log
192.168.2.3 - - [13/Oct/2016:21:40:58 +0800]
192.168.2.3 - - [13/Oct/2016:21:40:59 +0800]
//主机,我们的内部主机IP为10.1.249.158
//而httpd的访问日志为192.168.2.3的访问
//说明成功实现了源地址转换


8.目标地址转换示例(对80端口的转换)

iptables练习

[root@luyou ~]# iptables -t filter -F 
[root@luyou ~]# iptables -t nat -F
[root@luyou ~]# iptables -t nat -A PREROUTING -d 10.1.249.125 -p tcp --dport 80 -j DNAT --to-destination 192.168.2.4
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            10.1.249.125        tcp dpt:80 to:192.168.2.4

[root@luyou ~]# netstat -tln | grep "\<80\>"  此时本机上并没有开放80端口
[root@wai ~]# curl http://10.1.249.125
hello  --> 此时我们访问为 luyou 主机上的80端口  由上面可知,此服务器上并没有开放80,而是将请求送往 后端服务器


9.目标地址和端口转换示例(对22端口的转换)

[root@luyou ~]# iptables -t filter -F  FORWARD
[root@luyou ~]# iptables -t nat -A PREROUTING -d 10.1.249.125 -p tcp --dport 22022 -j DNAT --to-destination 192.168.2.4:22
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            10.1.249.125        tcp dpt:22022 to:192.168.2.4:22 
[root@wai ~]# ssh -p 22022 10.1.249.125  --> 连接 luyou 设备的22022端口
[root@nei ~]# --> 连接到 nei  内网的主机上来
[root@luyou ~]# iptables -t nat -L -n -v
Chain PREROUTING (policy ACCEPT 42 packets, 4633 bytes)
 pkts bytes target     prot opt in     out     source               destination         --> 有报文成功匹配到
    1    60 DNAT       tcp  --  *      *       0.0.0.0/0            10.1.249.125        tcp dpt:22022 to:192.168.2.4:22 

//此时我们请求luyou这台NAT服务器的22022端口的ssh服务,此时我们nat路由器的ssh服务并没有监听在22022端口,
//而是将目标IP和目标端口都进行了转换了,使我们连接到了内网的ssh服务器上

 

原创文章,作者:kang,如若转载,请注明出处:http://www.178linux.com/79146

(0)
kangkang
上一篇 2017-06-24
下一篇 2017-06-24

相关推荐

  • 详解Linux下用户和组的配置文件

    本文将介绍Linux下用户和组的概念及相关配置文件,通过本文,能更好的认识/etc/pssswd ,/etc/group,以及UID、GID的概念,最后将通过修改用户配置文件来实现用户和组的管理工作,对Linux的多任务、多用户有一个更清楚的认识。 一、用户(USER) 在LInux上用户的管理工作是通过修改相关的配置文件来实现的,在日常的工作中主要包含对用…

    Linux干货 2016-10-23
  • ubuntu kylin虚拟机安装KVM

    1.确定物理机CPU是否支持虚拟化    查看方法1:计算机->属性->处理器(Inter(R) Core(TM) i5-2450M),然后去网上查看自己的电脑CPU是否支持虚拟化,    地址http://ark.intel.com/zh-cn/找到自己CPU的信息,虚拟化技术后是否显示为yes,如果是n…

    Linux干货 2016-10-15
  • 马哥教育网络班22期第五周博客作业

    正则表达式与grep 文本编辑工具     grep:是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来;     sed :是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用;     awk…

    Linux干货 2016-10-19
  • linux之/home目录转移分区。

    linux之/home目录转移分区。     I,基本思路,将/home目录的数据转移到新的分区,再将/home目录挂载到新的分区。     II,添加硬盘,进行分区,添加新硬盘不重启机器识别命令echo “- – -”  /sys/class/scsi_host/host#/scan,然后进行 …

    Linux干货 2017-06-19
  • bash特性及bash脚本编程初步

    终端,附着在终端的接口程序: GUI:KDE, GNome, Xfce CLI:/etc/shells bash zsh fish   bash的特性: 命令行展开:~, {} 命令别名:alias, unalias 命令历史:history 文件名通配:glob 快捷键:Ctrl+a, e, u, k, l 命令补全:$PATH 路径补全: &n…

    Linux干货 2016-11-21
  • http协议

    ##socket套接字– 套接字,进程间通信IPC的一种实现,允许位于不同主机(或同一主机)上不同进程之间进行通信和数据交换。– socketAPI:封装了内核中所提供的socket通信相关的系统调用– socketDomain:根据其所使用的地址– AF_INET:Address Family,Ipv4&#8…

    Linux干货 2017-12-06