Billion Laughs DoS 攻击似乎可以通过简单地阻止扩展 XML 文件中的实体来预防。有没有办法在 Python 的 xlrd 库中执行此操作(即某种标志)?如果没有,是否有推荐的方法来避免攻击?
最佳答案
不单独使用 xlrd
此时 xlrd 中没有选项可以防止任何类型的 XML 炸弹。在 the source code ,xlsx数据被传递给python内置的xml.etree
进行解析,没有任何验证:
import xml.etree.ElementTree as ET
def process_stream(self, stream, heading=None):
if self.verbosity >= 2 and heading is not None:
fprintf(self.logfile, "\n=== %s ===\n", heading)
self.tree = ET.parse(stream)
但是,可以使用 defusedxml 修补 ElementTree
如评论中所述,defusedxml是一个直接针对不同类型的 XML 炸弹的安全问题的包。来自文档:
Instead of:
from xml.etree.ElementTree import parse et = parse(xmlfile)
alter code to:
from defusedxml.ElementTree import parse et = parse(xmlfile)
它还提供了给标准库打补丁的功能。由于 xlrd 正在使用它,因此您可以结合使用 xlrd 和 defusedxml 来读取 Excel 文件,同时保护自己免受 XML 炸弹的攻击。</p>
Additionally the package has an untested function to monkey patch all stdlib modules with defusedxml.defuse_stdlib().
关于python - Python的xlrd如何防止 "billion laughs"DoS攻击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35471335/