1+ #include "extension_encryption.h"
2+
3+ extension_encryption_ctx * extension_statuses [MAX_EXTENSIONS ] = { 0 };
4+
5+ BOOL extension_encryption_add (extension_encryption_ctx * ExtensionCtx ) {
6+ BOOL ret = FALSE;
7+
8+ if (ExtensionCtx == NULL || !ExtensionCtx -> encryptable ) {
9+ dprintf ("[extension_encryption][extension_encryption_add] Either ExtensionCtx is NULL or Extension is not encryptable." );
10+ return ret ;
11+ }
12+
13+ for (int i = 0 ; i < MAX_EXTENSIONS ; i ++ ) {
14+ if (extension_statuses [i ] == NULL ) {
15+ extension_statuses [i ] = ExtensionCtx ;
16+ ret = TRUE;
17+ break ;
18+ }
19+ }
20+ if (!ret ) {
21+ dprintf ("[extension_encryption][extension_encryption_add] Couldn't locate an empty member in extension_statuses array." );
22+ }
23+ return ret ;
24+ }
25+
26+ BOOL extension_encryption_remove (extension_encryption_ctx * ExtensionCtx ) {
27+ BOOL ret = FALSE;
28+
29+ if (ExtensionCtx == NULL ) {
30+ dprintf ("[extension_encryption][extension_encryption_remove] ExtensionCtx is NULL." );
31+ return ret ;
32+ }
33+
34+ for (int i = 0 ; i < MAX_EXTENSIONS ; i ++ ) {
35+ if (extension_statuses [i ] == ExtensionCtx ) {
36+ extension_statuses [i ] = NULL ;
37+ ret = TRUE;
38+ break ;
39+ }
40+ }
41+ if (!ret ) {
42+ dprintf ("[extension_encryption][extension_encryption_remove] Couldn't locate ExtensionCtx in extension_statuses array." );
43+ }
44+ return ret ;
45+ }
46+
47+ BOOL extension_encryption_encrypt (extension_encryption_ctx * ExtensionCtx ) {
48+ RC4_CTX RC4 = { 0 };
49+ size_t KeyLength = 0 ;
50+ BOOL ret = FALSE;
51+ unsigned char buff [4096 ] = { 0 };
52+ DWORD diff = 4096 ;
53+ size_t ByteCounter = 0 ;
54+
55+ if (ExtensionCtx == NULL || !ExtensionCtx -> encryptable || ExtensionCtx -> encrypted || !ExtensionCtx -> size || ExtensionCtx -> key == NULL || ExtensionCtx -> loc == NULL ) {
56+ dprintf ("[extension_encryption][extension_encryption_encrypt] Invalid ExtensionCtx." );
57+ return ret ;
58+ }
59+
60+ KeyLength = strlen (ExtensionCtx -> key );
61+
62+ if (!KeyLength || !InitRc4 (& RC4 , ExtensionCtx -> key , KeyLength )) {
63+ dprintf ("[extension_encryption][extension_encryption_encrypt] Either KeyLength is 0 or InitRc4 failed." );
64+ return ret ;
65+ }
66+
67+ for (DWORD i = 0 ; i != ExtensionCtx -> size ; i += diff ) {
68+ if ((ExtensionCtx -> size - i ) < 4096 ) {
69+ diff = ExtensionCtx -> size - i ;
70+ }
71+ ret = ReadProcessMemory (GetCurrentProcess (), (unsigned char * )ExtensionCtx -> loc + i , buff , diff , & ByteCounter );
72+ if (!ret || ByteCounter != diff ) {
73+ dprintf ("[extension_encryption][extension_encryption_encrypt] ReadProcessMemory failed with error 0x%x" , GetLasatError ());
74+ break ;
75+ }
76+ if (!RC4Cipher (& RC4 , buff , diff )) {
77+ dprintf ("[extension_encryption][extension_encryption_encrypt] RC4Cipher failed." );
78+ ret = FALSE;
79+ break ;
80+ }
81+ ret = WriteProcessMemory (GetCurrentProcess (), (unsigned char * )ExtensionCtx -> loc , buff , diff , & ByteCounter );
82+ if (!ret || ByteCounter != diff ) {
83+ dprintf ("[extension_encryption][extension_encryption_encrypt] WriteProcessMemory failed with error 0x%x" , GetLastError ());
84+ break ;
85+ }
86+ }
87+ if (ret ) {
88+ ExtensionCtx -> encrypted = !ExtensionCtx -> encrypted ;
89+ }
90+ return ret ;
91+ }
92+
93+ BOOL extension_encryption_decrypt (extension_encryption_ctx * ExtensionCtx ) {
94+ RC4_CTX RC4 = { 0 };
95+ size_t KeyLength = 0 ;
96+ BOOL ret = FALSE;
97+ unsigned char buff [4096 ] = { 0 };
98+ DWORD diff = 4096 ;
99+ size_t ByteCounter = 0 ;
100+
101+ if (ExtensionCtx == NULL || !ExtensionCtx -> encryptable || !ExtensionCtx -> encrypted || !ExtensionCtx -> size || ExtensionCtx -> key == NULL || ExtensionCtx -> loc == NULL ) {
102+ dprintf ("[extension_encryption][extension_encryption_decrypt] Invalid ExtensionCtx." );
103+ return ret ;
104+ }
105+
106+ KeyLength = strlen (ExtensionCtx -> key );
107+
108+ if (!KeyLength || !InitRc4 (& RC4 , ExtensionCtx -> key , KeyLength )) {
109+ dprintf ("[extension_encryption][extension_encryption_decrypt] Either KeyLength is 0 or InitRc4 failed." );
110+ return ret ;
111+ }
112+
113+ for (DWORD i = 0 ; i != ExtensionCtx -> size ; i += diff ) {
114+ if ((ExtensionCtx -> size - i ) < 4096 ) {
115+ diff = ExtensionCtx -> size - i ;
116+ }
117+ ret = ReadProcessMemory (GetCurrentProcess (), (unsigned char * )ExtensionCtx -> loc + i , buff , diff , & ByteCounter );
118+ if (!ret || ByteCounter != diff ) {
119+ dprintf ("[extension_encryption][extension_encryption_decrypt] ReadProcessMemory failed with error 0x%x" , GetLasatError ());
120+ break ;
121+ }
122+ if (!RC4Cipher (& RC4 , buff , diff )) {
123+ dprintf ("[extension_encryption][extension_encryption_decrypt] RC4Cipher failed." );
124+ ret = FALSE;
125+ break ;
126+ }
127+ ret = WriteProcessMemory (GetCurrentProcess (), (unsigned char * )ExtensionCtx -> loc , buff , diff , & ByteCounter );
128+ if (!ret || ByteCounter != diff ) {
129+ dprintf ("[extension_encryption][extension_encryption_decrypt] WriteProcessMemory failed with error 0x%x" , GetLastError ());
130+ break ;
131+ }
132+ }
133+ if (ret ) {
134+ ExtensionCtx -> encrypted = !ExtensionCtx -> encrypted ;
135+ }
136+ return ret ;
137+ }
138+
139+ void extension_encryption_encrypt_unused () {
140+ for (int i = 0 ; i < MAX_EXTENSIONS ; i ++ ) {
141+ if (extension_statuses [i ] == NULL || !extension_statuses [i ]-> encryptable || extension_statuses [i ]-> encrypted || (GetTickCount () - extension_statuses [i ]-> LastUsedTime ) < 600000 ) {
142+ continue ;
143+ }
144+ if (!extension_encryption_encrypt (extension_statuses [i ])) {
145+ dprintf ("[extension_encryption][extension_encryption_encrypt_unused] extension_statuses[%d] couldn't be encrypted." , i );
146+ }
147+ }
148+ }
0 commit comments