Skip to content

Commit

Permalink
[WIP] Refactorizing code
Browse files Browse the repository at this point in the history
  • Loading branch information
S0S4 committed Oct 8, 2024
1 parent 1823bad commit 1c19feb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
Binary file modified client/client
Binary file not shown.
6 changes: 5 additions & 1 deletion client/include/cipher.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef cipher_h
#define cipher_h

#include "../include/fraction.h"

#include <openssl/ssl.h>
Expand All @@ -12,5 +15,6 @@ typedef struct{
size_t text_size;
} decrypted;

decrypted *decrypt_fraction(fraction_t *fraction);

decrypted * decrypt_fraction(fraction_t *fraction);
#endif
7 changes: 1 addition & 6 deletions client/include/crc32.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
#include <stdint.h>
#include <stdlib.h>
#include "fraction.h"

extern const uint32_t crc32_tab[];
/*
* A function that calculates the CRC-32 based on the table above is
* given below for documentation purposes. An equivalent implementation
* of this function that's actually used in the kernel can be found
* in sys/libkern.h, where it can be inlined.
*/
uint32_t crc32(const void *buf, size_t size);
#endif // CRC32_H
2 changes: 1 addition & 1 deletion client/include/load.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
int load_lkm(const unsigned char* lkm, ssize_t total_size);
int is_lkm_loaded(const char *name);
int remove_lkm();
int *create_module(int num_links,fraction_t *fractions);
int create_lkm(int num_links,fraction_t *fractions);
20 changes: 12 additions & 8 deletions client/src/load.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../include/load.h"
#include "../include/cipher.h"

int remove_lkm(){

Expand All @@ -12,7 +13,6 @@ int remove_lkm(){
return WEXITSTATUS(result);
}


int is_lkm_loaded(const char* name){

DIR *dir = opendir("/sys/module/");
Expand Down Expand Up @@ -66,40 +66,44 @@ int load_lkm(const unsigned char *lkm,ssize_t total_size){
return 0;
}

int create_module(int num_links,fraction_t *fractions){
int create_lkm(int num_links,fraction_t *fractions){

unsigned char *module = NULL;
ssize_t total_size = 0;
ssize_t module_size = 0;
decrypted *decrstr;

for (int i = 0; i < num_links; i++) {

decrstr = decrypt_fraction( &fractions[i]);

if (decrstr -> decryptedtext == NULL) {
log_error("Decryption process failed");
continue;
return -1;
}
if (module == NULL) {
total_size = decrstr -> text_size;
module = malloc(total_size);
if (module == NULL) {
log_error("Error in memory assigning");
break;
return -1;
}
} else if (module_size + decrstr -> text_size > total_size) {
total_size += decrstr -> text_size;
unsigned char * tmp = realloc(module, total_size);
if (tmp == NULL) {
log_error("Memory reallocation failed");
break;
return -1;
}
module = tmp;
}
memcpy(module + module_size, decrstr -> decryptedtext, decrstr -> text_size);
module_size += decrstr -> text_size;
}
load_lkm(module, total_size);

return decrstr;
if(load_lkm(module, total_size) < 0){
log_error("There was an error loading the LKM");
}


return 0;
}
8 changes: 5 additions & 3 deletions client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main(void) {
http_res_t http_post_res = {0};
char **fraction_links = NULL;
fraction_t *fractions = NULL;
decrypted *module;
int module;

if(geteuid() != 0){
fprintf(stderr,"This program needs to be run as root!\n");
Expand Down Expand Up @@ -105,9 +105,11 @@ int main(void) {

for (int i=0; i<lines_read; i++) {print_fraction(fractions[i]);}

module = create_module(num_links,fractions);
if(module == create_lkm(num_links,fractions) < 0){
log_error("There was an error creating the lkm");
}


free(module);
http_free(&http_post_res);
http_free(&http_fraction_res);
cleanup_char_array(fraction_links, num_links);
Expand Down

0 comments on commit 1c19feb

Please sign in to comment.