{"id":98,"date":"2022-10-20T01:42:03","date_gmt":"2022-10-20T01:42:03","guid":{"rendered":"https:\/\/ryancv-demo.bslthemes.com\/?p=98"},"modified":"2022-10-20T08:03:02","modified_gmt":"2022-10-20T08:03:02","slug":"bypassing-default-laravel-upload-validation","status":"publish","type":"post","link":"https:\/\/shwani.dev\/?p=98","title":{"rendered":"Bypassing Laravel Default File Upload Validation"},"content":{"rendered":"\n<p>Hello Fellow Hackers, I am Ahmed Kameran , Security Researcher from Kurdistan, Iraq.<\/p>\n\n\n\n<p>Hope you are doing well. I want to talk about one of my research&#8217;s in Laravel Core framework which allows to bypass Laravel default upload validation using different PHP extensions.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Laravel Default behavior for file upload restriction<\/strong><\/p>\n\n\n\n<p>Laravel by default will block set of PHP extensions from uploading into the laravel application using this function.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"275\" src=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image.png\" alt=\"\" class=\"wp-image-390\" srcset=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image.png 786w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-300x105.png 300w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-768x269.png 768w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-184x64.png 184w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure>\n\n\n\n<p>If you try to upload a file with these blocked extensions the validation will return an exception as these extensions were blocked by Laravel, let&#8217;s try to upload a file with (.php) extension:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"476\" src=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/blog1-1024x476.png\" alt=\"\" class=\"wp-image-391\" srcset=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/blog1-1024x476.png 1024w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/blog1-300x139.png 300w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/blog1-768x357.png 768w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/blog1-184x86.png 184w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/blog1.png 1067w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>As we can see from above screenshot uploading a file with .php extension was blocked by Laravel file validation because .php was in this array list of blocked extensions:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-1.png\" alt=\"\" class=\"wp-image-392\" width=\"610\" height=\"124\" srcset=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-1.png 373w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-1-300x61.png 300w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-1-184x37.png 184w\" sizes=\"(max-width: 610px) 100vw, 610px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Bypassing Laravel File Upload Validation<\/strong><\/p>\n\n\n\n<p>The Laravel default function to prevent uploading .php extensions was not blocking .php7 or .php8 file extensions as some web servers will treat them as a normal php executable file.<\/p>\n\n\n\n<p>Now if we try to upload a file with .php7 extension the uploading will be successful as we can see in below screenshot:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"387\" src=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-39-34-1024x387.png\" alt=\"\" class=\"wp-image-394\" srcset=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-39-34-1024x387.png 1024w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-39-34-300x113.png 300w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-39-34-768x290.png 768w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-39-34-184x70.png 184w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-39-34.png 1039w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>As some web servers will treat .php7 or .php8 file extension as a valid PHP executable like the example below which we uploaded .php7 and it got executed on the Apache web server as a valid PHP which can lead to <strong>Remote command execution<\/strong> on any server that accepts php7 or php8 files:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"450\" src=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-46-09-1024x450.png\" alt=\"\" class=\"wp-image-396\" srcset=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-46-09-1024x450.png 1024w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-46-09-300x132.png 300w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-46-09-768x338.png 768w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-46-09-184x81.png 184w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/Screenshot-from-2022-10-19-23-46-09.png 1037w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Fixing the issue<\/strong><\/p>\n\n\n\n<p>I just opened a pull request to Laravel core that i added php7 and php8 into the array of blocked list PHP extensions.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"804\" height=\"270\" src=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-2.png\" alt=\"\" class=\"wp-image-399\" srcset=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-2.png 804w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-2-300x101.png 300w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-2-768x258.png 768w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-2-184x62.png 184w\" sizes=\"(max-width: 804px) 100vw, 804px\" \/><\/figure>\n\n\n\n<p>You can find my pull request <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/laravel\/framework\/pull\/44512\" target=\"_blank\">HERE<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Vulnerable Laravel Applications<\/strong><\/p>\n\n\n\n<p>This issue was fixed in the Laravel&#8217;s latest version (<a href=\"https:\/\/github.com\/laravel\/framework\/releases\/tag\/v9.36.1\">v9.36.1<\/a>) so all versions before that were vulnerable to this type of attack.<\/p>\n\n\n\n<p>Vulnerable versions<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p>9.x<\/p>\n\n\n\n<p>8.x<\/p>\n\n\n\n<p>7.x<\/p>\n\n\n\n<p>6.x<\/p>\n\n\n\n<p>5.x<\/p>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Vulnerable Web servers<\/strong><\/p>\n\n\n\n<p>During my research i found out, A lot of cloud and host providers will configure there web servers to execute php7 and php8 file extensions as a normal php file.<\/p>\n\n\n\n<p>Here is an example of vulnerable configured Apache web server.<br>This is contents of \/etc\/apache2\/conf\/mime.types file<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"796\" height=\"366\" src=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-3.png\" alt=\"\" class=\"wp-image-403\" srcset=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-3.png 796w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-3-300x138.png 300w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-3-768x353.png 768w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-3-184x85.png 184w\" sizes=\"(max-width: 796px) 100vw, 796px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Fixing Web servers<\/strong><\/p>\n\n\n\n<p>Make sure your web server configuration didn&#8217;t treat other php extension types as a valid php file like example above.<\/p>\n\n\n\n<p>Also you can add .htaccess file to uploads directory to prevent execution of PHP in uploads directory which is controlled by end users.<br>Add this to .htaccess file in uploads directory<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"789\" height=\"52\" src=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-4.png\" alt=\"\" class=\"wp-image-410\" srcset=\"https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-4.png 789w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-4-300x20.png 300w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-4-768x51.png 768w, https:\/\/shwani.dev\/wp-content\/uploads\/2022\/10\/image-4-184x12.png 184w\" sizes=\"(max-width: 789px) 100vw, 789px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Mitigation<\/strong><\/p>\n\n\n\n<p>Upgrade Laravel to latest version (<a href=\"https:\/\/github.com\/laravel\/framework\/releases\/tag\/v9.36.1\">v9.36.1<\/a>) or make sure you configure your web server correctly so its does not treat other .php extensions as a normal PHP executable.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Thanks<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Fellow Hackers, I am Ahmed Kameran , Security Researcher from Kurdistan, Iraq. Hope you are doing well. I want&#8230;<\/p>\n","protected":false},"author":1,"featured_media":418,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[4,20,19,18,22,21],"acf":[],"_links":{"self":[{"href":"https:\/\/shwani.dev\/index.php?rest_route=\/wp\/v2\/posts\/98"}],"collection":[{"href":"https:\/\/shwani.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shwani.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shwani.dev\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shwani.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=98"}],"version-history":[{"count":24,"href":"https:\/\/shwani.dev\/index.php?rest_route=\/wp\/v2\/posts\/98\/revisions"}],"predecessor-version":[{"id":421,"href":"https:\/\/shwani.dev\/index.php?rest_route=\/wp\/v2\/posts\/98\/revisions\/421"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shwani.dev\/index.php?rest_route=\/wp\/v2\/media\/418"}],"wp:attachment":[{"href":"https:\/\/shwani.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shwani.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shwani.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}