Linux网络数据捕获之原始套接字

处于一些目的,有时需要对到达网口的所有网络数据进行捕获,系统也提供了这样的接口,稍微懂网络编程的都知道SOCK_DGRAM、SOCK_STREAM,差不多就UDP、TCP之类的吧。但是还有一个很少用的叫SOCK_RAW,原始套接字,使用它你可以捕获网卡上的所有网络数据,当然这需要超级用户权限。贴个列子吧,网上摘的,具体出处忘了

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/if.h>
#include <linux/sockios.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>        /* the L2 protocols */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUFFER_MAX 2048

int main(int argc, char *argv[])
{

    int sock, n_read;
    char buffer[BUFFER_MAX];    

    struct sockaddr_ll sll;
    struct ifreq ifstruct;

    if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
    {
        perror( "create socket error");
        return -1;
    }

    memset(&sll, 0, sizeof(sll));
    sll.sll_family = PF_PACKET;
    sll.sll_protocol = htons(ETH_P_ALL);

    //get net card index ethx->index
    strcpy(ifstruct.ifr_name, "eth0");
    ioctl(sock, SIOCGIFINDEX, &ifstruct);
    sll.sll_ifindex = ifstruct.ifr_ifindex;

    //bind net card
    if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) == -1)
    {
        perror("bind error:\n");
        return -1;
    }

    while(1)
    {
        n_read = recvfrom(sock, buffer, 2048, 0, NULL, NULL);
        if(n_read <= 0)
        {
            perror("recvfrom\n");
            return -1;
        }
	//process packet
    }
    return 0;
}


其实原始套接字不仅可以捕获数据,也可以发送数据,而且是任意格式的数据,从MAC头到IP头之类的数据段都在自己的控制范围之内,什么ARP攻击、 DDoS攻击之类的都离不开原始套接字吧,不过这些封包是重点,这里篇幅有限不涉及封包了吧,只介绍如何将数据发出去

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/if.h>
#include <linux/sockios.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>        /* the L2 protocols */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUFFER_MAX 2048

int main(int argc, char *argv[])
{
    int sockfd;
    int n_write;        
    int n_res;
    
    struct sockaddr_ll sll;
    struct ifreq ifstruct;

    char buffer[BUFFER_MAX];
    char MAC_BUFFER[ETH_ALEN]= {0x00,0x18,0x82,0xab,0xd2,0xf9};
    char TYPE_BUFFER[2] = {0x88,0x66};

    if((sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
    {
        perror("create socket error:");
        return -1;
    }
    n_res = 0;
    n_write = 0;

    memset(&sll, 0, sizeof(sll));
    sll.sll_family = PF_PACKET;
    sll.sll_protocol = htons(ETH_P_ALL);
    
    //get netcard interface index ethx->ifindex
    strcpy(ifstruct.ifr_name, "eth0");
    ioctl(sockfd, SIOCGIFINDEX, &ifstruct);
    sll.sll_ifindex = ifstruct.ifr_ifindex;

    //get the local netcard mac
    strcpy(ifstruct.ifr_name, "eth0");
    ioctl(sockfd, SIOCGIFHWADDR, &ifstruct);
    memcpy(sll.sll_addr, ifstruct.ifr_ifru.ifru_hwaddr.sa_data, ETH_ALEN);
    sll.sll_halen = ETH_ALEN;
  
    //bind the netcard
    if(bind(sockfd, (struct sockaddr *)&sll, sizeof(sll)) == -1)
    {
        perror("bind error:");
        return -1;
    }

    //get the netcard work mode
    memset(&ifstruct, 0, sizeof(ifstruct));
    strcpy(ifstruct.ifr_name, "eth0");
    if(ioctl(sockfd, SIOCGIFFLAGS, &ifstruct) == -1)
    {
        perror("iotcl error:");
        return -1;
    }

    //set the netcard work mode
    ifstruct.ifr_flags |= IFF_PROMISC;

    if(ioctl(sockfd, SIOCSIFFLAGS, &ifstruct) == -1)
    {
       perror("iotcl()\n");
       printf("Fun:%s Line:%d\n", __func__, __LINE__);
       return -1;
    } 

    memcpy(buffer,MAC_BUFFER,ETH_ALEN);
    memcpy(buffer+6,sll.sll_addr,ETH_ALEN);
    memcpy(buffer+12,TYPE_BUFFER,2);

    while(1)
    {
        n_res = sendto(sockfd, buffer, 1024, 
                0, (struct sockaddr *)&sll, sizeof(sll));
        
        if(n_res < 0)
        {
            perror("sendto error:");
            return -1;
        }

        n_write += n_res;

        if(n_write >= 2048 * 2560)
        {
            break;
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值