• iptables


    iptables

    iptable的组件

    • netfilter: 在内核态中, 由3表5链组成
    • iptable: 在用户态中, 是管理netfilter的工具, 对3表5链进行增删改

    iptable的3表

    filter

    • 负责过滤数据包, 包括的规则链为: input, output 和 forward

    nat

    • 负责网络地址转换, 包括的规则链为: prerouting, output 和 postrouting

    mangle

    • 修改数据包内容, 一般用于为数据包打标签, 包括的规则链为: prerouting, input, output, forward 和 postrouting

    iptable中的5链

    input

    • 匹配目标ip是本机的数据包

    output

    • 出口数据包, 一般不在此链上做配置

    forward(Linux需要开启网络间转发功能ip_forward)

    • 匹配流经本机的数据包

    prerouting

    • 修改目的地址, 用来做DNAT

    postrouting

    • 修改源地址, 用来做SNAT
    • 如, 内网通过路由器NAT将内网中主机的IP地址修改为运营商的公网IP地址来上网

    iptables命令使用

    格式

    • iptables [-t table] <-A|-I|-D|-P> LINK_NAME <-F|-s|-d|-sport|-dport|-p|-i|-o> [-j] <ACCEPT|REJECT|DROP|SNAT|DNAT>

    SNAT使用

    • -j SNAT --to IP[-IP][:PORT-PORT]
    • iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -p tcp -j SNAT --to 1.1.1.1

    DNAT使用

    • -j DNAT --to IP[-IP][:PORT-PORT]
    • iptables -t nat -A PREROUTING --dport 80 -p tcp -j DNAT 192.168.1.10:81

    使用建议

    • 在配置好所有的防火墙规则之后添加iptable -t filter -A INPUT -j DROP
    • 注意: 一定要在最后添加, 否则可能会导致远程连接断开, 如果是远程阿里云主机则需要可能要重新安装OS了
    • 还可以执行周期性计划
    */15 * * * * iptables -t filter -P INPUT ACCEPT
    */15 * * * * iptables -t filter -F
    

    保存

    • service iptables save

    常用规则

    刚刚开始一个新的iptables时

    1. iptables -t filter -A INPUT -i lo -j ACCEPT # 开放loopback
    2. iptables -t filter -A INPUT -p tcp -m multiport --dports 20,21,22,80,198,443,3306,8080,8388,8443 -j ACCEPT
    3. iptables -t filter -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    4. iptables -t filter -P INPUT DROP
    5. 在处理完开发者需要的规则之后, 最后iptables -t filter -A INPUT -j DROP
  • 相关阅读:
    Nginx得知——Hello World模
    Web静态和动态项目委托代理基于面向方面编程AOP
    PIC16SCM设置不同IO功耗端口状态的影响
    $.ajax通路RESTful Web Service一个错误:Unsupported Media Type
    什么是Entitlement
    加解密
    Types of Security Vulnerabilities
    fork后子进程从哪里开始执行
    进程间通信(IPC)介绍
    Using URL Schemes to Communicate with Apps
  • 原文地址:https://www.cnblogs.com/megachen/p/10346316.html
Copyright © 2020-2023  润新知