Preventing Privilege Escalation
Introduction
Welcome to the fourth lesson of the "Broken Access Control" course! In this lesson, we will explore the concept of privilege escalation, a critical aspect of broken access control vulnerabilities.
By understanding how attackers can exploit these vulnerabilities to gain unauthorized access to higher-level privileges, you'll be better equipped to secure your applications. Let's dive in and learn how to prevent privilege escalation! 🚀
Understanding Privilege Escalation
Privilege escalation occurs when an attacker gains elevated access to resources that are normally protected from an application or user. This is a common and severe vulnerability that can give an attacker full control over an application or system.
There are two main types of privilege escalation:
-
Vertical Privilege Escalation: This is when an attacker gains higher-level privileges. It's like a regular hotel guest getting a master keycard that opens every room.
- Example: A regular user manipulates a request to change their role to
admin. - Example: An attacker accesses an admin-only dashboard (e.g.,
/admin/dashboard) that lacks proper access control checks.
- Example: A regular user manipulates a request to change their role to
-
Horizontal Privilege Escalation: This is when an attacker gains access to the resources of another user with the same level of privileges. It's like a hotel guest using their keycard to open the room next door.
- Example: A user changes the
idin a URL like/orders/123to/orders/124and successfully views another user's order. - Example: An attacker accesses another user's private messages or profile information.
- Example: A user changes the
Understanding these concepts is vital for securing web applications against unauthorized access. Let's look at a vulnerable code example to see how these vulnerabilities can manifest in real applications.
Vulnerable Code Example
Let's examine a code snippet that demonstrates a vulnerability allowing vertical privilege escalation. This example shows how a lack of proper validation can lead to unauthorized role changes.
The vulnerability lies in the for loop. It iterates through all key-value pairs in the JSON payload sent by the client and uses setattr to update the user object's attributes directly. This is known as mass assignment. Because there is no filter, an attacker can include sensitive fields like role in their request, and the server will blindly update them.
