一个后门

参照云舒大神博客上的后门框架做了一些改动,另外自己写了服务端。

不看windows了。

不看windows了。。

代码地址

https://github.com/superfish9/wint/tree/master

进程(C代码)

Unix下一个简单的shell,Ctrl-C显示10条历史命令的功能还没弄好,以后抽时间弄吧。。

////////////////////////////////////////////////////////////////
//author : superfish
//date : 2015/12/10
//name : simpleshell.c
////////////////////////////////////////////////////////////////
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
 
#define MAXLINE 80
#define HISTORYLEN 10
 
typedef struct
{
	int num;
	char hisline[HISTORYLEN][MAXLINE];
}HISTORY;
 
HISTORY history;
 
int setup(char inputBuffer[], char *args[], int *background);
int insertHISTORY(char inputBuffer[]);
int handle_SIGINT();
 
int main(void)
{
	// 创建信号处理器
	struct sigaction handler;
	handler.sa_handler = handle_SIGINT;
	sigaction(SIGINT, &handler, NULL);
 
	char inputBuffer[MAXLINE];
	int background, i, set;
	char *args[MAXLINE/2+1];
	pid_t pid;
 
	history.num = 0;
 
	while(1){
		background = 0;
		printf("COMMAND -> ");
 
		set = setup(inputBuffer, args, &background);
		if(set == -1){
			for(i = 0;(args[i] != NULL) && (i < MAXLINE/2 + 1);i++){
				free(args[i]);
			}
			printf("Params are too many!\n");
			continue;
		}
		if(set == -2){
			printf("No fit line in last %d lines!\n", HISTORYLEN);
			continue;
		}
		if(set == -3){
			printf("Line is too long!\n");
			continue;
		}
		if(set == -4){
			continue;
		}
		if(set == -5){
			printf("History lines is empty!\n");
			continue;
		}
		pid = fork();
		if(pid < 0){
			fprintf(stderr, "Fork failed!\n");
			exit(-1);
		}else if(pid == 0){
			execvp(args[0], args);
			exit(0);
		}else{
			if(!background){
				wait(NULL);
			}else{
				strcat(inputBuffer, "&");
			}
			insertHISTORY(inputBuffer);
			for(i = 0;args[i] != NULL;i++){
				free(args[i]);
			}
		}
	}
 
	return 0;
}
 
int setup(char inputBuffer[], char *args[], int *background)
{
	int len, i, j;
	char *p, *q;
 
	memset(inputBuffer, 0, strlen(inputBuffer));
	fgets(inputBuffer, MAXLINE, stdin);
	len = strlen(inputBuffer);
 
	if(inputBuffer[0] == '\n'){
		return -4; //空命令
	}
 
	if(inputBuffer[len-1] != '\n'){
		if(len < MAXLINE-1){
			exit(1); //处理Ctrl-D
		}
		fflush(stdin);
		return -3; //命令超长度限制
	}
	inputBuffer[len-1] = '\0'; //去掉\n
	len--;
 
	if(inputBuffer[0] == 'r'){
		if(inputBuffer[1] == '\0'){
			if(history.num == 0){
				return -5; //历史命令为空
			}
			if(history.num < HISTORYLEN){
				strcpy(inputBuffer, history.hisline[history.num-1]);
			}else{
				strcpy(inputBuffer, history.hisline[HISTORYLEN-1]);
			}
			printf("%s\n", inputBuffer);
		}else if(inputBuffer[1] == ' '){
			for(i = 0;i < HISTORYLEN;i++){
				if(history.hisline[HISTORYLEN-1-i][0] == inputBuffer[2]){
					strcpy(inputBuffer, history.hisline[HISTORYLEN-1-i]);
					printf("%s\n", inputBuffer);
					break;
				}
			}
			if(i == HISTORYLEN){
				return -2; //历史命令里没有
			}
		}else{
			;
		}
	}
 
	if(inputBuffer[len-1] == '&'){
		*background = 1;
		inputBuffer[len-1] = '\0';
	}
 
	p = (char *)malloc(MAXLINE);
	q = p;
	strcpy(p, inputBuffer);
	for(i = 0, j = 0;j <= strlen(inputBuffer);j++){
		if(inputBuffer[j] == ' ' || inputBuffer[j] == '\0'){
			*(p+j) = '\0';
 
			if(!(i < MAXLINE/2 + 1)){
				free(q);
				return -1; //参数数量太多
			}
			args[i] = (char *)malloc(strlen(p)+1);
			strcpy(args[i], p);
			p += (j+1);
			i++;
		}
	}
	args[i] = NULL;
	free(q);
 
	return 0;
}
 
int insertHISTORY(char inputBuffer[])
{
	int i;
 
	if(history.num < HISTORYLEN){
		strcpy(history.hisline[history.num], inputBuffer);
	}else{
		for(i = 0;i < HISTORYLEN - 1;i++){
			strcpy(history.hisline[i], history.hisline[i+1]);
		}
		strcpy(history.hisline[i], inputBuffer);
	}
	history.num++;
 
	return 0;
}
 
int handle_SIGINT()
{
	int i, j;
	char buffer[MAXLINE];
	memset(buffer, 0, MAXLINE);
 
	if(history.num = 0){
		strcpy(buffer, "History lines is empty!\n");
		write(STDOUT_FILENO, buffer, strlen(buffer));
 
		return -1;
	}
 
	for(i = history.num - MAXLINE + 1, j = 0;i <= history.num;i++){
		if(i <= 0){
			continue;
		}
		sprintf(buffer, "%s", i);
		strcat(buffer, "  ");
		strcat(buffer, history.hisline[j]);
		write(STDOUT_FILENO, buffer, strlen(buffer));
		memset(buffer, 0, MAXLINE);
		j++;
	}
 
	return 0;
}