首页 > 其他分享 >[转]使用 exec 函数时需要注意的一些安全问题

[转]使用 exec 函数时需要注意的一些安全问题

时间:2022-08-22 18:36:52浏览次数:104  
标签:__ 函数 exec TokenInfo 安全 line type start string

转载地址:https://www.ucloud.cn/yun/37950.html
众所周知,在 python 中可以使用 exec 函数来执行包含 python 源代码的字符串:

>>> code = """
   ...: a = "hello"
   ...: print(a)
   ...: """
>>> exec(code)
hello
>>> a
"hello"

exec 函数的这个功能很是强大,慎用。如果一定要用的话,那么就需要注意一下下面这些安全相关的问题。

全局变量和内置函数
在 exec 执行的代码中,默认可以访问执行 exec 时的局部变量和全局变量, 同样也会修改全局变量。如果 exec 执行的代码是根据用户提交的数据生产的话,这种默认行为就是一个安全隐患。

如何更改这种默认行为呢?可以通过执行 exec 函数的时候再传两个参数的方式来 修改这种行为(详见 之前 关于 exec 的文章):

>>> g = {}
>>> l = {"b": "world"}
>>> exec("hello = "hello" + b", g, l)
>>> l
{"b": "world", "hello": "helloworld"}
>>> g
{"__builtins__": {...}}
>>> hello
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
...
NameError: name "hello" is not defined

如果要限制使用内置函数的话,可以在 globals 参数中定义一下 builtins 这个 key:

>>> g = {}
>>> l = {}
>>> exec("a = int("1")", g, l)
>>> l
{"a": 1}
>>> g = {"__builtins__": {}}
>>> exec("a = int("1")", g, l)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
NameError: name "int" is not defined
>>>

现在我们限制了访问和修改全局变量以及使用内置函数,难道这样就万事大吉了吗? 然而并非如此,还是可以通过其他的方式来获取内置函数甚至 os.system 函数。

另辟蹊径获取内置函数和 os.system
通过函数对象:

>>> def a(): pass
...
>>> a.__globals__["__builtins__"]
>>> a.__globals__["__builtins__"].open

通过内置类型对象:

>>> for cls in {}.__class__.__base__.__subclasses__():
...     if cls.__name__ == "WarningMessage":
...         b = cls.__init__.__globals__["__builtins__"]
...         b["open"]
...
>>>

获取 os.system:

>>> cls = [x for x in [].__class__.__base__.__subclasses__() if x.__name__ == "_wrap_close"][0]
>>> cls.__init__.__globals__["path"].os
>>>

对于这两种办法又如何应对呢? 一种办法就是禁止访问以 _ 开头的属性:

如果可以控制 code 的生成,那么就在生成 code 的时候判断

如果不能的话,可以通过 dis 模块分析生成的 code (dist 无法分析嵌套函数的代码)

使用 tokenize 模块:

    In [68]: from io import BytesIO
    In [69]: code = """
       ....: a = "b"
       ....: a.__str__
       ....: def b():
       ....:     b.__get__
       ....: """
    In [70]: t = tokenize(BytesIO(code.encode()).readline)
    In [71]: for x in t:
       ....:     print(x)
       ....:
    TokenInfo(type=59 (ENCODING), string="utf-8", start=(0, 0), end=(0, 0), line="")
    TokenInfo(type=58 (NL), string="
", start=(1, 0), end=(1, 1), line="
")
    TokenInfo(type=1 (NAME), string="a", start=(2, 0), end=(2, 1), line="a = "b"
")
    TokenInfo(type=53 (OP), string="=", start=(2, 2), end=(2, 3), line="a = "b"
")
    TokenInfo(type=3 (STRING), string=""b"", start=(2, 4), end=(2, 7), line="a = "b"
")
    TokenInfo(type=4 (NEWLINE), string="
", start=(2, 7), end=(2, 8), line="a = "b"
")
    TokenInfo(type=1 (NAME), string="a", start=(3, 0), end=(3, 1), line="a.__str__
")
    TokenInfo(type=53 (OP), string=".", start=(3, 1), end=(3, 2), line="a.__str__
")
    TokenInfo(type=1 (NAME), string="__str__", start=(3, 2), end=(3, 9), line="a.__str__
")
    TokenInfo(type=4 (NEWLINE), string="
", start=(3, 9), end=(3, 10), line="a.__str__
")
    TokenInfo(type=1 (NAME), string="def", start=(4, 0), end=(4, 3), line="def b():
")
    TokenInfo(type=1 (NAME), string="b", start=(4, 4), end=(4, 5), line="def b():
")
    TokenInfo(type=53 (OP), string="(", start=(4, 5), end=(4, 6), line="def b():
")
    TokenInfo(type=53 (OP), string=")", start=(4, 6), end=(4, 7), line="def b():
")
    TokenInfo(type=53 (OP), string=":", start=(4, 7), end=(4, 8), line="def b():
")
    TokenInfo(type=4 (NEWLINE), string="
", start=(4, 8), end=(4, 9), line="def b():
")
    TokenInfo(type=5 (INDENT), string="    ", start=(5, 0), end=(5, 4), line="    b.__get__
")
    TokenInfo(type=1 (NAME), string="b", start=(5, 4), end=(5, 5), line="    b.__get__
")
    TokenInfo(type=53 (OP), string=".", start=(5, 5), end=(5, 6), line="    b.__get__
")
    TokenInfo(type=1 (NAME), string="__get__", start=(5, 6), end=(5, 13), line="    b.__get__
")
    TokenInfo(type=4 (NEWLINE), string="
", start=(5, 13), end=(5, 14), line="    b.__get__
")
    TokenInfo(type=6 (DEDENT), string="", start=(6, 0), end=(6, 0), line="")
    TokenInfo(type=0 (ENDMARKER), string="", start=(6, 0), end=(6, 0), line="")

从上面的输出我们可以知道当 type 是 OP 并且 string 等于 "." 时,下一条记录就是
点之后的属性名称。所以我们的检查代码可以这样写:

    import io
    import tokenize
    def check_unsafe_attributes(string):
        g = tokenize.tokenize(io.BytesIO(string.encode("utf-8")).readline)
        pre_op = ""
        for toktype, tokval, _, _, _ in g:
            if toktype == tokenize.NAME and pre_op == "." and tokval.startswith("_"):
                attr = tokval
                msg = "access to attribute "{0}" is unsafe.".format(attr)
                raise AttributeError(msg)
            elif toktype == tokenize.OP:
                pre_op = tokval

转载请注明本文地址:https://www.ucloud.cn/yun/37950.html

标签:__,函数,exec,TokenInfo,安全,line,type,start,string
From: https://www.cnblogs.com/tastepy/p/16613831.html

相关文章

  • Win7远程桌面发生身份验证错误要求的函数不受支持 (2019-06-15 11:48:50)
    今天登陆服务器突然登不上了,给我报了一个错误“发生验证错误要求的函数不受支持”,用同事的win7电脑和win10电脑都可以,就是我的不行,气死我了,然后我百度百度啊,用了好几种“......
  • [联合省选2021 A卷] 图函数
    经典套路还是不熟练啊。首先有一个显然的性质就是在计算\(f(u,G)\)时我们可以当成\(v\)以前的点都删了。假设有一个\(v\)之前的点没被删,如果\(v\)可以通过这个点......
  • STL next_permutation与prev_permutation函数
    刷剑指offer遇到元素排列问题No27字符串的排列函数使用题目描述:输入一个长度为n字符串,打印出该字符串中字符的所有排列,你可以以任意顺序返回这个字符串数组。例如......
  • js实现 find 函数
    //arr:要查找的数组,predict:要查找的key字符串或[key,value]数组,或对象{key,value},fromIndex:要从数组中第一个元素开始查,默认为0functionfind(arr,predict,......
  • Clickhouse windowFunnel函数使用
    --官方文档https://clickhouse.com/docs/zh/sql-reference/aggregate-functions/parametric-functions/#function-sequencecount对于事件进行连续跟踪分析能力,适用漏斗......
  • js实现 chunk 函数分组数组
    //自己实现functionchunk(list,size){letlen=list.length;if(size<1||!len){return[];}if(size>len){return[......
  • 功能安全和预期功能安全——iso26262和iso21448
    自动驾驶汽车功能安全的国际标准是iso26262,而自动驾驶预期功能安全的国际标准是iso21448。这两者之间的关系如何呢?参考资料:1、揭秘ISO21448,它是自动驾驶行业的新风向标......
  • Go并发编程与并发安全
    一、前言1、goroutine的并发:一个go程序进程中,在同一时间跑多个goroutine,那么这些goroutine是并发的。虽然现在计算机CPU一般有多个计算核心,但是核心数也是较少的,而一个计......
  • "visual studio 2012 安装程序引擎 拒绝访问" 错误 居然退出360安全卫士就可以了。
    需要卸载vs20122015这些版本。卸载vs2012失败如下图,想着要不先升级vs2012的升级包,也是提示这样的错误。百度了不少,其实也是如下篇博客那样写拒绝。(24条消息)"visu......
  • ThreadPoolTaskExecutor线程池创建
    packagecom.xx.xx.config;importjava.util.concurrent.ThreadPoolExecutor;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework......