CSV (Formula) Injection

User-supplied values are written into CSV or spreadsheet exports without neutralisation, so a spreadsheet program can run them as formulas when the export is opened.

SeverityLow
CVSS 3.13 CVSS:3.1/AV:N/AC:H/PR:L/UI:R/S:C/C:L/I:N/A:N Open in the calculator
CVSS 4.02 CVSS:4.0/AV:N/AC:L/AT:P/PR:L/UI:A/VC:N/VI:N/VA:N/SC:L/SI:N/SA:N Open in the calculator
CWECWE-1236: Improper Neutralization of Formula Elements in a CSV File
MITRE ATT&CKT1204.002 Malicious File, T1559.002 Dynamic Data Exchange
CategoryInjection

Description

The application writes values entered by users, such as names, addresses, comments or ticket subjects, into CSV or spreadsheet exports without neutralising them. Spreadsheet programs such as Microsoft Excel and LibreOffice Calc treat a cell that begins with =, +, - or @, and in some cases a tab or carriage return character, as a formula when the file is opened. An attacker who can place text in a field that is later exported can therefore embed a formula that is evaluated on the computer of whoever opens the file, typically a member of staff or an administrator.

Common payloads use HYPERLINK formulas that send the contents of other cells to an external server controlled by the attacker when the cell is clicked, formulas that fetch external data, and legacy Dynamic Data Exchange (DDE) formulas that can start other programs on Windows, which can run commands on the victim's computer and give the attacker a foothold on a staff workstation. Formulas can also simply show misleading values. Current versions of Microsoft Excel do not start other programs through DDE unless a setting that is off by default has been turned on, and the common spreadsheet programs usually warn the user before fetching external content, so the more serious outcomes depend on older software, changed settings or the user accepting warnings. The attack needs the victim to open the file and usually to interact with it, and the risk is higher where exports are routinely opened by privileged staff.

The weakness is identified by entering harmless formulas, such as =1+1, into fields that appear in exports, exporting the data and opening it in a spreadsheet program to see whether the formula is evaluated. Exports built without quoting each field correctly can also allow a separator character in the input to start a new cell that begins with a formula.

Remediation

Neutralise formula characters in every exported cell that can contain user-supplied text:

  • When a cell begins with =, +, -, @, a tab or a carriage return, prefix it with a single quotation mark ('), so that spreadsheet programs treat it as text. Fields that are validated as numbers can be left unchanged.
  • Generate CSV with a library that encloses every field in double quotation marks and doubles any quotation marks within it, so that input cannot break out of its cell.
  • Where users need a spreadsheet rather than CSV, generate the file with a library that writes user-supplied values as text cells rather than formulas.
  • Validate input on entry where formula characters are not expected, such as in names, as defence in depth.
  • Advise staff not to enable content or accept warnings in exported files.
import csv def neutralise(value): text = str(value) if text.startswith(('=', '+', '-', '@', '\t', '\r')): return "'" + text return text writer = csv.writer(output, quoting=csv.QUOTE_ALL)

Confirm the fix by exporting records that contain test formulas and opening the file: each should be displayed as text.

References


Add This Finding to Your Library

In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.

Free trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Web Applications Findings

Every Web Applications finding