Home  >  Article  >  Backend Development  >  Usage analysis of eval() and exec() functions in Python

Usage analysis of eval() and exec() functions in Python

不言
不言forward
2019-03-25 09:55:243115browse

The content of this article is about the usage analysis of eval() and exec() functions in Python. It has certain reference value. Friends in need can refer to it. I hope it will help You helped.

Python provides many built-in utility functions (Built-in Functions), and in the latest Python 3 official documentation, it lists 69.

Most of the functions are commonly used by us, such as print(), open() and dir(). Although some functions are not commonly used, they can play an extraordinary role in certain scenarios. role. Built-in functions can be "promoted", which means that they have their own unique features and are useful.

Therefore, mastering the usage of built-in functions has become a skill that we should light up.

1. Basic usage of eval

Syntax: eval(expression, globals=None, locals =None)

It has three parameters, of which expression is a string type expression or code object used for calculation; globals and locals are optional parameters, and the default value is None.

Specifically, expression can only be a single expression and does not support complex code logic, such as assignment operations, loop statements, etc. (PS: A single expression does not mean "simple and harmless", see Section 4 below)

globals is used to specify the global namespace at runtime. The type is a dictionary. By default, the current The module's built-in namespace. locals specifies the local namespace at runtime, the type is a dictionary, and the value of globals is used by default. When both are defaulted, the scope when the eval function is executed is followed. It is worth noting that these two do not represent the real namespace. They only work during operation and are destroyed after operation.

x = 10

def func():
    y = 20
    a = eval('x + y')
    print('a: ', a)
    b = eval('x + y', {'x': 1, 'y': 2})
    print('x: ' + str(x) + ' y: ' + str(y))
    print('b: ', b)
    c = eval('x + y', {'x': 1, 'y': 2}, {'y': 3, 'z': 4})
    print('x: ' + str(x) + ' y: ' + str(y))
    print('c: ', c)

func()

Output result:

a:  30
x: 10 y: 20
b:  3
x: 10 y: 20
c:  4

It can be seen that when a namespace is specified, the variable will be searched in the corresponding namespace. Furthermore, their values ​​do not overwrite values ​​in the actual namespace.

2. Basic usage of exec

Syntax: exec(object[, globals[, locals ]])

In Python2, exec is a statement, but Python3 transforms it into a function, just like print. exec() is highly similar to eval(), and the three parameters have similar meanings and functions.

The main difference is that the first parameter of exec() is not an expression, but a code block, which means two things: first, it cannot evaluate the expression and return it; second, it It can execute complex code logic and is relatively more powerful. For example, when a new variable is assigned in a code block, the variable may survive in the namespace outside the function.

>>> x = 1
>>> y = exec('x = 1 + 1')
>>> print(x)
>>> print(y)
2
None

It can be seen that the namespaces inside and outside exec() are connected, and variables are passed out from here, unlike the eval() function, which requires a variable to receive the execution result of the function.

3. Some detailed analysis

Both functions are very powerful. They execute the string content as valid code. This is a string-driven event and is of great significance. However, in actual use, there are many tiny details. Here are a few of the ones I know.

Common uses: Convert strings to corresponding objects, such as string to list, string to dict, string to tuple, etc.

>>> a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>> print(eval(a))
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
>>> a = "{'name': 'Python猫', 'age': 18}"
>>> print(eval(a))
{'name': 'Python猫', 'age': 18}

# 与 eval 略有不同
>>> a = "my_dict = {'name': 'Python猫', 'age': 18}"
>>> exec(a)
>>> print(my_dict)
{'name': 'Python猫', 'age': 18}

The return value of the eval() function is the execution result of its expression. In some cases, it will be None, such as when the expression is a print() statement, or the append() of a list When operating, the result of this type of operation is None, so the return value of eval() will also be None.

>>> result = eval('[].append(2)')
>>> print(result)
None

The return value of the exec() function will only be None, which has nothing to do with the result of the execution statement. Therefore, there is no need to assign a value to the exec() function. If the executed statement contains return or yield , the value they produce cannot be used outside the exec function.

>>> result = exec('1 + 1')
>>> print(result)
None

The globals and locals parameters in the two functions function as a whitelist, which prevents the data within the scope from being abused by limiting the scope of the namespace.

conpile() The code object compiled by the function can be used as the first parameter of eval and exec. compile() is also a magical function. The last article I translated, "Python Sassy Operations: Dynamically Defining Functions", demonstrated the operation of dynamically defining functions.

Paradoxical local namespace: As mentioned earlier, variables within the exec() function can change the original namespace, but there are exceptions.

def foo():
    exec('y = 1 + 1\nprint(y)')
    print(locals())
    print(y)

foo()

According to the previous understanding, the expected result is that the variable y will be stored in the local variable, so the two printing results will be 2, but the actual result is:

2
{'y': 2}
Traceback (most recent call last):
...(略去部分报错信息)
    print(y)
NameError: name 'y' is not defined

I clearly see that there is a variable y in the local namespace, but why does it get an error saying it is undefined?

原因与 Python 的编译器有关,对于以上代码,编译器会先将 foo 函数解析成一个 ast(抽象语法树),然后将所有变量节点存入栈中,此时 exec() 的参数只是一个字符串,整个就是常量,并没有作为代码执行,因此 y 还不存在。直到解析第二个 print() 时,此时第一次出现变量 y ,但因为没有完整的定义,所以 y 不会被存入局部命名空间。

在运行期,exec() 函数动态地创建了局部变量 y ,然而由于 Python 的实现机制是“运行期的局部命名空间不可改变 ”,也就是说这时的 y 始终无法成为局部命名空间的一员,当执行 print() 时也就报错了。

至于为什么 locals() 取出的结果有 y,为什么它不能代表真正的局部命名空间?为什么局部命名空间无法被动态修改?可以查看我之前分享的《Python 动态赋值的陷阱》,另外,官方的 bug 网站中也有对此问题的讨论,查看地址:https://bugs.python.org/issue...

若想把 exec() 执行后的 y 取出来的话,可以这样:z = locals()['y'] ,然而如果不小心写成了下面的代码,则会报错:

def foo():
    exec('y = 1 + 1')
    y = locals()['y']
    print(y)
    
foo()

#报错:KeyError: 'y'
#把变量 y 改为其它变量则不会报错

KeyError 指的是在字典中不存在对应的 key 。本例中 y 作了声明,却因为循环引用而无法完成赋值,即 key 值对应的 value 是个无效值,因此读取不到,就报错了。

此例还有 4 个变种,我想用一套自恰的说法来解释它们,但尝试了很久,未果。留个后话吧,等我想明白,再单独写一篇文章。

4、为什么要慎用 eval() ?

很多动态的编程语言中都会有 eval() 函数,作用大同小异,但是,无一例外,人们会告诉你说,避免使用它。

为什么要慎用 eval() 呢?主要出于安全考虑,对于不可信的数据源,eval 函数很可能会招来代码注入的问题。

>>> eval("__import__('os').system('whoami')")
desktop-fa4b888\pythoncat
>>> eval("__import__('subprocess').getoutput('ls ~')")
#结果略,内容是当前路径的文件信息

在以上例子中,我的隐私数据就被暴露了。而更可怕的是,如果将命令改为rm -rf ~ ,那当前目录的所有文件都会被删除干净。

针对以上例子,有一个限制的办法,即指定 globals 为 {'__builtins__': None} 或者 {'__builtins__': {}} 。

>>> s = {'__builtins__': None}
>>> eval("__import__('os').system('whoami')", s)
#报错:TypeError: 'NoneType' object is not subscriptable

__builtins__ 包含了内置命名空间中的名称,在控制台中输入 dir(__builtins__) ,就能发现很多内置函数、异常和其它属性的名称。在默认情况下,eval 函数的 globals 参数会隐式地携带__builtins__ ,即使是令 globals 参数为 {} 也如此,所以如果想要禁用它,就得显式地指定它的值。

上例将它映射成 None,就意味着限定了 eval 可用的内置命名空间为 None,从而限制了表达式调用内置模块或属性的能力。

但是,这个办法还不是万无一失的,因为仍有手段可以发起攻击。

某位漏洞挖掘高手在他的博客中分享了一个思路,令人大开眼界。其核心的代码是下面这句,你可以试试执行,看看输出的是什么内容。

>>> ().__class__.__bases__[0].__subclasses__()

关于这句代码的解释,以及更进一步的利用手段,详见。(地址:http://www.php.cn/python-tutorials-416494.html

另外还有一篇博客,不仅提到了上例的手段,还提供了一种新的思路:

#警告:千万不要执行如下代码,后果自负。
>>> eval('(lambda fc=(lambda n: [c 1="c" 2="in" 3="().__class__.__bases__[0" language="for"][/c].__subclasses__() if c.__name__ == n][0]):fc("function")(fc("code")(0,0,0,0,"KABOOM",(),(),(),"","",0,""),{})())()', {"__builtins__":None})

这行代码会导致 Python 直接 crash 掉。具体分析在:http://www.php.cn/python-tutorials-416495.html

除了黑客的手段,简单的内容也能发起攻击。像下例这样的写法, 将在短时间内耗尽服务器的计算资源。

>>> eval("2 ** 888888888", {"__builtins__":None}, {})

如上所述,我们直观地展示了 eval() 函数的危害性,然而,即使是 Python 高手们小心谨慎地使用,也不能保证不出错。

在官方的 dumbdbm 模块中,曾经(2014年)发现一个安全漏洞,攻击者通过伪造数据库文件,可以在调用 eval() 时发起攻击。(详情:https://bugs.python.org/issue...)

无独有偶,在上个月(2019.02),有核心开发者针对 Python 3.8 也提出了一个安全问题,提议不在 logging.config 中使用 eval() 函数,目前该问题还是 open 状态。(详情:https://bugs.python.org/issue...)

如此种种,足以说明为什么要慎用 eval() 了。同理可证,exec() 函数也得谨慎使用。

5、安全的替代用法

既然有种种安全隐患,为什么要创造出这两个内置方法呢?为什么要使用它们呢?

理由很简单,因为 Python 是一门灵活的动态语言。与静态语言不同,动态语言支持动态地产生代码,对于已经部署好的工程,也可以只做很小的局部修改,就实现 bug 修复。

那有什么办法可以相对安全地使用它们呢?

ast 模块的 literal() 是 eval() 的安全替代,与 eval() 不做检查就执行的方式不同,ast.literal() 会先检查表达式内容是否有效合法。它所允许的字面内容如下:

strings, bytes, numbers, tuples, lists, dicts, sets, booleans, 和 None

一旦内容非法,则会报错:

import ast
ast.literal_eval("__import__('os').system('whoami')")

报错:ValueError: malformed node or string

不过,它也有缺点:AST 编译器的栈深(stack depth)有限,解析的字符串内容太多或太复杂时,可能导致程序崩溃。

至于 exec() ,似乎还没有类似的替代方法,毕竟它本身可支持的内容是更加复杂多样的。

最后是一个建议:搞清楚它们的区别与运行细节(例如前面的局部命名空间内容),谨慎使用,限制可用的命名空间,对数据源作充分校验。

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的python视频教程栏目!

The above is the detailed content of Usage analysis of eval() and exec() functions in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete