-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
172 lines (130 loc) · 5.26 KB
/
Copy pathmain.cpp
File metadata and controls
172 lines (130 loc) · 5.26 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
// SX Payload Packer for WIN32
// Based on TX SX Pro Custom Payload Packer by CTCaer
#include <windows.h>
#include <commctrl.h>
#include <string>
#include <fstream>
#include <vector>
#include "hash_sha256.h"
HWND hStatusBar = nullptr;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void repackPayload(std::wstring fullFilename);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"SX";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"SXPayloadPacker", WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT,
300, 70, nullptr, nullptr, hInstance, nullptr);
if (!hwnd) { return 0; }
HWND hLabel = CreateWindowEx(0, L"STATIC", L"Drop payload here", WS_CHILD | WS_VISIBLE | SS_CENTER,
0, 0, 300, 20, hwnd, nullptr, hInstance, nullptr);
if (!hLabel) { return 0; }
hStatusBar = CreateWindowEx(0, STATUSCLASSNAME, nullptr, WS_CHILD | WS_VISIBLE,
0, 30, 200, 20, hwnd, nullptr, hInstance, nullptr);
if (!hStatusBar) { return 0; }
int statuses[] = {SB_SIMPLE};
SendMessage(hStatusBar, SB_SETPARTS, 1, (LPARAM)statuses);
SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)L"");
DragAcceptFiles(hwnd, TRUE);
ShowWindow(hwnd, nCmdShow);
MSG msg {};
while (GetMessage(&msg, nullptr, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DROPFILES:
{
auto hDrop = (HDROP)wParam;
UINT cFiles = DragQueryFile(hDrop, 0xFFFFFFFF, nullptr, 0);
if (cFiles > 1)
{
SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)L"ERROR: Please drop just one file");
return 0;
}
TCHAR szFile[MAX_PATH];
DragQueryFile(hDrop, 0, szFile, MAX_PATH);
repackPayload(szFile);
DragFinish(hDrop);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
}
return 0;
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
void repackPayload(std::wstring fullFilename)
{
const std::string inFilename = std::string(fullFilename.begin(), fullFilename.end());
std::ifstream file(inFilename, std::ios::binary);
if (!file.is_open())
{
const std::string msg = "File not found: " + inFilename;
SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)L"ERROR: File not found");
return;
}
file.seekg(0, std::ios::end);
std::vector<char> payload(file.tellg());
file.seekg(0, std::ios::beg);
file.read(payload.data(), static_cast<std::streamsize>(payload.size()));
file.close();
const std::string filePath = inFilename.substr(0, inFilename.find_last_of('\\'));
const std::string outputOriginalFilename = filePath + "\\boot.dat";
std::string outputFilename = outputOriginalFilename;
unsigned i = 0;
while (std::ifstream(outputFilename).good())
{
outputFilename = outputOriginalFilename + "_" + std::to_string(i++);
}
std::ofstream outfile(outputFilename, std::ios::binary);
if (!outfile.is_open())
{
MessageBoxA(nullptr, "Error opening the target file", "ERROR", MB_OK);
return;
}
std::vector<char> header;
header.assign({0x43, 0x54, 0x43, 0x61, 0x65, 0x72, 0x20, 0x42, 0x4F, 0x4F, 0x54, 0x00,
0x56, 0x32, 0x2E, 0x35 });
hash_sha256 sha;
sha.sha256_init();
sha.sha256_update(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
const sha256_type shaOResult = sha.sha256_final();
header.insert(header.end(), shaOResult.begin(), shaOResult.end());
auto payloadDestination = std::string({ 0x00, 0x00, 0x01, 0x40 });
header.insert(header.end(), payloadDestination.begin(), payloadDestination.end());
header.resize(header.size() + sizeof(unsigned));
*reinterpret_cast<unsigned*>(&header[header.size() - sizeof(unsigned)]) = static_cast<unsigned>(payload.size());
header.resize(header.size() + sizeof(unsigned));
*reinterpret_cast<unsigned*>(&header[header.size() - sizeof(unsigned)]) = 0;
header.resize(header.size() + 0xA4, '\x00');
sha.sha256_init();
sha.sha256_update(reinterpret_cast<const uint8_t*>(header.data()), header.size());
const sha256_type shaHResult = sha.sha256_final();
header.insert(header.end(), shaHResult.begin(), shaHResult.end());
outfile.write(header.data(), static_cast<std::streamsize>(header.size()));
outfile.write(payload.data(), static_cast<std::streamsize>(payload.size()));
outfile.close();
std::wstring msg = std::wstring(L"Saved to: ");
msg.append(outputFilename.begin(), outputFilename.end());
SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)msg.c_str());
}