FROM FINDING TO FIX
Summary
OpenCanary reads its configuration from the current directory before it looks at the system-wide location, and the portscan module inserts one of the configuration values into shell commands that it runs with sudo. A local user who can write a configuration file to a directory from which root later starts the daemon can run any command as root.
Affected Components
The configuration is loaded in Config.__init__ in opencanary/config.py. The first file found wins, and the current directory comes first:
files = [
configfile, # current directory
"%s/.%s" % (expanduser("~"), configfile), # home directory
"/etc/opencanaryd/%s" % configfile, # system-wide location
]
In opencanary/modules/portscan.py, CanaryPortscan.__init__ reads the path to iptables from the configuration, and set_iptables_rules places it at the start of twelve shell commands executed with os.system:
self.iptables_path = self.config.getVal("portscan.iptables_path", False)
...
os.system(
'sudo {0} -t mangle -A PREROUTING -p tcp -i lo -j LOG ...'.format(
iptables_path, self.lorate
)
)
The value is neither validated nor quoted. The rate values in the same commands are not exploitable, because they are converted to integers first.
Root Cause and Further Instances
The underlying flaw is that values from a file an unprivileged user may control are passed to a shell running as root. We searched the code base for other places where OpenCanary runs external programs and found two more uses of the same pattern. They are not exploitable on their own, but should be fixed together with the finding:
| Location | What happens | Exploitable |
|---|---|---|
portscan.py, set_iptables_rules |
portscan.iptables_path from the configuration runs in a root shell |
Yes |
config.py, Config.__init__ |
A broken configuration file is copied with cp through a shell, using its file name |
No, the name is fixed |
portscan.py, detectNFTables |
iptables is looked up through the PATH of the calling user |
Only with a manipulated PATH |
Steps to Reproduce
-
As an unprivileged user, create
opencanary.confin a directory you can write to, with the portscan module enabled and a command appended to theiptablespath:{ "portscan.enabled": true, "portscan.iptables_path": "/usr/sbin/iptables; id > /tmp/opencanary-poc; true" } -
Wait for an administrator to start the daemon from that directory with
opencanaryd --start. The start script launches the daemon throughsudo -E, so it runs as root but still reads the configuration from the current directory. -
/tmp/opencanary-pocnow exists, is owned by root, and showsuid=0(root).
Rating and Priority
We rate the finding 6.7 (Medium). NVD rates it 7.8, assuming no account is needed. In practice, the attacker needs a local account (PR:L), and root has to start the daemon from a directory the attacker can write to (AC:H, UI:R).
What this means for you depends on how you run OpenCanary. On a dedicated sensor that only administrators log into, and where the configuration lives in /etc/opencanaryd, the finding can wait for the next regular update. Where several people share the host, or the daemon is started from a project or home directory, fix it first: a successful attack gives full control over a machine that sits inside your network by design.
Remediation
Fix the root cause rather than the single instance:
-
Trust only the system-wide configuration. Load
/etc/opencanaryd/opencanary.conffirst, and warn when a file from the current directory is used. -
Do not pass configuration values to a shell. Remove the
portscan.iptables_pathoption, look upiptablesonly in the standard system directories, and run it with an argument list instead of a shell command:iptables_path = shutil.which("iptables", path="/usr/sbin:/usr/bin:/sbin:/bin") subprocess.run([iptables_path, "-t", "mangle", "-A", "PREROUTING", ...], check=True) -
Apply the same change to the other two instances, so the pattern does not come back.
-
Drop root privileges once the daemon no longer needs them, so that any future flaw of this kind runs with less power.
What Happened
We worked with Thinkst’s developers on the fix. Our changes, by David Baker Effendi and Andrei Dreyer, load the system-wide configuration first and run external programs only from trusted system directories, and drop root privileges by default. They were released in OpenCanary 0.9.4. In 0.9.5, Thinkst removed the iptables_path option and reworked privilege dropping. The advisory credits our team for finding and fixing the issue.
This is what our remediation support looks like: we work on the fixes with your developers, retest each one, and can turn the pattern into a query that runs in your pipeline. See how we work.
Talk to Us About Your Project
Tell us what you are building and what worries you about it. We reply within one business day.
Contact Us