BinaryFormatter — TextFormattingRunProperties

hkln1
tradahacking
Published in
4 min readAug 31, 2021
  • Phần này mình sẽ giới thiệu các bạn một formatter class là BinaryFormatter. BinaryFormatter dùng để serialize object vào định dạng binary stream. Nó được khai báo ở namespace System.Runtime.Serialization.Formatters.Binary
  • Quan sát ta thấy nó implement interface IRemotingFormatter, IFormatter. Ngoài ra nó cũng có các method serialize, deserialize và proxy selector (thuật ngữ này mình cũng đã có đề cập ở .NET Deserialization 101)

Gadgets chain

  • Hầu hết các gadgets trong ysoserial.net đều support BinaryFormatter. Tuy nhiên mình sẽ đi vào phân tích chain TextFormattingRunProperties trước tiên. Vì các chain khác điều có nguồn gốc từ chain này.

Gadget TextFormattingRunProperties

  • Class dùng để tạo object serialize (TextFormattingRunPropertiesMarshal) implement interface ISerializable, có annotation [Serializable]. Để có thể serialize thành công ta phải tạo một constructor
  • Khi serialize, nó sẽ gọi method GetObjectData của object TextFormattingRunPropertiesMarshal. Vì trong trường hợp này ko có proxy selector
  • SerializationInfo info là object đại diện lưu giữ info của object sẽ serialize. Trong method GetObjectData ta set type của object info TextFormattingRunProperties, sau đó add field ForegroundBrush với giá trị của biến _xaml. Mà biến _xaml được gán thông qua constructor
  • Đây là hàm chính để serialize (mình có viết luôn dòng deserialize)
  • Biến xaml_payload được đọc từ file 1.txt, sau đó truyền và gán vào _xaml thông qua việc tạo instance TextFormattingRunPropertiesMarshal. Sau đó dùng binaryFormatter.Serialize để serialize.
  • Đây là nội dung của file 1.txt (ở phần này các bạn không cần hiểu syntax của file này, và lý do tại sao nó trigger calc, ở những phần sau mình sẽ giải thích nó, tạm thời mình chấp nhận khi nó được hàm xml parser nó sẽ thực thi code)
  • Bây giờ ta đi vào debug để hiểu nguyên nhân tại sao gadget này nó trigger code execution. Sau khi build xong chương trình, ta mở dnSpy (nhớ dùng ver win32) load hai file exe và dll để debug
  • Ở đây mình có add thêm file Microsoft.PowerShell.Editor.dll vào, bởi vì trong mã gen payload, mình có set type của object info TextFormattingRunProperties, mà class này được khai báo ở namespace Microsoft.VisualStudio.Text.Formatting trong file Microsoft.PowerShell.Editor.dll
  • Sau khi add xong, bấm Ctrl + Shift + K rồi search `TextFormattingRunProperties`. Khi deserialize, nó sẽ gọi constructor của object mà ta serialize (trong trường hợp này là TextFormattingRunProperties). Tiếp tục nó sẽ gọi hàm GetObjectFromSerializationInfo và truyền tham số là string ForegroundBrush
  • Khi đi vào hàm GetObjectFromSerializationInfo nó lấy giá trị lưu trong field ForegroundBrush của object info (nội dung của file 1.txt ta đã gán vào lúc serialize) và xử lý nó với hàm XamlReader.Parse. Lúc này code sẽ được thực thi.

Debug

  • Vào Debug tab, Chọn Start Debugging. Ô Executable chọn tới file exe. Ô Break at chọn Don’t Break

Bấm Ok

  • Nó sẽ ném ra exception với popup calc tại RuntimeMethodHandle.SerializationInvoke(this, target, info, ref context);
  • Set breakpoint tại dòng đó và chạy lại. Sau đó bấm F11 ta sẽ thấy nó nhảy vào construtor TextFormattingRunProperties.

Phần này tạm dừng ở đây, hẹn các bạn phần kế tiếp ;)

--

--