Tips to Secure Your Web Hosting Server
A friend of mine, Elliot Swan got hacked - one of his JavaScript files had been modified to contain some sort of advertisement. Here are a few tips you can do to lock your server down, starting with simple things and getting more advanced at the bottom.

- Change your web hosting password.
- Change your SSH / root login username (if possible) and password.
- Change your FTP username (if possible) and password.
- If you changed your FTP username, chown the files to the new user through SSH by typing "chown -R username:usergroup YOUR_WEB_PATH". If you don't know the usergroup, check out the current files by typing, "ls -la YOUR_WEB_PATH"
- Change your permissions to the lowest number to allow your website to still work, this might be 444, 644, 655, 744, or 755. "chmod -R 755 YOUR_WEB_PATH"
- make sure there are no authorized keys found in your "~/.ssh/" folder. Type "ls ~/.ssh", and then "rm authorized_keys" if it is there unless of course you use that for authentication. Authorized keys allows you to ssh without a username and a password, because you put your id on your computer and on the server.
- (tip via friend, Grant Wood) There is a linux service called, "aide" that can email you when files are changed, but that is fairly intense to setup.
- (tip from friend) Check your log files in /var/log to make sure there is no unauthorized connections that you haven't made.
- Update your php.ini file with better PHP settings.
- Upgrade any installed CMS or web-based software.
If all else fails, switch web hosts or hire a server admin, because you are screwed.
Comments
I had thought it was some sort of PHP injection or something that let them rewrite the file. As it turned out, they had somehow gotten the FTP password. I changed that, and nothing more happened.
It seemed strange to me that they had full access to all my files and only inserted a bit right at the end. Wouldn't it have been better to load a completely new site with some advertisements for them?
The only reason I had found out was that Google mailed me and told me that my site was trying to make their spiders execute malicious code. Oops!
2. Or better yet, disable root login
3. Disable FTP and only allow SFTP
6. Authorized keys are secure–but only as secure as the physical security to getting to your personal computer that has the keys on it.
--
And how about some more!
11. Install fail2ban on Linux. It will ban connections from IPs that have multiple unauthorized attempts in a row.
12. Change your SSH port
13. Set your IP Tables to only allow the ports and types of connections you need (HTTP, SSH, SMTP, etc)
14. Subscribe to your operating systems security mailing list to find out when security patches are available.
15. Only allow MySQL connections from localhost
For PHP in shared hosting environments, make sure your PHP code runs as your own user (using PHP-CGI or such). If your PHP runs as the same user as everybody else, they can likely read and/or write to your files.
If you find that you've been hacked you have to seriously consider the possibility that:
1. the code you have written is the vulnerability
2. the hacker has installed a back door to get back into your system
If either of those are the case, then you could do everything listed in this article and in the comments and you wouldn't have fixed anything. The hacker could still get back in.
These days, almost all hackers get in because you haven't upgraded the apps you have installed, you have a security hole in your own app, or your password has been compromised. You should definitely secure your server using tips like this, but you also have to write secure web app code. Your code takes into consideration SQL injection attacks and malicious file names right?
After being hacked, if you can, it is best to wipe out your server OS and start over from scratch from a hopefully clean backup of your site/app.
Another useful note: if your entire site/web app is version controlled with git or svn or whatever... you can easily run status/diff against your site and see any new files or files that have been changed. That will instantly let you spot any shenanigans with your code/file data. (It won't however help you with database data or system files.)
- Your app code is very important -- I did mention to upgrade any CMSs or installed packages.
- If you have the option of restoring your server from the ground up, great.
- SVN / git diff also is a great tip.
cheers,
Thomas
I would suggest not using the root account at all - use sudo to divy up priviledges to various accounts, and lock the password for root. Allowing only certain users to ssh in and forcing login by publickey also does wonders.
For my ftp setup, I have users chrooted into their home directories, and then I've mounted (with --bind) directories they need to access into their ~, so they can access only those folders. Pretty nifty.
Thanks.
This is a nice site!!