蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线解析 RFC 903 28 字节 RARP 反向请求/应答帧,校验广播域限制与 BOOTP/DHCP 替代对比

PYTHONWindows x64、CPython 3.12.14 离线网络协议帧编解码与状态机验证脚本(纯本地内存执行,未发起外部网络连接)。explore-rarp-protocol.py
explore-rarp-protocol.py182 行
"""Offline network protocol verification lab for Chapter 21 (rarp-protocol)."""
import ipaddress
import json
import struct

VERSION = "CPython 3.12.14 offline network protocol frame & state-machine verifier"

RARP_FMT = "!HHBBH6s4s6s4s"
ETHERTYPE_ARP = 0x0806
ETHERTYPE_RARP = 0x8035


def mac_to_bytes(mac: str) -> bytes:
    return bytes(int(x, 16) for x in mac.split(":"))


def bytes_to_mac(raw: bytes) -> str:
    return ":".join(f"{b:02x}" for b in raw)


def pack_rarp(oper: int, sha: str, spa: str, tha: str, tpa: str) -> bytes:
    return struct.pack(
        RARP_FMT,
        1,
        0x0800,
        6,
        4,
        oper,  # 3=RARP Request, 4=RARP Reply
        mac_to_bytes(sha),
        ipaddress.IPv4Address(spa).packed,
        mac_to_bytes(tha),
        ipaddress.IPv4Address(tpa).packed,
    )


def unpack_rarp(pkt: bytes) -> dict:
    htype, ptype, hlen, plen, oper, sha, spa, tha, tpa = struct.unpack(RARP_FMT, pkt)
    return {
        "htype": htype,
        "ptype": ptype,
        "hlen": hlen,
        "plen": plen,
        "oper": oper,
        "sha": bytes_to_mac(sha),
        "spa": str(ipaddress.IPv4Address(spa)),
        "tha": bytes_to_mac(tha),
        "tpa": str(ipaddress.IPv4Address(tpa)),
    }


def case_rarp_28byte_reverse_lookup():
    client_mac = "02:00:5e:10:20:30"
    server_mac = "02:00:5e:00:00:01"
    server_ip = "10.0.5.1"
    rarp_table = {client_mac: "10.0.5.42"}

    # Diskless workstation broadcasts RARP Request (OPER=3) with its own MAC in both SHA and THA
    req = pack_rarp(3, client_mac, "0.0.0.0", client_mac, "0.0.0.0")
    req_d = unpack_rarp(req)
    assigned_ip = rarp_table[req_d["tha"]]

    # RARP server unicasts RARP Reply (OPER=4) with assigned TPA
    rep = pack_rarp(4, server_mac, server_ip, req_d["tha"], assigned_ip)
    rep_d = unpack_rarp(rep)

    expected = {
        "ethertype_hex": "0x8035",
        "packet_bytes": 28,
        "req_oper": 3,
        "rep_oper": 4,
        "assigned_ip": "10.0.5.42",
        "unicast_dst_mac": client_mac,
    }
    actual = {
        "ethertype_hex": hex(ETHERTYPE_RARP),
        "packet_bytes": len(req),
        "req_oper": req_d["oper"],
        "rep_oper": rep_d["oper"],
        "assigned_ip": rep_d["tpa"],
        "unicast_dst_mac": rep_d["tha"],
    }
    return {
        "name": "rarp_28byte_reverse_lookup",
        "summary": "打包并解析 RFC 903 28 字节 RARP 反向请求(OPER=3)与单播应答(OPER=4)实现无盘站 MAC→IP 映射",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_rarp_unknown_mac_and_spoof_risk():
    rarp_table = {"02:00:5e:10:20:30": "10.0.5.42"}
    unknown_mac = "02:00:5e:99:99:99"
    has_mapping = unknown_mac in rarp_table

    # RARP has no authentication: a rogue responder can answer first with an invalid IP
    rogue_rep = unpack_rarp(pack_rarp(4, "de:ad:00:00:00:01", "10.0.5.99", "02:00:5e:10:20:30", "10.0.5.250"))
    expected = {
        "unknown_mac_found": False,
        "rarp_has_auth_field": False,
        "rogue_reply_oper": 4,
        "rogue_assigned_ip": "10.0.5.250",
    }
    actual = {
        "unknown_mac_found": has_mapping,
        "rarp_has_auth_field": False,
        "rogue_reply_oper": rogue_rep["oper"],
        "rogue_assigned_ip": rogue_rep["tpa"],
    }
    return {
        "name": "rarp_unknown_mac_and_spoof_risk",
        "summary": "验证未登记 MAC 查询无结果以及 RARP 协议缺少认证字段易遭伪造应答欺骗的局限",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_rarp_l2_broadcast_domain_limit():
    # RARP runs directly over Ethernet (L2 EtherType 0x8035), so L3 routers do not forward it across subnets
    router_forwards_l2_broadcast = False
    subnets = ["VLAN10", "VLAN20", "VLAN30"]
    required_rarp_servers = len(subnets)
    required_dhcp_servers_with_relay = 1

    expected = {
        "router_forwards_rarp": False,
        "rarp_servers_needed_for_3_subnets": 3,
        "dhcp_servers_needed_with_relay": 1,
    }
    actual = {
        "router_forwards_rarp": router_forwards_l2_broadcast,
        "rarp_servers_needed_for_3_subnets": required_rarp_servers,
        "dhcp_servers_needed_with_relay": required_dhcp_servers_with_relay,
    }
    return {
        "name": "rarp_l2_broadcast_domain_limit",
        "summary": "对比链路层 RARP 无法跨路由广播域部署与 DHCP 中继集中分配在多子网中的运维差异",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_rarp_vs_bootp_dhcp_capabilities():
    matrix = {
        "RARP": {"layer": "L2", "ip": True, "mask": False, "gateway": False, "dns": False, "dynamic_lease": False},
        "BOOTP": {"layer": "L7/UDP", "ip": True, "mask": True, "gateway": True, "dns": True, "dynamic_lease": False},
        "DHCP": {"layer": "L7/UDP", "ip": True, "mask": True, "gateway": True, "dns": True, "dynamic_lease": True},
    }
    expected = {
        "rarp_only_ip": True,
        "bootp_provides_full_static_cfg": True,
        "dhcp_adds_dynamic_lease": True,
    }
    actual = {
        "rarp_only_ip": matrix["RARP"]["ip"] and not (matrix["RARP"]["mask"] or matrix["RARP"]["gateway"] or matrix["RARP"]["dns"]),
        "bootp_provides_full_static_cfg": matrix["BOOTP"]["mask"] and matrix["BOOTP"]["gateway"] and not matrix["BOOTP"]["dynamic_lease"],
        "dhcp_adds_dynamic_lease": matrix["DHCP"]["dynamic_lease"],
    }
    return {
        "name": "rarp_vs_bootp_dhcp_capabilities",
        "summary": "核对 RARP、BOOTP 与 DHCP 在网络配置项完整度与动态租期管理上的演进替代关系",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_rarp_28byte_reverse_lookup(),
        case_rarp_unknown_mac_and_spoof_risk(),
        case_rarp_l2_broadcast_domain_limit(),
        case_rarp_vs_bootp_dhcp_capabilities(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线网络协议帧编解码与状态机验证脚本(纯本地内存执行,未发起外部网络连接)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线网络协议帧编解码与状态机验证脚本(纯本地内存执行,未发起外部网络连接)。

入口文件:projects/np-rarp-protocol-lab/explore-rarp-protocol.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-rarp-protocol.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线网络协议帧编解码与状态机验证脚本(纯本地内存执行,未发起外部网络连接)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:网络协议

课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a2722.zip

下载课程关联工程包:网络协议

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2740.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习