kawaii

Linux下C语言实现简单的网络嗅探器
#include <stdio.h> #include <unistd.h> #inclu...
扫描右侧二维码阅读全文
29
2018/12

Linux下C语言实现简单的网络嗅探器

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#define BUFFER_MAX 2048
int main(int argc, char *argv[])
{
      int sock, n_read, proto;
      char buffer[BUFFER_MAX];
      char *ethhead, *iphead, *tcphead, *udphead, *icmphead, *p;
  if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0)
  {       
      fprintf(stdout, "create socket error\n");
      exit(0);
}        
while(1)
{
      n_read = recvfrom(sock, buffer, 2048, 0, NULL, NULL);
      
   if(n_read < 42)
   {
      fprintf(stdout, "Incomplete header, packet corrupt\n");
      continue;
   }
      ethhead = buffer;
      p = ethhead; 
      int n = 0XFF; 
      printf("MAC: %.2X:%02X:%02X:%02X:%02X:%02X==>"
      "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",
      p[6]&n, p[7]&n, p[8]&n, p[9]&n, p[10]&n, p[11]&n,
      p[0]&n, p[1]&n, p[2]&n,p[3]&n, p[4]&n, p[5]&n);
            iphead = ethhead + 14;
            p = iphead + 12;
      printf("IP: %d.%d.%d.%d => %d.%d.%d.%d\n",
      p[0]&n, p[1]&n, p[2]&n, p[3]&n,
      p[4]&n, p[5]&n, p[6]&n, p[7]&n);
      proto = (iphead + 9)[0];
      p = iphead;
      printf("Protocol: ");
            switch(proto)
   {
      case IPPROTO_ICMP: printf("ICMP\n");break;
      case IPPROTO_IGMP: printf("IGMP\n");break;
      case IPPROTO_IPIP: printf("IPIP\n");break;
      case IPPROTO_TCP :
      case IPPROTO_UDP :
      printf("%s,", proto == IPPROTO_TCP ? "TCP": "UDP");
      printf("source port: %u,",(p[20]<<8)&0XFF00 | p[21]&0XFF);
      printf("dest port: %u\n", (p[22]<<8)&0XFF00 | p[23]&0XFF);
      printf("\n"); break;
     case IPPROTO_RAW : printf("RAW\n");break;
     default:printf("Unkown, please query in include/linux/in.h\n");
   }
  }
 }
Last modification:December 29th, 2018 at 03:52 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment