惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
C
Cybersecurity and Infrastructure Security Agency CISA
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Know Your Adversary
Know Your Adversary
S
Securelist
I
Intezer
AWS News Blog
AWS News Blog
L
LINUX DO - 热门话题
P
Privacy International News Feed
Recent Announcements
Recent Announcements
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 聂微东
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
The GitHub Blog
The GitHub Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
爱范儿
爱范儿
B
Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
S
Secure Thoughts
K
Kaspersky official blog
N
News | PayPal Newsroom
O
OpenAI News
Last Week in AI
Last Week in AI
C
Check Point Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tor Project blog
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
D
Docker
Hugging Face - Blog
Hugging Face - Blog
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
博客园 - 司徒正美
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
P
Palo Alto Networks Blog

Jiajun的技术笔记

你好,2026! TiDB 源码阅读(六):TiDB Coprocessor 源码解析 性能优化的核心思想 TiDB 源码阅读(五):索引 TiDB 源码阅读(四):AST、逻辑计划、物理计划 CockroachDB Serverless Architecture podman 无故退出 Cursor Control-L (CTRL-L) Keyboard Shortcuts in Terminal Replace docker with podman Using xmonad with xfce4 A RC script for freebsd frpc 自己动手写一个k8s controller AI 会取代你的(编程)岗位吗? 自建DERP服务器提升Tailscale连接速度(使用Nginx转发) 自动升级Docker容器 再读《程序员修炼之道-从小工到专家》 让浏览器下载文件 再读《软件随想录》/《黑客与画家》/《软技能》 HTTP 压力测试中的 Coordinated Omission 2的补码 编程语言中的 context 是什么? flutter macOS 构建出错 Flatpak 使用小记 Golang CAS 操作是怎么实现的 PostgreSQL 当MQ来使用 Clash 结合 工作VPN 的网络设计 使用 PostgreSQL 搭建 JuiceFS PostgreSQL 配置优化和日志分析 有GitHub Copilot?那就可以搭建你的ChatGPT4服务 窗口函数的使用(以PG为例) 读《为什么学生不喜欢上学》 OpenAI Prompt Engineering 摘录和总结 读《打造真正的新产品》 VueJS 总结 Linux 自动挂载 alist 提供的webdav FreeBSD 使用 vm-bhyve 安装Debian虚拟机 FreeBSD 和 Linux 网卡聚合实现提速 GPT 帮我搞定了时区转换问题 长任务系统如何处理? macOS/Linux 编译 InputLeap 使用开源软KVM - synergy-core 解决 macOS 终端hostname一直变化问题 KVM 共享 Intel 集成显卡 PromQL 备忘 读《格鲁夫给经理人的第一课》 读《打开心智》 为什么要把复杂的联表操作拆成多个单表查询? 红包系统的设计 MySQL Index Condition Pushdown Optimization Go mod 简明教程 OpenWRT 使用 Android/iOS USB 网络 搭建旁路由 Golang gRPC 错误处理 编写可维护的单元测试代码 OAuth 2 详解(六):Authorization Code Flow with PKCE OAuth 2 详解(五):Device Authorization Flow OAuth 2 详解(三):Resource Owner Password Credentials Grant OAuth 2 详解(四):Client Credentials Flow OAuth 2 详解(二):Implict Grant Flow OAuth 2 详解(一):简介及 Authorization Code 模式 ElasticSearch 学习笔记 三种git流程以及发版模型 错误处理实践 权限模型(RBAC/ABAC) OIDC(OpenID Connect) 简介 任务队列简介 PostgreSQL 操作笔记 使用Drone CI构建CI/CD系统 Golang migrate 做数据库变更管理 使用PostgreSQL做搜索引擎 Nginx 源码阅读(三): 连接池、内存池 Nginx 源码阅读(二): 请求处理 Nginx 源码阅读(一): 启动流程 Go 泛型简明教程 KVM 显卡穿透给 Windows 使用 HTTP Router 处理 Telegram Bot 按钮回调 使用反射(reflect)对结构体赋值 GIN 是如何绑定参数的 你好 2022(2021 年终总结) 用Go导入大型CSV到PostgreSQL 使用 OpenWRT 搭建软路由 使用软KVM切换器 barrier 共享键鼠 SQL 防注入及原理 使用 gomock 测试 Go 代码 gevent不是黑魔法(二): gevent 实现 gevent不是黑魔法(一): greenlet 实现 用 entgo 替代 gorm 应用内使用crontab不是那么方便 单测时要不要 mock 数据库? Sentry 自建指南 用selenium完成自动化任务 用闲置的安卓手机做垃圾电话短信过滤 推荐三个时间管理工具 一次事故反思 当JS遇到uint64:JS整数溢出问题 SQLite3 存储以及ACID原理 Redis源码阅读:pub/sub实现 Redis源码阅读:zset实现 Redis源码阅读:bitmap 位图的运算 Redis源码阅读:set是怎么做交并集运算的?
FreeBSD ipfw使用教程
Jiajun Huang · 2020-04-13 · via Jiajun的技术笔记

FreeBSD,古老的UNIX系统,最近在研究它的ipfw防火墙,鉴于国内相关资料较少,我就记录下来,以飨读者。

首先在FreeBSD 12中,ipfw已经默认编译进内核了,所以中文资料包括很多英文资料里,还需要编译的,就不用看了,那是过时的。

注意ipfw有一个比较坑的地方,那就是它默认会有一条规则,规则号为65536,是不可以删除的,这条规则会把所有流量都切断, 所以还没配置好之前,千万不要随意启动ipfw,否则就会面临无法连上远程FreeBSD的尴尬问题了。

$ sudo ipfw list
Password:
65535 deny ip from any to any

ipfw的规则是这样的,ipfw有一个规则编号,按照规则编号一次进行处理,所以由于最后一条是deny所有流量,就会产生刚才所说的 那个问题。

我们来看看ipfw的规则长啥样:

RULE FORMAT
     The format of firewall rules is the following:

           [rule_number] [set set_number] [prob match_probability] action
           [log [logamount number]] [altq queue] [{tag | untag} number] body

     where the body of the rule specifies which information is used for
     filtering packets, among the following:

        Layer-2 header fields                 When available
        IPv4 and IPv6 Protocol                SCTP, TCP, UDP, ICMP, etc.
        Source and dest. addresses and ports
        Direction                             See Section PACKET FLOW
        Transmit and receive interface        By name or address
        Misc. IP header fields                Version, type of service,
                                              datagram length, identification,
                                              fragment flag (non-zero IP
                                              offset), Time To Live
        IP options
        IPv6 Extension headers                Fragmentation, Hop-by-Hop
                                              options, Routing Headers, Source
                                              routing rthdr0, Mobile IPv6
                                              rthdr2, IPSec options.
        IPv6 Flow-ID
        Misc. TCP header fields               TCP flags (SYN, FIN, ACK, RST,
                                              etc.), sequence number,
                                              acknowledgment number, window
        TCP options
        ICMP types                            for ICMP packets
        ICMP6 types                           for ICMP6 packets
        User/group ID                         When the packet can be
                                              associated with a local socket.
        Divert status                         Whether a packet came from a
                                              divert socket (e.g., natd(8)).
        Fib annotation state                  Whether a packet has been tagged
                                              for using a specific FIB
                                              (routing table) in future
                                              forwarding decisions.

  • rule_number 是从1到65536的一个数字,这些规则会按照这个数值从小到大依次检查,如果规则号相同,就会按照在文件中的顺序检查
  • set, tag, untag, altqprob 不管,prob 是用来随机丢包用的
  • log 表示是否打日志
  • action 是我们要对流量采取的行动,比如 deny, allow
  • body 就是我们的具体规则,它的语法是 [proto from src to dst] [options]

我们来看一个具体的规则:

ipfw add 100 allow ip from not 1.2.3.4 to any,其中 ip 这里是协议,可选值是 ip, tcp, udp,而 src 和 dst 可以是 一个ip地址,一个网络号,也可以是 any,比如通常我们不想把本机出去的流量给掐掉,那么就加上这么一句:

ipfw -q add 110 allow all from any to any out

注意,src 和 dst 都可以加一个具体的端口号,比如我们要允许别的机器可以访问22:

ipfw -q add 130 allow tcp from any to any 22 in

配置ipfw

看完了规则的语法要求,我们来看看该怎么配置ipfw,执行以下命令:

$ sudo sysrc firewall_enable="YES"  # 允许防火墙开机自启
$ sudo sysrc firewall_type="open"  # 让系统把流量通过,这样就可以使用防火墙
$ sudo sysrc firewall_script="/etc/ipfw.rules"  # 制定ipfw规则的路径,我们待会儿在这里编辑规则
$ sudo sysrc firewall_logging="YES"  # 这样ipfw就可以打日志
$ sudo sysrc firewall_logif="YES"  # 把日志打到 `ipfw0` 这个设备里

然后编辑 /etc/ipfw.rules

IPF="ipfw -q add"
ipfw -q -f flush

#loopback 
$IPF 10 allow all from any to any via lo0
$IPF 20 deny all from any to 127.0.0.0/8
$IPF 30 deny all from 127.0.0.0/8 to any
$IPF 40 deny tcp from any to any frag

# statefull
$IPF 50 check-state
$IPF 60 allow tcp from any to any established
$IPF 70 allow all from any to any out keep-state
$IPF 80 allow icmp from any to any

# open port for ssh
$IPF 110 allow all from any to any out
$IPF 130 allow tcp from any to any 22 in

# deny and log everything 
$IPF 500 deny log all from any to any

最后,启动ipfw:

$ sudo service ipfw start

参考资料: