CVE-2009-2940
PyGreSQL 3.8.1 and 4.0 for Python have a security flaw with PQescapeStringConn, allowing remote attacks with multibyte encoding issues
- CVSS 7.5
- CWE-77: Improper Neutralization of Special Elements Used in a Command ('Command Injection') – Score: 6.5
- Input Validation and Sanitization
- Remote
The pygresql module 3.8.1 and 4.0 for Python does not properly support the PQescapeStringConn function, which might allow remote attackers to leverage escaping issues involving multibyte character encodings.
- CVSS base score
- 7.5
- Published
- 2009-10-22
- OWASP
- A06 Vulnerable and Outdated Components
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Input Validation and Sanitization
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- pygresql
- Fixed by upgrading
- Yes
Solution
Update pygresql to version 4.1 or higher.
Vulnerable code sample
import pgdb
def unsafe_insert(connection, user_input):
# Directly embedding user input into the SQL query, which can lead to SQL injection
query = "INSERT INTO users (username) VALUES ('" + pgdb.escape_string(user_input) + "')"
with connection.cursor() as cursor:
cursor.execute(query)
# Example usage
if __name__ == "__main__":
conn = pgdb.connect("dbname=test user=postgres password=secret")
user_input = "user_input_with_multibyte_字符" # Example multibyte input
unsafe_insert(conn, user_input)
conn.commit()
conn.close()Patched code sample
import pgdb
def safe_insert(connection, user_input):
# Use parameterized queries to avoid SQL injection and escaping issues
query = "INSERT INTO users (username) VALUES (%s)"
with connection.cursor() as cursor:
cursor.execute(query, (user_input,))
# Example usage
if __name__ == "__main__":
conn = pgdb.connect("dbname=test user=postgres password=secret")
user_input = "user_input_with_multibyte_字符" # Example multibyte input
safe_insert(conn, user_input)
conn.commit()
conn.close()Published fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- Support for PQescapeStringConn and PQescapeByteaConn. (PyGreSQL/PyGreSQL)
module/pg.py - Added support for PQescapeStringConn() and PQescapeByteaConn(). Promoted the _quote() functions to methods of the connection. Made them use the former functions instead of manually and faulitly handling backslashes and quotes. (PyGreSQL/PyGreSQL)
module/pg.py,module/pgdb.py
Cite this entry
@misc{vaitp:cve20092940,
title = {{PyGreSQL 3.8.1 and 4.0 for Python have a security flaw with PQescapeStringConn, allowing remote attacks with multibyte encoding issues}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2009},
note = {VAITP Python Vulnerability Dataset, entry CVE-2009-2940},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2009-2940/}}
}
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 ::
