-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuiMain.cpp
More file actions
363 lines (338 loc) · 13.1 KB
/
GuiMain.cpp
File metadata and controls
363 lines (338 loc) · 13.1 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#define NOMINMAX
#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <dwmapi.h>
#include <tchar.h>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cctype>
#include "ObfuscationOptions.hpp"
#include "Obfusk8Core.hpp"
#include "ObfuscationInline.hpp"
#include "imgui.h"
#include "backends/imgui_impl_win32.h"
#include "backends/imgui_impl_dx11.h"
#include "Menu.hpp"
#include "BackdropTests.hpp"
#include "FeatureColorTests.hpp"
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dwmapi.lib")
static ID3D11Device* g_pd3dDevice = nullptr;
static ID3D11DeviceContext* g_pd3dDeviceContext = nullptr;
static IDXGISwapChain* g_pSwapChain = nullptr;
static ID3D11RenderTargetView* g_mainRenderTargetView = nullptr;
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
static HWND g_hWnd = nullptr;
static bool g_enable_frameless = true;
static bool g_is_minimized = false;
#define TRAY_MSG (WM_APP + 1)
static NOTIFYICONDATA nid = {};
static void AddTrayIcon(HWND hwnd)
{
memset(&nid, 0, sizeof(nid));
nid.cbSize = sizeof(nid);
nid.hWnd = hwnd;
nid.uID = 1;
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
nid.uCallbackMessage = TRAY_MSG;
nid.hIcon = (HICON)LoadIcon(nullptr, IDI_APPLICATION);
lstrcpynW(nid.szTip, L"Obfuscator", ARRAYSIZE(nid.szTip));
Shell_NotifyIcon(NIM_ADD, &nid);
}
static void RemoveTrayIcon()
{
if (nid.hWnd) Shell_NotifyIcon(NIM_DELETE, &nid);
nid = {};
}
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif
#ifndef DWMWA_BORDER_COLOR
#define DWMWA_BORDER_COLOR 34
#endif
#ifndef DWMWA_NCRENDERING_POLICY
#define DWMWA_NCRENDERING_POLICY 2
#endif
#ifndef DWMNCRP_DISABLED
#define DWMNCRP_DISABLED 1
#endif
static void ApplyFrameless(HWND hwnd, bool enable)
{
if (!hwnd) return;
if (enable)
{
LONG style = GetWindowLong(hwnd, GWL_STYLE);
style &= ~(WS_CAPTION);
style |= WS_POPUP | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU;
SetWindowLong(hwnd, GWL_STYLE, style);
LONG ex = GetWindowLong(hwnd, GWL_EXSTYLE);
ex |= WS_EX_APPWINDOW;
SetWindowLong(hwnd, GWL_EXSTYLE, ex);
BOOL dark = TRUE;
DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &dark, sizeof(dark));
COLORREF border = RGB(31,25,66); // match purple background
DwmSetWindowAttribute(hwnd, DWMWA_BORDER_COLOR, &border, sizeof(border));
int policy = DWMNCRP_DISABLED;
DwmSetWindowAttribute(hwnd, DWMWA_NCRENDERING_POLICY, &policy, sizeof(policy));
MARGINS m = { -1, -1, -1, -1 };
DwmExtendFrameIntoClientArea(hwnd, &m);
SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
}
}
static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
return true;
switch (msg)
{
case WM_NCCALCSIZE:
if (g_enable_frameless)
return 0; // remove non-client area
break;
case WM_CLOSE:
OutputDebugStringW(L"WM_CLOSE received\n");
DestroyWindow(hWnd);
return 0;
case WM_SIZE:
g_is_minimized = (wParam == SIZE_MINIMIZED);
if (g_pd3dDevice != nullptr && wParam != SIZE_MINIMIZED)
{
if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = nullptr; }
g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
ID3D11Texture2D* pBackBuffer;
g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &g_mainRenderTargetView);
pBackBuffer->Release();
}
return 0;
case WM_SYSCOMMAND:
if ((wParam & 0xfff0) == SC_KEYMENU) return 0;
break;
case WM_DESTROY:
RemoveTrayIcon();
PostQuitMessage(0);
return 0;
case TRAY_MSG:
if (lParam == WM_LBUTTONDBLCLK)
{
ShowWindow(hWnd, SW_RESTORE);
RemoveTrayIcon();
return 0;
}
break;
case WM_NCHITTEST:
{
LRESULT hit = DefWindowProc(hWnd, msg, wParam, lParam);
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
ScreenToClient(hWnd, &pt);
RECT rc; GetClientRect(hWnd, &rc);
const int edge = 8;
const int top_bar = 60;
bool left = pt.x < edge;
bool right = pt.x > (rc.right - edge);
bool top = pt.y < edge;
bool bottom = pt.y > (rc.bottom - edge);
if (top && left) return HTTOPLEFT;
if (top && right) return HTTOPRIGHT;
if (bottom && left)return HTBOTTOMLEFT;
if (bottom && right)return HTBOTTOMRIGHT;
if (left) return HTLEFT;
if (right) return HTRIGHT;
if (bottom) return HTBOTTOM;
ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureMouse) return HTCLIENT;
if (pt.y < top_bar) return HTCAPTION;
return hit;
}
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
bool MinimizeWindow(HWND hwnd)
{
LRESULT r = SendMessage(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
BOOL ok = ShowWindowAsync(hwnd, SW_MINIMIZE);
if (!ok) OutputDebugStringW(L"Minimize failed\n");
return ok || (r != 0);
}
bool MaximizeToggle(HWND hwnd)
{
WINDOWPLACEMENT wp = { sizeof(WINDOWPLACEMENT) };
if (!GetWindowPlacement(hwnd, &wp)) { OutputDebugStringW(L"GetWindowPlacement failed\n"); return false; }
BOOL ok = ShowWindow(hwnd, (wp.showCmd == SW_SHOWMAXIMIZED) ? SW_RESTORE : SW_MAXIMIZE);
if (!ok) OutputDebugStringW(L"ShowWindow maximize/restore failed\n");
return ok;
}
bool CloseWindowSafe(HWND hwnd)
{
LRESULT r = SendMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
if (!PostMessage(hwnd, WM_CLOSE, 0, 0)) { OutputDebugStringW(L"PostMessage(WM_CLOSE) failed\n"); }
// Fallback: destroy immediately and exit if message loop didn't quit
DestroyWindow(hwnd);
return (r != 0);
}
static bool CreateDeviceD3D(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC sd = {};
sd.BufferCount = 2;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd;
sd.SampleDesc.Count = 1;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
UINT createDeviceFlags = 0;
D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevelArray[1] = { D3D_FEATURE_LEVEL_11_0 };
if (D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, featureLevelArray, 1,
D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
return false;
ID3D11Texture2D* pBackBuffer;
g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &g_mainRenderTargetView);
pBackBuffer->Release();
return true;
}
static void CleanupDeviceD3D()
{
if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = nullptr; }
if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = nullptr; }
if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = nullptr; }
if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; }
}
static void CreateRenderTarget()
{
ID3D11Texture2D* pBackBuffer;
g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &g_mainRenderTargetView);
pBackBuffer->Release();
}
static void ResetRenderTarget()
{
if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = nullptr; }
CreateRenderTarget();
}
static std::vector<char> readBinaryFile(const std::string& filename)
{
std::ifstream file(filename, std::ios::binary);
if (!file) throw std::runtime_error(OBFUSCATE_STRING("Cannot open input file: ") + filename);
return std::vector<char>((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
}
static void writeBinaryFile(const std::string& filename, const std::vector<char>& data)
{
std::ofstream file(filename, std::ios::binary);
if (!file) throw std::runtime_error(OBFUSCATE_STRING("Cannot open output file: ") + filename);
file.write(data.data(), data.size());
}
static bool OpenFileDialog(std::string& outPath)
{
OPENFILENAMEW ofn = {};
wchar_t szFile[MAX_PATH] = L"";
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = nullptr;
ofn.lpstrFile = szFile;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = L"Executable Files (*.exe)\0*.exe\0All Files (*.*)\0*.*\0";
ofn.nFilterIndex = 1;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileNameW(&ofn))
{
int len = lstrlenW(szFile);
int size_needed = WideCharToMultiByte(CP_UTF8, 0, szFile, len, nullptr, 0, nullptr, nullptr);
outPath.resize(size_needed);
WideCharToMultiByte(CP_UTF8, 0, szFile, len, &outPath[0], size_needed, nullptr, nullptr);
return true;
}
return false;
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int)
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, _T("ObfuscatorV5"), nullptr };
RegisterClassEx(&wc);
HWND hwnd = CreateWindowEx(WS_EX_APPWINDOW, wc.lpszClassName, _T("ObfuscatorV5"), WS_POPUP | WS_THICKFRAME, 100, 100, 900, 700, nullptr, nullptr, wc.hInstance, nullptr);
g_hWnd = hwnd;
ApplyFrameless(hwnd, g_enable_frameless);
if (!CreateDeviceD3D(hwnd)) { CleanupDeviceD3D(); UnregisterClass(wc.lpszClassName, wc.hInstance); return 1; }
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ApplyStyle();
SetupFonts();
ImGui_ImplWin32_Init(hwnd);
ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
ObfuscationOptions opts;
char key = 0x42;
std::string status;
static std::vector<std::string> logLines;
RunBackdropUnitTests(logLines);
RunFeatureColorTests(logLines);
static MenuState ui;
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
continue;
}
if (g_is_minimized) { Sleep(16); continue; }
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
MenuOutput m = RenderMenu(ui, opts, logLines, status, g_hWnd);
if (m.request_browse)
{
std::string path;
if (OpenFileDialog(path)) {
strncpy_s(ui.inputBuf, sizeof(ui.inputBuf), path.c_str(), _TRUNCATE);
}
}
if (m.request_obfuscate)
{
try {
status.clear();
std::string inputFile = ui.inputBuf;
std::string outputFile = ui.outputBuf;
std::vector<char> data = readBinaryFile(inputFile);
std::stringstream capture;
std::streambuf* oldBuf = std::cout.rdbuf(capture.rdbuf());
obfuscate(data, key, opts);
std::cout.rdbuf(oldBuf);
writeBinaryFile(outputFile, data);
status = std::string("Obfuscated: ") + outputFile;
std::string logs = capture.str();
size_t start = 0; while (true) { size_t pos = logs.find('\n', start); std::string line; if (pos == std::string::npos) { if (start < logs.size()) { line = logs.substr(start); } else { break; } } else { line = logs.substr(start, pos - start); start = pos + 1; }
line.erase(line.begin(), std::find_if(line.begin(), line.end(), [](unsigned char ch){ return !std::isspace(ch); }));
line.erase(std::find_if(line.rbegin(), line.rend(), [](unsigned char ch){ return !std::isspace(ch); }).base(), line.end());
if (!line.empty()) logLines.push_back(line);
if (pos == std::string::npos) break; }
} catch (const std::exception& e) {
status = std::string("Error: ") + e.what();
logLines.push_back(status);
}
}
ImGui::Render();
const float clear_color_with_alpha[4] = { 31.0f/255.0f, 25.0f/255.0f, 66.0f/255.0f, 1.0f };
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, nullptr);
g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
g_pSwapChain->Present(1, 0);
}
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
CleanupDeviceD3D();
DestroyWindow(hwnd);
UnregisterClass(_T("ObfuscatorV5"), wc.hInstance);
return 0;
}