1
1
#include <stdio.h>
2
+ #include <time.h>
2
3
#include <unistd.h>
3
4
#include <stdlib.h>
4
5
#include <sys/syscall.h>
@@ -17,14 +18,20 @@ static inline int memfd_create(const char *name, unsigned int flags) {
17
18
* for this program to mask its intentions a little bit.
18
19
*/
19
20
int main (int argc , char * argv [], char * envp []) {
21
+ int i ;
20
22
int fd ;
21
23
int in ;
22
24
size_t offset ;
23
25
size_t filesize ;
24
26
unsigned char * key ;
25
27
void * program ;
28
+ char characters [] = \
29
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" ;
26
30
27
31
32
+ /* Seed RNG */
33
+ srand (time (NULL ));
34
+
28
35
/* Calculate size of the stub + encrypted ELF */
29
36
filesize = get_file_size (argv [0 ]);
30
37
@@ -53,6 +60,10 @@ int main(int argc, char *argv[], char *envp[]) {
53
60
if (rc4 (program , filesize - offset , key ) == 1 )
54
61
return EXIT_FAILURE ;
55
62
63
+ /* Overwrite key with random shit to hide its true contents. */
64
+ for (; * key ; key ++ )
65
+ * key = characters [rand () % sizeof (characters ) - 1 ];
66
+
56
67
/* Some operating systems may not supply this function. This has only
57
68
* been tested on modern Linux distributions (as of 2018). Alternatively,
58
69
* you can modify this to utilize a temporary file or shm_open(). We use the
@@ -62,12 +73,18 @@ int main(int argc, char *argv[], char *envp[]) {
62
73
if (fd == -1 )
63
74
return EXIT_FAILURE ;
64
75
76
+ /* Write decrypted program data to memory file descriptor */
65
77
if (write (fd , program , filesize - offset ) != filesize - offset )
66
78
return EXIT_FAILURE ;
67
79
68
- /* Attempt to execute decrypted ELF which is stored in memory. */
69
- close (in );
80
+ /* Overwrite decrypted program with randomness before unmapping it.*/
81
+ for (i = 0 ; i < filesize - offset ; i ++ , program ++ )
82
+ * ((char * )program ) = rand () % 0xff ;
83
+
70
84
munmap (program , filesize );
85
+ close (in );
86
+
87
+ /* Attempt to execute decrypted ELF which is stored in memory fd. */
71
88
fexecve (fd , argv , envp );
72
89
close (fd );
73
90
0 commit comments