home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / test / TCP / tcp+data.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  5.9 KB  |  180 lines

  1. /*
  2.  *  $Id: tcp+data.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  TCP Packet assembly tester (with payload)
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  *
  32.  */
  33.  
  34. #if (HAVE_CONFIG_H)
  35. #include "../../include/config.h"
  36. #endif
  37. #include "../libnet_test.h"
  38.  
  39. int
  40. main(int argc, char **argv)
  41. {
  42.     int sock, c;
  43.     u_long src_ip, dst_ip;
  44.     u_short src_prt, dst_prt;
  45.     u_char *cp, *buf; 
  46.     u_char *payload = "hello world";
  47.     int payload_s = strlen(payload);
  48.  
  49.     printf("TCP + payload packet building/writing test\n");
  50.  
  51.     src_ip  = 0;
  52.     dst_ip  = 0;
  53.     src_prt = 0;
  54.     dst_prt = 0;
  55.     while((c = getopt(argc, argv, "d:s:")) != EOF)
  56.     {
  57.         switch (c)
  58.         {
  59.             /*
  60.              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
  61.              *  point cp to the last dot of the IP address/port string and
  62.              *  then seperate them with a NULL byte.  The optarg now points to
  63.              *  just the IP address, and cp points to the port.
  64.              */
  65.             case 'd':
  66.                 if (!(cp = strrchr(optarg, '.')))
  67.                 {
  68.                     usage(argv[0]);
  69.                 }
  70.                 *cp++ = 0;
  71.                 dst_prt = (u_short)atoi(cp);
  72.                 if (!(dst_ip = name_resolve(optarg, 1)))
  73.                 {
  74.                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
  75.                     exit(EXIT_FAILURE);
  76.                 }
  77.                 break;
  78.             case 's':
  79.                 if (!(cp = strrchr(optarg, '.')))
  80.                 {
  81.                     usage(argv[0]);
  82.                 }
  83.                 *cp++ = 0;
  84.                 src_prt = (u_short)atoi(cp);
  85.                 if (!(src_ip = name_resolve(optarg, 1)))
  86.                 {
  87.                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
  88.                     exit(EXIT_FAILURE);
  89.                 }
  90.                 break;
  91.         }
  92.     }
  93.  
  94.     if (!src_ip || !src_prt || !dst_ip || !dst_prt)
  95.     {
  96.         usage(argv[0]);
  97.         exit(EXIT_FAILURE);
  98.     }
  99.  
  100.     /*
  101.      *  Get our block of memory for the packet.  This time, we need memory
  102.      *  for the packet headers and payload.
  103.      */
  104.     buf = malloc(TCP_H + IP_H + payload_s);
  105.     if (!buf)
  106.     {
  107.         perror("No memory for packet");
  108.         exit(EXIT_FAILURE);
  109.     }
  110.  
  111.     /*
  112.      *  Open our raw IP socket and set IP_HDRINCL.
  113.      */
  114.     sock = open_raw_sock(IPPROTO_RAW);
  115.     if (sock == -1)
  116.     {
  117.         perror("No socket");
  118.         exit(EXIT_FAILURE);
  119.     }
  120.     
  121.     /*
  122.      *  Build the IP header (shown exploded for commenting).
  123.      */
  124.     build_ip(TCP_H + payload_s,             /* Size of the payload */
  125.             0,                              /* IP tos */
  126.             242,                            /* IP ID */
  127.             0,                              /* Frag stuff */
  128.             48,                             /* TTL */
  129.             IPPROTO_TCP,                    /* Transport protocol */
  130.             src_ip,                         /* Source IP */
  131.             dst_ip,                         /* Destination IP */
  132.             NULL,                           /* Pointer to payload (none) */
  133.             0,
  134.             buf);                           /* Packet header memory */
  135.  
  136.     /*
  137.      *  Build the TCP header.
  138.      */
  139.     build_tcp(src_prt,                      /* Source TCP port */
  140.             dst_prt,                        /* Destination TCP port */
  141.             11111,                          /* Sequence number */
  142.             99999,                          /* Acknowledgement number */
  143.             TH_SYN|TH_ACK,                  /* Control flags */
  144.             1024,                           /* Window size */
  145.             0,                              /* Urgent pointer */
  146.             payload,                        /* Pointer to payload */
  147.             payload_s,                      /* Payload size */
  148.             buf + IP_H);                    /* Packet header memory */
  149.  
  150.     /*
  151.      *  Calculate the TCP header checksum (IP header checksum is *always* done
  152.      *  by the kernel.
  153.      */
  154.     do_checksum(buf, IPPROTO_TCP, TCP_H + payload_s);
  155.  
  156.     /*
  157.      *  Write the packet to the network.
  158.      */
  159.     c = write_ip(sock, buf, TCP_H + IP_H + payload_s);
  160.     if (c < TCP_H + IP_H + payload_s)
  161.     {
  162.         fprintf(stderr, "write_ip\n");
  163.     }
  164.     printf("Completed, wrote %d bytes\n", c);
  165.     free(buf);
  166.  
  167.     return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
  168. }
  169.  
  170.  
  171. void
  172. usage(u_char *name)
  173. {
  174.     fprintf(stderr,
  175.         "usage: %s -s source_ip:source_port -d destination_ip:destination_port\n",
  176.         name);
  177. }
  178.  
  179. /* EOF */
  180.