Linux 混杂模式抓包

 浮桥轶事   2019-04-09 10:49   394 人阅读  0 条评论

#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

#include <errno.h>

#include <arpa/inet.h>

#include <netinet/udp.h>

#include <stdlib.h>

#include <unistd.h>

#include <signal.h>

#include <net/if.h>

#include <netinet/ip.h>

#include <sys/ioctl.h>

#include <fcntl.h>

#include <sys/stat.h>

#include <linux/if_ether.h>

#include <net/ethernet.h>

#include <time.h>

#include <linux/if_packet.h>


char buf_rec[40960];//buffer to recev data

char buf_es[1600];


struct ifreq ifr;

int do_promisc(char *nif,int sock)

{

//struct ifreq ifr;

 

strncpy(ifr.ifr_name,nif,strlen(nif)+1);


if(ioctl(sock,SIOCGIFFLAGS,&ifr)==-1)

  {

  perror("ioctl");

exit(2);

  }


  ifr.ifr_flags |= IFF_PROMISC;

  if(ioctl(sock, SIOCSIFFLAGS, &ifr)==-1)

  {

  perror("ioctl");

exit(3);

  }

if(ioctl(sock, SIOCGIFHWADDR, &ifr) < 0 ) {

        perror("failed to get interface hw address!" );

        return -1;

    }

    if(ioctl(sock, SIOCGIFINDEX, &ifr) < 0 )  {

        perror("failed to get interface index!" );

        return -1;

    }

return 0;

}//change the flag of netcard to promisc mod


int main()

{

//struct sockaddr_in addr;

  //struct ether_header *peth;

  //struct iphdr *pip;

  //struct udphdr *pudp;

// time_t tw;

  int i,sockfd,r,str_size;

unsigned int len;

  char *ptemp;

  //struct tm *now;

struct timeval tvs;

    struct timeval tve;


signal(SIGPIPE,SIG_IGN);


  //get socket

  if((sockfd = socket(PF_PACKET,SOCK_DGRAM,htons(ETH_P_IP)))==-1)

  {

  perror("socket");

  exit(1);

  }


  do_promisc("eth0",sockfd);//change the netcart to promisc mod

  while(1){


  len = sizeof(addr);

  if((r = recvfrom(sockfd,(char*)buf_rec,sizeof(buf_rec),0,(struct sockaddr*)&addr,&len))==-1)

  {

    perror("recvfrom");

    continue;

  }



  if(pip->protocol == IPPROTO_UDP)

  {

printf("fff");

  }

  }


return 0;

}


打赏
本文地址:http://www.fuxiqiao.com/post/17.html
版权声明:本文为原创文章,版权归 浮桥轶事 所有,欢迎分享本文,转载请保留出处!

 发表评论


表情

还没有留言,还不快点抢沙发?