HackCert
Advanced 11 min read May 25, 2026

SSTI Exploitation: সার্ভার সাইড টেমপ্লেট ইনজেকশনের মাধ্যমে ওয়েব অ্যাপ্লিকেশনে রিমোট কোড এক্সিকিউশন!

Server-Side Template Injection (SSTI) detection, template engine fingerprinting, sandbox escape এবং RCE technique-এর গভীর প্রযুক্তিগত গাইড।

Ayesha Siddika Rahman
Web Application Pentester
share
SSTI Exploitation: সার্ভার সাইড টেমপ্লেট ইনজেকশনের মাধ্যমে ওয়েব অ্যাপ্লিকেশনে রিমোট কোড এক্সিকিউশন!
Overview

আধুনিক ওয়েব অ্যাপ্লিকেশনের development efficiency বাড়াতে template engine ব্যাপক ব্যবহৃত হয় — Jinja2, Twig, Freemarker, Velocity, Handlebars, Smarty, ERB এবং আরও অনেক। এই template engine-গুলো developer-দের logic এবং presentation আলাদা করার সুবিধা দেয়, কিন্তু যদি user-supplied input কোনো safe escaping ছাড়া template-এ ঢোকে, তবে জন্ম নেয় একটি অত্যন্ত বিপজ্জনক vulnerability class — Server-Side Template Injection বা SSTI। James Kettle তাঁর ২০১৫ সালের গবেষণায় SSTI-কে security community-র সামনে systematically উপস্থাপন করেন এবং দেখিয়ে দেন যে অধিকাংশ template engine-এ যথেষ্ট template logic থাকে যা শেষ পর্যন্ত Remote Code Execution-এ পরিণত হতে পারে। SSTI আজও bug bounty platform-গুলোতে high-severity finding হিসেবে গণ্য হয় এবং প্রায়ই $৫,০০০ থেকে $২৫,০০০+ payout পায়।

SSTI বনাম XSS এবং অন্যান্য Injection

অনেক developer SSTI এবং Cross-Site Scripting (XSS)-কে বিভ্রান্ত করেন, কিন্তু এ দুটি fundamentally আলাদা। XSS client-side (browser)-এ JavaScript execute করে এবং user-impact limited। SSTI server-side-এ template engine-এর syntax execute করে এবং প্রায়শই full server compromise-এর দিকে নিয়ে যায়। যেমন একটি Jinja2 application-এ user input direct render হলে {{7*7}} পাঠালে রেসপন্সে 49 দেখা যাবে — এটি SSTI confirmation।

XSS-এ payload <script>alert(1)</script> browser-এ execute হয়, কিন্তু SSTI-তে payload template engine-এর syntax (যেমন {{ }}, <% %>, ${ }, #{ }, {% %}) follow করে এবং server-side execute হয়। Code Injection এবং SSTI কাছাকাছি হলেও আলাদা — code injection-এ direct interpreter (eval(), exec()) ঘটে, SSTI-তে template engine intermediary হিসেবে কাজ করে কিন্তু প্রায়ই eventually code execution পর্যন্ত পৌঁছানো যায়।

Detection Methodology

SSTI খুঁজে বের করতে James Kettle-এর proposed methodology সবচেয়ে কার্যকর — Detect, Identify, Exploit। প্রথমে probe payload দিয়ে template engine আছে কিনা নিশ্চিত করতে হয়। ${7*7}, {{7*7}}, <%= 7*7 %>, #{7*7} — এই payload গুলো বিভিন্ন engine-এ render হয়ে 49 দেখাবে।

Identification phase-এ specific template engine চিহ্নিত করতে হয়। কারণ প্রতিটি engine-এর syntax, available object এবং exploitation technique আলাদা। {{7*'7'}} payload Twig-এ 49 দেখাবে কিন্তু Jinja2-এ 7777777 দেখাবে (string repetition)। Smarty-তে {$smarty.version} engine version reveal করবে। Freemarker-এ ${"foo"?upper_case} syntax check করা যায়। Error message-ও valuable hint দেয় — কিছু engine traceback-এ engine name এবং version expose করে।

Exploitation phase-এ confirmed engine-এর জন্য specific payload তৈরি করা হয়। এই phase-এ goal থাকে sandbox escape করা, system command execute করা এবং sensitive data access করা। প্রতিটি engine-এ unique escape technique আছে যা attacker-দের ভালোভাবে জানা থাকে।

Jinja2 (Python) Exploitation

Python Flask এবং Django-তে ব্যাপক ব্যবহৃত Jinja2-তে SSTI exploitation-এর জন্য Python object hierarchy traversal করতে হয়। {{ ''.__class__.__mro__[1].__subclasses__() }} দিয়ে সব loaded class-এর list পাওয়া যায়, যেখানে subprocess.Popen, os._wrap_close ইত্যাদি useful class থাকে।

Classic Jinja2 RCE payload: {{ ''.__class__.__mro__[1].__subclasses__()[396]('id', shell=True, stdout=-1).communicate()[0] }} — তবে index number Python version-অনুসারে পরিবর্তিত হয়। আরও robust approach হলো {{ request.application.__globals__.__builtins__.__import__('os').popen('id').read() }}, যা Flask context-এ কাজ করে।

Modern Jinja2 environment-এ অনেক payload sanitized হয় — __class__, __mro__, __subclasses__ এর মতো dunder method filter করা হতে পারে। এর জন্য attacker bypass technique ব্যবহার করেন — |attr filter দিয়ে attribute access, hex/unicode encoding (\x5f\x5fclass\x5f\x5f), bracket notation (['__class__']) ইত্যাদি। Flask-Security-Too-এর মতো extension-এ পাওয়া historic vulnerability এই pattern গুলো demonstrate করে।

Twig (PHP) এবং Freemarker (Java)

Twig PHP template engine, যা Symfony framework-এর সাথে ব্যাপক ব্যবহৃত। Twig-এ classic payload: {{ _self.env.registerUndefinedFilterCallback("exec") }}{{ _self.env.getFilter("id") }} — যা arbitrary system command execute করতে পারে। আধুনিক Twig version-এ sandbox mode থাকলে এই technique বন্ধ থাকে।

Freemarker Java template engine-এ exploitation Object Wrapper-এর through হয়। Classic payload: <#assign ex="freemarker.template.utility.Execute"?new()>${ex("id")} — এটি freemarker.template.utility.Execute class instantiate করে system command চালায়। Velocity-তে #set($x="") $x.getClass().forName("java.lang.Runtime").getMethod("getRuntime",null).invoke(null,null).exec("id") style reflection-based exploitation।

Spring এবং Spring Boot application-এ Thymeleaf এবং SpEL (Spring Expression Language) injection প্রচলিত। CVE-2022-22965 (Spring4Shell) আংশিকভাবে এই category-তে পড়ে। Apache OFBiz, Atlassian Confluence (CVE-2022-26134), VMware Workspace ONE-এর মতো enterprise software-এ Freemarker/Velocity SSTI-এর কারণে catastrophic CVE published হয়েছে।

Node.js এবং Ruby Template Engine

Node.js ecosystem-এ Handlebars, EJS, Pug, Nunjucks ব্যবহৃত হয়। Handlebars-এ classic exploitation: {{#with (lookup "" 'constructor') as |c|}} {{c.constructor.constructor.constructor.constructor "return process"}}{{/with}} — যদিও আধুনিক Handlebars-এ এই technique restricted।

EJS-এ <%= 7*7 %> arbitrary JavaScript execute করতে পারে — <%= require('child_process').execSync('id') %> direct RCE দেয়। Pug-তে similar approach, যেখানে template-এ JavaScript embed করা যায়। Nunjucks (Mozilla-developed) প্রায়ই server-side ব্যবহৃত হয় এবং এর SSTI-তে {{ range.constructor("return global.process.mainModule.require('child_process').execSync('id')")() }} style payload কাজ করে।

ERB (Embedded Ruby) Ruby on Rails-এর default template engine। <%= system("id") %>, <%= id %>, <%= IO.popen("id").read %> — সব direct RCE payload। Slim এবং Haml-এও similar pattern। Ruby ecosystem-এ template injection প্রায়ই full ActiveRecord access দেয়, যা database manipulation এবং mass assignment exploitation সম্ভব করে।

Sandbox Escape Technique

আধুনিক template engine-গুলো নিরাপত্তার জন্য sandbox mode অফার করে যেখানে dangerous function blocked থাকে। কিন্তু creative attacker সবসময় escape technique খুঁজে বের করেন। Jinja2-র SandboxedEnvironment-এ __init_subclass__, __class_getitem__ এর মতো magic method অনেক সময় filter-এর বাইরে থাকে।

Twig sandbox mode-এ allowed filter এবং function specifically configure করা থাকে। তবে অনেক ক্ষেত্রে convert_encoding, filter, map এর মতো filter-এর misuse RCE দিতে পারে। Freemarker sandbox-এ new keyword blocked থাকলেও reflection-based bypass সম্ভব। Sandbox bypass একটি ongoing arms race — প্রতিটি new bypass discover হওয়ার পর engine vendor patch release করে এবং নতুন bypass খুঁজে বের হয়।

Polyglot payload — যা multiple engine-এ একসাথে কাজ করে — pentesting-এ time save করে। PortSwigger Research এবং HackTricks-এ এই ধরনের comprehensive payload library available। SSTImap এবং tplmap automatic exploitation tool, যা detection এবং exploitation দুটোই handle করে।

বাস্তব জগতের Case Study

২০২১ সালের Atlassian Confluence CVE-2021-26084 ছিল একটি OGNL injection vulnerability (technically SSTI category-তে পড়ে) যা unauthenticated attacker-কে full RCE দিত। হাজার হাজার Confluence server worldwide compromise হয়েছিল এবং ransomware operator-রা ব্যাপকভাবে এটি exploit করে।

২০২২ সালের CVE-2022-26134 (Confluence OGNL injection) আবার একই pattern — ${('zzz'+'a'+'a').toString()} payload URL-এ inject করলেই RCE। ২০২২ সালের Apache OFBiz CVE-2023-49070-এ XML-RPC interface-এর through SSTI-related RCE পাওয়া গিয়েছিল। VMware-এর বিভিন্ন product-এ ServerSide template injection মাঝে মাঝে discover হয়।

Uber-এর একটি old vulnerability-তে Twig SSTI পাওয়া গিয়েছিল যা $১০,০০০ bug bounty পেয়েছিল। GitLab-এর Liquid template engine-এ পাওয়া SSTI প্রায় $২০,০০০ পেয়েছে। Shopify-তেও Liquid SSTI আকর্ষণীয় findings এনে দিয়েছে। এই case study-গুলো প্রমাণ করে যে বড় বড় technology company-ও SSTI-র শিকার হতে পারে।

প্রতিরোধ ও সর্বোত্তম অনুশীলন

SSTI প্রতিরোধে সবচেয়ে গুরুত্বপূর্ণ principle হলো user input কখনোই template syntax হিসেবে interpret হতে দেওয়া যাবে না। Template-এর content fixed এবং reviewed হতে হবে, user input শুধু variable হিসেবে pass হবে। render_template_string(user_input) এর মতো pattern সম্পূর্ণ avoid করতে হবে। বরং render_template('template.html', user_data=user_input) ব্যবহার করতে হবে।

যদি user-supplied template একান্ত প্রয়োজন (যেমন email template, report customization), তবে sandboxed environment বাধ্যতামূলক। Jinja2-র SandboxedEnvironment, Twig-এর SecurityPolicy, Freemarker-এর TemplateClassResolver — এগুলো ব্যবহার করতে হবে strict allowlist-এর সাথে।

Defense-in-depth approach-এ application থেকে OS-level command execution capability সম্পূর্ণ remove করতে হবে যদি না নিতান্তই প্রয়োজন হয়। Container-based isolation, seccomp profile, AppArmor/SELinux policy — এসব runtime protection layer SSTI exploit-এর impact কমায়। Web Application Firewall-এ SSTI signature এবং anomaly detection rule deploy করা যায়, তবে এগুলো bypass সম্ভব।

Code review-এ template-related code-এ extra attention দিতে হবে। SAST tool (Semgrep, CodeQL) SSTI pattern detect করতে পারে। DAST এবং penetration testing-এ specifically SSTI test scenario include করতে হবে। Developer training-এ template engine security best practice cover করতে হবে।

Key Takeaways

SSTI একটি অত্যন্ত impactful কিন্তু comparatively underappreciated vulnerability class, যা প্রায়শই hidden থাকে সাধারণ code review-এর সময়। একটি innocent template render-এর line কয়েক মিনিটে পুরো server compromise-এ পরিণত হতে পারে। Developer-দের জন্য rule একদম clear — user input কখনোই raw template syntax হিসেবে treat করবেন না। Security professional-দের জন্য SSTI detection এবং exploitation skill modern web application pentesting-এর core component হয়ে উঠেছে। যত বেশি no-code এবং low-code platform জনপ্রিয় হচ্ছে যেখানে user-defined template এবং formula common, তত বেশি SSTI-র attack surface বাড়ছে। তাই continuous learning, threat modeling এবং hands-on practice ছাড়া এই domain-এ master হওয়া অসম্ভব।

আপনার জ্ঞান যাচাই করতে প্রস্তুত? আজই HackCert-এ SSTI Exploitation MCQ Quiz-টি দিন!

Related articles

back to all articles