1.策略与规则链

iptables 服务把用于处理或过滤流量的策略条目称之为规则,多条规则可以组成一个规则链,而规则链则依据数据包处理位置的不同进行分类,具体如下:

在进行路由选择前处理数据包(PREROUTING);
    处理流入的数据包(INPUT);
    处理流出的数据包(OUTPUT);
    处理转发的数据包(FORWARD);
    在进行路由选择后处理数据包(POSTROUTING)。

一般来说,从内网向外网发送的流量一般都是可控且良性的,因此我们使用最多的就是INPUT 规则链,该规则链可以增大黑客人员从外网入侵内网的难度。

处理匹配流量的动作:

ACCEPT(允许流量通过)、REJECT(拒绝流量通过)、LOG(记录日志信息)、DROP(拒绝流量通过)。

DROP:直接将流量丢弃而且不响应;

REJECT :在拒绝流量后再回复一条“您的信息已经收到,但是被扔掉了”信息,从而让流量发送方清晰地看到数据被拒绝的响应信息。

当把 Linux 系统中的防火墙策略修改成DROP 拒绝动作后,流量发送方会看到响应超时的提醒。但是流量发送方无法判断流量是被拒绝,还是接收方主机当前不在线:

当把 Linux 系统中的防火墙策略设置为REJECT 拒绝动作后,流量发送方会看到端口不可达的响应;

举例: reject

[root@localhost ~]# ping -c 4 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
From 192.168.10.10 icmp_seq=1 Destination Port Unreachable
From 192.168.10.10 icmp_seq=2 Destination Port Unreachable
From 192.168.10.10 icmp_seq=3 Destination Port Unreachable
From 192.168.10.10 icmp_seq=4 Destination Port Unreachable
--- 192.168.10.10 ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3002ms

drop

[root@localhost ~]# ping -c 4 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
--- 192.168.10.10 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3000ms

2.iptables中的基本命令参数

1)把INPUT规则链的默认规则设置为拒绝:

[root@localhost ~]# iptables -P INPUT  DROP

此时ssh远程连接会断开。

2)向INPUT规则链中添加允许ICMP 和SSH流量进入的策略规则:

root@localhost ~]# iptables -I INPUT -p icmp -j ACCEPT

此时从远程设备ping 通测试机

3)向INPUT规则链中添加允许10.10.65.65远程SSH登录的策略规则:

[root@localhost ~]# iptables -I INPUT -s 10.10.65.65/32 -p tcp --dport 22 -j ACCEPT

4)查看规则链

[root@localhost ~]# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 10.10.65.65 anywhere tcp dpt:ssh
ACCEPT icmp -- anywhere anywhere Chain FORWARD (policy ACCEPT)
target prot opt source destination Chain OUTPUT (policy ACCEPT)
target prot opt source destination

5)清除所有的防火墙规则链,默认规则除外

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination Chain FORWARD (policy ACCEPT)
target prot opt source destination Chain OUTPUT (policy ACCEPT)
target prot opt source destination

此时ssh远程连接会断开。

6)向INPUT 规则链中添加拒绝所有人访问本机12345 端口的策略规则:

[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable
REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable

7)向INPUT 规则链中添加拒绝所有主机访问本机1000~1024 端口的策略规则:

[root@localhost ~]# iptables -A INPUT -p tcp --dport 1000:1024 -j REJECT
[root@localhost ~]# iptables -A INPUT -p udp --dport 1000:1024 -j REJECT

注:使用iptables 命令配置的防火墙规则默认会在系统下一次重启时失效,如果想让配置的防火墙策略永久生效,还要执行保存命令:

[root@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]

3.centos 7 中没有iptables 和service iptables save 指令使用失败问题解决方案

如果执行service iptables save 命令来保存iptables时失败,报出:

The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

解决方法:

systemctl stop firewalld 关闭防火墙

yum install iptables-services 安装或更新服务

再使用systemctl enable iptables 启动iptables

最后 systemctl start iptables 打开iptables

再执行service iptables save

重启iptables服务:

service iptables restart

执行完毕之后/etc/syscofig/iptables文件就有了

最新文章

  1. jQuery—常用操作
  2. JS高级群的日常
  3. JavaScript与DOM
  4. Servlet题库
  5. (淘宝无限适配)手机端rem布局详解(转载非原创)
  6. 怎么学习C++?
  7. sudo: 无法解析主机:<主机名>
  8. bzoj 2618 2618: [Cqoi2006]凸多边形(半平面交)
  9. mysql存储过程、函数和触发器的创建 [转]
  10. Oralce 按分隔符把一列转成多行
  11. ubuntu下nvm,node以及npm的安装与使用
  12. Matlab中用内建函数代替for循环
  13. ThinkPHP 3.1.2 控制器的模块和操作
  14. 51cto培训课程
  15. Android项目---快递查询
  16. Internal Server Error
  17. 核心C#
  18. BZOJ 1303 【CQOI2009】中位数图
  19. APP性能测试(电量)
  20. Linux - 用户权限相关命令

热门文章

  1. Liunx运维(九)-Liunx进程管理命令
  2. Typecho 主题更换
  3. JVM--理解介绍
  4. ABP vNext 审计日志获取真实客户端IP
  5. 3.k8s存储之ConfigMap、Secret
  6. GitLab的安装及使用
  7. Spring Boot GraphQL 实战 03_分页、全局异常处理和异步加载
  8. 记一次Goroutine与wg导致的问题
  9. java进阶(31)--TreeSet集合、TreeMap集合、自平衡二叉树
  10. 【Problems】:JSON parse error: Invalid UTF-8 start byte 0xbd;