iptables VS firewalld 防火墙常用方法 —— 筑梦之路

iptables常用

#查看规则

iptables -vnL

#封禁单个ip

iptables -I INPUT -s x.x.x.x -j DROP

-I 表示插入规则

#封禁ip段

iptables -I INPUT -s x.x.x.0/24 -j DROP

#解禁ip

iptables -D INPUT -s ***.***.***.*** -j DROP

-D 表示删除规则

# 备份和恢复

iptables-save > /etc/iptables.rules

iptables-restore < /etc/iptables.rules
基于ipset对大量ip进行封禁

ipset 常用命令

集合类型:分为三大类,即hash、bitmap、list
其中bitmap最大容量为65536;hash默认容量是65536,可通过maxelem指定更大的容量;而list类型的元素为hash或bitmap集合,其容量则更大
hash方式有:
hash:net,iface hash:net,port hash:net hash:ip,port,net hash:ip,port,ip hash:ip,port hash:ip
bitmap方式有:
bitmap:port bitmap:ip,mac bitmap:ip

# 创建集合
ipset create xxx hash:net
ipset create xxx hash:net maxelem 1000000 #指定最大容量
ipset create xxx hash:net hashsize 4096 maxelem 1000000 #指定hashsize和最大容量 hashsize默认为1024,必须为2的倍数
ipset create xxx hash:ip timeout 300 #创建带默认超时时间的集合,注,单个添加的条目时间可超过该值,过期后,条目会自动删除
ipset create xxx hash:ip family inet hashsize 1024 maxelem 65536 timeout 300 #完整的命令


 
# 查看集合
ipset list #会输出详细信息,包括条目及每个条目的剩余时间
ipset list xxx
ipset list -n #仅输出集合名称
ipset list -t #不输出元素

# 添加条目
ipset add xxx 192.168.1.1
ipset add xxx 192.168.1.1 timeout 60
ipset -exist add xxx 192.168.1.1 timeout 60 #添加条目,若存在,则更新timeout时间 说明,不使用 -exist 则条目已存在时会报错

# 针对不同集合类型的示例
ipset add foo 192.168.1/24
ipset add foo 192.168.1.1,12:34:56:78:9A:BC
ipset add foo 80
ipset add foo 192.168.1.0/24,80-82
ipset add foo 192.168.1.1,udp:53
ipset add foo 10.1.0.0/16,80
ipset add foo 192.168.1.1,80,10.0.0.1
ipset add foo 192.168.2,25,10.1.0.0/16
ipset add foo 192.168.0/24,eth0

# 删除条目
ipset del xxx 192.168.1.1 #注,删除一个不存在的条目会报错
ipset -exist del xxx 192.168.1.1

# 查询条目
ipset test xxx 192.168.1.1

# 清空集合
ipset flush #清空所有集合
ipset flush xxx #清空指定的集合

# 将规则保存到文件
ipset save xxx -f xxx.txt #导出文件甚至包含集合创建命令
ipset save xxx #输出到屏幕# 从文件还原
ipset restore -f xxx.txt #注意,集合名称在文件中已存在,若存在该集合,则导入失败


 
# 删除集合
ipset destroy #删除所有集合
ipset destroy xxx #删除指定的集合

# 集合重命名
ipset rename xxx yyy #改名为yyy
在iptables中使用集合
# 添加
iptables -I INPUT -m set –match-set xxx src -p tcp -j DROP
iptables -I INPUT -m set –match-set xxx src -p udp -j DROP
# 删除
iptables -D INPUT -m set –match-set xxx src -p tcp -j DROP
iptables -D INPUT -m set –match-set xxx src -p udp -j DROP
# 查看
iptables -L | grep match-set
# 保存
service iptables save
复现实验

#构造简易蜜罐

#!python3
from flask import Flask
from flask import request
import sys

app = Flask(__name__)

@app.route('/')
def ip():
    ip = request.remote_addr
    return ip


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=sys.argv[1])

#运行简易版蜜罐,蜜罐端口为 12345
python3 ./honey_pot.py 12345

定义的蜜罐端口,如果存在“入侵者”扫描,则将对方列为黑名单进行封堵

#创建一个蜜罐
ipset create honey-pot hash:ip

#添加规则
iptables -I INPUT \
          -p tcp --dport 12345 \
          -j SET --add-set honey-pot src

##如果有人访问 12345 端口,则将访问者的地址加到 honey-pot 组里去

#局域网内发送请求

curl 192.168.80.20:12345

#服务端查看

ipset list honey-pot

此时创建的 honey-pot 对象组中已经自动添加了一个对象,说明诱捕功能生效。此时只是将诱捕到的IP自动添加到对象组,并没有对诱捕IP实行封堵,下一步,我们对 honey-pot 对象组实行自动封堵。

#封禁ip


iptables -I INPUT \
         -p tcp -m set --match-set honey-pot src \
         -j DROP

以上命令是对 honey-pot 对象组实行 DROP 动作。当入侵者不小心扫描到了蜜罐端口,对方IP就会自动写入 honey-pot 对象组,然后就自动被封了

#查看生效的规则

iptables -nL --line-number

执行 iptables-save 可以查看规则写法
iptables -A INPUT -p tcp -m set --match-set honey-pot src -j DROP
iptables -A INPUT -p tcp -m tcp --dport 12345 -j SET --add-set honey-pot src
iptables -A INPUT -p tcp -m tcp --dport 12345 -j ACCEPT


扩展:

针对firewalld又该怎么用呢?

封禁IP

# 单个IP
firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='222.222.222.222' reject"  

# IP段 
firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='222.222.222.0/24' reject" 

# 单个IP的某个端口
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=192.168.1.2 port port=80  protocol=tcp  accept" 

ipset方式:

# 单个IP

# firewall-cmd --permanent --zone=public --new-ipset=blacklist --type=hash:ip

# firewall-cmd --permanent --zone=public --ipset=blacklist --add-entry=222.222.222.222

# 网段

# firewall-cmd --permanent --zone=public --new-ipset=blacklist --type=hash:net

# firewall-cmd --permanent --zone=public --ipset=blacklist --add-entry=222.222.222.0/24

# 导入规则

# firewall-cmd --permanent --zone=public --new-ipset-from-file=/path/blacklist.xml

# 进行封禁

# firewall-cmd --permanent --zone=public --add-rich-rule='rule source ipset=blacklist drop'

IP封禁和端口

# firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=192.168.1.2 port port=80  protocol=tcp  accept"

只对192.168.1.2这个IP只能允许80端口访问  (拒绝访问只需把  accept 换成 reject、删除该规则把 –add-rich-rule 改成 –remove-rich-rule即可)

# firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=192.168.1.2/24 port port=80  protocol=tcp  accept"

只对192.168.1.2这个IP段只能允许80端口访问(拒绝访问只需把  accept 换成 reject、删除该规则把 –add-rich-rule 改成 –remove-rich-rule即可)


-------------------------------------------------------------------------
firewalld其他用法:


双网卡内网网卡不受防火墙限制

# firewall-cmd --permanent --zone=public --add-interface=eth1   
公网网卡–zone=public默认区域

# firewall-cmd --permanent --zone=trusted --add-interface=eth2
内网网卡–zone=trusted是受信任区域 可接受所有的网络连接

重新载入以生效

# firewall-cmd --reload

配置 public zone 的 icmp

查看所有支持的 icmp 类型

# firewall-cmd --get-icmptypes
destination-unreachable echo-reply echo-request parameter-problem redirect router-advertisement router-solicitation source-quench time-exceeded
列出

# firewall-cmd --zone=public --list-icmp-blocks
添加 echo-request 屏蔽

# firewall-cmd --zone=public --add-icmp-block=echo-request [--timeout=seconds]
移除 echo-reply 屏蔽

# firewall-cmd --zone=public --remove-icmp-block=echo-reply

配置 public zone 的端口转发

打开端口转发

# firewall-cmd --zone=public --add-masquerade
然后转发 tcp 22 端口至 3753

# firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toport=3753
转发 22 端口数据至另一个 ip 的相同端口上

# firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toaddr=192.168.1.100
转发 22 端口数据至另一 ip 的 2055 端口上

# firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toport=2055:toaddr=192.168.1.100

配置 external zone 中的 ip 地址伪装

查看

# firewall-cmd --zone=external --query-masquerade
打开伪装

# firewall-cmd --zone=external --add-masquerade
关闭伪装

# firewall-cmd --zone=external --remove-masquerade

管理服务

添加 smtp 服务至 work zone

# firewall-cmd --zone=work --add-service=smtp
移除 work zone 中的 smtp 服务

# firewall-cmd --zone=work --remove-service=smtp

管理网卡接口

列出 public zone 所有网卡

# firewall-cmd --zone=public --list-interfaces
将 eth0 添加至 public zone,永久

# firewall-cmd --zone=public --permanent --add-interface=eth0
eth0 存在与 public zone,将该网卡添加至 work zone,并将之从 public zone 中删除

# firewall-cmd --zone=work --permanent --change-interface=eth0
删除 public zone 中的 eth0,永久

# firewall-cmd --zone=public --permanent --remove-interface=eth0

管理端口

列出 dmz 级别的被允许的进入端口

# firewall-cmd --zone=dmz --list-ports
允许 tcp 端口 8080 至 dmz 级别

# firewall-cmd --zone=dmz --add-port=8080/tcp
允许某范围的 udp 端口至 public 级别,并永久生效

# firewall-cmd --zone=public --add-port=5060-5059/udp --permanent

参考资料:

基于ipset对大量IP进行封禁(Iptables) | 程序员灯塔

使用 iptables 自动封堵扫描者IP

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值