Skip to content

Authenticated (user role) SQL injection in `OrderAndPaginate` (GHSL-2023-270)

High severity GitHub Reviewed Published Jan 11, 2024 in 0xJacky/nginx-ui • Updated Jan 11, 2024

Package

gomod github.com/0xJacky/Nginx-UI (Go)

Affected versions

< 2.0.0.beta.9

Patched versions

2.0.0.beta.9

Description

Summary

The OrderAndPaginate function is used to order and paginate data. It is defined as follows:

func OrderAndPaginate(c *gin.Context) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		sort := c.DefaultQuery("order", "desc")

		order := fmt.Sprintf("`%s` %s", DefaultQuery(c, "sort_by", "id"), sort)
		db = db.Order(order)

		...
	}
}

By using DefaultQuery, the "desc" and "id" values are used as default values if the query parameters are not set. Thus, the order and sort_by query parameter are user-controlled and are being appended to the order variable without any sanitization.
The same happens with SortOrder, but it doesn't seem to be used anywhere.

func SortOrder(c *gin.Context) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		sort := c.DefaultQuery("order", "desc")
		order := fmt.Sprintf("`%s` %s", DefaultQuery(c, "sort_by", "id"), sort)
		return db.Order(order)
	}
}

This issue was found using CodeQL for Go: Database query built from user-controlled sources.

Proof of Concept

Based on this setup using uozi/nginx-ui:v2.0.0-beta.7.
In order to exploit this issue, we need to find a place where the OrderAndPaginate function is used. We can find it in the GET /api/dns_credentials endpoint.

func GetDnsCredentialList(c *gin.Context) {
	cosy.Core[model.DnsCredential](c).SetFussy("provider").PagingList()
}

The PagingList function is defined as follows:

func (c *Ctx[T]) PagingList() {
	data, ok := c.PagingListData()
	if ok {
		c.ctx.JSON(http.StatusOK, data)
	}
}

And the PagingListData function is defined as follows:

func (c *Ctx[T]) PagingListData() (*model.DataList, bool) {
	result, ok := c.result()
	if !ok {
		return nil, false
	}

	result = result.Scopes(c.OrderAndPaginate())
	...
}

Using the following request, an attacker can retrieve arbitrary values by checking the order used by the query. That is, the result of the comparison will make the response to be ordered in a specific way.

GET /api/dns_credentials?sort_by=(CASE+WHEN+(SELECT+1)=1+THEN+id+ELSE+updated_at+END)+ASC+--+ HTTP/1.1
Host: 127.0.0.1:8080
Authorization: <<JWT TOKEN>

You can notice the order change by changing =1 to =2, and so the comparison will return false and the order will be updated_at instead of id.

Impact

This issue may lead to Information Disclosure

References

@0xJacky 0xJacky published to 0xJacky/nginx-ui Jan 11, 2024
Published to the GitHub Advisory Database Jan 11, 2024
Reviewed Jan 11, 2024
Published by the National Vulnerability Database Jan 11, 2024
Last updated Jan 11, 2024

Severity

High

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Network
Attack complexity
High
Privileges required
None
User interaction
None
Scope
Unchanged
Confidentiality
High
Integrity
Low
Availability
Low

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:L/A:L

EPSS score

0.049%
(20th percentile)

Weaknesses

CVE ID

CVE-2024-22196

GHSA ID

GHSA-h374-mm57-879c

Source code

Credits

Loading Checking history
See something to contribute? Suggest improvements for this vulnerability.