Preventing file inclusion:
Whitelist user input.
- Match each input to the file to be loaded + default value for all other inputs.
- Whitelist may contain all existing paths used in frontend and utilize this list to match user input
- Whitelist can take many shapes like a database that matches IDs to file + case-match script that matches names to files, or even static json map with names and files that can be matched.
Preventing directory traversal:
IF attackers control directory they can escape the webapp and attack something they are more familiar with or use a universal attack chain
Directory traversal allows attackers to do some example attacks:
- read etcpasswd
- find ssh keys
- find other serices on the box and read the
tomcat-users.xmlfile - discover valid php session cookies + perform session hijacking
- read current webapp config and source code
Best way : use program lang or framework builtin tool to pull only filename. PHP for example has basename()
- read path
- only return filename portion
- if only filename is given it will return just filename.
- if just path given it will treat whatever is after the final / as a filename
- downside: if application needs to enter directories it cannot
if implementing this yourself may run into a weird edgecase:
run the command cat .?/.*/.?/etc/passwd. You'll see Bash allows for the ? and * wildcards to be used as a .. Now type php -a to enter the PHP Command Line interpreter and run echo file_get_contents('.?/.*/.?/etc/passwd');. You'll see PHP does not have the same behaviour with the wildcards, if you replace ? and * with ., the command will work as expected.
assume we let php execute bash with system() function the attacker would be able to bypass directory traversal prevention
if we use native functions to the framework we are using there's a chance other users would catch edge cases like thsi and fix it before it gets exploited in our webapp
sanitize user input to recursively remove any attempts of traversing directories:
while(substr_count($input, '../', 0)) {
$input = str_replace('../', '', $input);
};
- recursively removes
../substrings - even if resulting string contains
../it is removed - preventing some bypasses used in this module
lfi / path traversal prevention web server configurations
php, examples
- allow_url_fopen
- allow_url_include set these to off - globally disable the inclusion of remote files.
DOCKER usage is common to lock webapps to their webroot dir - preventing non-web-related files from being accessed
- if this isnt an option many proglangs have a way to prevent accessing files outside of the web dir
- php example:
open_basedir = /var/wwwinphp.iniconfig file
Ensure potentially dangerous modules are disabled like
- https://www.php.net/manual/en/wrappers.expect.php
- https://httpd.apache.org/docs/2.4/mod/mod_userdir.html
WAF is a universal way to harden applications
- ex. ModSecurity
important thing to avoid is false positives and blocking non-malicious requests
ModSec minimizes false positives by offering a permissive mode which will only report incidences that would have been blocked, allowing defenders to tune rules and make sure no legit req is blocked.
Hardening =/= unhackable. Giving you a fighting chance at detecting attacks when they occur.