【问题标题】:how to print flags in TCP header of raw packets using libpcap如何使用 libpcap 在原始数据包的 TCP 标头中打印标志
【发布时间】:2011-01-05 12:50:42
【问题描述】:

sniffex.c 是一个基于 libpcap 的程序,用于嗅探和显示一些数据包信息。如何修改它以打印 TCP 标志的值 - urg、ack、psh、rst、syn 和 fin?请帮忙..

【问题讨论】:

    标签: tcp header packet-capture packet-sniffers libpcap


    【解决方案1】:

    如果您在使用 sniff_tcp 的地方检查此代码,请确保打印出来 th_flags 该结构的成员,包含您需要的标志。

    /* TCP header */
        struct sniff_tcp {
            u_short th_sport;   /* source port */
            u_short th_dport;   /* destination port */
            tcp_seq th_seq;     /* sequence number */
            tcp_seq th_ack;     /* acknowledgement number */
    
            u_char th_offx2;    /* data offset, rsvd */
        #define TH_OFF(th)  (((th)->th_offx2 & 0xf0) >> 4)
            u_char th_flags;
        #define TH_FIN 0x01
        #define TH_SYN 0x02
        #define TH_RST 0x04
        #define TH_PUSH 0x08
        #define TH_ACK 0x10
        #define TH_URG 0x20
        #define TH_ECE 0x40
        #define TH_CWR 0x80
        #define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
            u_short th_win;     /* window */
            u_short th_sum;     /* checksum */
            u_short th_urp;     /* urgent pointer */
    };
    

    一般你会有这样的东西(有点丑,你可以做得更好):

    //this is already there in the code:
        printf("   Src port: %d\n", ntohs(tcp->th_sport));
        printf("   Dst port: %d\n", ntohs(tcp->th_dport))
    
    //you add:
        if (tcp->th_flags & TH_ECE){
            printf("   Flag: TH_ECE");
        }
        if (tcp->th_flags & TH_RST){
            printf("   Flag: TH_RST");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 2012-04-13
      • 2021-12-25
      • 2013-03-04
      相关资源
      最近更新 更多