zoukankan      html  css  js  c++  java
  • libnet TCP示例

    [root@TD18 tmp]#gcc -o 1 1.c -lnet
    [root@TD18 tmp]#./1
    please enter Host address
    11.11.11.11
    please enter local address:
    22.22.22.22
    wrote 126 byte TCP packet
    [root@TD18 tmp]#cat 1.c
    #include<stdio.h>
    #include<string.h>
    #include<pcap.h>
    #include<libnet.h>
    
    main(void)
    { int c ;
       u_char *cp;
       libnet_t *l;
       libnet_ptag_t t;
       char *payload;
       u_char HostAddr[255],MyAddr[255];
       u_short payload_s;
       u_long src_ip,dst_ip;
       u_short src_prt,dst_prt;
    
    
       char errbuf[LIBNET_ERRBUF_SIZE];
       printf("please enter Host address
    ");
      scanf("%s",HostAddr);
       printf("please enter local address:
    ");
      scanf("%s",MyAddr);
       l=libnet_init( LIBNET_LINK,NULL,errbuf);
      if(l==NULL)
        {
           printf("libnet failed: %s",errbuf);
           exit(EXIT_FAILURE);
        }
      dst_ip=inet_addr(HostAddr);
      src_ip=inet_addr(MyAddr);
      dst_prt=0;
      src_prt=0;
    
      payload="adafhafhasfjashfasjfasjkfhsadjkfajskfjasdhfjaksfjkaf";
      payload_s=strlen(payload);
    t=libnet_build_tcp_options("0303120102040111101277777777000000000000",20,l,0);
    if(t==-1)
       {printf("cant build TCP options: %s
    ",libnet_geterror(l));
       goto bad;}
    
       t=libnet_build_tcp(src_prt,dst_prt,0x01010101,0x02020202,TH_SYN,32767,0,10,
                       LIBNET_TCP_H+20+payload_s,payload,payload_s,l,0 );
    if(t==-1)
              {
                 printf("cant build TCP header:%s
    ",libnet_geterror(l));
                 goto bad;
              }
    
        t=libnet_build_ipv4(LIBNET_IPV4_H+LIBNET_TCP_H+20+payload_s,0,242,0,64,IPPROTO_TCP,0,src_ip,dst_ip,NULL,0,l,0);
    
    if(t==-1)
      {
       printf("cant build IP header:%s
    ",libnet_geterror(l));
       goto bad;
      }
      t=libnet_build_ethernet("00:0B:6A:63:5E:11","00:0B:6A:63:5E:22",ETHERTYPE_IP,NULL,0,l,0);
    
    if(t==-1)
    {printf("cant buid ethernet header:%s
    ",libnet_geterror(l));
      goto bad;
    }
      c=libnet_write(l);
    if(c==-1)
      {printf("write error:%s
    ",libnet_geterror(l));
       goto bad;
      }
    else { printf("wrote %d byte TCP packet
    ",c);
         }
    libnet_destroy(l);
    return(EXIT_SUCCESS);
    bad:
       libnet_destroy(l);
       return(EXIT_FAILURE);
    
    }
  • 相关阅读:
    4.20 每日一练
    4.19 每日一练
    4.18 每日一练
    Python函数初
    Python的文件操作
    python购物车
    python深浅拷贝,集合以及数据类型的补充
    Python 代码块 小数据池
    Python字典
    Python 列表操作
  • 原文地址:https://www.cnblogs.com/RodYang/p/3350751.html
Copyright © 2011-2022 走看看