<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/_stylesheets/atom_stylesheet.xsl"?>
<entry
        xmlns="http://www.w3.org/2005/Atom"
        xmlns:pktz="https://pktz.fr/schema/"
>
    <title>Command injection in matrix-appservice-irc</title>
    <id>https://pktz.fr/matrix/security/2022-appservice-irc-command-injection/</id>
    <published>2022-08-23</published>
    <updated>2022-08-24</updated>
    <author>
        <name>Val Lorentz</name>
        <uri>https://valentin-lorentz.fr/</uri>
    </author>
    <pktz:keywords>
        Matrix, matrix-appservice-irc, NVT#1532845, GHSA-37hr-348p-rmf4, GHSA-52rh-5rpj-c3w6, CVE-2022-29166
    </pktz:keywords>
    <content type="xhtml" xml:lang="en">
        <div xmlns="http://www.w3.org/1999/xhtml">
            <pktz:toc />

            <section>
            <h2 id="description">Description</h2>

            <section>
            <h3 id="background">Background</h3>

<p>
The IRC protocol is based on lines, delimited by <code>&lt;CRLF&gt;</code> (aka <code>\r\n</code>).
For example, sending two messages to users A and B looks like this:

<pre>
<![CDATA[
  PRIVMSG A :first message<CRLF>
  PRIVMSG B :second message<CRLF>
]]>
</pre>
</p>


<p>
When sending messages from Matrix IRC, matrix-appservice-irc correctly
sanitizes <code>\n</code> away, by re-adding the right prefixes to each message.
For example, the Matrix message

<pre>
  {
    "msgtype": "m.text",
    "first line\nsecond line"
  }
</pre>

to #chan is turned into:

<pre>
<![CDATA[
  PRIVMSG #chan :first line<CRLF>
  PRIVMSG #chan :second line<CRLF>
]]>
</pre>
</p>


<p>
However, some IRC servers also support <code>&lt;CR&gt;</code> alone as delimiter (see
<a href="https://modern.ircdocs.horse/#compatibility-with-incorrect-software">
the Modern-IRC specification</a>).
For example, this includes Solanum, the IRC server used by <a href="https://libera.chat/">Libera.chat</a>.
</p>
            </section>

            <section>
            <h3 id="missing-sanitization">First issue: missing sanitization</h3>

<p>
matrix-appservice-irc did not sanitize <code>&lt;CR&gt;</code>, which means the Matrix message

<pre>
<![CDATA[
  {
    "msgtype": "m.text",
    "first line\u000dsecond line"
  }
]]>
</pre>

is turned into:

<pre>
<![CDATA[
  PRIVMSG #chan :first line<CR>
  PRIVMSG #chan :second line<CRLF>
]]>
</pre>
</p>

<p>
This alone is only a minor issue, as someone sending the payload is the
"owner" of the IRC puppet.
</p>
            </section>

            <section>
            <h3 id="reply-fallback">Second issue: repeated reply fallback</h3>

<p>
However, under certain circumstances, matrix-appservice-irc repeats the
original message (ie. the reply fallback) when sending a reply. This
means that if an attacker sends the above Matrix and tricks a victim
into replying to it with "answer", the Matrix event of the answer is
something like this:

<pre>
<![CDATA[
  {
    "body": "> <@attacker:matrix.org> first line\rsecond line\nanswer",
    "format": "org.matrix.custom.html",
    "formatted_body": "<mx-reply><blockquote><a href=\"redacted\">In reply to</a> <a href=\"https://matrix.to/#/@attacker:matrix.org\">@attacker:matrix.org</a><br>first line\rsecond line</blockquote></mx-reply>answer",
    "m.relates_to": {
      "m.in_reply_to": {
        "event_id": "redacted"
      }
    },
    "msgtype": "m.text",
    ...
  }
]]>
</pre>

which matrix-appservice-irc then translates to IRC commands:

<pre>
<![CDATA[
  PRIVMSG #chan :first line<CR>
  second line<CRLF>
  PRIVMSG #chan :answer<CRLF>
]]>
</pre>
</p>
            </section>

            <section>
            <h3 id="wrapup">Wrapping-up</h3>

<p>
This second line is under the attacker's control, so they can run any
command as the victim's puppet. For example, it could be <code>QUIT :bye</code> to
make them disconnect, or <code>PRIVMSG NickServ :SET PASSWORD hunter2</code> to
change the password of the victim's account on the IRC network.
</p>

<p>
The obvious limitations to this attack is that victims would see the
message they are replying to; but this might be hidden by attackers by
adding colors, surrounding text, or simply the victim being unaware of
the issue.
</p>
            </section>

            <section>
            <h3 id="conclusion">Conclusion</h3>

<p>
On <time>2022-05-04</time>, Matrix.org published a fix to the first issue (missing sanitization), fixing this vulnerability.
The second issue (attacker-controlled text being repeated when replying) is <a href="https://github.com/matrix-org/matrix-appservice-irc/issues/1521">being tracked independently as a "Tolerable" issue</a> and not fixed at this time; though <a href="https://github.com/matrix-org/matrix-spec-proposals/pull/3676">planned changes to the Matrix specification</a> will make it a non-issue eventually.
</p>
            </section>
            </section>

            <section>
            <h2 id="links">Links</h2>
            <ul>
                <li><a href="https://github.com/matrix-org/node-irc/security/advisories/GHSA-52rh-5rpj-c3w6">matrix-org/node-irc advisory</a></li>
                <li><a href="https://github.com/matrix-org/matrix-appservice-irc/security/advisories/GHSA-37hr-348p-rmf4">matrix-appservice-irc advisory</a></li>
                <li><a href="https://matrix.org/blog/2022/05/04/0-34-0-security-release-for-matrix-appservice-irc-high-severity/">Matrix.org's announcement</a></li>
            </ul>
            </section>

            <section>
            <h2 id="timeline">Timeline</h2>

<dl>
    <dt><time>2022-04-22</time></dt>
    <dd>Reported to security@matrix.org (the content above as-is, minus the conclusion and some reformatting).</dd>
    <dt><time>2022-04-22</time></dt>
    <dd>Got an automatic reply (assigned <code>NVT#1532845</code>).</dd>
    <dt><time>2022-04-26</time></dt>
    <dd>
        Given the lack of human reply within 24 hours (unexpected given
        <a href="https://web.archive.org/web/20220501080802/https://matrix.org/security-disclosure-policy/">Matrix.org's security policy</a>),
        I pinged a friend who works at Element
    </dd>
    <dt><time>2022-04-29</time></dt>
    <dd>Matrix.org informed me "This is currently being worked on and there will be a security release happening soon to fix the issue (exact date to be determined)".</dd>
    <dt><time>2022-05-04</time></dt>
    <dd>
        Fix for the first issue (missing sanitization) was published and <a href="https://matrix.org/blog/2022/05/04/0-34-0-security-release-for-matrix-appservice-irc-high-severity/">announced</a>,
        bridges hosted by Matrix.org were updated, Libera.chat and OFTC banned old versions of the bridge to prevent abuse.
    </dd>
    <dt><time>2022-08-23</time></dt>
    <dd>Copy-edited from the initial email sent to Matrix.org, and published.</dd>
</dl>
            </section>
        </div>
    </content>
</entry>
