1 /*
2  * This file is part of Gspoof-3 (a console/gtk+ tcp/ip packet forger)
3  *
4  * $Name: gfunct.c $
5  * $Version: 3.2 $
6  * $Date: 2003/12/22 16:30:03 $
7  * $Author: Embyte <embyte@madlab.it> $
8  * $Copyright: Copyright (C) 2002-2003 by embyte $
9  * $License: This software is under GPL version 2 of license $
10  *
11  */
12 
13 #include <gtk/gtk.h>
14 #include <libnet.h>
15 
16 #include "ginclude.h"
17 #include "gfuncts.h"
18 #include "common.h"
19 
20 #include "interface.h"
21 
22 /* private functions */
23 int CheckValues();
24 u_short datalen; /* data (tcp payload) lenght */
25 
Initialize()26 int Initialize()
27 {
28    GtkTextIter start, end;
29    GtkTextTag *tag;
30 
31    TextBuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (TextView));
32    gtk_text_buffer_insert_at_cursor (TextBuffer,
33 				     "\nWelcome to Gspoof, V. 3.2\nWritten by Embyte (c) 2002-2003\nLicensed under GPL domain\n\n", -1);
34 
35    /* put colors */
36    gtk_text_buffer_get_bounds (TextBuffer, &start, &end);
37    tag = gtk_text_buffer_create_tag (TextBuffer, "banner", "foreground", "red", NULL);
38    gtk_text_buffer_apply_tag (TextBuffer, tag, &start, &end);
39 
40    RestoreDefault();
41 
42    return 0;
43 }
44 
RestoreDefault()45 int RestoreDefault()
46 {
47    struct libnet_ether_addr *ethaddr; /* eth address */
48    libnet_t *l;
49 
50    /* put default value */
51    bzero (&f, 6);
52    bzero (&ck, 2);
53    memset (data, '\0', 128);
54    memset (device, '\0', 10);
55 
56    /* loading libnet core */
57    if ((l=libnet_init(LIBNET_LINK, NULL, ebuf))==NULL)
58      {
59 	fprintf (stderr, "Error creationg libnet file context : %s\n", ebuf);
60 	ExitFailure();
61      }
62 
63    /* we probe for device */
64    strcpy (device, libnet_getdevice(l));
65    if (device==NULL)
66      {
67 	fprintf (stderr, "Error: cannot get device name : %s\n", libnet_geterror(l));
68 	ExitFailure();
69      }
70 
71    /* we probe for device's IP and MAC */
72    if ((shost = libnet_get_ipaddr4(l))==-1)
73      {
74 	fprintf (stderr, "Error: autodetect device ip address failed: %s\n", libnet_geterror(l));
75 	ExitFailure();
76      }
77    if ((ethaddr=libnet_get_hwaddr(l))==NULL)
78      {
79 	fprintf (stderr, "Error: autodetect device MAC address failed: %s\n", libnet_geterror(l));
80 	ExitFailure();
81      }
82 
83    /* generate random values */
84    libnet_seed_prand(l);
85    id = (u_short) libnet_get_prand(LIBNET_PRu16);
86    seq = libnet_get_prand(LIBNET_PRu32);
87    ack = libnet_get_prand(LIBNET_PRu32);
88    urgp = (u_short) libnet_get_prand(LIBNET_PRu16);
89 
90    /* setup ecn */
91    ipv4_tos.dscp=0x02;
92    ipv4_tos.ecn_ct=0;
93    ipv4_tos.ecn_ce=0;
94    UpdateTos();
95 
96    /* erase libnet context *l */
97    if (l)
98      libnet_destroy(l);
99 
100    /* Setup interface for default values*/
101    /* Eth */
102    gtk_entry_set_text (GTK_ENTRY (iface_entry), device);
103    gtk_entry_set_text (GTK_ENTRY (srcmac_entry), emb_hex_ntoa(ethaddr->ether_addr_octet));
104    gtk_entry_set_text (GTK_ENTRY (dstmac_entry), "");
105 
106    /* Ip */
107    gtk_entry_set_text (GTK_ENTRY (srcaddr_entry), libnet_addr2name4(shost, LIBNET_DONT_RESOLVE));
108    gtk_entry_set_text(GTK_ENTRY (dstaddr_entry), "");
109    gtk_entry_set_text (GTK_ENTRY (ttl_entry), "64");
110    gtk_entry_set_text(GTK_ENTRY (id_entry),  ltostr(id));
111 
112    /* Calculate TOS */
113    gtk_entry_set_text (GTK_ENTRY (tos_entry), ltostr(tos));
114 
115    /* Tcp */
116    gtk_entry_set_text (GTK_ENTRY (srcport_entry), "0");
117    gtk_entry_set_text (GTK_ENTRY (dstport_entry), "0");
118    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (syn_checkbutton), TRUE);
119    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fin_checkbutton), FALSE);
120    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (push_checkbutton),FALSE);
121    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ack_checkbutton), FALSE);
122    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rst_checkbutton), FALSE);
123    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (urg_checkbutton), FALSE);
124    gtk_entry_set_text(GTK_ENTRY (seq_entry), ltostr(seq));
125    gtk_entry_set_text(GTK_ENTRY (ack_entry), ltostr(ack));
126    gtk_entry_set_text (GTK_ENTRY (win_entry), "32767");
127    gtk_entry_set_text(GTK_ENTRY (urg_entry), ltostr(urgp));
128 
129    /* Set default variable values */
130    f.syn=1;
131    m.number=10;
132    m.delay=100;
133 
134    return 0;
135 }
136 
SendPacket()137 int SendPacket()
138 {
139    /* BEGIN of variables ' declaration */
140    libnet_t *l;
141    char *dataptr;
142 
143    u_short sport, dport;
144    u_short flag=0x00;
145    short w=0; /* byte written */
146 
147    /* temp variable 4 eth addr */
148    u_char *seth_temp;
149    u_char *deth_temp;
150    u_char *seth=(u_char *) NULL;
151    u_char *deth=(u_char *) NULL;
152    u_char *ethtype;
153    u_short evalue=0x0000;
154 
155    /* other opt */
156    u_short ttl;
157    u_short win;
158 
159    u_long c;
160    /* END of variables declaration */
161 
162    /* initialize libnet context (*l) */
163    if (ck.linkl)
164      {
165 	memset (device, '\0', 10);
166 	strncpy (device, (char *) gtk_entry_get_text(GTK_ENTRY(iface_entry)), 6);
167 	l=libnet_init(LIBNET_LINK, device, ebuf);
168      }
169    else
170      l=libnet_init(LIBNET_RAW4, NULL, ebuf);
171 
172    if (l==NULL)
173      {
174 	info ("Error creating libnet context : %s\n", ebuf);
175 	return -1;
176      }
177 
178    if (ck.debug)
179      info ("Libnet contex created\n");
180 
181    ///* reading global and necessary variables *///
182 
183    /* if we work on datalink */
184    if (ck.linkl)
185      {
186         /* eth */
187 	seth_temp = (u_char *) gtk_entry_get_text(GTK_ENTRY(srcmac_entry));
188 	deth_temp = (u_char *) gtk_entry_get_text(GTK_ENTRY(dstmac_entry));
189 
190 	seth=calloc (6, sizeof (u_char));
191 	deth=calloc (6, sizeof (u_char));
192 	if ((seth=emb_hex_aton(seth_temp))==NULL)
193 	  {
194 	     info ("Invalid source MAC address\n");
195 	     return -1;
196 	  }
197 	if ((deth=emb_hex_aton(deth_temp))==NULL)
198 	  {
199 	     info ("Invalid destination MAC address\n");
200 	     return -1;
201 	  }
202 
203 	ethtype = (u_char *) gtk_entry_get_text (GTK_ENTRY (ethtype_combo_entry));
204 	if (!strcmp(ethtype, "IP"))
205 	  evalue = 0x0800;
206 	else
207 	  evalue = 0x0900;
208      }
209 
210    /* Check values */
211    if (CheckValues()<0)
212      return -1;
213 
214    /* ip */
215    shost = libnet_name2addr4(l, (char *) gtk_entry_get_text (GTK_ENTRY(srcaddr_entry)), LIBNET_RESOLVE);
216    if (shost==-1)
217      {
218 	info ("Invalid source ip address: %s\n", libnet_geterror(l));
219 	return -1;
220      }
221    dhost = libnet_name2addr4(l, (char *) gtk_entry_get_text (GTK_ENTRY(dstaddr_entry)), LIBNET_RESOLVE);
222    if (dhost==-1)
223      {
224 	info ("Invalid destination ip address: %s\n", libnet_geterror(l));
225 	return -1;
226      }
227 
228    tos = atoi (gtk_entry_get_text (GTK_ENTRY(tos_entry)));
229    ttl = atoi (gtk_entry_get_text (GTK_ENTRY(ttl_entry)));
230    id  = atoi (gtk_entry_get_text (GTK_ENTRY(id_entry)));
231 
232    /* tcp */
233    sport = atoi(gtk_entry_get_text(GTK_ENTRY(srcport_entry)));
234    dport = atoi(gtk_entry_get_text (GTK_ENTRY(dstport_entry)));
235    if (f.syn) flag+=TH_SYN;
236    if (f.ack) flag+=TH_ACK;
237    if (f.fin) flag+=TH_FIN;
238    if (f.rst) flag+=TH_RST;
239    if (f.psh) flag+=TH_PUSH;
240    if (f.urg) flag+=TH_URG;
241    if (f.ece) flag+=TH_ECE;
242    if (f.cwr) flag+=TH_CWR;
243    seq = strtoul (gtk_entry_get_text (GTK_ENTRY(seq_entry)), NULL, 10);
244    ack = strtoul (gtk_entry_get_text (GTK_ENTRY(ack_entry)), NULL, 10);
245    win = atoi (gtk_entry_get_text (GTK_ENTRY(win_entry)));
246    urgp = atoi (gtk_entry_get_text (GTK_ENTRY(urg_entry)));
247 
248    /* data */
249    if (ck.data)
250      {
251 	datalen = strlen (data);
252 	if (!datalen)
253 	  {
254 	     info ("You have selected payload option but you haven't entered one\n");
255 	     return -1;
256 	  }
257 	dataptr=data;
258      }
259    else
260      {
261 	datalen = 0;
262 	dataptr = (char *) NULL; /* fixed: libnet_build_tcp(): payload inconsistency */
263      }
264 
265    if (ck.debug)
266      info ("Variables have been read\n");
267 
268    /* build packet */
269    if (libnet_build_tcp(sport, dport,
270 			seq, ack,
271 			flag,
272 			win,
273 			0,
274 			urgp,
275 			LIBNET_TCP_H+datalen,
276 			dataptr,
277 			datalen,
278 			l, 0)==-1)
279      {
280 	info ("Error creating tcp header: %s\n", libnet_geterror(l));
281 	return -1;
282      }
283 
284    if (ck.debug)
285      info ("Tcp header build\n");
286 
287    if (libnet_build_ipv4(40+datalen,
288 			 tos,
289 			 id,
290 			 0,
291 			 ttl,
292 			 IPPROTO_TCP,
293 			 0,
294 			 shost, dhost,
295 			 NULL, 0, l, 0)==-1)
296      {
297 	info ("Error crating ip header: %s\n", libnet_geterror(l));
298 	return -1;
299      }
300 
301    if (ck.debug)
302      info ("Ip header build\n");
303 
304    if (ck.linkl) /* add ethernet header */
305      {
306 	if (libnet_build_ethernet (deth, seth,
307 				   evalue,
308 				   NULL, 0, l, 0)==-1)
309 	  {
310 	     info ("Error creating datalink header: %s\n", libnet_geterror(l));
311 	     return -1;
312 	  }
313 	if (ck.debug)
314 	  info ("Ethernet header build\n");
315      }
316 
317    if (ck.multi)
318      {
319 	info ("Sending %lu packets (delay = %lu ms): ", m.number, m.delay);
320 
321 	for (c=0; c<m.number; c++)
322 	  {
323 	     if ((w=libnet_write(l))==-1)
324 	       {
325 		  info ("Error writing packet : %s\n", libnet_geterror(l));
326 		  return -1;
327 	       }
328 	     info (". ");
329 	     usleep (m.delay*1000);	/* ms */
330 	  }
331 	info ("\nPackets correctly written (%lu x %d bytes)\n", m.number, w);
332      }
333    else
334      {
335 	if ((w=libnet_write(l))==-1)
336 	  {
337 	     info ("Error writing packet : %s\n", libnet_geterror(l));
338 	     return -1;
339 	  }
340 	info ("Packet correctly written (%d bytes)\n", w);
341      }
342 
343    libnet_destroy(l);
344    if (ck.debug)
345      info ("Finished, libnet context closed\n");
346 
347    return 0;
348 
349 }
350 
info(const char * format,...)351 int info (const char *format, ...)
352 {
353    GtkTextIter start, end;
354    GtkTextMark *mark;
355    char s[128]; /* MAX 128! */
356 
357    va_list  ap;
358    va_start (ap, format);
359    vsprintf (s, format, ap);
360 
361    gtk_text_buffer_get_bounds (TextBuffer, &start, &end);
362    gtk_text_buffer_insert (TextBuffer, &end, s, -1);
363    gtk_text_buffer_get_bounds (TextBuffer, &start, &end);
364    mark = gtk_text_buffer_create_mark (TextBuffer, "scrollmark", &end, FALSE);
365    gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (TextView), mark, 0.2, FALSE, 0.0, 0.0);
366    gtk_text_buffer_delete_mark (TextBuffer, mark);
367    gdk_flush();
368 
369    while (g_main_iteration(FALSE));
370 
371    va_end (ap);
372 
373    return 0;
374 }
375 
ExitFailure()376 void ExitFailure()
377 {
378    fprintf (stderr, "\nCritical error! Quitting!\n\n");
379    gtk_exit(-1);
380 }
381 
CheckValues()382 int CheckValues()
383 {
384    /* ip */
385    if (atoi(gtk_entry_get_text(GTK_ENTRY(ttl_entry)))<0 ||
386        atoi(gtk_entry_get_text(GTK_ENTRY(ttl_entry)))>255)
387      {
388 	info ("Invalid time to live (ttl) value\n");
389 	return -1;
390      }
391    if (atol(gtk_entry_get_text(GTK_ENTRY(id_entry)))<0 ||
392        atol(gtk_entry_get_text(GTK_ENTRY(id_entry)))>65535)
393      {
394 	info ("Invalid id value\n");
395 	return -1;
396      }
397    if (atoi(gtk_entry_get_text(GTK_ENTRY(tos_entry)))<0 ||
398        atoi(gtk_entry_get_text(GTK_ENTRY(tos_entry)))>255)
399      {
400 	info ("Invalid type of service (tos) value\n");
401 	return -1;
402      }
403 
404    /* tcp */
405    if (atol(gtk_entry_get_text(GTK_ENTRY(srcport_entry)))<0 ||
406        atol(gtk_entry_get_text(GTK_ENTRY(srcport_entry)))>65535)
407      {
408 	info ("Invalid source port value\n");
409 	return -1;
410      }
411    if (atol(gtk_entry_get_text(GTK_ENTRY(dstport_entry)))<0 ||
412        atol(gtk_entry_get_text(GTK_ENTRY(dstport_entry)))>65535)
413      {
414 	info ("Invalid destination port value\n");
415 	return -1;
416      }
417    if (strtoll(gtk_entry_get_text(GTK_ENTRY(seq_entry)), NULL, 10)<0 ||
418        strtoll(gtk_entry_get_text(GTK_ENTRY(seq_entry)), NULL, 10)>4294967295U)
419      {
420 	info ("Invalid sequence number value\n");
421 	return -1;
422      }
423    if (strtoll(gtk_entry_get_text(GTK_ENTRY(ack_entry)), NULL, 10)<0 ||
424        strtoll(gtk_entry_get_text(GTK_ENTRY(ack_entry)), NULL, 10)>4294967295U)
425      {
426 	info ("Invalid acknowledgment value\n");
427 	return -1;
428      }
429    if (atol(gtk_entry_get_text(GTK_ENTRY(win_entry)))<0 ||
430        atol(gtk_entry_get_text(GTK_ENTRY(win_entry)))>65535)
431      {
432 	info ("Invalid win size value\n");
433 	return -1;
434      }
435    if (atol(gtk_entry_get_text(GTK_ENTRY(urg_entry)))<0 ||
436        atol(gtk_entry_get_text(GTK_ENTRY(urg_entry)))>65535)
437      {
438 	info ("Invalid urg pointer value\n");
439 	return -1;
440      }
441 
442    return 0;
443 }
444 
UpdateTos()445 void UpdateTos()
446 {
447    u_short tos_dscp;
448    u_short tos_ecn_ct;
449 
450    tos_dscp=ipv4_tos.dscp<<2;
451    tos_ecn_ct=ipv4_tos.ecn_ct<<1;
452    tos=tos_dscp|tos_ecn_ct|ipv4_tos.ecn_ce;
453 
454    gtk_entry_set_text(GTK_ENTRY(tos_entry), ltostr(tos));
455 }
456