-
Notifications
You must be signed in to change notification settings - Fork 33
/
ELFcrypt.h
315 lines (254 loc) · 6.97 KB
/
ELFcrypt.h
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
#include <elf.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#define ENTRY ((unsigned char *)0x400000)
#define CRYPTED __attribute__((section(".crypted")))
/* fatal() -- Prints a message and exits with EXIT_FAILURE
*
* Args:
* fmt - va_args-style format strings (like printf)
*
* Returns:
* Nothing.
*/
void fatal(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
/* is_valid_elf() -- Determine if data is a valid ELF file.
*
* Args:
* header - ELF header.
*
* Returns:
* 1 if ELF magic checks are successful.
* 0 if the magic bytes do not match.
*/
int is_valid_elf(Elf64_Ehdr *header) {
if (!header)
return 0;
if (header->e_ident[EI_MAG0] != ELFMAG0 ||
header->e_ident[EI_MAG1] != ELFMAG1 ||
header->e_ident[EI_MAG2] != ELFMAG2 ||
header->e_ident[EI_MAG3] != ELFMAG3)
return 0;
return 1;
}
/* get_elf_size() -- Calculate size of ELF data within a file.
*
* Args:
* progname - ELF file
*
* Returns:
* size of ELF data if successful.
* -1 if an error occurred.
*
* Note: if an error occurs, errno is set appropriately.
*/
size_t get_elf_size(const char *progname) {
int fd;
void *ELFheaderdata;
Elf64_Ehdr *ELFheader;
size_t elfsize;
ELFheaderdata = malloc(64);
fd = open(progname, O_RDONLY);
if (fd == -1) {
errno = ENOENT;
return -1;
}
read(fd, ELFheaderdata, 64);
ELFheader = (Elf64_Ehdr *)ELFheaderdata;
if (is_valid_elf(ELFheader) == 0) {
errno = ENOEXEC;
return -1;
}
elfsize = ELFheader->e_shoff + (ELFheader->e_shnum * ELFheader->e_shentsize);
close(fd);
free(ELFheaderdata);
return elfsize;
}
/* get_elf_section() -- Get address of ELF section within data.
*
* Args:
* data - ELF data in memory
* section - section name to search for
*
* Returns:
* Address of section if successful.
* NULL if section does not exist.
*/
Elf64_Shdr *get_elf_section(void *data, const char *section) {
int i;
char *offset;
/* Populate ELF headers and section headers from mmapped file */
Elf64_Ehdr *ELFheader = (Elf64_Ehdr *)data;
Elf64_Shdr *sectionheader = (Elf64_Shdr *)(data + ELFheader->e_shoff);
Elf64_Shdr *next = §ionheader[ELFheader->e_shstrndx];
/* Make sure this is a valid ELF before proceeding */
if (is_valid_elf(ELFheader) == 0)
fatal("Input file is not a valid ELF file.\n");
offset = data + next->sh_offset;
/* Search for "section" and return address of matching section header */
for (i = 0; i < ELFheader->e_shnum; i++) {
if (!strcmp((char *)offset + sectionheader[i].sh_name, section)) {
return §ionheader[i];
}
}
return NULL;
}
/* get_file_size() -- Determine the size of a file.
*
* Args:
* filename - Path to file.
*
* Returns:
* Size of file in bytes on success.
* -1 if something went wrong.
*/
size_t get_file_size(const char *filename) {
struct stat s;
if (stat(filename, &s) == -1) {
fprintf(stderr, "Failed to stat file %s: %s\n", filename, strerror(errno));
return -1;
}
return s.st_size;
}
/* get_password() -- Prompt user for a password and password confirmation.
*
* Args:
* None
*
* Returns:
* Pointer to string containing the password.
*
* Note: this is limited to 256 byte passwords because this is the maximum
* key length RC4 is able to use.
*/
char *get_password() {
int i = 0;
char *key;
char keyconfirm[256];
do {
if (i) {
printf("Passwords do not match\n");
sleep(3);
}
if ((key = getpass("Enter passphrase: ")) == NULL) {
printf("Bad password.\n");
continue;
}
strncpy(keyconfirm, key, sizeof(keyconfirm));
if ((key = getpass("Confirm passphrase: ")) == NULL) {
printf("Bad password.\n");
continue;
}
i = 1;
} while (strcmp(key, keyconfirm));
/* zero out key from memory */
memset(keyconfirm, 0, sizeof(keyconfirm));
return key;
}
/* rc4() -- Encrypt data using RC4 encryption algorithm.
*
* Args:
* data - Data to encrypt
* size - Length of data
* key - Passphrase to encrypt data with.
*
* Returns:
* 0 if successful.
* 1 if unsuccessful.
*/
int rc4(unsigned char *data, size_t size, const unsigned char *key) {
int i;
int rc4i;
int rc4j;
unsigned char rc4s[256];
unsigned int tmp;
if (strlen((char *)key) > sizeof(rc4s)) {
fprintf(stderr, "Key must be under %ld bytes\n", sizeof(rc4s));
return 1;
}
/* Key-scheduling algorithm */
for (i = 0; i < sizeof(rc4s); i++)
rc4s[i] = i;
for (rc4i = 0, rc4j = 0; rc4i < sizeof(rc4s); rc4i++) {
rc4j = (rc4j + rc4s[rc4i] + key[rc4i % strlen((char *)key)]) % sizeof(rc4s);
/* swap s[i] and s[j] */
tmp = rc4s[rc4j];
rc4s[rc4j] = rc4s[rc4i];
rc4s[rc4i] = tmp;
}
/* encrypt data */
for (rc4i = 0, rc4j = 0, i = 0; i < size; i++) {
rc4i = (rc4i + 1) % sizeof(rc4s);
rc4j = (rc4j + rc4s[rc4i]) % sizeof(rc4s);
/* swap s[i] and s[j] */
tmp = rc4s[rc4j];
rc4s[rc4j] = rc4s[rc4i];
rc4s[rc4i] = tmp;
tmp = rc4s[(rc4s[rc4i] + rc4s[rc4j]) % sizeof(rc4s)];
data[i] ^= tmp;
}
return 0;
}
/* ELFdecrypt() -- Decrypt .crypted section of ELF file.
*
* Args:
* pass - If desired, pass the key in here. This is not very secure,
* but provides some obfuscation.
*
* Returns:
* Nothing
*
* Note: if the ELFCRYPT environment variable is set, this will attempt to use
* its contents as the encryption key.
*/
void ELFdecrypt(char *pass) {
int section_length;
int crypted_section;
char *key;
unsigned char *ptr;
unsigned char *ptr2;
size_t pagesize;
uintptr_t pagestart;
int size;
if (pass == NULL) {
key = getenv("ELFCRYPT");
if (key == NULL) {
key = getpass("Enter passphrase: ");
} else {
unsetenv("ELFCRYPT");
}
} else {
key = strdup(pass);
}
/* Retrieve crypted section offset and size stored by ELFcrypt */
crypted_section = *((int *)(ENTRY + 0x09));
section_length = *((short *)(ENTRY + 0x0d));
/* Calculate offsets and sizes */
ptr = ENTRY + crypted_section;
ptr2 = ENTRY + crypted_section + section_length;
pagesize = sysconf(_SC_PAGESIZE);
pagestart = (uintptr_t)ptr & -pagesize;
size = (ptr2 - (unsigned char *)pagestart);
if (mprotect((void *)pagestart, size, PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
fatal("mprotect(): %s\n", strerror(errno));
/* Decrypt using specified key */
rc4(ENTRY + crypted_section, section_length, (unsigned char *)key);
if (mprotect((void *)pagestart, size, PROT_READ | PROT_EXEC) < 0)
fatal("mprotect(): %s\n", strerror(errno));
/* Erase key */
memset(key, 0, strlen(key));
}