Skip to content

Commit 6c647ef

Browse files
authored
Merge pull request rapid7#790 from xHector1337/add-extension-encryption
Extension Encryption For Meterpreter: Add RC4 Dependency
2 parents 189c351 + cda8be4 commit 6c647ef

6 files changed

Lines changed: 76 additions & 2 deletions

File tree

c/meterpreter/source/common/common_core.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ typedef enum
8585
*/
8686
#define LOAD_LIBRARY_FLAG_LOCAL (1 << 2)
8787

88+
/*!
89+
* @brief Indicates that the library in question supports runtime encryption.
90+
* @detail Libraries can be encrypted runtime, depending on how they interact with the system
91+
* if this flag is present, it means this library support the runtime encryption.
92+
*/
93+
#define LOAD_LIBRARY_EXTENSION_ENCRYPTABLE (1 << 3)
94+
8895
/*! @brief An indication of whether the challen is synchronous or asynchronous. */
8996
#define CHANNEL_FLAG_SYNCHRONOUS (1 << 0)
9097
/*! @brief An indication of whether the content written to the channel should be compressed. */

c/meterpreter/source/metsrv/rc4.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "rc4.h"
2+
3+
BOOL InitRc4(RC4_CTX* Context, unsigned char* Key, size_t len) {
4+
unsigned char T = 0;
5+
unsigned int j = 0;
6+
7+
if (Context == NULL || Key == NULL || len == 0) {
8+
return FALSE;
9+
}
10+
11+
memset(Context, 0x00, sizeof(RC4_CTX));
12+
13+
for (unsigned int i = 0; i < 256; i++) {
14+
Context->s[i] = i;
15+
}
16+
17+
for (unsigned int i = 0; i < 256; i++) {
18+
j = (j + Context->s[i] + Key[i % len]) % 256;
19+
T = Context->s[i];
20+
Context->s[i] = Context->s[j];
21+
Context->s[j] = T;
22+
}
23+
24+
Context->i = 0;
25+
Context->j = 0;
26+
27+
return TRUE;
28+
}
29+
30+
BOOL RC4Cipher(RC4_CTX* Context, unsigned char* buf, size_t len) {
31+
unsigned char T = 0;
32+
unsigned int i = Context->i;
33+
unsigned int j = Context->j;
34+
35+
for (unsigned int k = 0; k < len;k++) {
36+
i = (i+1) % 256;
37+
j = (j + Context->s[i]) % 256;
38+
T = Context->s[i];
39+
Context->s[i] = Context->s[j];
40+
Context->s[j] = T;
41+
buf[k] ^= Context->s[(Context->s[i] + Context->s[j]) % 256];
42+
}
43+
44+
Context->i = i;
45+
Context->j = j;
46+
47+
return TRUE;
48+
}

c/meterpreter/source/metsrv/rc4.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <windows.h>
2+
3+
typedef struct {
4+
unsigned int i, j;
5+
unsigned char s[256];
6+
} RC4_CTX;
7+
8+
BOOL InitRc4(RC4_CTX* Context, unsigned char* Key, size_t len);
9+
BOOL RC4Cipher(RC4_CTX* Context, unsigned char* buf, size_t len);
10+

c/meterpreter/source/metsrv/remote_dispatch.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ DWORD request_core_loadlib(Remote *remote, Packet *packet)
423423
if ((flags & LOAD_LIBRARY_FLAG_EXTENSION) && library)
424424
{
425425
res = load_extension(library, bLibLoadedReflectivly, remote, response, first);
426+
if (flags & LOAD_LIBRARY_EXTENSION_ENCRYPTABLE) {
427+
dprintf("[DEBUG] This extension can be encrypted!");
428+
}
426429
}
427430

428431
} while (0);

c/meterpreter/workspace/metsrv/metsrv.vcxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ copy /y "$(TargetDir)$(TargetFileName)" "$(ProjectDir)..\..\output\"</Command>
570570
<ClInclude Include="..\..\source\metsrv\pool_party.h" />
571571
<ClInclude Include="..\..\source\metsrv\pool_party_ext.h" />
572572
<ClInclude Include="..\..\source\metsrv\winapi.h" />
573+
<ClInclude Include="..\..\source\metsrv\rc4.h" />
573574
</ItemGroup>
574575
<ItemGroup>
575576
<ClCompile Include="..\..\source\metsrv\base.c" />
@@ -601,6 +602,7 @@ copy /y "$(TargetDir)$(TargetFileName)" "$(ProjectDir)..\..\output\"</Command>
601602
<ClCompile Include="..\..\source\logging\logging.c" />
602603
<ClCompile Include="..\..\source\metsrv\pool_party.c" />
603604
<ClCompile Include="..\..\source\metsrv\winapi.c" />
605+
<ClCompile Include="..\..\source\metsrv\rc4.c" />
604606
</ItemGroup>
605607
<ItemGroup>
606608
<MASM Include="..\..\source\ReflectiveDLLInjection\dll\src\GateTrampoline32.asm">
@@ -618,4 +620,4 @@ copy /y "$(TargetDir)$(TargetFileName)" "$(ProjectDir)..\..\output\"</Command>
618620
<ImportGroup Label="ExtensionTargets">
619621
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
620622
</ImportGroup>
621-
</Project>
623+
</Project>

c/meterpreter/workspace/metsrv/metsrv.vcxproj.filters

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
<ClInclude Include="..\..\source\metsrv\metapi.h" />
2828
<ClInclude Include="..\..\source\metsrv\pool_party.h" />
2929
<ClInclude Include="..\..\source\metsrv\pool_party_ext.h" />
30+
<ClInclude Include="..\..\source\metsrv\winapi.h" />
31+
<ClInclude Include="..\..\source\metsrv\rc4.h" />
3032
</ItemGroup>
3133
<ItemGroup>
3234
<ClCompile Include="..\..\source\metsrv\pivot_tree.c" />
@@ -57,9 +59,11 @@
5759
<ClCompile Include="..\..\source\metsrv\metapi.c" />
5860
<ClCompile Include="..\..\source\logging\logging.c" />
5961
<ClCompile Include="..\..\source\metsrv\pool_party.c" />
62+
<ClCompile Include="..\..\source\metsrv\winapi.c" />
63+
<ClCompile Include="..\..\source\metsrv\rc4.c" />
6064
</ItemGroup>
6165
<ItemGroup>
6266
<MASM Include="..\..\source\ReflectiveDLLInjection\dll\src\GateTrampoline64.asm" />
6367
<MASM Include="..\..\source\ReflectiveDLLInjection\dll\src\GateTrampoline32.asm" />
6468
</ItemGroup>
65-
</Project>
69+
</Project>

0 commit comments

Comments
 (0)