CVE-2024-39705
NLTK 3.8.1 RCE via untrusted pickled code in data package downloads.
- CVSS 9.8
- CWE-502
- Input Validation and Sanitization
- Remote
NLTK through 3.8.1 allows remote code execution if untrusted packages have pickled Python code, and the integrated data package download functionality is used. This affects, for example, averaged_perceptron_tagger and punkt.
- CWE
- CWE-502
- CVSS base score
- 9.8
- Published
- 2024-06-27
- OWASP
- A01 Broken Access Control
- Orthogonal defect classification
- Checking
- Code defect classification
- Incorrect Check
- Category
- Input Validation and Sanitization
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- NLTK
- Fixed by upgrading
- Yes
Solution
Upgrade NLTK to version 3.8.2 or later.
Vulnerable code sample
import pickle
import base64
def load_user_session(session_data):
"""Load user session - VULNERABLE to pickle RCE"""
try:
# Dangerous: pickle.loads can execute arbitrary code
decoded = base64.b64decode(session_data)
session = pickle.loads(decoded)
return session
except Exception:
return None
# Malicious payload example (DO NOT USE):
# payload = pickle.dumps(os.system('rm -rf /'))
# encoded = base64.b64encode(payload)Patched code sample
import json
import hmac
import hashlib
import secrets
from typing import Dict, Any, Optional
class SecureSessionManager:
def __init__(self, secret_key: str):
self.secret_key = secret_key.encode()
def create_session_token(self, session_data: Dict[str, Any]) -> str:
"""Create secure session token with HMAC"""
# Use JSON instead of pickle - only safe data types
json_data = json.dumps(session_data, sort_keys=True)
# Create HMAC signature for integrity
signature = hmac.new(
self.secret_key,
json_data.encode(),
hashlib.sha256
).hexdigest()
# Combine data and signature
token = f"{json_data}:{signature}"
return base64.b64encode(token.encode()).decode()
def load_user_session(self, session_token: str) -> Optional[Dict[str, Any]]:
"""Securely load user session with validation"""
try:
# Decode token
decoded = base64.b64decode(session_token).decode()
# Split data and signature
if ':' not in decoded:
return None
json_data, signature = decoded.rsplit(':', 1)
# Verify HMAC signature
expected_sig = hmac.new(
self.secret_key,
json_data.encode(),
hashlib.sha256
).hexdigest()
if not hmac.compare_digest(signature, expected_sig):
return None
# Parse JSON data (safe deserialization)
session_data = json.loads(json_data)
# Additional validation
if not isinstance(session_data, dict):
return None
return session_data
except Exception:
return None
# Secure usage
session_mgr = SecureSessionManager("your-secret-key")
token = session_mgr.create_session_token({"user_id": 123, "role": "user"})
session = session_mgr.load_user_session(token)Payload
import pickle
import os
# Example payload that could execute arbitrary code
payload = pickle.dumps(os.system('echo Vulnerability Exploited!'))
# This payload could be saved and then loaded by the vulnerable NLTK function
Cite this entry
@misc{vaitp:cve202439705,
title = {{NLTK 3.8.1 RCE via untrusted pickled code in data package downloads.}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2024},
note = {VAITP Python Vulnerability Dataset, entry CVE-2024-39705},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2024-39705/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
