# Portswigger Server-side Template Injection
## 1. **Basic server-side template injection**
Description
> This lab is vulnerable to server-side template injection due to the unsafe construction of an ERB template.
To solve the lab, review the ERB documentation to find out how to execute arbitrary code, then delete the morale.txt file from Carlos's home directory.
Khi truy cập post về product đầu tiên thì xuất hiện thông báo ``out of stock`` được lấy từ tham số ``message`` trên URL -> Có thể dính XSS hoặc SSTI. Nhưng khi test XSS thì không thành công vì đã bị encode.

**Detect SSTI**: Fuzz ``message`` bằng các kí tự ``${{<%[%'"}}%\`` ta thấy server báo lỗi và phát hiện server sử dụng template ERB của Ruby.

**Identify** : Đọc syntax của ERB và thử với payload ``<%= 7*7 %>``, ta thấy server render ra ``49`` -> Ứng dụng bị dính SSTI

**Exploit**: Sử dụng payload ``<%= system('rm /home/carlos/morale.txt') %>`` đẻ xóa file ``morale.txt`` -> server thực thi thành công và trả về ``true``.

## 2. **Basic server-side template injection (code context)**
Description
> This lab is vulnerable to server-side template injection due to the way it unsafely uses a Tornado template. To solve the lab, review the Tornado documentation to discover how to execute arbitrary code, then delete the morale.txt file from Carlos's home directory.
You can log in to your own account using the following credentials: wiener:peter
Sau khi đăng nhập, ứng dụng có chức năng chỉnh sửa tên hiển thị của user tại các post theo Name, First Name hoặc Nickname.

Mặc định là Name -> Comment thử vào 1 post thì thấy hiển thị ``Peter Wiener``.

Thay đổi cách hiển thị bằng nickname.
Lúc này tên tác giả comment đã đổi thành nickname.

**Detect**: Thử fuzz ``blog-post-author-display`` bằng các kí tự đặc biệt.

thì server trả lỗi kèm theo tên template là Tornado.

Lúc này có thể mường tưởng rằng server render tên author bằng ``{{blog-post-author-display}}``
**Identify**: Test ``blog-post-author-display=7*'7'`` ta thấy giả định trên hoàn toàn đúng.

**Exploit**: Sử dụng payload sau theo syntax của Tornado để thực hiện xóa file ``morale.txt``.
```
blog-post-author-display=7*'7'}}{% import os %}{{os.system('rm /home/carlos/morale.txt')
```
## 3. **Server-side template injection using documentation**
Description
> This lab is vulnerable to server-side template injection. To solve the lab, identify the template engine and use the documentation to work out how to execute arbitrary code, then delete the morale.txt file from Carlos's home directory.
You can log in to your own account using the following credentials: content-manager:C0nt3ntM4n4g3r
Sau khi đăng nhập, tại mỗi post, ứng dụng có chức năng cho phép ``edit template``.

Mình có thể tùy ý sử dụng syntax của template. Mặc định trong file ban đầu, template sử dụng syntax ``${}``. Thử với ``${7*7}`` ta thấy ứng dụng render ra ``49`` thành công.

Khi truyền vào expression lỗi ``${a}`` thì server báo lỗi chứa tên template là ``freemaker``.

Đọc docs cũng như tham khảo payload có sẵn, ta biết được object ``Execute`` để RCE.
``${"freemarker.template.utility.Execute"?new()("rm /home/carlos/morale.txt")}``
## 4. **Server-side template injection in an unknown language with a documented exploit**
Description
> This lab is vulnerable to server-side template injection. To solve the lab, identify the template engine and find a documented exploit online that you can use to execute arbitrary code, then delete the morale.txt file from Carlos's home directory.
Ứng dụng render lỗi thông qua tham số ``message`` trên URL.

Fuzz ``message`` bằng ``${{<%[%'"}}%\``, ta thấy server trả lỗi kèm theo tên template engine Handlebars của Nodejs -> Server có thể dính SSTI.

Tham khảo payload có sẵn tại hacktrick, ta có thể RCE qua SSTI.

Sửa câu lệnh thành ``rm /home/carlos/morale.txt`` và gửi request với payload đã được URL-encoded.

## 5. **Server-side template injection with information disclosure via user-supplied objects**
Description
> This lab is vulnerable to server-side template injection due to the way an object is being passed into the template. This vulnerability can be exploited to access sensitive data.
To solve the lab, steal and submit the framework's secret key.
You can log in to your own account using the following credentials: content-manager:C0nt3ntM4n4g3r
Như các bước trước đó thì ta detect được template mà trang web đang sử dụng là django.
Sau khi tham khảo docs cũng như [Link](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Server%20Side%20Template%20Injection#django-templates), ta biết được django có hàm built-in {% debug %} để leak thông tin debug. Kết quả trả về có chứa object khác product là settings.

Thông thường trong ứng dụng Django, secret key sẽ được lưu tại biến ``SECRET_KEY`` trong object ``settings``. Sử dụng syntax ``{{ settings.SECRET_KEY }}`` và ta lấy được secret key cần tìm.
## 6. **Server-side template injection in a sandboxed environment**
Description
> This lab uses the Freemarker template engine. It is vulnerable to server-side template injection due to its poorly implemented sandbox. To solve the lab, break out of the sandbox to read the file my_password.txt from Carlos's home directory. Then submit the contents of the file.
You can log in to your own account using the following credentials: content-manager:C0nt3ntM4n4g3r
Như lab thứ 3 và biết được template mà nó đang sử dụng là Freemaker.
Thử RCE thì bằng payload:
``<#assign ex = "freemarker.template.utility.Execute"?new()>${ ex("id")}``
Kết quả trả về ``freemarker.template.utility.Execute is not allowed ...`` -> Ứng dụng chỉ cho phép edit trong môi trường sandbox, có thể là **Sandbox is based on method blocklist**.

Sau khi đọc [whitepaper](https://media.defcon.org/DEF%20CON%2028/DEF%20CON%20Safe%20Mode%20presentations/DEF%20CON%20Safe%20Mode%20-%20Alvaro%20Mun%CC%83oz%20and%20Oleksandr%20Mirosh%20-%20Room%20For%20Escape%20Scribbling%20Outside%20The%20Lines%20Of%20Template%20Security.pdf) của Blackhat về cách bypass sandbox, ta có thể dùng cách sau dựa vào object product sẵn có:
```
<#assign classloader=product.class.protectionDomain.classLoader>
<#assign owc=classloader.loadClass("freemarker.template.ObjectWrapper")>
<#assign dwf=owc.getField("DEFAULT_WRAPPER").get(null)>
<#assign ec=classloader.loadClass("freemarker.template.utility.Execute")>
${dwf.newInstance(ec,null)("<SYSTEM CMD>")}
```