From d050bdd5dad96e48a1cf2ea1218cebd59fd9dede Mon Sep 17 00:00:00 2001 From: Pin Date: Fri, 22 Oct 2021 22:04:38 -0400 Subject: [PATCH] Initial commit --- .gitignore | 1 + Makefile | 17 +++++ README.md | 11 +++ cmd/shell.c | 23 +++++++ include/cmd_utils.h | 14 ++++ src/cmd_utils.c | 158 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 224 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 cmd/shell.c create mode 100644 include/cmd_utils.h create mode 100644 src/cmd_utils.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e660fd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9ff9e08 --- /dev/null +++ b/Makefile @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..f77528e --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/cmd/shell.c b/cmd/shell.c new file mode 100644 index 0000000..4616160 --- /dev/null +++ b/cmd/shell.c @@ -0,0 +1,23 @@ +#include +#include +#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; +} diff --git a/include/cmd_utils.h b/include/cmd_utils.h new file mode 100644 index 0000000..b3d4ac2 --- /dev/null +++ b/include/cmd_utils.h @@ -0,0 +1,14 @@ +#include +#include + +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); + diff --git a/src/cmd_utils.c b/src/cmd_utils.c new file mode 100644 index 0000000..734e5c1 --- /dev/null +++ b/src/cmd_utils.c @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include +#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; +}