Skip to content

Python: XML RPC Dotted Names #3910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions python/ql/src/experimental/Security/XmlRpcDottedNames.qhelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Enabling the <code>allow_dotted_names</code> option allows intruders to access your module’s global
variables and may allow intruders to execute arbitrary code on your machine. Only use this example only within a secure, closed network.
</p>

<references>

<li>Python Language Reference: <a href="https://docs.python.org/3/library/xmlrpc.server.html#xmlrpc.server.SimpleXMLRPCServer">
Basic XML-RPC servers</a>.
</li>

</references>
</qhelp>
24 changes: 24 additions & 0 deletions python/ql/src/experimental/Security/XmlRpcDottedNames.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @name The allow_dotted_names option may allow intruders to execute arbitrary code
* @description Enabling the allow_dotted_names option allows intruders to access your module’s global variables and may allow intruders to execute arbitrary code on your machine.
* @kind problem
* @problem.severity warning
* @id python/xml-rpc-dotted-names
* @tags reliability
* security
*/

import python

from CallNode call, ControlFlowNode allow_dotted_names, Attribute a
where
a.getLocation().getStartLine() = call.getLocation().getStartLine() and
a.getName() = "register_instance" and
not call.getLocation().getFile().inStdlib() and
(
allow_dotted_names = call.getArgByName("allow_dotted_names") or
allow_dotted_names = call.getArg(1)
) and
allow_dotted_names.getNode().toString() = "True"
select a,
"Enabling the allow_dotted_names option allows intruders to access your module’s global variables and may allow intruders to execute arbitrary code on your machine."
Comment on lines +11 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A modern way to write this would be

Suggested change
import python
from CallNode call, ControlFlowNode allow_dotted_names, Attribute a
where
a.getLocation().getStartLine() = call.getLocation().getStartLine() and
a.getName() = "register_instance" and
not call.getLocation().getFile().inStdlib() and
(
allow_dotted_names = call.getArgByName("allow_dotted_names") or
allow_dotted_names = call.getArg(1)
) and
allow_dotted_names.getNode().toString() = "True"
select a,
"Enabling the allow_dotted_names option allows intruders to access your module’s global variables and may allow intruders to execute arbitrary code on your machine."
import python
import semmle.python.ApiGraphs
from API::CallNode call, DataFlow::Node allow_dotted_names
where
call =
API::moduleImport("xmlrpc")
.getMember("server")
.getMember("SimpleXMLRPCServer")
.getReturn()
.getMember("register_instance")
.getACall() and
allow_dotted_names = call.getParameter(1, "allow_dotted_names").getAValueReachingSink() and
allow_dotted_names.asExpr() instanceof True
select allow_dotted_names,
"Enabling the allow_dotted_names option allows intruders to access your module’s global variables and may allow intruders to execute arbitrary code on your machine."

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| XmlRpcDottedNames.py:19:5:19:28 | Attribute | Enabling the allow_dotted_names option allows intruders to access your module\u2019s global variables and may allow intruders to execute arbitrary code on your machine. |
26 changes: 26 additions & 0 deletions python/ql/test/experimental/Security/XmlRpcDottedNames.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copied from official python 3 documentation, at https://docs.python.org/3/library/xmlrpc.server.html#xmlrpc.server.SimpleXMLRPCServer

import sys, datetime
from xmlrpc.server import SimpleXMLRPCServer

class ExampleService:
def getData(self):
return '42'

class currentTime:
@staticmethod
def getCurrentTime():
return datetime.datetime.now()

with SimpleXMLRPCServer(("localhost", 8000)) as server:
server.register_function(pow)
server.register_function(lambda x,y: x+y, 'add')
# WARNING: Only set the allow_dotted_names flag to true within a secure, closed network.
server.register_instance(ExampleService(), allow_dotted_names=True)
server.register_multicall_functions()
print('Serving XML-RPC on localhost port 8000')
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nKeyboard interrupt received, exiting.")
sys.exit(0)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
experimental/Security/XmlRpcDottedNames.ql