securityman
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Nhắc đến OGNL Injection thì trong đầu thường nghĩ ngay đến Struts2 Framework hoặc Confluence vì đã có rất nhiều CVE và các bài phân tích đã public trên 2 đối tượng này. Khi mới tiếp xúc OGNL injection thì hầu như chỉ biết copy/paste payload đem về sử dụng chứ cũng không rõ cách người ta xây dựng payload hoặc payload nó hoạt động như thế nào. Bài viết này sẽ chia sẻ cơ bản về OGNL để giúp có cái nhìn chi tiết hơn. **1. OGNL là gì** OGNL được viết tắt bởi Object-Graph Navigation Language, không biết dịch sao cho dễ hiểu, tạm hiểu "ngôn ngữ duyệt object thông qua graph". OGNL cho phép set/get các thuộc tính của java object, cho nên hay được sử dụng trong mô hình MVC để bind java object -> View. Tham khảo thêm: - https://commons.apache.org/dormant/commons-ognl/index.html - https://github.com/orphan-oss/ognl > OGNL stands for Object-Graph Navigation Language; it is an expression language for getting and setting properties of Java objects. You use the same expression for both getting and setting the value of a property. > > The ognl.Ognl class contains convenience methods for evaluating OGNL expressions. You can do this in two stages, parsing an expression into an internal form and then using that internal form to either set or get the value of a property; or you can do it in a single stage, and get or set a property using the String form of the expression directly. **2. Tìm hiểu OGNL thông qua example** Tạo một project java. ```java= import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException; class Group { String groupName; Group(String name){ groupName = name; } public String getGroupName(){ return this.groupName; } } class Company{ String companyName; Group group; public void setCompanyName(String name){ this.companyName = name; } public void setGroup(Group g){ this.group = g; } public String getCompanyName(){ return this.companyName; } public Group getGroup(){ return this.group; } } class Center{ String CenterName; Company company; public void setCenterName(String name){ this.CenterName = name; } public void setCompany(Company c){ this.company = c; } public String getCenterName(){ return this.CenterName; } public Company getCompany(){ return this.company; } } public class OGNL { public static void main(String[] args) throws OgnlException { Company newcompany = new Company(); newcompany.setCompanyName("VNPT-IT"); newcompany.setGroup(new Group("VNPT-GROUP")); Center attt = new Center(); attt.setCenterName("VCI"); attt.setCompany(newcompany); Center ic = new Center(); ic.setCenterName("IC"); ic.setCompany(newcompany); OgnlContext context = new OgnlContext (); context.setRoot (attt); context.put("ic",ic); // Tham chiếu đến root object thì không cần sử dụng dấu # Object attt_name = Ognl.getValue ("CenterName",context,context.getRoot()); Object attt_company = Ognl.getValue ( "company.companyName" ,context ,context.getRoot ()); Object attt_group = Ognl.getValue ( "company.group.groupName " , context, context.getRoot()); System.out.println ("Trung tâm: " + attt_name + "\r\nCông ty: " + attt_company + "\r\nTập Đoàn: " + attt_group); System.out.println ("-----------------------------------"); // Sử dụng dấu # để tham chiếu đến non-root object Object ic_name = Ognl.getValue ("#ic.CenterName",context,context.getRoot()); Object ic_company = Ognl.getValue ( "#ic.company.companyName" ,context ,context.getRoot ()); Object ic_group = Ognl.getValue ( "#ic.company.group.groupName " , context, context.getRoot()); System.out.println ("Trung tâm: " + ic_name + "\r\nCông ty: " + ic_company + "\r\nTập Đoàn: " + ic_group); } } ``` Trong đoạn code trên định nghĩa 3 class: - Group: gồm property ***groupName*** - Company: gồm property ***companyName*** & object ***group*** (type Group) - Center: gồm property ***centerName*** & object ***company*** (type Company) Khởi tạo thông tin liên quan đến 2 trung tâm VCI & IC: Center attt & Center ic. Chạy đoạn code trên sẽ output ra kết quả như sau: ![image](https://hackmd.io/_uploads/HJNdopLwp.png) Giả sử đoạn code trên được sửa lại: ```java= ... Ognl.setValue("centerName",context,context.getRoot(),"edited-VCI"); Object attt_name = Ognl.getValue ("centerName",context,context.getRoot()); Object attt_company = Ognl.getValue ( "company.companyName" ,context ,context.getRoot ()); Object attt_group = Ognl.getValue ( "company.group.groupName " , context, context.getRoot()); System.out.println ("Trung tâm: " + attt_name + "\r\nCông ty: " + attt_company + "\r\nTập Đoàn: " + attt_group); ... ``` Output lúc này: ![image](https://hackmd.io/_uploads/H1UAT6UP6.png) Sử dụng OGNL hoàn toàn có thể truy xuất (getter) hoặc thay đổi giá trị (setter) property của object. Tiếp theo đi vào tìm hiểu cách setter/getter của OGNL nó như thế nào. **3. Cách hoạt động** Thanks to chatgpt: > In Object Graph Navigation Language (OGNL), the OgnlContext is a class that represents the context in which OGNL expressions are evaluated. It holds variables and other contextual information that can be referenced within OGNL expressions. The OgnlContext is used to manage the state of the OGNL evaluation process. Quay lại đoạn code ban đầu, dễ thấy "OgnlContext context = new OgnlContext ();" được sử dụng để lưu trữ các object Center attt&ic đã được tạo. Cho dễ hiểu thì nó lưu objects và cung cấp thông tin cho quá trình duyệt object. Class OgnlContext thực chất là implementation từ Map type. ![image](https://hackmd.io/_uploads/rkjwhoPDa.png) Sau khi google một vòng thì Ognl.getValue() và Ognl.setValue() chính là sink của loại lỗ hổng này. Đặt breakpoint và tiến hành debug. context.getRoot() -> chính là object ***attt***. ![image](https://hackmd.io/_uploads/BJOeT-uv6.png) ![image](https://hackmd.io/_uploads/BJGbUGuPa.png) call tới parseExpression(expression) để parse expression "centerName" sang dạng tree. ![image](https://hackmd.io/_uploads/SJjhrG_vp.png) return lại Node ![image](https://hackmd.io/_uploads/rkGzPM_vp.png) Sau đó call evaluateGetValueBody() -> getValueBody() ![image](https://hackmd.io/_uploads/Hk6-_GdPT.png) thì getValueBody() chính là nơi sẽ lấy ra giá trị của property **centerName**. getValueBody -> OgnlRuntime.getProperty() -> getPossibleProperty() -> getMethodValue() ![image](https://hackmd.io/_uploads/r1pqtz_w6.png) Đến đoạn này thì đơn giản nó sử dụng java reflection để lấy ra giá trị của thuộc tính là chuỗi "VCI". ![image](https://hackmd.io/_uploads/HkMQ9GOD6.png) Tóm lại stacktrace sẽ như này: > invokeMethod:495, OgnlRuntime (ognl) > getMethodValue:904, OgnlRuntime (ognl) > getPossibleProperty:54, ObjectPropertyAccessor (ognl) > getProperty:122, ObjectPropertyAccessor (ognl) > getProperty:1616, OgnlRuntime (ognl) > getValueBody:96, ASTProperty (ognl) > evaluateGetValueBody:171, SimpleNode (ognl) > getValue:213, SimpleNode (ognl) > getValue:333, Ognl (ognl) > getValue:378, Ognl (ognl) > getValue:357, Ognl (ognl) > main:64, OGNL **4. Tiến hành debug execute-command payload** ```java= Map context = new HashMap(); Ognl.getValue("@java.lang.Runtime@getRuntime().exec(\"calc\")", context, ""); ``` - Dấu @ được sử dụng để truy cập tới static method ![image](https://hackmd.io/_uploads/r1gu0zdvT.png) So sánh với phần 3, có thể thấy sau khi parse thì ASTChain được return thay vì ASTProperty, cũng dễ hiểu vì expression đưa vào là *@java.lang.Runtime@getRuntime().exec("calc")*. > In Object Graph Navigation Language (OGNL), ASTChain and ASTProperty are classes representing Abstract Syntax Tree (AST) nodes that are part of the OGNL parsing and evaluation process. They are specific to the internal structure of OGNL expressions. > - The ASTChain class represents a chain of property access or method invocations in an OGNL expression. It is part of the AST structure that OGNL builds during the parsing phase. > - The ASTProperty class represents a property access in an OGNL expression. It is used when accessing the value of a property or invoking a getter method on an object. > Tham khảo thêm về AST: https://dev.to/balapriya/abstract-syntax-tree-ast-explained-in-plain-english-1h38 ![image](https://hackmd.io/_uploads/SJcEl7ODT.png) Dễ thấy payload đưa vào sẽ được parse vào 2 Node ASTStaticMethod và ASTMethod. Chạy vòng lặp cho this.children ![image](https://hackmd.io/_uploads/HkJ--XOPp.png) - i=0 -> ASTStaticMethod ![image](https://hackmd.io/_uploads/Hy44M7Owp.png) Cuối cùng obtain được object Runtime. ![image](https://hackmd.io/_uploads/HJPDzXdP6.png) - i=1 -> ASTMethod Cũng tương tự vậy ![image](https://hackmd.io/_uploads/HJHdX7dPp.png) Vì có rất nhiều định nghĩa method exec() ![image](https://hackmd.io/_uploads/S1kGVXdva.png) Tiếp tục call tới callAppropriateMethod() để lấy ra method exec() phù hợp với tham số truyền vào. ![image](https://hackmd.io/_uploads/rJoPVQ_Pa.png) Tiếp theo là java reflection. ![image](https://hackmd.io/_uploads/S1YY47uvp.png) Qua đó phần nào cũng có thể hiểu được cách payload nó hoạt động ra làm sao.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully