Code Review: bWAPP Unrestricted File Upload

Source Code

As an example, we are going to do a code review on bWAPP "Unrestricted File Upload" challenges:

bWAPP Source Code

Security Level 0

In this level, there is no filter at all:

We can simply upload a PHP webshell with names like webshell.php and this webshell can be found at images/.

Security Level 1

In this level, the filter used is called file_upload_check_1:

This function is defined in functions_external.php:

This implementation uses blacklist to filter out unwanted file extensions. Specifically, the following file extensions are blocked:

  • asp

  • aspx

  • dll

  • exe

  • jsp

  • php

Of course this implementation is not sufficient at all. For example, we can try things like php3/php4/php5 as file extension and bypass the check.

Security Level 2

In this level, the filter used is called file_upload_check_2:

This function is defined in functions_external.php:

This implementation uses whitelist to only allow wanted file extensions. Specifically, the following file extensions are accepted:

  • jpeg

  • jpg

  • png

  • gif

This implementation looks sufficient at first, but if you dig deeper into the function calls, you will find this line of code:

Take a look at the PHP manual:

in_array - PHP Manual

The nuance is that the in_array() function takes 3 arguments but this code only used 2. The third argument bool $strict is set to false by default, which indicates loose comparison. PHP loose comparison is a weird "feature" and it is the source of many dumb vulnerabilities. For example:

You may think the output will be bool(false), but no, the output is bool(true)! This is because in loose comparison, we have 0 == "apple". In fact, every string started with letter is evalated to 0 in loose comparison. I am speechless.

Last updated

Was this helpful?