1 /*
2     nast
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 */
19 
20 #include "include/nast.h"
21 
22 struct pkt
23 {
24    u_long seq;
25    u_long ack;
26 };
27 char errbuf[256];
28 struct pkt info;
29 
30 /* reset a connection */
31 
rst(char * dev,u_long ip_src,u_long ip_dst,u_short sport,u_short dport)32 int rst (char *dev,u_long ip_src,u_long ip_dst,u_short sport,u_short dport)
33 {
34    struct libnet_ipv4_hdr *ip;
35    struct libnet_tcp_hdr *tcp;
36    libnet_t *l;
37    u_short n;
38 
39 #ifdef HAVE_LIBNCURSES
40    if(graph)
41      init_scr();
42 #endif
43 
44    if (demonize)
45      {
46 	w_error(0,"Is very useless demonize me in resetting connection! Omit");
47 	demonize=0;
48      }
49 
50    pcap_lookupnet (dev,&netp,&maskp,errbuf);
51 
52    if((descr=pcap_open_live(dev, BUFSIZ, PROMISC, 10, errbuf)) == NULL)
53      {
54 	w_error(1, "pcap_open_live: %s\n", errbuf);
55      }
56 
57    offset=(device(dev,descr));
58 
59    if (sport && dport)
60      n_print("princ",1,1,0,"- Waiting for SEQ ACK (%s:%d -> %s:%d)\n",libnet_addr2name4(ip_src , 0),sport,libnet_addr2name4(ip_dst , 0),dport);
61    else if (!dport && sport)
62      n_print("princ",1,1,0,"- Waiting for SEQ ACK (%s:%d -> %s)\n",libnet_addr2name4(ip_src , 0),sport,libnet_addr2name4(ip_dst , 0));
63    else if (!sport && dport)
64      n_print("princ",1,1,0,"- Waiting for SEQ ACK (%s -> %s:%d)\n",libnet_addr2name4(ip_src , 0),libnet_addr2name4(ip_dst , 0),dport);
65 
66    for (;;)
67      {
68 
69 	packet = (u_char *) pcap_next(descr, &hdr);
70 
71 	ip = (struct libnet_ipv4_hdr *) (packet + offset);
72 	if (ip->ip_p != IPPROTO_TCP)
73 	  continue;
74 	if ((ip->ip_src.s_addr != ip_src) || (ip->ip_dst.s_addr != ip_dst))
75 	  continue;
76 	tcp = (struct libnet_tcp_hdr *) (packet + offset + sizeof (struct libnet_ipv4_hdr));
77 
78 	if (!(tcp->th_flags & TH_ACK))
79 	  continue;
80 
81 	/* the specified port are not either zero */
82 	if (sport && dport)
83 	  {
84 	     if ((tcp->th_sport != htons(sport)) || (tcp->th_dport != htons(dport)))
85 	       continue;
86 	  }
87 	/* dport is 0 */
88 	else if (!dport && sport)
89 	  {
90 	     if ((tcp->th_sport != htons(sport)))
91 	       continue;
92 	     /* DPORT */
93 	     dport = htons (tcp->th_dport);
94 	  }
95 	/* sport is 0 */
96 	else if (!sport && dport)
97 	  {
98 	     if ((tcp->th_dport != htons(dport)))
99 	       continue;
100 	     /* SPORT */
101 	     sport = htons (tcp->th_sport);
102 	  }
103 
104 	info.seq = htonl (tcp->th_seq);
105 	info.ack = htonl (tcp->th_ack);
106 
107 	n_print("princ",3,1,0,"- Stoled SEQ (%lu) ACK (%lu)...\n", info.seq, info.ack);
108 	break;
109      }
110 
111    pcap_close(descr);
112 
113    /* second part */
114 
115    if ((l = libnet_init (LIBNET_RAW4, NULL, errbuf))==NULL)
116      {
117 	w_error(1, "libnet_init: %s\n", errbuf);
118      }
119 
120    if (libnet_build_tcp (sport, dport, info.seq, info.ack, TH_RST, 32767, 0, 0, LIBNET_TCP_H, NULL, 0, l, 0)==-1)
121      {
122 	libnet_destroy (l);
123 	w_error(1, "Error building tcp header : %s\n" ,libnet_geterror(l));
124      }
125 
126    if (libnet_build_ipv4 (LIBNET_TCP_H + LIBNET_IPV4_H, 0x08, 35320, 0, 64, IPPROTO_TCP, 0, ip_src , ip_dst , NULL, 0, l, 0)==-1)
127      {
128 	libnet_destroy (l);
129 	w_error(1, "Error building ip header : %s\n", libnet_geterror(l));
130      }
131 
132    /* send 2 packet for security :) */
133    for (n = 0; n < 2 ; n++)
134      if (libnet_write (l) == -1)
135        {
136 	  libnet_destroy(l);
137 	  w_error(1, "Error writing packet on wire : %s\n", libnet_geterror(l));
138        }
139 
140    libnet_destroy(l);
141    n_print("princ",5,1,0,"- Connection has been resetted\n\n");
142 
143    return (0);
144 }
145