File: queso-mod.c

package info (click to toggle)
cheops 0.61-15.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,272 kB
  • ctags: 1,312
  • sloc: ansic: 12,731; sh: 155; makefile: 151
file content (491 lines) | stat: -rw-r--r-- 9,907 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/***************************************************************
 *
 * Proyecto:	QueSO ( Que Sistema Operativo ?? )      
 * Autor:       Jordi Murgo <savage@apostols.org>
 * Descripcion:	Determina el tipo de Sistema Operativo de una
 *              maquina concreta a partir del comportamiento 
 *              de su pila TCP/IP ante paquetes TCP 'raros'.
 *              Ver el codigo para mas informacion.
 * Licencia:	GNU GPL 
 *
 * Modified to fit cheops by Mark Spencer
 *
 ***************************************************************
 * Agradecimientos a ToXyN, b0fh y a los colegas del canal #hack
 *		   especialmente a syn por la traduccion del doc
 ***************************************************************
 * CVS: $Id: queso-mod.c,v 1.9 2001/01/22 06:54:23 nbastin Exp $
 ***************************************************************/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

#include <gtk/gtk.h>
#include "tcpip-mod.h"
#include "cheops.h"



static int VECES = 1;
static int caught_idle = 0;
static int caught_timeout = 0;

int try_ports[] = { 80, 139, 23 };

static int queue_lock = 0;

static char CFGFILE[255] = QUESO_CONF;

void debugtcp (unsigned long, tcprec);
void usage (const char *);

spoofrec *squeue=NULL;

#define SILENT	0


/* Fake locking functions.  We'll fill them out later */
static int grab_queue_lock()
{
	if (queue_lock)
		return 0;
	else
		queue_lock = 1;
	return 1;
}

static void free_queue_lock()
{
	queue_lock = 0;
}

int detect_host(spoofrec *sr)
{
	/* -1 means failed to know the OS, try again */

	char *c;
        char *os;
	os = ident_os(sr->dest.sin_addr.s_addr, sr->dport, sr->r);
	if (!os)
		return 0;
	c = strchr(os, ':');
	if (c) {
		c[strlen(c)-1]='\0';
		*c='\0';
		c++;
	}  else	{
		if (sr->retries<MAX_RETRIES) 
			return -1;
		c="desktop.xpm";
	}
	if (c) {
		set_status("Processing replies...");
		add_unique(sr->dest.sin_addr.s_addr, c, os, (struct net_page *)sr->data);
	}
	return 0;

}

void tcpip_socket_cb(void *data, int fd, GdkInputCondition condition)
{
     struct in_addr ia;
     tcprec tcp;
     spoofrec *sr;
     unsigned short dport;
     unsigned short sport;
     unsigned short pos;
     fd_set fds;
     struct timeval tv;

do {
     if ((ia.s_addr = gettcp (&tcp))) {
        dport = ntohs(tcp.dport);
        sport = ntohs(tcp.sport);
	sr = squeue;
	while(sr) {
		if (sr->dest.sin_addr.s_addr == ia.s_addr) {
			if (sr->dport == sport) 
				break;
		}
		sr=sr->next;
	}
	if (sr) {
		pos = dport - sr->sport;
		if (pos <= MAXPKT) {
#ifdef DEBUG
			printf("Received reply '%d' from '%s'\n", pos, inet_ntoa(sr->dest.sin_addr));
#endif
	      		sr->r[pos].seq = tcp.seqnum ? 1 : 0;
	      		sr->r[pos].ack = tcp.acknum ? (ntohl(tcp.acknum)-sr->seq+ACK_HACK) : 0;
	      		if(sr->r[pos].ack > RANDOM_ACK)
			 	sr->r[pos].ack = RANDOM_ACK;
	      		sr->r[pos].win = ntohs (tcp.window);
	      		sr->r[pos].flag = tcp.flags;
	      		sr->r[pos].set = 1;
	      		sr->r[pos].urg = tcp.urgentptr ? 1 : 0;
#if 1
			/* Immediately expire */
			if (pos == MAXPKT) 
				sr->timeout=1;
#endif			
		}
#ifdef DEBUG		
		 else 
			printf("response from '%s' too high (%d)\n", inet_ntoa(sr->dest.sin_addr), pos);
#endif
	}
     }
     tv.tv_sec = 0;
     tv.tv_usec = 0;
     FD_ZERO(&fds);
     FD_SET(fd, &fds);
} while (select(fd + 1, &fds, NULL, NULL, &tv) > 0);
}

static int
xmit_check_os (spoofrec *in)
{
  spoofrec spoof;
  unsigned short s;
  unsigned long myseq;



  srand (time (NULL) & 0x0000ffff);
  in->sport = s = (rand () % 26000) + 4000;
  in->seq = spoof.seq = myseq = rand ();
  in->ack = spoof.ack = 0;
  in->timeout = time(NULL) + LIFE;
  spoof.from = in->from;
  spoof.dest = in->dest;
  spoof.dport = in->dport;


  /*-- PKT 0 --*/
  spoof.sport = s++;
  sendtcp (&spoof, SYN, VECES);
  usleep (Zzz);

  /*-- PKT 1 --*/
  spoof.sport = s++;
  sendtcp (&spoof, SYN | ACK, VECES);
  usleep (Zzz);

  /*-- PKT 2 --*/
  spoof.sport = s++;
  sendtcp (&spoof, FIN, VECES);
  usleep (Zzz);

  /*-- PKT 3 --*/
  spoof.sport = s++;
  sendtcp (&spoof, FIN | ACK, VECES);
  usleep (Zzz);
  
  /*-- PKT 4 --*/
  spoof.sport = s++;
  sendtcp (&spoof, SYN | FIN, VECES);
  usleep (Zzz);

  /*-- PKT 5 --*/
  spoof.sport = s++;
  sendtcp (&spoof, PSH, VECES);
  usleep (Zzz);

  /*-- PKT 6 --*/
  spoof.sport = s++;
  sendtcp (&spoof, SYN | XXX | YYY, VECES);
  usleep (Zzz);

  return 0;
}

char *ident_os(unsigned int dest_addr, short dport, OSRES *r)
{
  FILE *f;
  char line[1024];
  struct in_addr dest;
  dest.s_addr = dest_addr;
  /*---------- CHECK RESULT -----------*/
  if ((f = fopen (find_file(CFGFILE), "r")))
    {
      static char osname[256];		/* should be smaller than line[], 256 should suffice */
      unsigned short flag1 = 0, found = 0, linez = 0;
      unsigned short pn = 0, ps = 0, pa = 0, pw = 0, pf = 0, pu = 0;
      char *p;

      while (fgets (line, sizeof (osname) - 1, f))
	{
	  if (line[0] == '\n')
	    {
	      if (flag1 && found == linez)
		{
		  fclose (f);
		  return osname;
		  printf ("%s:%d\t%s", inet_ntoa (dest), dport, osname);
		  if (osname[1] == '-')
		    return NULL;	/* Not accurate response */
		  else
		    return NULL;
		}
	    }

	  if (line[0] == '*')
	    {
	      strcpy (osname, line);
	      flag1 = 1;
	      found = 0, linez = 0;
	      continue;
	    }

	  /*------ PARSE LINE ---*/
	  linez++;
	  p = strtok (line, " ");
	  if (p && isdigit (*p))
	    pn = atoi (p);
	  else
	    {
	      linez = 0;
	      flag1 = 0;
	      found = 0;
	      continue;
	    }
	  
	  /*-- seq --*/
	  p = strtok (NULL, " ");
	  if (p)
	    ps = atoi (p);

	  /*-- ack --*/
	  p = strtok (NULL, " ");
	  if (p) 
	    {
	      if( *p == 'R' )
		pa = RANDOM_ACK;
	      else if( *p == '+' )
		pa = atoi (p)+ACK_HACK; /*-- extended ACK field --*/
	      else 
		pa = atoi (p);
	    }
	  
	  /*-- win --*/
	  p = strtok (NULL, " ");
	  if (p)
	    pw = 0xffff & strtol (p, NULL, 16);

	  /*-- flags --*/
	  p = strtok (NULL, " \n");
	  if (p)
	    {
	      pf = 0;
	      if (strchr (p, 'S'))
		pf |= SYN;
	      if (strchr (p, 'R'))
		pf |= RST;
	      if (strchr (p, 'A'))
		pf |= ACK;
	      if (strchr (p, 'F'))
		pf |= FIN;
	      if (strchr (p, 'P'))
		pf |= PSH;
	      if (strchr (p, 'X'))
		pf |= XXX;
	      if (strchr (p, 'Y'))
		pf |= YYY;
	      if (strchr (p, 'U'))
		pu = 1;
	      else
		pu = 0;
	    }


	  if (!r[pn].set)
	    {
	      if (pf)
		{
		  found = 0;
		  flag1 = 0;
		  continue;
		}
	      found++;
	      continue;
	    }

	  if ( 
	      ( pa>=ACK_HACK?(pa==r[pn].ack):pa==(r[pn].ack>0) ) &&
	      ps == r[pn].seq &&
	      ((pw == r[pn].win)
	       || (pw == 1 && r[pn].win)
	       || (!pw && !r[pn].win)
	       ) &&
	      pf == r[pn].flag &&
	      pu == r[pn].urg )
	    {
	      found++;
	      continue;
	    }
	  else
	    {
	      found = 0;
	      flag1 = 0;
	      continue;
	    }
	}

      fseek (f, 0L, SEEK_END);
      fclose (f);
      
      return NULL;
    }

  fprintf (stderr, "Can't open RO %s \n", CFGFILE);
  return NULL;
}

int timeout_queue(void *data)
{
	spoofrec *sp, *last=NULL;
	time_t current;
	int n;
	
	if (!grab_queue_lock()) {
#ifdef DEBUG
  		printf("Whoa, locked, can't timeout...\n");
#endif
		/* Try again */
		return TRUE;
	}
	sp = squeue;
	while(sp) {
		current = time(NULL);
		if (current > sp->timeout) {
#ifdef DEBUG
			printf("expiring '%s'\n",inet_ntoa(sp->dest.sin_addr));
#endif
			if (!detect_host(sp)) {
				if (last) {
					last->next = sp->next;
					g_free(sp);
					sp = last;
				} else {
					squeue = sp->next;
					g_free(sp);
					sp = squeue;
				}
			} else {
				/* try again */
#ifdef DEBUG
				printf("retry on '%s'\n", inet_ntoa(sp->dest.sin_addr));
#endif
				sp->retries++;
				if (sp->dport != try_ports[sp->retries]) {
					/* On the last try, do port 23 */
  					for (n = 0; n <= MAXPKT; n++)
      						sp->r[n].set = 0;
					sp->dport = try_ports[sp->retries];
#ifdef DEBUG
					printf("trying alternate port %d\n", sp->dport);
#endif
				}
				xmit_check_os(sp);
				sp->timeout = current + LIFE;
			}
		}
		last = sp;
		if (sp)
			sp=sp->next;
	}
	if (squeue) {
		free_queue_lock();
		return TRUE;
	} else {
		free_queue_lock();
		caught_timeout =0;
		gdk_window_set_cursor(main_window.window->window, NULL);
		set_status("Discovery complete");
		return FALSE;
	}
}

int idle_run_queue(void *data)
{
  spoofrec *sp;
  if (!grab_queue_lock()) {
#ifdef DEBUG
  	printf("Whoa, locked, can't idle...\n");
#endif
	/* Try again */
	return TRUE;
  }
  sp = squeue;
  while(sp) {
  	if (sp->timeout == 0)
		break;
  	sp=sp->next;
  }
  if (!sp) {
  	caught_idle = 0;
	free_queue_lock();
	return FALSE;
  }
  if (sp->timeout == 0) {
  	xmit_check_os (sp);
	free_queue_lock();
	return TRUE;
  } else {
  	caught_idle = 0;
	free_queue_lock();
 	return FALSE;
  }
}

int examine_host_queue(unsigned int addr, unsigned short port, struct net_page *page)
{
  spoofrec *sp;
  int n;
  
  sp = g_new0(spoofrec, 1);
  for (n = 0; n <= MAXPKT; n++)
    {
      sp->r[n].set = 0;
    }
  sp->dest.sin_family = AF_INET;
  sp->dest.sin_addr.s_addr = addr;
  sp->data = page;
  sp->dport = port;
  sp->from.sin_family = AF_INET;
  sp->from.sin_port = 0;
  sp->retries = 0;
  sp->from.sin_addr = getlocalip (sp->dest.sin_addr.s_addr);
    
  if (!sp->from.sin_addr.s_addr)
      {
	fprintf (stderr, "Warning: Unable to determine Local IP for %s\n", inet_ntoa(sp->dest.sin_addr));
      }

  sp->timeout = 0;
  grab_queue_lock();
  sp->next = squeue;
  squeue = sp;
  free_queue_lock();
  if (!caught_idle) {
  	gtk_idle_add(idle_run_queue, NULL);  
	caught_idle =  1;
  }
  if (!caught_timeout) {
  	gtk_timeout_add(100, timeout_queue, NULL);
	caught_timeout = 1;
  }
  return 0;

}