Adventures in Validating IPv4 Addresses, (Thu, Oct 26th)

Category :

SANS Full Feed

Posted On :

It should be pretty easy to validate an IP address. IPv4 addresses are 32-bit unsigned integers, and IPv6 addresses are 128-bit unsigned integers. Things get “interesting” when developers attempt to validate IP addresses as a string. There have been a few interesting vulnerabilities around this issue (CVE-2021-28918, CVE-2021-29921, CVE-2021-29418).

So, let’s take a look at how IPv4 addresses may be represented:

Dotted Decimal. This is by far the most common representation:
Dotted Octal. A leading zero indicates that the byte value is octal. Try “ping” and you may see it reaching out to
Hexadecimal: 0xa.0xa.0xa.0xa becomes “”
Decimal integer: just use a function like inet_aton to convert the IP into a decimal number. There are some big/little ending issues that Didier and Jessy recently talked about. “” becomes 134744072. 
Hexadecimal integers 0x08080808 work as well. 
Hexadecimal: 0xa.0xa.0xa.0xa becomes “”
Mixed Octal/Decimal/hex. For example, 010.8.0×8.8 is also ‘’

This all becomes important if you are attempting to validate if the IP address is syntactically valid and if you are also interested in restricting the IP address to a specific subnet. And the most common mistake is to treat IPv4 addresses as a string.

In my opinion, there is only one “right” way to validate IPv4 addresses:

Convert the address string to an unsigned long integer using the socket libraries inet_aton() function. For Python:
Now, use bitmasking to check if the address is in a specific subnet (or not).
Finally, convert the address back from an unsigned long integer to a string using inet_ntoa (if needed)

Anything else is likely going to get you into trouble. The socket library is handy because it is usually used to establish connections. By using inet_aton, your chances increase significantly so that your input validation interprets the IP address the same way it will be when a connection is established using this IP address.

Any other suggestions?

Johannes B. Ullrich, Ph.D. , Dean of Research,

(c) SANS Internet Storm Center. Creative Commons Attribution-Noncommercial 3.0 United States License.