Python NaN Injection
In Python, a NaN can cause a slew of errors leading to numerous failure conditions. Test for NaNs using a white-box scenario to avoid fallout.
Python is often referred to as “type-safe” by people who aren’t aware of the fact that it is actually “duck typed.” Meaning, that in Python, if the variable ‘walks like a duck and talks like a duck, it must be a duck.’ However, a variable can be injected with a string called NaN (which stands for Not a Number) and when it encounters another variable, it can consume it. For instance, a numeric integer like the number 1 can be added to a float that contains a NaN and the output will be a NaN. NaN then becomes extremely greedy.
Further complicating matters, there are many ways to get to NaN, including a simple division of a positive and negative infinity. And, worse yet, infinity can be reached through the addition of non-infinite real numbers such as 1e308 + 1e308. In this way, there are several ways to create infinity, negative infinity, and ultimately, NaN in Python. Further, there is a negative integer overflow on the Ubuntu/FreeBSD implication of NumPy (v1.21) in the presence of NaN as it is cast to an int.
Python (as of 3.6.9) has several issues that cause subsequent problems such as failing to be IEEE754 compliant, as well as broken sorting functions that can lead to many failure conditions, authentication backdoors, denial of service, privilege escalation, runaway processes, and general logic flaws. Therefore, the actual damage potential is extremely difficult to know in advance.
Tenable and NTT Application Security recommend not testing for this issue in a black-box production environment due to how dangerous the testing can be. It should be tested only in pre-production and ideally in a white-box scenario so that all elements can be exercised. Careful use of isnan() on all elements that are consumed should be the standard fix outside of a broad fix.
Testing for the presence of Python, on the other hand, can be safely accomplished through various known Python application headers, predictable URL locations of known Python applications and Python-specific error messages. That said, NaN Injection can surface within applications that leverage back-end cron jobs, or APIs that may be hidden from an external assessment. There is no known WAF or IDS rule to prevent NaN Injection as of the time of writing.
For more details, please see the git repository.
Visit the Tenable.asm product page to learn more about attack surface management.
Related Articles
- Asset Management
- Attack Surface Management