Skip to content

Instantly share code, notes, and snippets.

@aixxe
Last active October 29, 2025 02:29
Show Gist options
  • Select an option

  • Save aixxe/73669496749d38f6c3eefe6343ed45fa to your computer and use it in GitHub Desktop.

Select an option

Save aixxe/73669496749d38f6c3eefe6343ed45fa to your computer and use it in GitHub Desktop.
Unoptimized standalone MessageBoxW in C++ (x64)
#include <cstdint>
#include <windows.h>
#include <winternl.h>
/**
* djb2 hash algorithm, but case insensitive
* http://www.cse.yorku.ca/~oz/hash.html
*/
auto constexpr __forceinline make_hash(auto* str) -> std::uint32_t
{
auto hash = 5381ul;
while (auto const c = *str++)
hash = (hash << 5) + hash + (c & 0xDF);
return hash;
}
auto __forceinline find_module_by_hash(const std::uint32_t hash) -> std::uint8_t*
{
auto const peb = reinterpret_cast<PPEB>(__readgsqword(0x60));
auto const head = &peb->Ldr->InMemoryOrderModuleList;
for (auto current = head->Flink; current != head; current = current->Flink)
{
auto const entry = CONTAINING_RECORD(current, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
auto const basename = reinterpret_cast<UNICODE_STRING*>
(reinterpret_cast<BYTE*>(&entry->FullDllName) + sizeof(UNICODE_STRING));
if (make_hash(basename->Buffer) == hash)
return static_cast<std::uint8_t*>(entry->DllBase);
}
return nullptr;
}
auto __forceinline find_export_by_hash(std::uint8_t* dll, const std::uint32_t hash) -> std::uint8_t*
{
auto const dos = reinterpret_cast<PIMAGE_DOS_HEADER>(dll);
auto const nt = reinterpret_cast<PIMAGE_NT_HEADERS>(dll + dos->e_lfanew);
auto const [va, size] = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
auto const dir = reinterpret_cast<PIMAGE_EXPORT_DIRECTORY>(dll + va);
auto const names = reinterpret_cast<DWORD*>(dll + dir->AddressOfNames);
auto const functions = reinterpret_cast<DWORD*>(dll + dir->AddressOfFunctions);
auto const ordinals = reinterpret_cast<WORD*>(dll + dir->AddressOfNameOrdinals);
for (auto i = 0; i < dir->NumberOfNames; ++i)
if (make_hash(dll + names[i]) == hash)
return dll + functions[ordinals[i]];
return nullptr;
}
__attribute__((section(".text"))) alignas(1) const char USER32_name[] = "USER32.DLL";
__attribute__((section(".text"))) alignas(1) const char MessageBoxW_name[] = "MessageBoxW";
__attribute__((section(".text"))) alignas(1) const wchar_t caption[] = L"( •_•)>⌐■-■";
__attribute__((section(".text"))) alignas(1) const wchar_t text[] = L"(⌐■_■)";
auto entrypoint()
{
auto constexpr KERNEL32_hash = make_hash(L"kernel32.dll");
auto constexpr LoadLibraryA_hash = make_hash("LoadLibraryA");
auto constexpr GetProcAddress_hash = make_hash("GetProcAddress");
auto const kernel32 = find_module_by_hash(KERNEL32_hash);
auto const LoadLibraryA_ = reinterpret_cast<decltype(LoadLibraryA)*>
(find_export_by_hash(kernel32, LoadLibraryA_hash));
auto const GetProcAddress_ = reinterpret_cast<decltype(GetProcAddress)*>
(find_export_by_hash(kernel32, GetProcAddress_hash));
auto const handle = LoadLibraryA_(USER32_name);
auto const MessageBoxW_ = reinterpret_cast<decltype(MessageBoxW)*>
(GetProcAddress_(handle, MessageBoxW_name)); // alternatively, MAKEINTRESOURCE(0x86E)
MessageBoxW_(nullptr, text, caption, MB_OK | MB_ICONINFORMATION);
}

From a MSYS2 shell, switch to the UCRT64 environment and install prerequisites

# source shell ucrt64
# pacman -Syu --needed mingw-w64-ucrt-x86_64-toolchain vim

Compile standalone.cc to an object file

# g++ -std=c++23 standalone.cc -c -Oz -fno-exceptions -fno-asynchronous-unwind-tables

Copy the .text section to standalone.bin

# objcopy --dump-section .text=standalone.bin standalone.o

Strip any trailing NOP bytes (there's probably a better way to do this)

# truncate -s $(($(grep -aobP '[^\x90]' standalone.bin | tail -1 | cut -d: -f1) + 1)) standalone.bin

Dump to C array

# xxd -i standalone.bin
unsigned char standalone_bin[] = {
  0x6a, 0x60, 0x58, 0x65, 0x67, 0x48, 0x8b, 0x00, 0x48, 0x8b, 0x40, 0x18,
  0x4c, 0x8d, 0x48, 0x20, 0x48, 0x8b, 0x40, 0x20, 0x49, 0x39, 0xc1, 0x0f,
  0x84, 0x35, 0x01, 0x00, 0x00, 0x4c, 0x8b, 0x40, 0x50, 0xba, 0x05, 0x15,
  0x00, 0x00, 0x66, 0x41, 0x8b, 0x08, 0x66, 0x85, 0xc9, 0x74, 0x11, 0x6b,
  0xd2, 0x21, 0x81, 0xe1, 0xdf, 0x00, 0x00, 0x00, 0x49, 0x83, 0xc0, 0x02,
  0x01, 0xca, 0xeb, 0xe6, 0x81, 0xfa, 0xf5, 0xe4, 0xe1, 0x20, 0x75, 0x30,
  0x57, 0x56, 0x53, 0x48, 0x83, 0xec, 0x20, 0x48, 0x8b, 0x40, 0x20, 0x48,
  0x63, 0x50, 0x3c, 0x8b, 0x94, 0x10, 0x88, 0x00, 0x00, 0x00, 0x48, 0x01,
  0xc2, 0x8b, 0x5a, 0x20, 0x44, 0x8b, 0x4a, 0x1c, 0x44, 0x8b, 0x52, 0x24,
  0x44, 0x8b, 0x5a, 0x18, 0x31, 0xd2, 0x48, 0x8d, 0x3c, 0x18, 0xeb, 0x08,
  0x48, 0x8b, 0x00, 0xeb, 0x97, 0x48, 0xff, 0xc2, 0x44, 0x39, 0xda, 0x73,
  0x44, 0x44, 0x8b, 0x04, 0x97, 0xb9, 0x05, 0x15, 0x00, 0x00, 0x49, 0x01,
  0xc0, 0x41, 0x8a, 0x30, 0x40, 0x84, 0xf6, 0x74, 0x10, 0x6b, 0xc9, 0x21,
  0x81, 0xe6, 0xdf, 0x00, 0x00, 0x00, 0x49, 0xff, 0xc0, 0x01, 0xf1, 0xeb,
  0xe8, 0x81, 0xf9, 0xdb, 0x2f, 0x07, 0xb7, 0x75, 0xcc, 0x48, 0x01, 0xd2,
  0x48, 0x01, 0xc2, 0x42, 0x0f, 0xb7, 0x14, 0x12, 0x48, 0x8d, 0x14, 0x90,
  0x42, 0x8b, 0x34, 0x0a, 0x48, 0x01, 0xc6, 0xeb, 0x02, 0x31, 0xf6, 0x31,
  0xd2, 0x48, 0x01, 0xc3, 0x44, 0x39, 0xda, 0x73, 0x4a, 0x44, 0x8b, 0x04,
  0x93, 0xb9, 0x05, 0x15, 0x00, 0x00, 0x49, 0x01, 0xc0, 0x41, 0x8a, 0x38,
  0x40, 0x84, 0xff, 0x74, 0x10, 0x6b, 0xc9, 0x21, 0x81, 0xe7, 0xdf, 0x00,
  0x00, 0x00, 0x49, 0xff, 0xc0, 0x01, 0xf9, 0xeb, 0xe8, 0x81, 0xf9, 0xbf,
  0xc1, 0xcf, 0xde, 0x75, 0x19, 0x48, 0x01, 0xd2, 0x48, 0x01, 0xc2, 0x42,
  0x0f, 0xb7, 0x14, 0x12, 0x48, 0x8d, 0x14, 0x90, 0x42, 0x8b, 0x14, 0x0a,
  0x48, 0x8d, 0x1c, 0x10, 0xeb, 0x07, 0x48, 0xff, 0xc2, 0xeb, 0xb1, 0x31,
  0xdb, 0x48, 0x8d, 0x0d, 0x66, 0x00, 0x00, 0x00, 0xff, 0xd6, 0x48, 0x8d,
  0x15, 0x51, 0x00, 0x00, 0x00, 0x48, 0x91, 0xff, 0xd3, 0x6a, 0x40, 0x41,
  0x59, 0x4c, 0x8d, 0x05, 0x2a, 0x00, 0x00, 0x00, 0x48, 0x8d, 0x15, 0x15,
  0x00, 0x00, 0x00, 0x31, 0xc9, 0x48, 0x83, 0xc4, 0x20, 0x5b, 0x5e, 0x5f,
  0xff, 0xe0, 0x8b, 0x04, 0x25, 0x3c, 0x00, 0x00, 0x00, 0x0f, 0x0b, 0x90,
  0x28, 0x00, 0x10, 0x23, 0xa0, 0x25, 0x5f, 0x00, 0xa0, 0x25, 0x29, 0x00,
  0x00, 0x00, 0x28, 0x00, 0x20, 0x00, 0x22, 0x20, 0x5f, 0x00, 0x22, 0x20,
  0x29, 0x00, 0x3e, 0x00, 0x10, 0x23, 0xa0, 0x25, 0x2d, 0x00, 0xa0, 0x25,
  0x00, 0x00, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x42, 0x6f, 0x78,
  0x57, 0x00, 0x55, 0x53, 0x45, 0x52, 0x33, 0x32, 0x2e, 0x44, 0x4c, 0x4c,
  0x00
};
unsigned int standalone_bin_len = 409;

sclauncher can be used for testing

# ./sclauncher64.exe -f=standalone.bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment