-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_embedder.cpp
280 lines (236 loc) · 9.59 KB
/
hash_embedder.cpp
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
// hash_embedder.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdint>
#include <cstring>
#include <algorithm>
#include <mach-o/loader.h>
#include <mach-o/fat.h>
// Utility functions
uint64_t fnv1a_64(const uint8_t *data, size_t length)
{
const uint64_t FNV_PRIME = 0x100000001b3ULL;
const uint64_t OFFSET_BASIS = 0xcbf29ce484222325ULL;
uint64_t hash = OFFSET_BASIS;
for (size_t i = 0; i < length; ++i)
{
hash ^= data[i];
hash *= FNV_PRIME;
}
return hash;
}
__attribute__((always_inline)) inline uint8_t prng(uint8_t seed)
{
return (seed * 1103515245 + 12345) & 0xFF;
}
// Encryption function
__attribute__((always_inline)) inline void encrypt_text_section(uint8_t *remap, size_t text_size, uint8_t *key, size_t key_length)
{
for (size_t i = 0, key_index = 0; i < text_size; i++)
{
remap[i] ^= key[key_index];
key_index = (key_index + 1) % key_length;
key[key_index] = prng(key[key_index]);
}
}
// Mach-O processing functions
uint64_t compute_text_section_hash(const std::vector<uint8_t> &file_data, uint64_t &hash_offset, uint64_t &text_offset,
uint64_t &text_end_offset, uint64_t &init_offsets, uint64_t &init_offsets_end,
uint64_t &mod_init_funcs, uint64_t &mod_init_funcs_end, uint64_t &decrypt_init)
{
const mach_header_64 *header = reinterpret_cast<const mach_header_64 *>(file_data.data());
const load_command *cmd = reinterpret_cast<const load_command *>(file_data.data() + sizeof(mach_header_64));
uint64_t hash = 0;
for (uint32_t i = 0; i < header->ncmds; i++)
{
if (cmd->cmd == LC_SEGMENT_64)
{
const segment_command_64 *seg = reinterpret_cast<const segment_command_64 *>(cmd);
const section_64 *sec = reinterpret_cast<const section_64 *>(seg + 1);
for (uint32_t j = 0; j < seg->nsects; j++, sec++)
{
if (strcmp(seg->segname, "__TEXT") == 0 && strcmp(sec->sectname, "__text") == 0)
{
uintptr_t text_start = reinterpret_cast<uintptr_t>(header) + sec->offset;
uintptr_t text_end = text_start + sec->size;
std::vector<uint8_t> page_data(sec->size);
memcpy(page_data.data(), reinterpret_cast<const void *>(text_start), sec->size);
hash = fnv1a_64(page_data.data(), sec->size);
text_offset = sec->offset;
text_end_offset = text_offset + sec->size;
}
else if (strcmp(seg->segname, "__DATA") == 0 && strcmp(sec->sectname, "__hash") == 0)
{
hash_offset = sec->offset;
}
else if (strcmp(seg->segname, "__TEXT") == 0 && strcmp(sec->sectname, "__init_offsets") == 0)
{
init_offsets = sec->offset;
init_offsets_end = init_offsets + sec->size;
}
else if (strcmp(seg->segname, "__TEXT") == 0 && strcmp(sec->sectname, "__init") == 0)
{
decrypt_init = sec->offset;
}
else if (strcmp(seg->segname, "__DATA_CONST") == 0 && strcmp(sec->sectname, "__mod_init_func") == 0)
{
mod_init_funcs = sec->offset;
mod_init_funcs_end = mod_init_funcs + sec->size;
}
}
}
cmd = reinterpret_cast<const load_command *>(reinterpret_cast<const uint8_t *>(cmd) + cmd->cmdsize);
}
return hash;
}
void embed_hash(const std::string &filename, uint64_t hash, uint64_t offset)
{
std::fstream file(filename, std::ios::in | std::ios::out | std::ios::binary);
if (!file)
{
std::cerr << "Failed to open file: " << filename << std::endl;
return;
}
file.seekp(offset);
file.write(reinterpret_cast<const char *>(&hash), sizeof(hash));
file.close();
std::cout << "Embedded hash into " << filename << " at offset: 0x" << std::hex << offset << std::endl;
}
void encrypt_binary(const std::string &filename, uint64_t offset, uint64_t end_offset)
{
std::fstream file(filename, std::ios::in | std::ios::out | std::ios::binary);
if (!file)
{
std::cerr << "Failed to open file: " << filename << std::endl;
return;
}
file.seekp(offset);
std::vector<uint8_t> data(end_offset - offset);
file.read(reinterpret_cast<char *>(data.data()), data.size());
uint8_t key[] = {0x12, 0x34, 0x56, 0x78};
size_t key_length = sizeof(key) / sizeof(key[0]);
encrypt_text_section(data.data(), data.size(), key, key_length);
file.seekp(offset);
file.write(reinterpret_cast<const char *>(data.data()), data.size());
file.close();
std::cout << "Encrypted binary " << filename << " from offset: 0x" << std::hex << offset << " to 0x" << end_offset << std::endl;
}
void reorder_init_offsets(const std::string &filename, uint64_t offset, uint64_t end_offset, uint64_t decrypt_init)
{
std::fstream file(filename, std::ios::in | std::ios::out | std::ios::binary);
if (!file)
{
std::cerr << "Failed to open file: " << filename << std::endl;
return;
}
file.seekp(offset);
std::vector<uint32_t> data((end_offset - offset) / sizeof(uint32_t));
file.read(reinterpret_cast<char *>(data.data()), data.size() * sizeof(uint32_t));
auto it = std::find(data.begin(), data.end(), decrypt_init);
if (it != data.end())
{
std::rotate(data.begin(), it, it + 1);
}
file.seekp(offset);
file.write(reinterpret_cast<const char *>(data.data()), data.size() * sizeof(uint32_t));
file.close();
std::cout << "Reordered __init_offsets in " << filename << " from offset: 0x" << std::hex << offset << " to 0x" << end_offset << std::endl;
}
void reorder_mod_init_funcs(const std::string &filename, uint64_t offset, uint64_t end_offset, uint64_t decrypt_init)
{
std::fstream file(filename, std::ios::in | std::ios::out | std::ios::binary);
if (!file)
{
std::cerr << "Failed to open file: " << filename << std::endl;
return;
}
file.seekp(offset);
std::vector<uint64_t> data((end_offset - offset) / sizeof(uint64_t));
file.read(reinterpret_cast<char *>(data.data()), data.size() * sizeof(uint64_t));
auto it = std::find(data.begin(), data.end(), decrypt_init);
if (it != data.end())
{
std::rotate(data.begin(), it, it + 1);
}
file.seekp(offset);
file.write(reinterpret_cast<const char *>(data.data()), data.size() * sizeof(uint64_t));
file.close();
std::cout << "Reordered __mod_init_funcs in " << filename << " from offset: 0x" << std::hex << offset << " to 0x" << end_offset << std::endl;
}
void process_macho(const std::vector<uint8_t> &data, const std::string &filename, uint64_t fat_offset = 0)
{
uint64_t hash_offset = 0, text_start = 0, text_end = 0, init_offsets = 0, init_offsets_end = 0,
mod_init_funcs = 0, mod_init_funcs_end = 0, decrypt_init = 0;
uint64_t hash = compute_text_section_hash(data, hash_offset, text_start, text_end, init_offsets,
init_offsets_end, mod_init_funcs, mod_init_funcs_end, decrypt_init);
if (hash_offset > 0)
{
embed_hash(filename, hash, fat_offset + hash_offset);
}
else
{
std::cerr << "Failed to find __hash section in " << filename << std::endl;
}
if (init_offsets > 0 && init_offsets_end > 0 && decrypt_init > 0)
{
reorder_init_offsets(filename, fat_offset + init_offsets, fat_offset + init_offsets_end, decrypt_init);
}
else
{
std::cerr << "Failed to find __init_offsets section in " << filename << std::endl;
}
if (mod_init_funcs > 0 && mod_init_funcs_end > 0 && decrypt_init > 0)
{
reorder_mod_init_funcs(filename, fat_offset + mod_init_funcs, fat_offset + mod_init_funcs_end, decrypt_init);
}
else
{
std::cerr << "Failed to find __mod_init_funcs section in " << filename << std::endl;
}
if (text_start > 0)
{
encrypt_binary(filename, fat_offset + text_start, fat_offset + text_end);
}
else
{
std::cerr << "Failed to find __TEXT section in " << filename << std::endl;
}
std::cout << "Successfully computed and embedded hash of __TEXT section: 0x" << std::hex << hash << std::endl;
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <executable>" << std::endl;
return 1;
}
std::string filename = argv[1];
std::ifstream file(filename, std::ios::binary);
if (!file)
{
std::cerr << "Failed to open file: " << filename << std::endl;
return 1;
}
std::vector<uint8_t> data((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
const fat_header *fat = reinterpret_cast<const fat_header *>(data.data());
if (fat->magic == FAT_MAGIC || fat->magic == FAT_CIGAM)
{
uint32_t nfat_arch = ntohl(fat->nfat_arch);
const fat_arch *archs = reinterpret_cast<const fat_arch *>(data.data() + sizeof(fat_header));
for (uint32_t i = 0; i < nfat_arch; ++i)
{
const fat_arch *arch = &archs[i];
uint64_t offset = ntohl(arch->offset);
uint64_t size = ntohl(arch->size);
std::vector<uint8_t> arch_data(data.begin() + offset, data.begin() + offset + size);
process_macho(arch_data, filename, offset);
}
}
else
{
process_macho(data, filename);
}
return 0;
}