May 3, 2017 12:59 PM
(This post was last modified: May 3, 2017 01:00 PM by atom0s.)
Someone on Tuts4You made a request to make a way to invoke shellcode within C# without any imports to things such as VirtualAlloc / VirtualProtect etc. I came up with this method as all of C#'s built-in memory allocations via the Marshal class and similar all create with no execution access for protection purposes.
This method makes use of a memory mapped file which can be created with any protections that you want instead. This can be done all via the .NET frameworks built-in classes and such with no imports via DllImport. This is a simple example of creating a function via shellcode to get the PEB of a process. (Cheers to evlncrn8 for adjusting the shellcode from my original post to be more optimized.)
I am aware of the various assembly projects made for C# such as FASM and AssemblyJitter etc. however these projects all require external imports which was the goal of this idea, to not need any.
This method makes use of a memory mapped file which can be created with any protections that you want instead. This can be done all via the .NET frameworks built-in classes and such with no imports via DllImport. This is a simple example of creating a function via shellcode to get the PEB of a process. (Cheers to evlncrn8 for adjusting the shellcode from my original post to be more optimized.)
PHP Code
<?php
/**
* C# Shellcode Example
* (c) 2017 atom0s [[email protected]]
*
* Demonstrates how to invoke shellcode within C# using a memory mapped file.
*/
namespace ShellcodeExample
{
using System;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
class Program
{
/// <summary>
/// Function delegate to invoke the shellcode.
/// </summary>
/// <returns></returns>
private delegate IntPtr GetPebDelegate();
/// <summary>
/// Shellcode function used to obtain the PEB of the process.
/// </summary>
/// <returns></returns>
private unsafe static IntPtr GetPeb()
{
var shellcode = new byte[]
{
0x64, 0xA1, 0x30, 0x00, 0x00, 0x00, // mov eax, dword ptr fs:[30]
0xC3 // ret
};
MemoryMappedFile mmf = null;
MemoryMappedViewAccessor mmva = null;
try
{
// Create a read/write/executable memory mapped file to hold our shellcode..
mmf = MemoryMappedFile.CreateNew("__shellcode", shellcode.Length, MemoryMappedFileAccess.ReadWriteExecute);
// Create a memory mapped view accessor with read/write/execute permissions..
mmva = mmf.CreateViewAccessor(0, shellcode.Length, MemoryMappedFileAccess.ReadWriteExecute);
// Write the shellcode to the MMF..
mmva.WriteArray(0, shellcode, 0, shellcode.Length);
// Obtain a pointer to our MMF..
var pointer = (byte*)0;
mmva.SafeMemoryMappedViewHandle.AcquirePointer(ref pointer);
// Create a function delegate to the shellcode in our MMF..
var func = (GetPebDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(pointer), typeof(GetPebDelegate));
// Invoke the shellcode..
return func();
}
catch
{
return IntPtr.Zero;
}
finally
{
mmva?.Dispose();
mmf?.Dispose();
}
}
/// <summary>
/// Entry point.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
var peb = GetPeb();
Console.WriteLine("PEB is located at: {0:X8}", peb.ToInt32());
}
}
}
I am aware of the various assembly projects made for C# such as FASM and AssemblyJitter etc. however these projects all require external imports which was the goal of this idea, to not need any.



![[Image: rD7TvGj.gif]](https://i.imgur.com/rD7TvGj.gif)
![[Image: Syr3DlV.png]](https://i.imgur.com/Syr3DlV.png)
![[Image: PVoDiqS.png]](https://i.imgur.com/PVoDiqS.png)
