Linux抓包代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <netinet/ip.h>
  7. #include <string.h>
  8. #include <netdb.h>
  9. #include <netinet/tcp.h>
  10. #include <netinet/udp.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <signal.h>
  14. #include <net/if.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. #include <linux/if_ether.h>
  19. #include <net/ethernet.h>
  20. void die(char *why, int n)
  21. {
  22.     perror(why);
  23.     exit(n);
  24. }
  25. int do_promisc(char *nif, int sock )
  26. {
  27.     struct ifreq ifr;
  28.     strncpy(ifr.ifr_name, nif,strlen(nif)+1);
  29.     if ((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) //获得flag
  30.     {
  31.         die("ioctl", 2);
  32.     }
  33.     ifr.ifr_flags |= IFF_PROMISC; //重置flag标志
  34.     if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 ) //改变模式
  35.     {
  36.         die("ioctl", 3);
  37.     }
  38. }
  39. //修改网卡成PROMISC(混杂)模式
  40. char buf[40960]={0};
  41. int main()
  42. {
  43.     struct sockaddr_in addr;
  44.     struct ether_header *peth;
  45.     struct iphdr *pip;
  46.     struct tcphdr *ptcp;
  47.     struct udphdr *pudp;
  48.     char mac[16];
  49.     int i,sock, r, len;
  50.     char *data;
  51.     char *ptemp;
  52.     char ss[32],dd[32];
  53.     if ((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) //建立socket//man socket可以看到上面几个宏的意思以看
  54.     {
  55.         die("socket", 1);
  56.     }
  57.     do_promisc("eth0", sock); //eth0为网卡名
  58.     for (;;)
  59.     {
  60.         len = sizeof(addr);
  61.         r = recvfrom(sock,(char *)buf,sizeof(buf), 0, (struct sockaddr *)&addr,(socklen_t *)&len);
  62.         //调试的时候可以增加一个输出r的语句判断是否抓到包
  63.         if(r <=0)
  64.         {
  65.             continue;
  66.         }
  67.         buf[r] = 0;
  68.         ptemp = buf;
  69.         peth = (struct ether_header *)ptemp;
  70.         ptemp += sizeof(struct ether_header); //指针后移eth头的长度
  71.         pip = (struct iphdr *)ptemp; //pip指向ip层的包头
  72.         ptemp += sizeof(struct ip);//指针后移ip头的长度
  73.         switch (pip->protocol) //根据不同协议判断指针类型
  74.         {
  75.         case IPPROTO_TCP:
  76.             ptcp = (struct tcphdr *)ptemp; //ptcp指向tcp头部
  77.             printf("TCP pkt :FORM:[%s]:[%d]/n",inet_ntoa(*(struct in_addr*)&(pip->saddr)),ntohs(ptcp->source));
  78.             printf("TCP pkt :TO:[%s]:[%d]/n",inet_ntoa(*(struct in_addr*)&(pip->daddr)),ntohs(ptcp->dest));
  79.             break;
  80.         case IPPROTO_UDP:
  81.             pudp = (struct udphdr *)ptemp; //ptcp指向udp头部
  82.             printf("UDP pkt:/n len:%d payload len:%d from %s:%d to %s:%d/n",
  83.                    r,
  84.                    ntohs(pudp->len),
  85.                    inet_ntoa(*(struct in_addr*)&(pip->saddr)),
  86.                    ntohs(pudp->source),
  87.                    inet_ntoa(*(struct in_addr*)&(pip->daddr)),
  88.                    ntohs(pudp->dest)
  89.                   );
  90.             break;
  91.         case IPPROTO_ICMP:
  92.             printf("ICMP pkt:%s/n",inet_ntoa(*(struct in_addr*)&(pip->saddr)));
  93.             break;
  94.         case IPPROTO_IGMP:
  95.             printf("IGMP pkt:/n");
  96.             break;
  97.         default:
  98.             printf("Unkown pkt, protocl:%d/n", pip->protocol);
  99.             break;
  100.         } //end switch
  101.         perror("dump");
  102.     }
  103.     return 0;
  104. }
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值