原始套接字实现抓包,分析tcp-ip报文

模拟抓包,并且解析报文。
这边先贴上代码和部分资料。详细解释以后再写:
1.以太网帧
这里写图片描述

2.IP报文
这里写图片描述

3.TCP报文
这里写图片描述

4.UDP报文
这里写图片描述


#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/if_arp.h>
#include <stdio.h>
//解析出mac地址
void dump_mac(const char* header, char* buf, int start)
{
    printf("%s", header);
    int i;
    for(i=start; i<6+start; ++i)
    {
        printf("%02x:", (int)(unsigned char)buf[i]);
    }
    printf("\n");
}
//解析出以太网帧类型
unsigned short get_type(char* buf)
{
    unsigned short type = *(unsigned short*)(buf+12);
    return ntohs(type);
}
//解析出IP报文的8位协议类型 TCP:6/UDP:17
unsigned char get_ptype(unsigned char* ip)
{ 
    return ip[9];
}
//ip报文开始的位置
unsigned char* get_ip_start(char* buf)
{
    return buf + 14;
}

//TCP报文开始的位置
unsigned char* get_tcp_start(unsigned char* ip)
{
    // 先得到首部长度
    int hlen = (ip[0] & 0xf) * 4;
    printf("hlen = %d\n", hlen); // 20
    return ip + hlen;
}

//TCP报文数据
unsigned char* get_tcp_data(unsigned char* tcp)
{
    int hlen = ((tcp[12] & 0xf0) >> 4) * 4;
    return tcp + hlen;
}

void handle(char* buf, int length)
{
    unsigned short type = get_type(buf);
    if(type != 0x0800)
        return;

    // 只对IP报文进行分析
    unsigned char* ip = get_ip_start(buf);
    // 协议类型
    unsigned char ptype = get_ptype(ip);
    if(ptype != 6)
    {
        return; // 只对TCP进行分析
    }

    unsigned char* tcp = get_tcp_start(ip);
    unsigned char* tcp_data = get_tcp_data(tcp);
    printf("%s\n\n\n", tcp_data);
}

int main()
{
    // 创建了一个原始套接字
  //  int sock = socket(AF_PACKET, SOCK_RAW, htons(IPPROTO_RAW));
    int sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if(sock < 0)
    {
        perror("socket");
        return 0;
    }

    // 获得硬件信息,获取接口的下标
    struct ifreq ifstruct;
    strcpy(ifstruct.ifr_name, "eth0");
    // 获取接口index
    ioctl(sock, SIOCGIFINDEX, &ifstruct);
    // 获取本地MAC地址
    ioctl(sock, SIOCGIFHWADDR, &ifstruct);
    // 获取网卡设置
    ioctl(sock, SIOCGIFFLAGS, &ifstruct); 

    // 设置混杂模式
    ifstruct.ifr_flags |= IFF_PROMISC;

    // 绑定网口
    struct sockaddr_ll sll;
    sll.sll_family = AF_PACKET;
    sll.sll_ifindex = ifstruct.ifr_ifindex;
    sll.sll_protocol = htons(ETH_P_ALL);
    sll.sll_hatype = ARPHRD_ETHER;
    sll.sll_pkttype = PACKET_OTHERHOST;   
    sll.sll_halen = ETH_ALEN;   //MAC地址的长度
    sll.sll_addr[6] = 0; 
    sll.sll_addr[7] = 0;   
    bind(sock, (struct sockaddr *)&sll, sizeof(struct sockaddr_ll));

    // --------------------原始套接字创建完成--------------------------------
    char buf[1514];

    while(1)
    {
        int ret = read(sock, buf, sizeof(buf));    
        if(ret <= 0)
        {
            perror("read");
            return 0;
        }
        handle(buf, ret);
    }

}


















  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值