当前位置: 首页 > news >正文

tcpdump源码分析

进入tcpdump.c(函数入口)之前,先看一些头文件netdissect.h里定义了一个数据结构struct netdissect_options来描述tcdpump支持的所有参数动作,每一个参数有对应的flag, 在tcpdump 的main 里面, 会根据用户的传入的参数来增加相应flag数值, 最后根据这些flag数值来实现特定动作。各个参数含义请参考源代码注释。

1.netdissect.h 头文件

struct netdissect_options {int ndo_bflag;              /* 以 ASDOT 表示法打印 4 字节 AS 号 */int ndo_eflag;              /* 打印以太网头 */int ndo_fflag;              /* 不翻译“外部”IP 地址 */int ndo_Kflag;              /* 不检查 IP、TCP 或 UDP 校验和 */int ndo_nflag;              /* 将地址显示为数字形式 */int ndo_Nflag;              /* 打印主机名时去掉域名 */int ndo_qflag;              /* 简短输出 */int ndo_Sflag;              /* 打印原始 TCP 序列号 */int ndo_tflag;              /* 打印数据包到达时间 */int ndo_uflag;              /* 打印未解码的 NFS 句柄 */int ndo_vflag;              /* 冗余级别 */int ndo_xflag;              /* 以十六进制打印数据包 */int ndo_Xflag;              /* 以十六进制/ASCII 打印数据包 */int ndo_Aflag;              /* 仅以 ASCII 打印数据包,并将 TAB、LF、CR 和 SPACE 视为图形字符 */int ndo_Hflag;              /* 解析 802.11s 草案网状标准 */const char *ndo_protocol;   /* 协议 */jmp_buf ndo_early_end;      /* 用于 setjmp()/longjmp() 的 jmp_buf */void *ndo_last_mem_p;       /* 指向最后分配的内存块的指针 */int ndo_packet_number;      /* 在行首打印数据包编号 */int ndo_print_sampling;     /* 打印每 N 个数据包 */int ndo_suppress_default_print; /* 对未知的数据包类型不使用 default_print() */int ndo_tstamp_precision;   /* 请求的时间戳精度 */const char *program_name;   /* 使用该库的程序名称 */char *ndo_espsecret;               /* ESP 密钥 */struct sa_list *ndo_sa_list_head;  /* 由 print-esp.c 使用 */struct sa_list *ndo_sa_default;char *ndo_sigsecret;        /* 签名验证密钥 */int ndo_packettype;         /* 由 -T 指定的数据包类型 */int ndo_snaplen;            /* 抓取长度 */int ndo_ll_hdr_len;         /* 链路层头长度 *//* 全局指针,指向当前数据包的开始和结束(在打印过程中) */const u_char *ndo_packetp;const u_char *ndo_snapend;/* 保存的数据包边界和缓冲区信息的堆栈 */struct netdissect_saved_packet_info *ndo_packet_info_stack;/* 指向 if_printer 函数的指针 */if_printer ndo_if_printer;/* 指向输出内容的 void 函数的指针 */void (*ndo_default_print)(netdissect_options *,const u_char *bp, u_int length);/* 指向执行常规输出的函数的指针 */int  (*ndo_printf)(netdissect_options *,const char *fmt, ...)PRINTFLIKE_FUNCPTR(2, 3);/* 指向输出错误的函数的指针 */void NORETURN_FUNCPTR (*ndo_error)(netdissect_options *,status_exit_codes_t status,const char *fmt, ...)PRINTFLIKE_FUNCPTR(3, 4);/* 指向输出警告的函数的指针 */void (*ndo_warning)(netdissect_options *,const char *fmt, ...)PRINTFLIKE_FUNCPTR(2, 3);
};

字段解释:

  • 标志位

    • ndo_bflag: 决定是否以 ASDOT 表示法打印 4 字节 AS 号。
    • ndo_eflag: 决定是否打印以太网头。
    • ndo_fflag: 决定是否不翻译“外部”IP 地址。
    • ndo_Kflag: 决定是否不检查 IP、TCP 或 UDP 校验和。
    • ndo_nflag: 决定是否将地址保留为数字形式。
    • ndo_Nflag: 决定是否在打印主机名时去掉域名。
    • ndo_qflag: 决定是否进行简短输出。
    • ndo_Sflag: 决定是否打印原始 TCP 序列号。
    • ndo_tflag: 决定是否打印数据包到达时间。
    • ndo_uflag: 决定是否打印未解码的 NFS 句柄。
    • ndo_vflag: 设置冗余级别。
    • ndo_xflag: 决定是否以十六进制打印数据包。
    • ndo_Xflag: 决定是否以十六进制和 ASCII 打印数据包。
    • ndo_Aflag: 决定是否仅以 ASCII 打印数据包,并将 TAB、LF、CR 和 SPACE 视为图形字符。
    • ndo_Hflag: 决定是否解析 802.11s 草案网状标准。
  • 其他选项

    • ndo_protocol: 指定的协议。
    • ndo_early_end: 用于 setjmp()/longjmp() 的跳转缓冲区。
    • ndo_last_mem_p: 指向最后分配的内存块的指针。
    • ndo_packet_number: 决定是否在行首打印数据包编号。
    • ndo_print_sampling: 打印每 N 个数据包。
    • ndo_suppress_default_print: 决定是否对未知的数据包类型不使用 default_print()。
    • ndo_tstamp_precision: 请求的时间戳精度。
    • program_name: 使用该库的程序名称。
  • ESP 和签名相关

    • ndo_espsecret: ESP 密钥。
    • ndo_sa_list_head, ndo_sa_default: 由 print-esp.c 使用的 SA 列表。
    • ndo_sigsecret: 签名验证密钥。
  • 数据包和链接层相关

    • ndo_packettype: 由 -T 指定的数据包类型。
    • ndo_snaplen: 抓取长度。
    • ndo_ll_hdr_len: 链路层头长度。
    • ndo_packetp, ndo_snapend: 当前数据包的开始和结束指针。
    • ndo_packet_info_stack: 保存的数据包边界和缓冲区信息的堆栈。
  • 函数指针

    • ndo_if_printer: 指向 if_printer 函数的指针。
    • ndo_default_print: 指向输出内容的 void 函数的指针。
    • ndo_printf: 指向执行常规输出的函数的指针。
    • ndo_error: 指向输出错误的函数的指针。
    • ndo_warning: 指向输出警告的函数的指针。

此次需要关注的是 “ndo_snaplen: 抓取长度”变量。

2.从指针安全读取x字节的相关宏定义

在 print-eigrp.c 中有如下代码:

GET_BE_U_2(eigrp_com_header->checksum),

 继续追踪到 extract.h 文件:

#define GET_BE_U_2(p) get_be_u_2(ndo, (const u_char *)(p))

  继续追踪,仍在 extract.h 文件中:get_be_u_2 可见是一个内联函数

static inline uint16_t
get_be_u_2(netdissect_options *ndo, const u_char *p)
{if (!ND_TTEST_2(p))nd_trunc_longjmp(ndo);return EXTRACT_BE_U_2(p);
}

这个内联函数 get_be_u_2 做了以下几件事:

  1. 调用 ND_TTEST_2(p) 来检查是否可以从指针 p 安全地读取 2 个字节。
  2. 如果检查失败,调用 nd_trunc_longjmp(ndo) 进行错误处理,通常会跳转到某个提前定义的错误处理位置。
  3. 如果检查成功,调用 EXTRACT_BE_U_2(p) 从指针 p 提取 2 个字节的数据,并将其从网络字节顺序转换为主机字节顺序。

继续追踪 ND_TTEST_2 宏函数,仍在 extract.h 文件中:

#define ND_TTEST_2(p) ND_TTEST_LEN((p), 2)
//这个宏调用 ND_TTEST_LEN,传递参数 p 和长度 2,表示要检查从指针 p 开始的 2 个字节。 

继续追踪 ND_TTEST_LEN 宏函数,跳转到 netdissect.h 文件中:


/** True if "l" bytes from "p" were captured.** The "ndo->ndo_snapend - (l) <= ndo->ndo_snapend" checks to make sure* "l" isn't so large that "ndo->ndo_snapend - (l)" underflows.** The check is for <= rather than < because "l" might be 0.** We cast the pointers to uintptr_t to make sure that the compiler* doesn't optimize away any of these tests (which it is allowed to* do, as adding an integer to, or subtracting an integer from, a* pointer assumes that the pointer is a pointer to an element of an* array and that the result of the addition or subtraction yields a* pointer to another member of the array, so that, for example, if* you subtract a positive integer from a pointer, the result is* guaranteed to be less than the original pointer value). See**	https://www.kb.cert.org/vuls/id/162289*//** Test in two parts to avoid these warnings:* comparison of unsigned expression >= 0 is always true [-Wtype-limits],* comparison is always true due to limited range of data type [-Wtype-limits].*//** 如果从 "p" 开始的 "l" 字节被捕获,则返回真。** 通过 "ndo->ndo_snapend - (l) <= ndo->ndo_snapend" 的检查来确保* "l" 不会大到使 "ndo->ndo_snapend - (l)" 发生下溢。** 检查使用 <= 而不是 < 是因为 "l" 可能为 0。** 我们将指针转换为 uintptr_t 是为了确保编译器不会优化掉这些测试* (编译器被允许这样做,因为将整数加到指针上,或从指针上减去整数,* 假定指针是一个数组元素的指针,并且加法或减法的结果会生成另一个数组成员的指针,* 因此,例如,如果从指针上减去一个正整数,结果保证小于原始指针值)。参见**	https://www.kb.cert.org/vuls/id/162289*//** 分两部分测试以避免这些警告:* unsigned 表达式的比较总是大于等于 0 是始终为真的 [-Wtype-limits],* 由于数据类型的有限范围,比较总是为真 [-Wtype-limits]。*/#define IS_NOT_NEGATIVE(x) (((x) > 0) || ((x) == 0))#define ND_TTEST_LEN(p, l) \(IS_NOT_NEGATIVE(l) && \((uintptr_t)ndo->ndo_snapend - (l) <= (uintptr_t)ndo->ndo_snapend && \(uintptr_t)(p) <= (uintptr_t)ndo->ndo_snapend - (l)))
  1. 这个宏检查以下几个条件:

    • IS_NOT_NEGATIVE(l):长度 l 必须是非负数。
    • ((uintptr_t)ndo->ndo_snapend - (l) <= (uintptr_t)ndo->ndo_snapend):确保 ndo->ndo_snapend 减去 l 不会发生下溢。
    • ((uintptr_t)(p) <= (uintptr_t)ndo->ndo_snapend - (l)):确保指针 p 不会越过数据包的边界。

ND_TTEST_2 是一个宏,用于检查是否可以安全地从指定的指针 p 读取 2 个字节(即 16 位)。它通过验证内存边界和指针合法性来确保不会发生越界访问。具体来说,它结合了多个宏和函数,形成一个完整的边界检查机制。

总结

ND_TTEST_2 的主要目的是确保在从数据包读取数据时不会发生越界访问,从而保证程序的安全性和稳定性。它通过一系列的检查来验证指针和长度的合法性,防止因非法内存访问导致的崩溃或数据损坏。

3.setjmp/longjmp 非局部跳转函数的应用

现在存在的问题是没有搞清tcpdump 的执行流程:

调用顺序:

(gdb) bt
#0  eigrp_print (ndo=0x7fffffffcf00, pptr=0x8ea512 "\002\005\356h", len=40) at ./print-eigrp.c:233
#1  0x0000000000458255 in ip_demux_print (ndo=0x7fffffffcf00, bp=0x8ea512 "\002\005\356h", length=40, ver=4, fragmented=0, ttl_hl=2, nh=88 'X', iph=0x8ea4fe "E\300") at ./print-ip-demux.c:143
#2  0x00000000004227cf in ip_print (ndo=0x7fffffffcf00, bp=0x8ea4fe "E\300", length=60) at ./print-ip.c:487
#3  0x000000000041dbbc in ethertype_print (ndo=0x7fffffffcf00, ether_type=2048, p=0x8ea4fe "E\300", length=60, caplen=60, src=0x7fffffffccb0, dst=0x7fffffffcca0) at ./print-ether.c:536
#4  0x000000000041d680 in ether_common_print (ndo=0x7fffffffcf00, p=0x8ea4fe "E\300", length=60, caplen=60, print_switch_tag=0x0, switch_tag_len=0, print_encap_header=0x0, encap_header_arg=0x0) at ./print-ether.c:391
#5  0x000000000041d7c1 in ether_print (ndo=0x7fffffffcf00, p=0x8ea4f0 "\001", length=74, caplen=74, print_encap_header=0x0, encap_header_arg=0x0)at ./print-ether.c:448
#6  0x000000000041d81a in ether_if_print (ndo=0x7fffffffcf00, h=0x7fffffffce40, p=0x8ea4f0 "\001") at ./print-ether.c:464
#7  0x0000000000407ead in pretty_print_packet (ndo=0x7fffffffcf00, h=0x7fffffffce40, sp=0x8ea4f0 "\001", packets_captured=1) at ./print.c:414
#8  0x0000000000407343 in print_packet (user=0x7fffffffcf00 "", h=0x7fffffffce40, sp=0x8ea4f0 "\001") at ./tcpdump.c:3127
#9  0x00000000004f4f07 in pcap_offline_read (p=p@entry=0x8ea250, cnt=2147483647, cnt@entry=-1, callback=callback@entry=0x4072ed <print_packet>, user=user@entry=0x7fffffffcf00 "") at ./savefile.c:691
#10 0x00000000004e2baf in pcap_loop (p=0x8ea250, cnt=-1, callback=0x4072ed <print_packet>, user=0x7fffffffcf00 "") at ./pcap.c:2916
#11 0x00000000004066c2 in main (argc=3, argv=0x7fffffffe3c8) at ./tcpdump.c:2569

setjmp() 函数是在 #7 的 pretty_print_packet() 函数中调用的。

参考:

1、非局部跳转:8.6 非本地跳转 | 深入理解计算机系统(CSAPP) (gitbook.io)

2、 【C指针(五)】6种转移表实现整合longjmp()/setjmp()函数和qsort函数详解分析&&模拟实现-支付宝开发者社区 (alipay.com)

3、tcpdump 源码解析:https://www.cnblogs.com/pangblog/p/3364690.html

 分析2:tcpdump源码分析-CSDN博客

4、tcpdump 越界访问追踪: tcpdump 4.5.1 crash 深入分析-安全客 - 安全资讯平台

相关文章:

  • Python数据分析实验四:数据分析综合应用开发
  • AWS安全性身份和合规性之IAM Identity Center(AWS Single Sign-On)
  • 民国漫画杂志《时代漫画》第13期.PDF
  • AI早班车5.25
  • 【EXCEL_VBA_基础知识】10 使用Dir函数合并多个文件数据
  • python冰雹序列的探索与编程实现
  • Restful API设计与使用:介绍什么是RESTful架构,以及如何在Spring Boot中设计和实现Restful API
  • Mybatis源码剖析---第二讲
  • 【Java面试】一、Redis篇(上)
  • 链表-设计LRU缓存结构
  • uni-app App端实现文字语音播报(Ba-TTS)
  • PTA 6-4 配对问题
  • 如何参与github开源项目并提交PR
  • Linux下环境变量配置出错导致基础命令使用不了的问题解决
  • 抖音分享链接视频下载
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • Apache Spark Streaming 使用实例
  • Python进阶细节
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • vue脚手架vue-cli
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 前端面试总结(at, md)
  • 正则表达式
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • ​Redis 实现计数器和限速器的
  • ​浅谈 Linux 中的 core dump 分析方法
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • (1)Jupyter Notebook 下载及安装
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (ISPRS,2021)具有遥感知识图谱的鲁棒深度对齐网络用于零样本和广义零样本遥感图像场景分类
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (SERIES10)DM逻辑备份还原
  • (SERIES12)DM性能优化
  • (二)构建dubbo分布式平台-平台功能导图
  • (二)换源+apt-get基础配置+搜狗拼音
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (七)Activiti-modeler中文支持
  • (图)IntelliTrace Tools 跟踪云端程序
  • (推荐)叮当——中文语音对话机器人
  • (转)http协议
  • (转)Sublime Text3配置Lua运行环境
  • .[hudsonL@cock.li].mkp勒索病毒数据怎么处理|数据解密恢复
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .Net开发笔记(二十)创建一个需要授权的第三方组件
  • @staticmethod和@classmethod的作用与区别
  • @test注解_Spring 自定义注解你了解过吗?
  • [ 隧道技术 ] 反弹shell的集中常见方式(四)python反弹shell
  • [<事务专题>]
  • [52PJ] Java面向对象笔记(转自52 1510988116)
  • [Android] Android ActivityManager
  • [boost]使用boost::function和boost::bind产生的down机一例
  • [BUAA软工]第一次博客作业---阅读《构建之法》