Try   HackMD

Series phân tích các CVE Microsoft SharePoint Server - Part 1 - CVE 2021-34520

Với hi vọng một ngày nào đó sẽ có 0day của Sharepoint thì mình và anh Sơn đã đi phân tích các CVE cũ của Sharepoint để biết được các lỗi trước đây được khai thác như vào và hiểu rõ chúng. Trước khi đi vào phân tích về CVE 2021-34520 thì mình sẽ tìm hiểu về Sharepoint trước.

Tổng quan về Sharepoint

SharePoint là nền tảng quản lý tài liệu và cộng tác dựa trên web. Mặc dù rất linh hoạt, SharePoint chủ yếu được sử dụng để lưu trữ tài liệu và giao tiếp thông tin giữa các doanh nghiệp.

Với SharePoint, người dùng có thể tạo mạng nội bộ (hoặc hệ thống internet nội bộ) hoạt động giống như bất kỳ trang web nào khác. Các trang con có thể được tạo cho các phòng ban hoặc nhóm cụ thể. Thông qua không gian tập trung, bảo mật này, người dùng có thể truy cập, chia sẻ và chỉnh sửa tài liệu.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Một số tính năng mà SharePoint cung cấp bao gồm:

  • Chia sẻ bên ngoài doanh nghiệp: tệp và nội dung với mọi người cả bên trong và bên ngoài doanh nghiệp.
  • Các tính năng quản lý nội dung: giúp doanh nghiệp sắp xếp, quản lý nội dung bằng cách sử dụng thư viện (libraries), danh sách (lists), metadata, quản lý hồ sơ, các records, các chính sách v.v…
  • Các trang web nhóm/Team Site: cung cấp một nơi cho các nhóm người dùng được xác định trước để xem và cộng tác về nội dung, dữ liệu và tin tức/thông tin.
  • Các trang web giao tiếp/Communication Site: dùng để chia sẻ và truyền đạt thông điệp giữa các doanh nghiệp với các dynamic sub-sites có thể tuỳ chỉnh.
  • Ứng dụng dành cho thiết bị di động cho phép người dùng truy cập mạng nội bộ, trang nhóm/team sites và nội dung trên thiết bị Android, iOS và Windows.
  • Tự động hóa quy trình kinh doanh bằng cách tạo cảnh báo và quy trình làm việc.
  • Chức năng tìm kiếm giúp hiển thị những người có liên quan và nội dung quan trọng.

Setup và diff patch

Theo bản Security Update thì lỗi này được vá ở bản KB5001976 => Mình sẽ cần tải và cài đặt 2 bản sau để tiến hành diff code đó là KB5001946 (bản vuln) và KB5001976 (bản patch).

Mọi người có thể theo dõi các bản Sharepoint Update ở đây https://learn.microsoft.com/en-us/officeupdates/sharepoint-updates

Sau khi cài thành công bán vuln thì snapshot sau đó tiếp tục cài bản patch. Khi có 2 bản snapshot thì thực hiện lấy các source code liên quan đến SharePoint để tiến hành diff.

Sử dụng command dưới đây để lấy source code SharePoint

7z a dll.7z -r C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Sharepoint*.dll "C:\Program Files\Common Files\microsoft shared\Microsoft.Sharepoint*.dll" C:\inetpub\wwwroot\wss\Microsoft.Sharepoint*.dll

Sau khi gom các file dll thì tiến hành decompile bằng tool dnSpy và thực hiện diff bằng tool Winmerge. Để thực hiện diff rõ ràng nhất thì sau khi decompile thì nên dọn rác các file và sử dụng Filters trong Winmerge để lọc theo ý muốn (Phần này mỗi người sẽ có 1 cách khác nhau).

Theo Advisories ZDI thì lỗi này tồn tại ở class Microsoft.SharePoint.WorkflowActions.SetVariableActivity => lỗi này có thể liên quan đến workflow action trong việc sử dụng workflow. Vậy trước tiên phải đi tìm hiểu về workflow là gì trong sharepoint.

Có thể đọc tài liệu của Microsof SharePoint Workflow để hiểu thêm về workflow trong sharepoint, ở đây mình chỉ tóm tắt vài ý chính dễ hiểu về workflow như sau:

  • Workflow được xem như một quy trình công việc lặp lại những nhiệm vụ cần thực hiện.
  • Một workflow thì sẽ có nhiều step trong mỗi step thì một hoặc nhiều workflow action (activity) đảm nhận mỗi hoạt động cho workflow.
  • Có 2 kiểu workflow chính trong Sharepoint:
    • Built-in workflows: Các bộ template workflow được xây dựng sẵn trong Sharepoint.
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • Custom workflows: Được xây dựng bởi người dùng, để xây dựng được các workflow thì có 2 cách như trong tài liệu ở trên đề cập nhưng ở đây mình chỉ sài tới tool Microsoft SharePoint Designer 2013.

Sau khi tìm hiểu về workflow và diff code thì thấy được có một hàm mới được thêm vào trong bản patch có liên quan đến SetVariableActivity và advisories đã đề cập ở class Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Đọc thì thấy hàm này validate thuộc tính ValueType của class SetVariableActivity.

Trace tiếp thấy hàm IsGoodSetVariableActivityValueType được hàm IsGoodWorkflow gọi.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Tiếp tục hàm IsGoodWorkflow được sử dụng ở hàm CompileBytes.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Và hàm CompileBytes được gọi ở hàm DoWorkflowValidateWorkflowMarkup và sau đó hàm này được call ở hàm ValidateWorkflowMarkupAndCreateSupportObjects tại class Microsoft.SharePoint.SoapServer.WebPartPagesWebService.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Tìm hiểu về hàm ValidateWorkflowMarkupAndCreateSupportObjects theo document của Microsoft thì biết được, hàm này sẽ nhận nhiệm vụ validate và compiles các nội dung của file workflow được tải lên server => CVE này liên quan đến thuộc tính ValueType mà người dùng có thể tùy chỉnh.

Nhưng điều mình muốn là có thể RCE được nhưng từ ban đầu diff đến giờ không hề thấy sink có thể thực hiện việc này. Theo advisories thì lỗi nằm ở class SetVariableActivity và những đoạn code được thêm vào ở đoạn patch trên là validate liên quan đến class này => có thể sink cần RCE nằm ở trong này.

Class Microsoft.SharePoint.WorkflowActions.SetVariableActivity.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • Cần tìm hiểu về hàm Execute này thì biết được Called by the workflow runtime to execute an activity. có nghĩa là khi workflow được run và thực thi workflow action thì hàm này sẽ được gọi. document
  • objtype được lấy lần lượt thừ ValueValueType, để biết được 2 giá trị này là gì thì cần đặt debug ở đây và tạo 1 workflow sau đó start workflow đó thì sẽ nhảy vào breakpoint.
  • Ở khoanh đỏ thứ 3 thì có thế gọi constructor tùy ý nhưng class mình cần là class nào thì chưa thể tìm thấy. Trước khi đi tìm class thì mình cần debug để hiểu các tham số trên được lấy từ đâu và nó là gì.

Debug

Để debug thì trước tiên cần tạo upload lên 1 file bất kì lên như sau.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Tiếp tục tạo 1 workflow bằng Sharepoint Designer.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Nhập tên có workflow mình muốn tạo.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Chọn workflow action là Set Workflow Variable như vậy sẽ tạo được một SetVariableActivity

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Tạo variable và value lần lượt. Variable -> Hello và Value -> World, cả 2 đều là type String.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Cuối cùng public workflow đó.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Quay về với trang sharepoint chọn như sau để chạy workflow

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Đặt breakpoint và chạy workflow

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Sau khi debug thì thấy được các giá trị của objtype như khoanh đỏ bên dưới. Cụ thể là obj được gán bằng wordtype được gán là String.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Vậy có thể thấy rằng 2 giá trị của objtype là mình có thể control vì các giá trị này là của mình set ở bước tạo varialbe ở trên.

Check các file được tạo