Limited freemarker ssti to arbitrary liql query and manage lithium cms
A server-side template injection in a Freemarker template allowed the execution of restricted Freemarker functions, including a 'rest' function that could run LiQL queries and a 'restadmin' function that could perform privileged API calls. An attacker could read user email addresses and modify or delete users, messages, and collections.
we faced (w/ @celalerdik) an interesting ssti vulnerability on a bugcrowd's program. we could show the traditional '49' number when trying the ${77} command, also we could execute the assign directive reference like below.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
During a Bugcrowd program, my colleague @celalerdik and I found a Server-Side Template Injection (SSTI) in a FreeMarker template engine. The injection point was limited at first, but it eventually allowed us to run arbitrary LIQL (Lithium Query Language) queries and to manage the Lithium CMS behind the site. The report was accepted and rewarded as P1.
The target ran the Lithium CMS, which supports FreeMarker templates but in a restricted way. The target ran the Lithium CMS, which supports FreeMarker templates in a restricted way.
Setup
We worked as external researchers against a Bugcrowd program. We used Burp Suite's Intruder module to fuzz the .ftl files. No credentials, roles or special access are described in the report. The only precondition was the ability to reach the input that was rendered by FreeMarker.
Finding it
We started with the classic FreeMarker test payload:
The application returned 49, which confirmed that our input was being evaluated as a FreeMarker expression. We then tried the assign directive and it worked too:
<#assign attribute1="ssti">
<#assign attribute2="test">
${attribute1}${attribute2}
//prints sstitest
At this point it clearly looked like the FreeMarker template engine. However, the well-known FreeMarker SSTI payloads that rely on the freemarker.template.utility package did not work. For example, this one failed:
${"freemarker.template.utility.Execute"?new()("id")}
So we had a very limited SSTI. We started investigating other FreeMarker built-in functions to see what was still reachable.
Exploitation
Step 1: Confirm the include directive
We tried the template include function and it worked. That gave us a way to load other .ftl files from the server into our rendered template.
Step 2: Fuzz for existing templates
With include working, we fuzzed the .ftl files on the server using this payload:
<#include "*/FUZZ.ftl">
Burp Intruder returned search.ftl. That file contained the following FreeMarker code:
<#assign pageSize = http.request.parameters.name.page_size?number />
<#assign term = encode.url(http.request.parameters.name.term) />
<#assign blurbLength = http.request.parameters.name.blurb_length?number />
<#assign resultMessages = rest("/search/messages?restapi.response_style=view&q=" + term).messages.message />
<#assign totalResults = resultMessages?size />
...
The rest function looked important, so we focused on it. When we tried to access local files with it, we got a stack trace error.
Step 3: Move from FreeMarker to Lithium CMS
After finding information about the rest function in FreeMarker, we realised we were dealing with the Lithium CMS. Lithium supports FreeMarker templates, but in a very limited way. We changed our perspective from looking for RCE to looking at what Lithium's FreeMarker capability could do.
Step 4: Run LIQL queries
We were able to execute LIQL queries on the server with the following lines, and we fetched the last five messages on the website:
<#assign x= rest("2.0","/search?q=" + "SELECT subject FROM messages ORDER BY post_time DESC LIMIT 5"?url) />
<#list x.data.items as message >
<p>${message.subject}</p>
</#list>
#ref: https://devdocportal.lithium.com/t5/Community-API-v2-Reference/bd-p/restv2docs
We also fetched user data with this method. At that point everything looked normal, because anyone could already access the messages or users on the website. We were a little bored, but then the restadmin function saved us.
Step 5: Escalate with restadmin
We could not fetch users' email data using rest, but restadmin could fetch it.

Step 6: Write and delete through restadmin
We continued researching to increase the impact and tried the following commands:
<#assign messagePostBody = {
"type": "message",
"id": "1000",
"subject": "hacked-message"
} />
<#assign resp = restadmin("2.0", "/messages/1000", "PUT", messagePostBody) />
----
<#assign resp = restadmin("2.0", "/messages/1000", "DELETE") />
----
<#assign messagePostBody = {
"type": "user",
"id": "1000",
"login": "hacked-user"
} />
<#assign resp = restadmin("2.0", "/users/1000", "PUT", messagePostBody) />
----
<#assign resp = restadmin("2.0", "/users/1000", "DELETE") />
LIQL itself only supports select queries, but with this method we could send PUT, POST and DELETE API requests. That meant we could delete and change users, messages and all collections of the website.
After a while, the report was accepted and rewarded as P1.
Root cause
The application passed user-controlled input into a FreeMarker template and rendered it. FreeMarker's assign directive and arithmetic expressions were reachable, so the injection was confirmed. The freemarker.template.utility package was blocked, which is why the usual RCE payloads failed, but the include directive was still available. That let us read other .ftl files on the server, including search.ftl, which revealed the rest function and its parameters.
The rest function allowed LIQL queries, and the restadmin function allowed authenticated API calls with write methods. The template sandbox was therefore too permissive: it blocked one dangerous package but left the CMS API functions exposed to the template context. The report does not describe the exact configuration that allowed this.
Impact
An attacker who can reach the injection point can:
- Run arbitrary LIQL select queries and read messages and user data.
- Use
restadminto read data thatrestcannot, such as users' email addresses. - Send PUT, POST and DELETE requests to the Lithium API, changing or deleting users, messages and all collections of the website.
Fix
The report does not describe the fix that was applied. Input that reaches the template engine should be treated as data, not as template source.
Variants to test
- Try
${7*7}on any input that may be rendered by a template engine. A response of49confirms expression evaluation. - Try the
assigndirective with two variables and print them, as shown above. A positive result prints the concatenated values. - Try
${"freemarker.template.utility.Execute"?new()("id")}. If it fails, the utility package is blocked, but other built-ins may still work. - Try
<#include "*/FUZZ.ftl">with a wordlist of template names. A positive result renders the contents of an existing template file. - Look for CMS-specific helper functions such as
restandrestadminin included templates. A positive result lets you run LIQL queries or authenticated API calls. - Try
restadminwith PUT, POST and DELETE methods against objects such as messages and users. A positive result modifies or deletes the object.
What happened, in plain words
My colleague @celalerdik and I were taking part in a Bugcrowd program, which is a scheme where companies invite outside researchers to test their websites and pay for the problems they find. We found a bug of a type called Server-Side Template Injection, usually shortened to SSTI. To understand what that means, you first need to know what a template is.
Many websites build their pages from a template: a file that is mostly ordinary text and HTML, with small placeholders in it. When a visitor asks for a page, the website's server fills in those placeholders with real values and sends the finished page back. The software that does the filling in is called a template engine. FreeMarker is one such engine, popular in the Java world. A placeholder in FreeMarker looks like ${...}, and whatever is inside the braces is treated as a small program that the engine calculates.
SSTI happens when a website lets a visitor's own text end up inside a template and be treated as template code, rather than as plain data. It is the difference between a form that asks for your name and prints it back as text, and a form that takes your name, drops it into the template source, and runs it. If the engine runs it, the visitor can make the server calculate things and call functions on their behalf.
The injection point we found was limited at first: many of the usual tricks were blocked. But in the end it let us run arbitrary LIQL queries (LIQL is Lithium's query language, explained below) and effectively manage the Lithium CMS behind the site. A CMS, or content management system, is the software a company uses to store and publish its pages, users, posts and so on. The report was accepted and rewarded as P1, which on Bugcrowd is the highest priority rating.
The target ran the Lithium CMS. Lithium supports FreeMarker templates, but in a restricted way. The target ran the Lithium CMS, which supports FreeMarker templates in a restricted way.
Setup
We worked as external researchers against a Bugcrowd program. The tools we used were Burp Suite, a program that sits between your browser and the website so you can look at and change the requests going back and forth, including its Intruder module, which sends the same request many times with different values substituted in (useful for guessing file names), and a browser. No credentials, roles or special access are described in the report. The only precondition was the ability to reach the input that was rendered by FreeMarker.
Finding it
We started with the classic FreeMarker test payload:
This is the simplest possible check. ${7*7} is not a secret code; it is just arithmetic written in FreeMarker's placeholder syntax. If the website treats our input as plain text, the page will literally show ${7*7} back to us. If the website treats our input as template code, the engine will calculate seven times seven and the page will show 49. Seeing 49 is proof that our text was executed, not just displayed.
The application returned 49, which confirmed that our input was being evaluated as a FreeMarker expression. We then tried the assign directive and it worked too:
<#assign attribute1="ssti">
<#assign attribute2="test">
${attribute1}${attribute2}
//prints sstitest
Line by line: <#assign attribute1="ssti"> creates a variable named attribute1 and stores the text ssti in it. <#assign attribute2="test"> does the same for attribute2 with the text test. ${attribute1}${attribute2} prints the two variables one after the other, so the output is sstitest. The last line is just a comment in the report showing what the output was. A directive is a FreeMarker instruction that starts with <# and does something rather than printing a value; assign is the one that creates variables. The fact that this worked told us we could not only do arithmetic but also use FreeMarker's own language features.
At this point it clearly looked like the FreeMarker template engine. However, the well-known FreeMarker SSTI payloads that rely on the freemarker.template.utility package did not work. For example, this one failed:
${"freemarker.template.utility.Execute"?new()("id")}
A package, in Java, is a named group of ready-made code that a program can use. freemarker.template.utility is a package that ships with FreeMarker and contains handy classes, one of which is called Execute. The ?new() built-in tells FreeMarker to create a new object of the named class, and the ("id") part asks that object to run the operating system command id, which on Linux prints who the server is running as. This is the classic route from SSTI to running commands on the server, often called RCE (remote code execution). It failed here, which meant that package was blocked.
So we had a very limited SSTI. We started investigating other FreeMarker built-in functions to see what was still reachable. A built-in is a function that FreeMarker offers out of the box, written with a ? in front of its name, like ?new() above.
Exploitation
Step 1: Confirm the include directive
We tried the template include function and it worked. include is a FreeMarker directive that takes another template file and inserts its contents into the page being built, as if that file had been typed there. That gave us a way to load other .ftl files from the server into our rendered template. .ftl is simply the file extension FreeMarker templates use.
Step 2: Fuzz for existing templates
With include working, we fuzzed the .ftl files on the server using this payload:
<#include "*/FUZZ.ftl">
To fuzz means to try many guesses automatically and watch for the one that behaves differently. Here the guess is a file name: FUZZ is a placeholder that Burp Intruder replaces with each word from a list, one request at a time. The */ in front is a wildcard that lets the server look in subfolders rather than only the current one. If a guessed file exists, the server includes it and the response changes; if it does not, the response is different again. By comparing responses, we could tell which names were real.
Burp Intruder returned search.ftl. That file contained the following FreeMarker code:
<#assign pageSize = http.request.parameters.name.page_size?number />
<#assign term = encode.url(http.request.parameters.name.term) />
<#assign blurbLength = http.request.parameters.name.blurb_length?number />
<#assign resultMessages = rest("/search/messages?restapi.response_style=view&q=" + term).messages.message />
<#assign totalResults = resultMessages?size />
...
This is the source of a real template on the server, so it shows how the site itself builds its search page. Line by line: the first line reads a value called page_size from the incoming web request's parameters (the values sent along with the request, such as those in a URL's ?a=b part) and converts it to a number with ?number. The second line reads a parameter called term (the search words) and passes it through encode.url, which makes text safe to put inside a URL. The third line reads blurb_length and converts it to a number. Then rest(...) calls a function named rest with a URL built from /search/messages?restapi.response_style=view&q= plus the search term, and takes the .messages.message part of the answer. Finally ?size counts how many results came back. The ... means the file continued but the report only shows this part.
The rest function looked important, so we focused on it. When we tried to access local files with it, we got a stack trace error. A stack trace is the list of internal steps a program was taking when it crashed; servers sometimes show it to the visitor by mistake, which leaks information about how the software is built. Here it told us our attempt failed, but it also confirmed we were poking at something real.
Step 3: Move from FreeMarker to Lithium CMS
After finding information about the rest function in FreeMarker, we realised we were dealing with the Lithium CMS. Lithium supports FreeMarker templates, but in a very limited way. We changed our perspective from looking for RCE to looking at what Lithium's FreeMarker capability could do. In other words, instead of trying to run operating system commands, we asked: what can this template engine already do on this particular product, and can we abuse that?
Step 4: Run LIQL queries
We were able to execute LIQL queries on the server with the following lines, and we fetched the last five messages on the website:
<#assign x= rest("2.0","/search?q=" + "SELECT subject FROM messages ORDER BY post_time DESC LIMIT 5"?url) />
<#list x.data.items as message >
<p>${message.subject}</p>
</#list>
#ref: https://devdocportal.lithium.com/t5/Community-API-v2-Reference/bd-p/restv2docs
LIQL stands for Lithium Query Language. It is a small query language, similar in spirit to SQL (the language databases use), that lets you ask the Lithium platform for data: which messages, which users, in what order, how many. The rest function is how a template talks to Lithium's REST API. An API, or application programming interface, is a defined set of addresses a program can call to ask another program for data or to tell it to do something; a REST API is one that uses ordinary web requests (GET to read, PUT to replace, POST to create, DELETE to remove) at predictable URLs. The 2.0 in the call is the version of that API being used.
Line by line: <#assign x= rest(...) /> calls rest and stores the answer in a variable called x. The URL passed is /search?q= followed by the query text. The query text is SELECT subject FROM messages ORDER BY post_time DESC LIMIT 5, which reads as: give me the subject field of messages, newest first (ORDER BY post_time DESC), only five of them (LIMIT 5). The ?url at the end encodes the query so it is safe inside a URL. Then <#list x.data.items as message > starts a loop over each item in the answer, calling each one message, and <p>${message.subject}</p> prints that item's subject inside an HTML paragraph. </#list> closes the loop. The #ref: line is just a link to Lithium's API documentation that the authors noted for reference.
We also fetched user data with this method. At that point everything looked normal, because anyone could already access the messages or users on the website. That is an important point: reading public posts and profiles is not a vulnerability, since a normal visitor can do it too. We were a little bored, but then the restadmin function saved us.
Step 5: Escalate with restadmin
We could not fetch users' email data using rest, but restadmin could fetch it.

Step 6: Write and delete through restadmin
We continued researching to increase the impact and tried the following commands:
<#assign messagePostBody = {
"type": "message",
"id": "1000",
"subject": "hacked-message"
} />
<#assign resp = restadmin("2.0", "/messages/1000", "PUT", messagePostBody) />
----
<#assign resp = restadmin("2.0", "/messages/1000", "DELETE") />
----
<#assign messagePostBody = {
"type": "user",
"id": "1000",
"login": "hacked-user"
} />
<#assign resp = restadmin("2.0", "/users/1000", "PUT", messagePostBody) />
----
<#assign resp = restadmin("2.0", "/users/1000", "DELETE") />
The ---- lines are just separators between four separate attempts. The first block builds a small data structure (a set of named values, written with curly braces) describing a message with id 1000 and a new subject, then calls restadmin with the API version 2.0, the address /messages/1000, the method PUT and that data. PUT means replace this object with the data I am sending, so this attempt would rename an existing message. The second block calls restadmin with DELETE on the same address, which would remove that message. The third block does the same as the first but for a user: it builds data with type user, id 1000 and a new login name, and sends it with PUT to /users/1000, which would rename that user. The fourth block sends DELETE to /users/1000, which would remove that user. In each case the answer is stored in a variable called resp, which the report does not show.
LIQL itself only supports select queries, meaning it can only read. But with this method we could send PUT, POST and DELETE API requests. That meant we could delete and change users, messages and all collections of the website. A collection, in Lithium's terms, is a grouping of content, such as a forum or a blog section.
After a while, the report was accepted and rewarded as P1.
Root cause
The application passed user-controlled input into a FreeMarker template and rendered it. FreeMarker's assign directive and arithmetic expressions were reachable, so the injection was confirmed. The freemarker.template.utility package was blocked, which is why the usual RCE payloads failed, but the include directive was still available. That let us read other .ftl files on the server, including search.ftl, which revealed the rest function and its parameters.
The rest function allowed LIQL queries, and the restadmin function allowed authenticated API calls with write methods. The template sandbox was therefore too permissive: it blocked one dangerous package but left the CMS API functions exposed to the template context. A sandbox is a restricted environment meant to stop code from doing harm; the template context is the set of variables and functions a template is allowed to use. The report does not describe the exact configuration that allowed this.
Impact
An attacker who can reach the injection point can:
- Run arbitrary LIQL select queries and read messages and user data.
- Use
restadminto read data thatrestcannot, such as users' email addresses. - Send PUT, POST and DELETE requests to the Lithium API, changing or deleting users, messages and all collections of the website.
Fix
Input that reaches the template engine should be treated as data, not as template source.
Variants to test
- Try
${7*7}on any input that may be rendered by a template engine. A response of49confirms expression evaluation. - Try the
assigndirective with two variables and print them, as shown above. A positive result prints the concatenated values. - Try
${"freemarker.template.utility.Execute"?new()("id")}. If it fails, the utility package is blocked, but other built-ins may still work. - Try
<#include "*/FUZZ.ftl">with a wordlist of template names. A positive result renders the contents of an existing template file. - Look for CMS-specific helper functions such as
restandrestadminin included templates. A positive result lets you run LIQL queries or authenticated API calls. - Try
restadminwith PUT, POST and DELETE methods against objects such as messages and users. A positive result modifies or deletes the object.
Glossary
- API: a defined set of addresses a program can call to ask another program for data or to tell it to do something.
- assign: a FreeMarker directive that creates a variable and stores a value in it.
- Authenticated: a request that carries the site's own credentials, so the receiving system treats it as a trusted user rather than a visitor.
- Bugcrowd: a platform where companies invite outside researchers to test their systems and reward them for finding problems.
- Built-in: a function FreeMarker offers out of the box, written with a
?in front of its name. - Burp Suite: a tool that sits between a browser and a website so requests can be inspected and changed.
- CMS: content management system, the software a company uses to store and publish its pages, users and posts.
- Collection: in Lithium, a grouping of content such as a forum or a blog section.
- Directive: a FreeMarker instruction that starts with
<#and does something rather than printing a value. - FreeMarker: a template engine, popular in the Java world, that fills placeholders in a template to build a page.
- Fuzz: to try many guesses automatically and watch for the one that behaves differently.
- Include: a FreeMarker directive that inserts the contents of another template file into the page being built.
- Intruder: the Burp Suite module that sends the same request many times with different values substituted in.
- LIQL: Lithium Query Language, a small query language for asking the Lithium platform for data.
- Package: in Java, a named group of ready-made code a program can use.
- Payload: the piece of text or data an attacker sends to try to trigger a bug.
- P1: the highest priority rating on Bugcrowd.
- PUT / POST / DELETE: web request methods; PUT replaces an object, POST creates one, DELETE removes one.
- RCE: remote code execution, running commands on someone else's server.
- REST API: an API that uses ordinary web requests at predictable URLs.
- Sandbox: a restricted environment meant to stop code from doing harm.
- SSTI: Server-Side Template Injection, when a visitor's input is treated as template code and executed by the server.
- Stack trace: the list of internal steps a program was taking when it crashed, sometimes shown to visitors by mistake.
- Template: a file that is mostly text and HTML with small placeholders that get filled in to build a page.
- Template context: the set of variables and functions a template is allowed to use.
- Template engine: the software that fills in a template's placeholders to produce the finished page.
- Variable: a named place in a program that holds a value.
- Wildcard: a character such as
*that stands for any text, used to match many names at once.