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:

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:

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.

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***.


call tới parseExpression(expression) để parse expression "centerName" sang dạng tree.

return lại Node

Sau đó call evaluateGetValueBody() -> getValueBody()

thì getValueBody() chính là nơi sẽ lấy ra giá trị của property **centerName**.
getValueBody -> OgnlRuntime.getProperty() -> getPossibleProperty() -> getMethodValue()

Đế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".

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

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

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

- i=0 -> ASTStaticMethod

Cuối cùng obtain được object Runtime.

- i=1 -> ASTMethod
Cũng tương tự vậy

Vì có rất nhiều định nghĩa method exec()

Tiếp tục call tới callAppropriateMethod() để lấy ra method exec() phù hợp với tham số truyền vào.

Tiếp theo là java reflection.

Qua đó phần nào cũng có thể hiểu được cách payload nó hoạt động ra làm sao.