Chinaunix首页 | 论坛 | 博客
  • 博客访问: 218446
  • 博文数量: 40
  • 博客积分: 316
  • 博客等级: 二等列兵
  • 技术积分: 840
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-13 12:39
文章分类

全部博文(40)

文章存档

2014年(2)

2013年(28)

2012年(10)

分类: C/C++

2013-11-11 15:20:19


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/socket.h>
  4. #include <sys/types.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <sys/ioctl.h>
  8. #include <linux/if_ether.h>
  9. #include <linux/in.h>
  10. #include <net/if.h>
  11. #include <linux/filter.h>

  12. char buf[2048];
  13. //char result[]={0x09,0x01,0x3f,0x01,0x01,0x08,0x91,0x68,0x31,0x08,0x10,0x00,0x85,0xf8,0x00,0x32,0x24,0x0d,0x91,0x68,0x81,0x16,0x52,0x79,0x16,0xf2,0x00,0x08,0x31,0x11,0x60,0x71,0x83,0x21,0x23,0x1e,0x62,0x11,0x53,0xbb,0x96,0xcd,0x54,0x8c,0x5b,0xab,0xff,0x0c,0x4f,0x60,0x89,0x81,0x66,0x2f,0x57,0x50,0x57,0x30,0x94,0xc1,0x54,0x4a,0x8b,0xc9,0x62,0x11};
  14. char result[2048];
  15. char Two_2_3[]={};

  16. int d_deal(int num)
  17. {
  18.     int di=0;
  19.     int m = 0;
  20.     int m_len = 0;
  21.     int dst_len = 0;
  22.     printf("T_src:");
  23.     for ( di=7;di<=13;di++)
  24.     {
  25.         printf("%02x",((result[di] << 4 ) & 0xf0) | ((result[di] >> 4) & 0xf)); // change the positon of result[7] 68=>86
  26.     }
  27.     printf("\t");
  28.     m_len=(int)((result[17]+1)/2);
  29.     printf("T_dst:");
  30.     dst_len=19+m_len;
  31.     for (di=19; di < dst_len; di++)
  32.     {
  33.         printf("%02x",((result[di] << 4 ) & 0xf0) | ((result[di] >> 4) & 0xf)); // change the positon of result[7] 68=>86
  34.     }
  35.     printf("\ttext:");
  36.     for (di=dst_len + 10; di < num; di+=2)
  37.     {
  38.         if(result[di] == 0x00)
  39.         {
  40.             Two_2_3[m] = result[di+1];
  41.             m++;
  42.         }
  43.         else
  44.         {
  45.         Two_2_3[m]=(0xe0 | (result[di] & 0xf0) >> 4);
  46.         Two_2_3[m+1] = (0x80 | (result[di] & 0x0f) << 2 | (result[di+1] & 0xc0) >> 6);
  47.         Two_2_3[m+2] = (0x80 | result[di+1] & 0x3f);
  48.         m+=3;
  49.         }
  50.     }
  51.     printf("%s\n",Two_2_3);

  52. }


  53. int main(int argc,char **argv)
  54. {
  55.     int sock,n,i;
  56.     int j = 0;
  57.     int m = 0;
  58.     int tal_num = 0;
  59.     int d_len =0;
  60.     unsigned char *iphead,*ethhead;
  61.     struct ifreq ethreq;
  62. //    struct sigaction sighandle;

  63.     if ((sock = socket(PF_PACKET,SOCK_RAW,htons(ETH_P_IP))) < 0)
  64.     {
  65.         perror("socket failed");
  66.         exit(1);
  67.     }


  68.     /*set promiscuos mode*/
  69.     strncpy(ethreq.ifr_name, "wlp6s0", IFNAMSIZ);
  70.     if (ioctl(sock,SIOCGIFFLAGS,&ethreq) == -1)
  71.     {
  72.         perror("ioctl failed");
  73.         close(sock);
  74.         exit(1);
  75.     }
  76.     ethreq.ifr_flags |= IFF_PROMISC;
  77.     if (ioctl(sock,SIOCSIFFLAGS,&ethreq) == -1)
  78.     {
  79.         perror("ioctl failed");
  80.         close(sock);
  81.         exit(1);
  82.     }
  83.     
  84.     struct sock_filter bpf_code[] = {
  85.         { 0x28, 0, 0, 0x0000000c },
  86.         { 0x15, 0, 4, 0x000086dd },
  87.         { 0x30, 0, 0, 0x00000014 },
  88.         { 0x15, 0, 11, 0x00000011 },
  89.         { 0x28, 0, 0, 0x00000038 },
  90.         { 0x15, 8, 9, 0x00000035 },
  91.         { 0x15, 0, 8, 0x00000800 },
  92.         { 0x30, 0, 0, 0x00000017 },
  93.         { 0x15, 0, 6, 0x00000011 },
  94.         { 0x28, 0, 0, 0x00000014 },
  95.         { 0x45, 4, 0, 0x00001fff },
  96.         { 0xb1, 0, 0, 0x0000000e },
  97.         { 0x48, 0, 0, 0x00000010 },
  98.         { 0x15, 0, 1, 0x00000035 },
  99.         { 0x6, 0, 0, 0x00000051 },
  100.         { 0x6, 0, 0, 0x00000000 },
  101.     };
  102.     struct sock_fprog filter;
  103.     filter.len = sizeof(bpf_code)/sizeof(bpf_code[0]);
  104.     filter.filter = bpf_code;

  105.     //sighandle.sa_flags = 0;
  106.     //sighandle.sa_handler = sig_handler;
  107. //    sigemptyset(&sighandle.sa_mask);

  108. //    sigaction(SIGTERM,&sighandle,NULL);
  109. //    sigaction(SIGINT,&sighandle,NULL);
  110. //    sigaction(SIGQUIT,&sighandle,NULL);


  111.     if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) < 0)
  112.     {
  113.         perror("setsockopt failed");
  114.         close(sock);
  115.         exit(1);
  116.     }
  117.     
  118.     while (1)
  119.     {
  120.         int is_last = 0;
  121.         n = recvfrom(sock, buf, sizeof(buf), 0, NULL, NULL);
  122.         if (n < (14 + 20 + 8))
  123.         {
  124.             printf("invalid packet\n");
  125.             continue;
  126.         }
  127.         printf("%d bytes recieved\n",n);
  128.         ethhead = buf;
  129.     //    printf("dst port: %02x%02x\n",ethhead[36],ethhead[37]);
  130.     //    printf("channel type: %02x\n",ethhead[54]);
  131.     //    if ( 0xf == 15 ) printf("find ok\n%02x\n", (0x03 >> 4) | (0x03 << 4));
  132.         if (ethhead[54] == 0x08 && (ethhead[59] & 0x1) == 0x0 ) //the last bit of 59 byte decide if frame
  133.         {
  134.             d_len = (ethhead[60] & 0x2)?20:(is_last++,((ethhead[60] >> 2) & 0x3f));
  135.             tal_num+=d_len;
  136.             for (j=1; j <= d_len; m++,j++)//read len data
  137.             {
  138.                 result[m]=ethhead[60+j];
  139.             }
  140.             if (is_last) {m = 0;d_deal(tal_num);tal_num = 0;}
  141.         }
  142.     }
  143. }

bpf过滤的是upd 53 的流量,  gsm请过滤udp 4729 进行替换。 tcpdump udp and dst port 4729 -s 81 -dd
注:   wireshark中的信息部分,是2字节表示一个汉字,但是c语言中3个字节表示一个汉字。
1110 xxxx  10xxxxxx 10xxxxxx  ,  左边的16个x表示 wireshark中的2个字节。
阅读(2753) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~