关联:Python安全总览沙箱逃逸基础

python 内省机制(还是直接看下面文章吧)

https://dummykitty.github.io/posts/pyjail-theory-01-%E5%86%85%E7%9C%81%E6%9C%BA%E5%88%B6/ Python 的内省(Introspection)是一种动态获取对象信息的能力。通过内省,我们可以查看对象的类型,查看它的属性和方法,以及它继承的类等等。这种灵活性使得 Python 成为一种非常强大的动态语言。

以下是 Python 内省的一些主要工具和技术: 1. dir() 这个内置函数返回一个对象的所有属性和方法的列表,包括它从其父类继承的所有属性和方法。

例如:

   >>> dir("Hello World")
   ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
  1. type() 这个内置函数可以返回一个对象的类型。
   >>> type(123)
   <class 'int'>
  1. getattr(),setattr(), hasattr(), delattr() 这些内置函数用于获取设置检查删除对象的属性。 例如:
 class MyClass:
     def __init__(self):
       self.my_attribute = 123
 
   my_instance = MyClass()
 
   >>> getattr(my_instance, 'my_attribute')
   123
 
   >>> setattr(my_instance, 'my_attribute', 456)
   >>> print(my_instance.my_attribute)
   456
 
   >>> hasattr(my_instance, 'my_attribute')
   True
 
   >>> delattr(my_instance, 'my_attribute')
   >>> hasattr(my_instance, 'my_attribute')
   False
  1. help() 这个函数提供了关于一个对象(如类、方法、模块等)的详细信息。
    >>> help(str)
    Help on list object:
 
    class list(object)
    |  list(iterable=(), /)
    |  
    |  Built-in mutable sequence.
    |  
    |  If no argument is given, the constructor creates a new empty list.
    |  The argument must be an iterable if specified.
    |  
    |  Methods defined here:
    |  
    |  __add__(self, value, /)
    |      Return self+value.
    |  
    |  __contains__(self, key, /)
    |      Return key in self.
    |  
    |  __delitem__(self, key, /)
    |      Delete self[key].
    |  
    |  __eq__(self, value, /)
    |      Return self==value.
    |  
    |  __ge__(self, value, /)
    |      Return self>=value.
    |  
    |  __getattribute__(self, name, /)
    |      Return getattr(self, name).
    |  
    |  __getitem__(...)
    |      x.__getitem__(y) <==> x[y]
    |  
    |  __gt__(self, value, /)
    |      Return self>value.
    |  
    |  __iadd__(self, value, /)
    |      Implement self+=value.
    |  
    |  __imul__(self, value, /)
    |      Implement self*=value.
    |  
    |  __init__(self, /, *args, **kwargs)
    |      Initialize self.  See help(type(self)) for accurate signature.
    |  
    |  __iter__(self, /)
    |      Implement iter(self).
    |  
    |  __le__(self, value, /)
    |      Return self<=value.

python 魔术方法/属性(也可以看上面的文章)

__builtins__:获取内建模块 __import__:接收字符串作为参数,导入该字符串名称的模块。 因为是字符串参数,所以可以使用拼接绕过

__import__('o'+'s').system('ca'+'lc')。

__class__ :用于获取对象的类 __bases__:列出基类 __mro__:用于展示类的继承关系 __globals__:是一个特殊属性,能够以 dict 的形式返回函数(注意是函数)所在模块命名空间的所有变量,其中包含了很多已经引入的 modules。