首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python2.7 SimpleXMLRPCServer示例代码易受攻击吗?

Python2.7 SimpleXMLRPCServer示例代码易受攻击吗?
EN

Stack Overflow用户
提问于 2019-05-20 01:46:29
回答 1查看 380关注 0票数 1

Python2.7文档 for SimpleXMLRPCServer中,以下代码设置服务器:

代码语言:javascript
复制
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
                            requestHandler=RequestHandler)
server.register_introspection_functions()

# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)

# Register a function under a different name
def adder_function(x,y):
    return x + y
server.register_function(adder_function, 'add')

# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'div').
class MyFuncs:
    def div(self, x, y):
        return x // y

server.register_instance(MyFuncs())

# Run the server's main loop
server.serve_forever()

我读过Python的XMLRPC服务器可以是易受某些XML攻击,特别是“10亿笑”、“二次爆破”和“解压缩炸弹”。文档中的示例代码是否易受这些攻击的影响,是否需要进一步的方法来保护其免受这些漏洞的攻击?在这个示例代码之后,我正在对一些客户机-服务器代码进行建模,并想知道SimpleXMLRPCServer有多脆弱。如果这个示例代码易受攻击,那么我想我也需要在我的应用程序中做些什么。

编辑:

这就是我为解决XML漏洞而实现的。这是正确的吗?保护示例代码所需要的就足够了吗?

代码语言:javascript
复制
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

from defusedxml.xmlrpc import monkey_patch  # Protects against XML vulnerabilities.  See https://docs.python.org/2/library/xml.html
monkey_patch()

# Rest of code is the same as above
EN

回答 1

Stack Overflow用户

发布于 2019-05-20 04:02:50

库容易受到XML攻击的攻击,并且(攻击的性质就是它们的本质)示例代码不是攻击的证据,因为这不是漏洞所在:它们实际上存在于库对XML规范的正确实现中。对于客户端代码来说,保护自己不受API调用的正确执行并不容易。

如果您关心这些问题,可以尝试使用库defusedxml。根据xmlrpc.server的文档(这是Python3版本的SimpleXMLRPCServer),库defusedxml和它的朋友defusedxpat将来可能会被包含在Python3标准库中,而不是现在,仅仅因为它们会破坏向后兼容性。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56213470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档