首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python代码审计汇总

1、任意代码执行

任意代码执行需关注的函数,可使用正则搜索:

代码语言:javascript
复制
eval\(|exec\(|execfile\(|compile\(

需关注的危险库文件及函数有:

代码语言:javascript
复制
os.system/popen
timeit.timeit
platform.popen
commands.getstatusoutput
subprocess.popen/call/check_output
__import__("os").system("ls")
importlib.import_module('os').system('ls')

其中subprocess较为常见,防御办法需保证shell=True未设置 转义变量:Python 2.x使用pipes.quote(),Python 3.3或更高版本使用shlex.quote();错误示例代码:

代码语言:javascript
复制
subprocess.Popen('ls',shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT).stdout.read()
importsubprocess
def transcode_file():
filename = raw_input('Please provide the path for the file totranscode: ')
command = 'ffmpeg -i "{source}"output_file.mpg'.format(source=filename)
subprocess.call(command, shell=True)

除此之外需还关注模板注入(SSTI),全局搜索Template类,一般使用方法为Template(user_input)。

2、反序列化

Python中用于反序列化的模块有:

代码语言:javascript
复制
marshal
PyYAML——>yaml.safe_load()
pickle/cpickle
shelve
PIL
Unzip

示例利用代码:

代码语言:javascript
复制
import cPickle
cPickle.loads("cos\nsystem\n(S'uname -a'\ntR.")

3、权限绕过

定位鉴权代码及日志记录代码等,确认if判断、cookie、验证码机制等逻辑是否存在可绕过的缺陷。

4、SQL注入

SQL注入是各语言的普遍存在的问题,可通过SQL语句或特有关键字定位:

代码语言:javascript
复制
cursor.excute
ORM.raw()|extra()

产生原因大多也是来自于直接拼接,示例代码:

代码语言:javascript
复制
defuser_contacts(request):
user = request.GET['username']
sql = "SELECT * FROM user_contacts WHEREusername = %s"
cursor = connection.cursor()
cursor.execute(sql, [user])
results = cursor.fetchone()   #or results = cursor.fetchall()
cursor.close()

安全做法为采用:

ModelInstance.objects.raw(sql,[])或connection.objects.execute(sql,[])。

5、文件操作

Python代码中文件处理需关注的函数有:

代码语言:javascript
复制
file()
open()
codecs.open()

文件处理主要有上传功能,若未限制文件大小,可能导致ddos,未限制文件后缀,可导致任意文件上传,未给文件重命名,可能导致目录穿越,文件覆盖等问题。

其他包括任意文件下载,删除,写入,覆盖等,需对用户输入的文件名及路径进行校验,如对文件名未做校验则可目录穿越导致任意zip文件下载:

代码语言:javascript
复制
defexportCheck(request,filename):
    if re.match(r“*.zip”,filename):
        fullname = filename
    else:
        fullname = "/export/test.zip"
    print fullname
return HttpResponse(fullname)

除此以外可借鉴其他语言漏洞,如zip解压漏洞(tarfile/Zipfile)、跨目录解压、临时文件用完删除等漏洞。

6、XXE

关注Python代码是否导入使用xml处理解析类:

代码语言:javascript
复制
xml.dom.*
xml.etree.ElementTree
xml.sax.*

错误示例代码如:

代码语言:javascript
复制
from lxml import etree
tree1 = etree.parse('test.xml')
print etree.tostring(tree1.getroot())

7、SSRF

关注代码是否存在发起请求的库及函数,常见的有自带库requests.get()及urllib/urllib2库,用法为urllib.request.urlopen(url)。

8、XSS/重定向

关注返回值为rensponse的代码,重定向通常形如:

代码语言:javascript
复制
return HttpResponseRedirect

下列代码若输入可控则可造成XSS:

代码语言:javascript
复制
return HttpResponse('hello %s' %(name))

安全的写法为:

代码语言:javascript
复制
return render_to_response('hello.html', {'name':name})

9、日志相关

关注logging()函数及LOGGER 等关键字,查看是否输出口令、密钥和其他敏感信息及输入是否可插入%0A%0D进行日志伪造。

10、其他

(1)安全随机数:

当用于安全加密用途时,不可采用形如random.randint(0, 100)不安全的随机数生成机制;在Linux和类Unix下用,需使用/dev/random生成安全随机数,在windows下,使用random模块中的SystemRandom类来实现。

(2)格式化字符串:

使用形如下列格式化字符串容易造成敏感信息泄露:

代码语言:javascript
复制
"My name is %s" % ('jayway', )或"Myname is {}".format('jayway')
"My name is %(name)%" % {'name':'jayway'}

(3)沙箱逃逸:

沙箱逃逸是python安全必谈的话题,可使用各种编码、内置对象、PyCodeObj等进行沙箱逃逸,可参考链接:

代码语言:javascript
复制
https://hatboy.github.io/2018/04/19/Python%E6%B2%99%E7%AE%B1%E9%80%83%E9%80%B8%E6%80%BB%E7%BB%93/
下一篇
举报
领券