RDP 协议
解析微软 RDP 远程桌面协议的 3389 端口 TPKT+X.224 连接请求帧结构、TLS 与网络级身份验证(NLA)安全机制、键鼠输入与增量屏幕更新以及 VNC/SSH 替代对比。
学完这一章,你能够
- 解析微软 RDP 远程桌面协议的 3389 端口 TPKT+X.224 连接请求帧结构、TLS 与网络级身份验证(NLA)安全机制、键鼠输入与增量屏幕更新以及 VNC/SSH 替代对比。
RDP 定位与 3389 端口连接会话流程
RDP(Remote Desktop Protocol,远程桌面协议,规范见 MS-RDPBCGR)是微软开发的图形化远程桌面控制协议。它基于客户端-服务器架构,经由加密通道双向传递键盘鼠标输入、远端桌面画面更新及音频与外设重定向数据,让用户如同坐在远程主机前一样操作完整图形桌面。
建立连接时,客户端向服务器默认的 TCP `3389` 端口发送封装在 TPKT(RFC 1006)与 ITU-T X.224 Connection Request 中的初始协商帧;双方协商所采用的安全协议(如 TLS 或基于 CredSSP 的混合模式 NLA);随后服务器出示证书、客户端校验证书并协商加密套件,双方安全派生会话密钥,启用全程加密传输。
图形会话激活后,客户端持续将本地键盘扫描码与鼠标坐标事件封装为输入 PDU 发送给服务器,服务器渲染桌面后仅将发生变化的图形指令或脏矩形位图增量压缩回传给客户端。工作结束时,客户端发起断开请求,服务器清理虚拟通道并关闭或挂起当前桌面会话。
关键特性与典型应用场景
RDP 拥有五项显著优势:完整流畅的远程图形桌面访问与控制;内建 TLS/SSL 加密通信保护会话机密性;广泛的跨平台客户端生态,覆盖 Windows、macOS、Linux、Android 与 iOS;结合位图缓存、增量刷新与编解码压缩的高效带宽优化;以及在 Windows Server(配合远程桌面服务许可)上支持多用户并发独立桌面会话。
凭借高交互流畅度,RDP 广泛服务于企业员工居家远程办公、IT 运维团队远程桌面技术支持、Windows 服务器图形化运维管理以及远程教学实操演示等场景。
安全防护机制(NLA)与替代方案
由于远程桌面直接暴露操作系统交互入口,RDP 重点强化了三层安全机制:全程 TLS 加密传输防止窃听;结合口令或智能卡的身份验证;以及至关重要的网络级身份验证(NLA,Network Level Authentication)——它在服务器分配图形登录会话资源之前就先经由 CredSSP 完成用户鉴权,既显著收敛登录界面漏洞暴露面,也能有效缓解暴力破解与资源耗尽攻击。
在不同操作系统与网络环境下,可选用三类常见替代方案:开源跨平台的 VNC(Virtual Network Computing,默认 5900 端口,侧重帧缓冲传输,公网使用需叠加 TLS/SSH 加密);面向命令行高效运维的 SSH(22 端口);以及内置穿透中继、支持多平台的商业远程协助工具 TeamViewer。本站离线脚本完整打包解析了 TPKT(v3)+X.224 Connection Request(`0xE0`) 与 Connection Confirm(`0xD0`) 协商帧,并验证了键鼠输入/脏矩形带宽节省比例与 NLA 前置鉴权阻断逻辑。
离线解析 RDP TPKT+X.224 连接协商帧、键鼠输入与脏矩形位图增量更新及 NLA 防护
展开查看收起
"""Offline network protocol verification lab for Chapter 15 (rdp-protocol)."""
import json
import struct
VERSION = "CPython 3.12.14 offline network protocol frame & state-machine verifier"
def case_rdp_tpkt_x224_connection_request():
# RFC 1006 TPKT (4B) + ITU-T X.224 Connection Request TPDU (7B) + RDP Negotiation Request (8B, MS-RDPBCGR)
# requestedProtocols: 0x00000003 = PROTOCOL_SSL(0x01) | PROTOCOL_HYBRID_NLA(0x02)
rdp_neg_req = struct.pack("<BBHI", 0x01, 0x00, 8, 0x00000003)
x224_payload = b"Cookie: mstshash=admin\r\n" + rdp_neg_req
x224_li = 6 + len(x224_payload)
x224_cr = struct.pack("!BBHHB", x224_li, 0xE0, 0x0000, 0x1234, 0x00) + x224_payload
total_len = 4 + len(x224_cr)
tpkt_frame = struct.pack("!BBH", 3, 0, total_len) + x224_cr
# Decode
tpkt_ver, tpkt_res, tpkt_len = struct.unpack("!BBH", tpkt_frame[:4])
u_li, u_code, u_dst, u_src, u_cls = struct.unpack("!BBHHB", tpkt_frame[4:11])
neg_type, neg_flags, neg_len, req_protos = struct.unpack("<BBHI", tpkt_frame[-8:])
expected = {
"default_rdp_port": 3389,
"tpkt_version": 3,
"tpkt_len_matches": True,
"x224_cr_code": "0xe0",
"neg_type_request": 1,
"tls_requested": True,
"nla_hybrid_requested": True,
}
actual = {
"default_rdp_port": 3389,
"tpkt_version": tpkt_ver,
"tpkt_len_matches": tpkt_res == 0 and tpkt_len == len(tpkt_frame) and u_li == len(x224_cr) - 1,
"x224_cr_code": f"0x{u_code:02x}",
"neg_type_request": neg_type,
"tls_requested": bool(req_protos & 0x01),
"nla_hybrid_requested": bool(req_protos & 0x02),
}
return {
"name": "rdp_tpkt_x224_connection_request",
"summary": "构造并解析 MS-RDPBCGR TPKT(v3)+X.224 Connection Request(0xE0)+RDP 协商请求(TLS/NLA)帧",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_rdp_security_protocol_negotiation():
# X.224 Connection Confirm (0xD0) carrying RDP Negotiation Response (type=0x02, selected=0x02 Hybrid NLA)
neg_rsp = struct.pack("<BBHI", 0x02, 0x01, 8, 0x00000002)
x224_cc = struct.pack("!BBHHB", 6 + len(neg_rsp), 0xD0, 0x1234, 0x5678, 0x00) + neg_rsp
frame = struct.pack("!BBH", 3, 0, 4 + len(x224_cc)) + x224_cc
u_code = frame[5]
rsp_type, rsp_flags, rsp_len, selected_proto = struct.unpack("<BBHI", frame[11:19])
proto_names = {0: "RDP_STANDARD", 1: "TLS", 2: "HYBRID_NLA", 4: "RDSTLS"}
expected = {
"x224_cc_code": "0xd0",
"rsp_type_confirm": 2,
"selected_protocol": "HYBRID_NLA",
"ext_client_data_supported": True,
}
actual = {
"x224_cc_code": f"0x{u_code:02x}",
"rsp_type_confirm": rsp_type,
"selected_protocol": proto_names.get(selected_proto, "UNKNOWN"),
"ext_client_data_supported": bool(rsp_flags & 0x01) and rsp_len == 8,
}
return {
"name": "rdp_security_protocol_negotiation",
"summary": "解析 X.224 Connection Confirm(0xD0) 中的 RDP 安全协议协商响应,确认选中 CredSSP/NLA",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_rdp_input_and_bitmap_update_pdu():
# Simplified RDP client input event (type=1 keyboard scancode, type=2 mouse move) & server dirty-rect bitmap update
kb_event = struct.pack("<BHH", 1, 0x001E, 0x0000) # scancode 0x1E ('A'), keydown
mouse_event = struct.pack("<BHH", 2, 640, 360)
et1, scan, flags = struct.unpack("<BHH", kb_event)
et2, mx, my = struct.unpack("<BHH", mouse_event)
full_frame_bytes = 1920 * 1080 * 4
dirty_rect = (100, 100, 356, 228) # 256 x 128 changed region
dirty_bytes = (dirty_rect[2] - dirty_rect[0]) * (dirty_rect[3] - dirty_rect[1]) * 4
savings_pct = round((1.0 - dirty_bytes / full_frame_bytes) * 100, 2)
expected = {
"kb_scancode": "0x001e",
"mouse_xy": [640, 360],
"dirty_rect_bytes": 131072,
"bandwidth_saved_over_98pct": True,
}
actual = {
"kb_scancode": f"0x{scan:04x}" if et1 == 1 and flags == 0 else "",
"mouse_xy": [mx, my] if et2 == 2 else [],
"dirty_rect_bytes": dirty_bytes,
"bandwidth_saved_over_98pct": savings_pct > 98.0,
}
return {
"name": "rdp_input_and_bitmap_update_pdu",
"summary": "解析客户端键鼠输入 PDU 与服务端脏矩形位图增量更新,验证局部刷新对带宽占用的优化",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_rdp_nla_and_remote_alternatives():
def connect_rdp(nla_enabled, cred_valid):
if nla_enabled and not cred_valid:
return {"session_created": False, "result": "REJECTED_BEFORE_SESSION"}
if cred_valid:
return {"session_created": True, "result": "DESKTOP_ACTIVE"}
return {"session_created": True, "result": "LOGIN_SCREEN_EXPOSED"}
blocked = connect_rdp(True, False)
allowed = connect_rdp(True, True)
alternatives = {
"RDP": {"port": 3389, "gui": True, "built_in_tls": True},
"VNC": {"port": 5900, "gui": True, "built_in_tls": False},
"SSH": {"port": 22, "gui": False, "built_in_tls": True},
}
expected = {
"nla_blocks_unauth_before_session": True,
"nla_allows_valid_user": True,
"rdp_port": 3389,
"alternative_count": 3,
}
actual = {
"nla_blocks_unauth_before_session": (not blocked["session_created"]) and blocked["result"] == "REJECTED_BEFORE_SESSION",
"nla_allows_valid_user": allowed["session_created"] and allowed["result"] == "DESKTOP_ACTIVE",
"rdp_port": alternatives["RDP"]["port"],
"alternative_count": len(alternatives),
}
return {
"name": "rdp_nla_and_remote_alternatives",
"summary": "验证网络级身份验证(NLA)在分配桌面会话前拦截未认证请求,并对比 RDP/VNC/SSH 特性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_rdp_tpkt_x224_connection_request(),
case_rdp_security_protocol_negotiation(),
case_rdp_input_and_bitmap_update_pdu(),
case_rdp_nla_and_remote_alternatives(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
未启用 NLA 时服务器会先创建完整的远端图形会话并向未认证连接呈现登录画面;启用 NLA 后客户端必须在会话分配之前先通过 CredSSP 完成身份认证,未通过者会被直接拒绝。