If you run you own server (VPS, cloud instances, bare metal, no matter what), I guess you check your log files almost on daily basis, and probably you already run any the multiple available tools (the list is pretty long, but in my case I use logwatch) that helps you out to keep an eye in what is going on in your machine.
There is one thing in particular that drives me crazy. As soon as you expose a machine to the internet, people will be constantly trying to get SSH access:
Obviously, there are tools that will make your life easier (like fail2ban, totally recommended) and some security tweaks (just Google for “linux server hardening”) that you should be doing if you manage your own server, but even with all those things in place, I really don’t like people trying to access to my server 24/7… Call me crazy, if you like.
A simple solution would have been just to change the SSH port to a something different, like 22222, and this might work for you. Unfortunately, I spend most of the time behind a proxy server, and I can only access the most common ports (the same very ones that are scanned on regular basis), so I needed to keep ssh service running in port 22.
Last year I coded a small bash script (just for myself) that allowed me to open and close SSH port on demand. It is been working fine since then, and I don’t get annoyed by the ssh auth errors in logwath anymore. Now I migrated it to Python, and I thought it could be useful for anyone else, so here it is…
The script is not big deal, it will open/close certain ports that I preconfigured by reading a text file. Nothing new under the sun.
The only “particularity” of this script is that, instead of creating a service (webservice??) that allowed me to send the desired ssh state to the server (this would add another “attack vector”), I decided the script would pull the data from another location. I decided to use a (public) file in my dropbox folder.
If you want it to give it a try, these are the steps:
1 - Create a file in your dropbox folder and get the public link. Let’s say that you get something like this:
2 - Add to this file any of the options you defined in the script. ie:
1 2 3
3 - Upload this script to your server (github link available in the code snippet header):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
4 - Now check these variables in the script:
1 2 3 4
Adapt the script to fit your environment:
- Replace dbFile by the URL you got from Dropbox
- Set the path you need for the logFile
- The script will write the status of the services to my web server, so I can check the status easily. Set your path in htmlFile.
- Define the list of services you want to manage in servicesL. This has to match with the info you define in the dropbox file.
5 - Create an entry in crontab with the periodicity you need. In my case, opening/closing ssh port is not critical, so, as I don’t want Dropbox people to get angry at me, I set it up to 5 minutes :)
Now all you need to do is modify your dropbox file, and after a few minutes, check if you get the result you expected ;)
There is one main improvement that could be done, and it is adding some kind of signature to the file in order to ensure nobody is messing with us, but I have pretty good reasons not to do it (at the moment):
- I update the dropbox file from mobile devices every now and then, and adding a signature to the file would make this way too complex.
- This file is stored in my dropbox folder, so theoretically, nobody but me is able to modify it.
- The security of the server does not rely on this script. This is not meant to be “security through obscurity”.
If you are after something more professional, I recommend you to check Latch.
And that’s all folks… It is working pretty well for me, and I hope it works for you…
See you around