Initial commit

This commit is contained in:
Pin
2021-10-22 22:04:38 -04:00
commit d050bdd5da
6 changed files with 224 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
bin/

17
Makefile Normal file
View File

@@ -0,0 +1,17 @@
LIBRARIES = -Iinclude
OUTPUT_DIR = ./bin
OUTPUT_BIN = ${OUTPUT_DIR}/PROG
OUTPUT = -o ${OUTPUT_BIN}
SOURCES = ./src/* ./cmd/shell.c
build: output_dir
gcc -Wall ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=rshell}
debug: output_dir
gcc -Wall -g ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=rshell}
output_dir:
mkdir -p ${OUTPUT_DIR}
install: build
mv ${OUTPUT_BIN:PROG=rshell} /usr/sbin/rshell

11
README.md Normal file
View File

@@ -0,0 +1,11 @@
# Reverse Shell Shell
**IMPORTANT** this shell is **NOT** meant to be used for any serious applications.
This is for academic purposes only!
## Install
```
make build
make install
```

23
cmd/shell.c Normal file
View File

@@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include "cmd_utils.h"
#define clear() printf("\033[H\033[J")
int main() {
int status_return = 0;
char *cmd;
char **args;
init_shell();
do {
cmd = read_input();
args = split_cmd(cmd);
status_return = reverse_execute(args);
free(cmd);
free(args);
} while(status_return != 255);
return EXIT_SUCCESS;
}

14
include/cmd_utils.h Normal file
View File

@@ -0,0 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
int builtin_func_num();
int show_author();
void init_shell();
int reverse_exit(char **args);
int reverse_help(char **args);
char *read_input();
int reverse_external_execute(char **args);
void reverse_command(char *args);
int reverse_execute(char **args);
char **split_cmd(char *line);

158
src/cmd_utils.c Normal file
View File

@@ -0,0 +1,158 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "cmd_utils.h"
#define clear() printf("\033[H\033[J")
static char *builtin_functions[] = {
"tixe",
"pleh",
"rohtua"
};
int (*builtin_func[]) (char **) = {
&reverse_exit,
&reverse_help,
&show_author
};
int builtin_func_num() {
return sizeof(builtin_functions) / sizeof(char *);
}
int show_author() {
static const char* author_message =
"Author: Spencer\n"
"Description: My first \"reverse shell\"\n"
"(╯°□°)╯︵ ┻━┻\n"
"";
printf("%s", author_message);
return 0;
}
void init_shell() {
static const char* init_message =
"======================================\n"
"= This Shell is *NOT* for Production =\n"
"= For more information run help. =\n"
"= For information on the author: =\n"
"= run author. =\n"
"======================================\n";
clear();
printf("%s", init_message);
return;
}
int reverse_exit(char **args) {
printf("Exiting...\n");
return 255;
}
int reverse_help(char **args) {
printf("ESREVER\n");
return 0;
}
char *read_input() {
int c, input_len = 0;
char *user_input = malloc((input_len+1) * sizeof(char));
printf(">> ");
while((c=getchar()) !='\n' && c != EOF) {
input_len++;
user_input = realloc(user_input, (input_len+1)*sizeof(char));
user_input[input_len-1] = (char)c;
user_input[input_len] = '\0';
}
if(input_len == 0) {
user_input=NULL;
return user_input;
}
return user_input;
}
int reverse_external_execute(char **args) {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
if(execvp(args[0], args) == -1) {
printf("ERROR\n");
}
exit(EXIT_FAILURE);
} else if(pid < 0) {
printf("ERROR\n");
} else {
do {
waitpid(pid, &status, WUNTRACED);
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
void reverse_command(char *args) {
char *temp_string;
temp_string = malloc(strlen(args));
strcpy(temp_string, args);
for(int i = 0; i < strlen(args); i++) {
temp_string[i] = args[strlen(args)-(i+1)];
}
strcpy(args, temp_string);
free(temp_string);
return;
}
int reverse_execute(char **args) {
if (args[0] == NULL) {
return 1;
}
for(int i = 0; i < builtin_func_num(); i++) {
if(strcmp(args[0], builtin_functions[i]) == 0) {
return (*builtin_func[i])(args);
}
}
return reverse_external_execute(args);
}
char **split_cmd(char *line) {
size_t size = 8;
int pos = 0;
char **args = malloc(size * sizeof(char*));
char *arg = NULL;
if(line == NULL) {
*args=NULL;
return args;
}
reverse_command(line);
arg = strtok(line, " \t\r\n\a");
while (arg != NULL) {
args[pos] = arg;
pos++;
if (pos >= size) {
size += 1;
args = realloc(args, size * sizeof(char*));
}
arg = strtok(NULL, " \t\r\n\a");
}
args[pos] = NULL;
return args;
}