Python is often called “type safe” by people who aren’t aware of the fact that it is actually “duck typed” in the sense that if the variable ‘walks like a duck and talks like a duck, it must be a duck’. 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 becomes extremely greedy.
Further, there are many ways to get to NaN, including simple division of a positive and negative infinity. And worse yet infinity can be reached through addition of non-infinity 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.
Watch: Bit Discovery and NTT Application Security Presentation
Bit Discovery 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.
Bit Discovery and NTT Application Security recommend not testing for this issue in a black-box production environment due to how dangerous the testing can be.
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.