Question

My Sites Have Been Hacked - Whats the Best Security Prevention

Posted March 8, 2020 313 views
NginxWordPressSecurityUbuntu 18.04

Hi Guys

I have several wordpress sites hosted on an Ubuntu server driven by Nginx. Ive had to clean out many bogus php files and clean up my sites following an obvious hack

I have fail2ban installed and ssl Hardening, I have disabled root login and password access so only access should be via ssh.

I’m guessing this is a Cross Site Scripting attack so I’m wondering what else I can do to prevent a repeat of this in the future?

Any advice would be greatly appreciated

Thanks in advance

Matt

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hello, @thinknew

I will recommend you to check the folder’s structure and make sure that the permissions are not set too high. This should prevent people from accessing certain directories and also prevent them from uploading files.

Another thing is to disable shell_exec . The shellexec() function is an inbuilt function in PHP which is used to execute the commands via shell and return the complete output as a string. When it’s usually enabled people can use it to execute malicious scripts. In order to disable it open the PHP.ini file and add the shellexec to the disable_functions row/line:

disable_functions=shell_exec

You’ve mentioned that the sites are based on WordPress so there are several things you need to do:

  1. Always make sure that your WordPress installations are up to date. When there is a new update you can take a backup (in case something goes wrong) for the site and then update the WordPress version.

  2. Keep you plugins updated. When there is a new version of a plugin that you use, update it. You can again first take backup for the site in question.

  3. Remove any plugin which is not active. If you have any plugins which are currently inactive and you’re not using it will be better to have this removed.

  4. Install a security plugin like Wordfence, iThemes Security or Sucuri Security to keep everything protected.

  5. Occasionally checking through the site files for the sites, making sure nothing is out of place

You can also check the status of your sites site checking them through hack scanners such as http://sitecheck.sucuri.net/ http://isithacked.com/ http://www.unmaskparasites.com/

Hope this helps!

Regards,
Alex

  • Hi @Alex - Thanks for your feedback I appreciate that very much. Ive added shell_exec to my php.ini file and checked the sites on the hack filters - all seems to be well now (fingers crossed)

    Can I just ask one more question in relation to the permissions - what would you recommend a suitable permission level should be?

    Many Thanks in advance
    Matt

    • Hello, @thinknew

      You can make sure that there are no directories with 777 permissions. WordPress will be fine with 755 for directories and 644 for the files. You can execute the following command in order to update the file/folder permissions (you can take a backup of the site before updating the permissions in case something goes wrong, however this change should not make any negative impact on your site).

      You can execute it from the WordPress root directory, e.g /var/www/yourdomain.com/

      find . -type d -exec chmod 755 {} \; && find . -type f -exec chmod 644 {} \;
      

      Regards,
      Alex

Submit an Answer