258 lines
9.4 KiB
Python
Executable File
258 lines
9.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# cmpp-analyze.py — CMPP 联调 pcap 分析器(Issue#3 排查装备;纯标准库零依赖)
|
|
#
|
|
# 输入:tcpdump -i any 抓的 pcap(含环形切片);输出:按 TCP 连接分组的 CMPP 时间线
|
|
# + 异常自动标记(status≠0/result≠0/RST/握手失败/未知命令)。
|
|
# 用法:python3 cmpp-analyze.py <file.pcap> [--pkts]
|
|
import struct
|
|
import sys
|
|
import time
|
|
|
|
|
|
def fmt_ts(ts):
|
|
t = time.localtime(ts)
|
|
return "%02d:%02d:%02d.%03d" % (t.tm_hour, t.tm_min, t.tm_sec, int((ts % 1) * 1000))
|
|
|
|
# ---- CMPP 2.0 命令字 ----
|
|
CMDS = {
|
|
0x00000001: "CONNECT", 0x80000001: "CONNECT_RESP",
|
|
0x00000002: "TERMINATE", 0x80000002: "TERMINATE_RESP",
|
|
0x00000003: "SUBMIT", 0x80000003: "SUBMIT_RESP",
|
|
0x00000004: "DELIVER", 0x80000004: "DELIVER_RESP",
|
|
0x00000008: "ACTIVE_TEST", 0x80000008: "ACTIVE_TEST_RESP",
|
|
}
|
|
CONN_STATUS = {0: "成功", 1: "结构非法", 2: "账号密码错", 3: "版本/企业代码错",
|
|
4: "IP白名单拒", 5: "账号停用", 8: "连接数超限"}
|
|
SUBMIT_RESULT = {0: "成功", 1: "结构错", 8: "流量控制", 9: "通道号前缀不匹配", 10: "业务拒绝(余额/审核)"}
|
|
|
|
|
|
def trim_zero(b):
|
|
return b.rstrip(b"\x00").decode("utf-8", "replace")
|
|
|
|
|
|
# ---- pcap 文件层(大小端/纳秒魔数自适应) ----
|
|
def read_pcap(path):
|
|
with open(path, "rb") as f:
|
|
gh = f.read(24)
|
|
if len(gh) < 24:
|
|
raise SystemExit("pcap 头不足")
|
|
magic = gh[:4]
|
|
if magic == b"\xd4\xc3\xb2\xa1":
|
|
end, nano = "<", False
|
|
elif magic == b"\xa1\xb2\xc3\xd4":
|
|
end, nano = ">", False
|
|
elif magic == b"\x4d\x3c\xb2\xa1":
|
|
end, nano = "<", True
|
|
elif magic == b"\xa1\xb2\x3c\x4d":
|
|
end, nano = ">", True
|
|
else:
|
|
raise SystemExit("非 pcap 文件(magic=%s)" % magic.hex())
|
|
link = struct.unpack(end + "I", gh[20:24])[0]
|
|
while True:
|
|
rh = f.read(16)
|
|
if len(rh) < 16:
|
|
return
|
|
ts_s, ts_f, incl, _orig = struct.unpack(end + "IIII", rh)
|
|
data = f.read(incl)
|
|
if len(data) < incl:
|
|
return
|
|
yield (ts_s + (ts_f / 1e9 if nano else ts_f / 1e6), link, data)
|
|
|
|
|
|
# ---- 链路层 → IP(自适应 -i any 的 SLL/SLL2/以太+VLAN/RAW) ----
|
|
def to_ip(link, data):
|
|
if link == 1: # Ethernet(含 VLAN 堆叠)
|
|
if len(data) < 14:
|
|
return None
|
|
eth, off = data[12:14], 14
|
|
while eth == b"\x81\x00" and len(data) >= off + 4:
|
|
eth, off = data[off + 2:off + 4], off + 4
|
|
return data[off:] if eth == b"\x08\x00" else None
|
|
if link == 101: # RAW IP
|
|
return data
|
|
if link == 113: # LINUX_SLL
|
|
if len(data) < 16 or struct.unpack(">H", data[14:16])[0] != 0x0800:
|
|
return None
|
|
return data[16:]
|
|
if link == 276: # LINUX_SLL2
|
|
if len(data) < 20 or struct.unpack(">H", data[0:2])[0] != 0x0800:
|
|
return None
|
|
return data[20:]
|
|
if link == 0: # NULL/LOOP
|
|
return data[4:] if len(data) > 4 else None
|
|
return None
|
|
|
|
|
|
def parse_ip(data):
|
|
if len(data) < 20 or (data[0] >> 4) != 4:
|
|
return None
|
|
ihl = (data[0] & 0xF) * 4
|
|
if len(data) < ihl or data[9] != 6: # 仅 TCP
|
|
return None
|
|
src = ".".join(str(b) for b in data[12:16])
|
|
dst = ".".join(str(b) for b in data[16:20])
|
|
return src, dst, data[ihl:]
|
|
|
|
|
|
def parse_tcp(data):
|
|
if len(data) < 20:
|
|
return None
|
|
sport, dport, seq = struct.unpack(">HHI", data[0:8])
|
|
off = (data[12] >> 4) * 4
|
|
return sport, dport, seq, data[13], data[off:]
|
|
|
|
|
|
# ---- 流重组:双向归并;载荷按 seq 排序去重拼接(容重传/重叠) ----
|
|
def stream_key(src, sport, dst, dport):
|
|
a, b = (src, sport), (dst, dport)
|
|
return (a, b) if a <= b else (b, a)
|
|
|
|
|
|
def reassemble(events):
|
|
streams = {}
|
|
for ts, key, d, seq, flags, payload in events:
|
|
st = streams.setdefault(key, {"first": ts, "last": ts,
|
|
"segs": [], "data": {0: [], 1: []}})
|
|
st["last"] = ts
|
|
st["segs"].append((ts, d, flags))
|
|
if payload:
|
|
st["data"][d].append((seq & 0xFFFFFFFF, payload))
|
|
for st in streams.values():
|
|
for d in (0, 1):
|
|
buf, expect = bytearray(), None
|
|
for seq, payload in sorted(st["data"][d], key=lambda x: x[0]):
|
|
if expect is None or seq == expect:
|
|
buf += payload
|
|
elif seq < expect:
|
|
skip = expect - seq
|
|
if skip < len(payload):
|
|
buf += payload[skip:]
|
|
else:
|
|
continue
|
|
else:
|
|
buf += payload # 缺块乱序:尽力拼接
|
|
expect = (seq + len(payload)) & 0xFFFFFFFF
|
|
st["data"][d] = bytes(buf)
|
|
return streams
|
|
|
|
|
|
# ---- CMPP PDU 切分与解码 ----
|
|
def split_pdus(buf):
|
|
out, off = [], 0
|
|
while off + 12 <= len(buf):
|
|
total = struct.unpack(">I", buf[off:off + 4])[0]
|
|
if total < 13 or total > 65535:
|
|
break
|
|
if off + total > len(buf):
|
|
break # 尾部截断(环形切片)
|
|
out.append((struct.unpack(">I", buf[off + 4:off + 8])[0],
|
|
struct.unpack(">I", buf[off + 8:off + 12])[0],
|
|
buf[off + 12:off + total]))
|
|
off += total
|
|
return out, buf[off:]
|
|
|
|
|
|
def dec_connect(body):
|
|
return "sp=%s ver=0x%02x ts=%d" % (trim_zero(body[0:6]), body[22],
|
|
struct.unpack(">I", body[23:27])[0])
|
|
|
|
|
|
def dec_connect_resp(body):
|
|
if len(body) >= 20:
|
|
st, layout = struct.unpack(">I", body[0:4])[0], "2.0(4B)"
|
|
ver = body[20] if len(body) > 20 else 0
|
|
else:
|
|
st, layout = (body[0] if body else -1), "3.0(1B)"
|
|
ver = body[17] if len(body) > 17 else 0
|
|
return "布局=%s(%dB) status=%d(%s) ver=0x%02x" % (
|
|
layout, len(body), st, CONN_STATUS.get(st, "?"), ver), st
|
|
|
|
|
|
def dec_pdu(cmd, body):
|
|
name = CMDS.get(cmd, "UNKNOWN_0x%08x" % cmd)
|
|
detail, bad = "", False
|
|
if cmd == 0x00000001 and len(body) >= 27:
|
|
detail = dec_connect(body)
|
|
elif cmd == 0x80000001:
|
|
detail, st = dec_connect_resp(body)
|
|
bad = st != 0
|
|
elif cmd == 0x00000003 and len(body) >= 140:
|
|
n = body[116]
|
|
ml = body[117 + 21 * n] if len(body) > 117 + 21 * n else -1
|
|
detail = "srcid=%s %d码 %dB udhi=%d" % (trim_zero(body[95:116]), n, ml, body[45])
|
|
elif cmd == 0x80000003 and len(body) >= 9:
|
|
r = body[8]
|
|
detail = "msgid=%d result=%d(%s)" % (struct.unpack(">Q", body[0:8])[0],
|
|
r, SUBMIT_RESULT.get(r, "?"))
|
|
bad = r != 0
|
|
elif cmd == 0x00000004 and len(body) >= 65:
|
|
detail = "src=%s report=%s" % (trim_zero(body[42:63]), "是" if body[63] == 1 else "否")
|
|
return name, detail, bad
|
|
|
|
|
|
def flags_str(f):
|
|
s = ""
|
|
if f & 0x02: s += "SYN "
|
|
if f & 0x01: s += "FIN "
|
|
if f & 0x04: s += "RST "
|
|
return s.strip()
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
raise SystemExit("用法: cmpp-analyze.py <file.pcap> [--pkts]")
|
|
show_pkts, path = "--pkts" in sys.argv, sys.argv[1]
|
|
events, skipped = [], 0
|
|
for ts, link, data in read_pcap(path):
|
|
ip = to_ip(link, data)
|
|
if not ip:
|
|
continue
|
|
pr = parse_ip(ip)
|
|
if not pr:
|
|
continue
|
|
src, dst, tcp = pr
|
|
tp = parse_tcp(tcp)
|
|
if not tp:
|
|
continue
|
|
sport, dport, seq, flags, payload = tp
|
|
if sport != 7890 and dport != 7890:
|
|
skipped += 1
|
|
continue
|
|
key = stream_key(src, sport, dst, dport)
|
|
events.append((ts, key, 0 if (src, sport) == key[0] else 1, seq, flags, payload))
|
|
if not events:
|
|
print("无 7890 端口流量(过滤非 7890 包 %d 个)——确认客户端目标地址/端口。" % skipped)
|
|
return
|
|
streams = reassemble(events)
|
|
print("=" * 96)
|
|
print("pcap=%s | 连接数=%d | %.3f ~ %.3f" % (path, len(streams),
|
|
events[0][0], max(e[0] for e in events)))
|
|
print("=" * 96)
|
|
for i, (key, st) in enumerate(sorted(streams.items(), key=lambda x: x[1]["first"]), 1):
|
|
(s1, p1), (s2, p2) = key
|
|
print("\n[连接%d] %s:%d ⇄ %s:%d" % (i, s1, p1, s2, p2))
|
|
for d in (0, 1):
|
|
pdus, tail = split_pdus(st["data"][d])
|
|
a = (s1, p1) if d == 0 else (s2, p2)
|
|
b = (s2, p2) if d == 0 else (s1, p1)
|
|
t0 = next((t for t, dd, _ in st["segs"] if dd == d), st["first"])
|
|
for cmd, sq, body in pdus:
|
|
name, detail, bad = dec_pdu(cmd, body)
|
|
print(" %s %s:%d→%s:%d %-14s seq=%-6d %s%s" % (
|
|
fmt_ts(t0), a[0], a[1], b[0], b[1], name, sq, detail, " ⚠️" if bad else ""))
|
|
if len(tail) >= 4:
|
|
print(" %s %s:%d→%s:%d (尾段 %dB 未成完整 PDU)" % (fmt_ts(t0), a[0], a[1], b[0], b[1], len(tail)))
|
|
if show_pkts:
|
|
for ts, d, fl in st["segs"]:
|
|
fs = flags_str(fl)
|
|
if fs:
|
|
side = key[0] if d == 0 else key[1]
|
|
print(" %s TCP %-10s %s:%d" % (fmt_ts(ts), fs, side[0], side[1]))
|
|
print("\n" + "=" * 96)
|
|
print("⚠️=拒绝/异常码;CONNECT_RESP 布局自动判定(2.0=21B/3.0=18B)。")
|
|
print("客户端报-1:先看 CONNECT_RESP.status 及布局是否匹配其版本;无本机流量=地址/端口未指向本平台。")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|