Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the browser allow downloading the php file while wget doesn't? [MediaWiki installation]

During the installation of MediaWiki, I had to download a file called LocalSettings.php. When I download it by clicking on the download link (which happens to be http://localhost/mymediawiki/mw-config/index.php?localsettings=1), it downloads a php file, but when I download it by "copy link address" and using

$ wget URL -O output

it downloads a html file.

The following is the content of the index.php:

  • https://pastebin.com/08HZeirU

The following is the content I get with wget:

  • https://pastebin.com/d5aiG9Ph

Basically the page that offers the download link (which is http://localhost/mymediawiki/mw-config/index.php?page=Complete), it seems.

The following are some of researches I had:

  • https://security.stackexchange.com/questions/98425/is-there-a-way-to-download-a-php-file-without-it-being-executed
  • Possible to view PHP code of a website?
  • Is it possible to download PHP script from a web page with wget?

But I don't understand how clicking on the "download the file" link DOES download the php file. This makes me wonder ther must be a way to download the php file with some bash commands like wget or curl.

Please tell me how the browser is doing it, and any other way to do it through the termianl.

like image 709
user3290525 Avatar asked Sep 28 '18 15:09

user3290525


2 Answers

your browser has cookies, wget doesn't, it's almost certainly a file protected by cookies, only those with the correct (authentication?) cookies can access the file, wget can not. in chrome open the developer console, navigate to the Network tab, download the file in chrome, find the request in the network tab, right click on the tab and press "copy as curl", and you'll see what the request looks like with cookies, it'll look more like:

curl 'https://stackoverflow.com/posts/validate-body' -H 'cookie: prov=5fad00f3-5ed3-bd3b-3a8a; _ga=GA1.2.20207544.1508821; sgt=id=e366-9d13-4df2-84de-2042; _gid=GA1.2.129666.1538138077; acct=t=Jyl74nJBTyCIYQq5mc2sf&s=StN3CVV2B5Opj051ywy7' -H 'origin: https://stackoverflow.com' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: en-US,en;q=0.9' -H 'user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' -H 'content-type: application/x-www-form-urlencoded; charset=UTF-8' -H 'accept: /' -H 'referer: https://stackoverflow.com/' -H 'authority: stackoverflow.com' -H 'x-requested-with: XMLHttpRequest' --data $'body=your+browser+has+cookies%2C+wge&oldBody=&isQuestion=false' --compressed

  • and if you run that command in bash, you'll probably be able to download the file from the terminal.
like image 99
hanshenrik Avatar answered Sep 23 '22 17:09

hanshenrik


As you've seen in related questions, you can't download PHP source code just by requesting the file, at least as long as the HTTP Server has PHP extension enabled.

But that by no means imply you can't download a PHP script given the right circumstances.

You can have in the server a PHP script working as proxy to bypass PHP parsing and send you source code of any PHP file in the server. It just has to set up some headers and hand it to you with a simple readfile(filename). Of course, that is usually done after making sure that you are authorized.

Also, the address of a link isn't always the address you are actually accessing when you click the link. It may contain a onclick action that overrides the href, and it can be placed there dynamically so you won't know about it even if you check the HTML source code.

One easy way to figure out what the link is actually doing is to press Ctrl+Shift+C in Firefox or Chrome, go to the Network tab, and click the link to see what it really is accessing.

I bet it is not accessing the file directly like you think.

like image 23
Havenard Avatar answered Sep 21 '22 17:09

Havenard