Blog

Default Policy Rules and the First Matching Rule Wins

Content #

Within iptables, the default policies appear to be exceptions to the first-matching-rule-wins scenario. The default policy commands are not position dependent. They aren’t rules, per se. A chain’s default policy is applied after a packet has been compared to each rule on the chain without a match. This is notably different for nftables where the first matching rule always wins and there is no default policy.

For nftables a drop rule for incoming traffic can be added to the end of the chain and a reject rule can be added to the end of the OUTPUT filter chain. This will have the same overall effect as the iptables default policies. But it’s important to note that these rules should be added at the end of the firewall script and only after other rules to allow traffic have been created above them. Otherwise all traffic will be dropped or rejected from the computer where the firewall is running, including possibly your SSH session for configuring the firewall!

...

0.0.0.0/0(nftables)

Content #

0.0.0.0/0 在 nftables 规则中(合法)

nft add rule inet filter input ip saddr 0.0.0.0/0 accept

这表示:

接受所有 IPv4 地址 作为源地址(即,不管是 192.168.1.1 还是 8.8.8.8,都匹配)。不会真正匹配 0.0.0.0,因为 0.0.0.0 不能作为源地址出现在数据包中。

From #

不可能的目标地址(Impossible Destination Addresses)

Content #

地址 0.0.0.0 #

原因:0.0.0.0 通常表示“默认路由”或“未知地址”,不能用作数据包的目的地。

网络中的主机 0(host 0 on any network) #

例如 192.168.1.0。原因:通常 192.168.1.0 代表整个子网,而不是一个单独的主机 IP。

127.0.0.0/8 网段的任何主机 #

例如 127.1.2.3。原因:回环地址仅用于本机测试,不应该作为数据包的目标地址。

Class E 预留地址 #

例如 250.1.2.3。原因:这类地址不是用于公共互联网通信的,任何试图访问 Class E 地址的数据包都应被标记为异常。

From #

不可能的源地址(Impossible Source Addresses)

Content #

多播地址(Multicast Addresses) #

IP 地址范围:224.0.0.0 - 239.255.255.255 原因:多播地址用于组通信,不可能作为源地址 出现在普通数据包中。

广播地址(Broadcast Addresses) #

例如 255.255.255.255 或子网广播地址(如 192.168.1.255)。原因:广播地址用于向多个设备发送数据,不可能是单个设备的源地址。

0.0.0.0/8 网段 #

例如 0.1.2.3。原因:0.0.0.0 用于标识“未指定地址”或“本地网络”,不能作为源地址出现在正常通信中。 Address 0.0.0.0/8

127.0.0.0/8(Loopback) #

例如 127.0.0.1。原因:127.0.0.0/8 用于本地回环(loopback),意味着数据包永远不会通过网络传输,如果收到此类源地址的数据包,可能是伪造的。

Class E 预留地址 #

地址范围:240.0.0.0 - 255.255.255.255 原因:Class E 地址保留用于未来用途,不应该在实际网络流量中出现。

From #

rp_filter(Reverse Path Filtering,反向路径过滤)

Content #

rp_filter 依据 RFC 1812(IPv4 路由器要求)第 5.3.8 节的规定,对入站数据包的源地址进行验证。其核心规则如下:

  1. 检查入站数据包的源 IP 地址:

假设一个数据包从 接口 eth0 进入主机(或路由器),其 源地址为 192.168.1.100。

  1. 模拟查找该源地址的回程路径(Reverse Path Lookup,RPF):

设备检查本地 路由表,如果 主机需要向 192.168.1.100 发送数据包,它会选择哪个接口?假如根据路由表,去往 192.168.1.100 的数据包 应该从 eth1 发出,而不是 eth0,那么就说明该数据包的源 IP 可能是伪造的。

  1. 如果入站接口和预期出站接口不匹配,则丢弃数据包:

这种不匹配可能意味着该数据包的源地址被篡改(即来源欺骗)。在这种情况下,rp_filter 会 默默地丢弃该数据包(即不发送任何 ICMP 错误信息)。

作用 #

  1. 防止 IP 欺骗攻击:

攻击者可能伪造源 IP 地址来绕过访问控制或发动 DDoS 攻击,rp_filter 机制可以检测到不匹配的流量并丢弃它们。

  1. 提高网络安全性:

RFC 1812 建议 默认启用 该功能,以便路由器能够主动防御基于源地址欺骗的攻击,如 Smurf 攻击 和 反射放大攻击。

for f in /proc/sys/net/ipv4/conf/*/rp_filter; do
    echo "1" > $f
done

From #

ICMP redirect messages

Content #

ICMP redirect messages are sent to hosts by their adjacent routers. Their purpose is to inform the host that a shorter path is available. That is, the host and both routers are on the same network, and the new router is the router to which the original would send the packet as its next hop.

Routers generate redirect messages for hosts; hosts do not. Hosts are required to honor redirects and add the new gateway to their route cache, except in the cases indicated in RFC 1122, “Requirements for Internet Hosts—Communication Layers,” Section 3.2.2.2: “A Redirect message SHOULD be silently discarded if the new gateway address it specifies is not on the same connected (sub-) net through which the Redirect arrived [INTRO:2, Appendix A], or if the source of the Redirect is not the current first-hop gateway for the specified destination (see Section 3.3.1).”

...

sub:org-roam

Content #

org-roam.db数据库,nodes表。

sqlite> select id, length(id), hex(id) from nodes limit 1;
"000facb4-7bd9-4cfe-bf3c-f0d027263281"|38|2230303066616362342D376264392D346366652D626633632D66306430323732363332383122

sqlite> select id, file from nodes where trim(id) = '"000facb4-7bd9-4cfe-bf3c-f0d027263281"';
"000facb4-7bd9-4cfe-bf3c-f0d027263281"|"/home/luyanfei/kbase/20230125181201-做决策前先征询意见.org"

双引号是作为数据存储在id字段中的,非常古怪。

From #

net.ipv4.ip_forward

Content #

当net.ipv4.ip_forward的值为1时,表示允许IP数据包在不同的网络接口之间转发。这意味着Linux系统能够作为一台路由器使用,接收一个网络接口上的IP数据包,并根据路由信息将其发送到另一个网络接口,以便数据包能够到达目标主机。

From #