Описание
ASTEVAL Allows Malicious Tampering of Exposed AST Nodes Leads to Sandbox Escape
Summary
If an attacker can control the input to the asteval library, they can bypass its safety restrictions and execute arbitrary Python code within the application's context.
Details
The vulnerability is rooted in how asteval performs attribute access verification. In particular, the on_attribute node handler prevents access to attributes that are either present in the UNSAFE_ATTRS list or are formed by names starting and ending with __, as shown in the code snippet below:
While this check is intended to block access to sensitive Python dunder methods (such as __getattribute__), the flaw arises because instances of the Procedure class expose their AST (stored in the body attribute) without proper protection:
Since the body attribute is not protected by a naming convention that would restrict its modification, an attacker can modify the AST of a Procedure during runtime to leverage unintended behaviour.
The exploit works as follows:
-
The Time of Check, Time of Use (TOCTOU) Gadget:
In the code below, a variable named
unsafeis set based on whethernode.attris considered unsafe:unsafe = (node.attr in UNSAFE_ATTRS or (node.attr.startswith('__') and node.attr.endswith('__'))) -
Exploiting the TOCTOU Gadget:
An attacker can abuse this gadget by hooking any
AttributeAST node that is not in theUNSAFE_ATTRSlist. The attacker modifies thenode.attr.startswithfunction so that it points to a custom procedure. This custom procedure performs the following steps:- It replaces the value of
node.attrwith the string"__getattribute__"and returnsFalse. - Thus, when
node.attr.startswith('__')is evaluated, it returnsFalse, which causes the condition to short-circuit and setsunsafetoFalse. - However, by that time,
node.attrhas been changed to"__getattribute__", which will be used in the subsequentgetattr(sym, node.attr)call. An attacker can then use the obtained reference tosym.__getattr__to retrieve malicious attributes without needing to pass theon_attributechecks.
- It replaces the value of
PoC
The following proof-of-concept (PoC) demonstrates how this vulnerability can be exploited to execute the whoami command on the host machine:
Пакеты
asteval
<= 1.0.5
1.0.6
8.4 High
CVSS3
Дефекты
8.4 High
CVSS3