Highland Linux User Group

Linux Community
It is currently Mon Feb 06, 2012 8:32 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Turn On/Off Server Ports Remotely
PostPosted: Sun Feb 28, 2010 9:54 am 
Offline
Moderator
User avatar

Joined: Tue Oct 03, 2006 12:27 pm
Posts: 162
Location: Inverness UK
The most vulnerable for hackers in a server is "services".

Each service is running on a process, each service either requires a port or not at all.

common services require ports are FTP, Domain, Samba, Apache, Mail, SSH ....

This open up a big vulnerable exploits if any service is not up-to-date, there's no guaranty those services will not be exploited anytime.

The solution is block the port that you dont use, terminate services that not in use.

So let's say I've blocked all of the ports that i don't use in my server, but some port i MUST open so that i can get into the server to maintain, these ports are 21(FTP) and 22 (SSH), i don't want to leave them open all the time so that some so called "hackers" can "brute force" my server.

My first solution is to open to only one specific static IP, which is only that IP can access to my server and the rest will be dropped!
something like: My Pc ------------->Proxy IP-------------->My server which is "one way ticket"
I would recommend you to keep only HTTP port open (80) if you are using web service, other service you may want to keep open is Domain (53), Mail (25) close them if you're using free DNS.

The solution is blocking all of the ports that i don't use and keep port 80 open to the world and port 22 open to only one IP and deny the rest

My favorite firewall is ufw it simplify the iptables rules.
i don't think i have to guide you to install ufw if you're interested in this post to protect your server, however if you do have trouble of installing ufw just give me a shout here

to close the port you want, eg: port 110:

root@linux:~# ufw deny 110


to allow only from one IP, eg: 192.168.0.4 port 22

root@linux:~# ufw allow from 192.168.0.4 to any port 22


to allow port 80 to the world:

root@linux:~# ufw allow 80



You may specify multiple ports (comma separated list):

root@linux:~# ufw allow 80,443

i've deny everything except port 80 and port 22 to IP 192.168.0.4

now look at the rules

Code:
root@linux:~# ufw status
Status: active

To                         Action      From
--                         ------      ----
3306                       DENY        Anywhere
25                         DENY        Anywhere
110                        DENY        Anywhere
143                        DENY        Anywhere
443                        DENY        Anywhere
993                        DENY        Anywhere
995                        DENY        Anywhere
20                         DENY        Anywhere
53                         DENY        Anywhere
80                         ALLOW       Anywhere
21                         ALLOW       192.168.0.4
22                         ALLOW       192.168.0.4
21                         DENY        Anywhere
22                         DENY        Anywhere


or
root@linux:~#iptables -L
to see actual details
The most important thing here is to ALLOW the IP 192.168.0.4 BEFORE you deny the rest, if you do deny before then the firewall will just look at the deny rule first and "deny" you before it "allows" you.

Ok the first solution is done, the question is what if your server IP 192.168.0.4 is unreachable in a remote location and it's down, or you are somewhere else, you want to get into the server but the only way to it is shut, or no access to your second server which is 192.168.0.4, only port 80 is open, there's no chance to get in.

well, let's get into server from port 80 :)

think about what cron job could do, i thought what if i can get the cron to open and shut the port for me when i want.

to do this via port 80 i need apache and php enabled.

to get the cron to run sh /path/to/file/has/commad.sh

i called up cron job by type in CLI:

root@linux:~#crontab -e

Remember you must to be root to execute the cron, to open ports, normal user has no privileges to do such thing.

to get the cron run every 5 minutes:

*/5 * * * * sh /path/to/file/has/commad.sh

change it if you want it to run every 1 minute.

if you want to keep log of it:


*/5 * * * * sh /path/to/file/has/commad.sh >> /var/log/opensesami.log 2>&1


if you don't want to keep log just point it to the "black hole" /dev/null


*/5 * * * * sh /path/to/file/has/commad.sh >> /dev/null 2>&1


exit and save the cron.

now all i need is to write to the /path/to/file/has/commad.sh file to tell ufw the port that i want to open.

to do this i have to change the permission for it to read/write by web server.

root@linux:~#chmod 777 /path/to/file/has/commad.sh

ok now create a php file on my website which is hosted on my server to write to this file, now things are getting clearer for you.

i call it secrete.php

file: secrete.php
Code:
<?
$start = $_GET["start"];
$fn = "/path/to/file/has/commad.sh";
$content1 = htmlspecialchars(implode("",file($fn)));
if ($start == 1)
{
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
header("location:secrete.php");
}
else{
echo "
<form action='./secrete.php?start=1' method='post'>
<textarea rows='5' cols='40' name='content'>$content1</textarea><br>
<input type='submit' value='Add'>
</form>
";
}
?>


ok, place this file on your website somewhere safe, so you can access it on your web browser, eg: (secrete/secrete.php)i would recommend you to use password protect directory (.htaccess) to protect it,

now all you have to do is go on your website add in the command for cron to execute example i want to open port 22:


/usr/sbin/ufw allow 22


to deny

/usr/sbin/ufw deny 22


ok, that's it, just sit there for 1 minute and wait for the port to open and get in :).

you might wondering if both of the solutions are fail, you have No WAY to get in.

then the THIRD solution is just to let the server does the job itself, open and close ports as you wish!

how?

Well, if you've gone this far, i'm sure that you can think of the THIRD solution, if not, give me a shout, i'll write the THIRD solution here.
i'm not trying to hide it, it's just that i don't have much time, i'll continue when i do.
:) or if you think that your THIRD solution is even better, share it here so that people can experiment.

Thanks for your time.

_________________
Computers are like air conditioners, They stop working properly when you open Windows!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2010 7:37 am 
Offline
Moderator
User avatar

Joined: Tue Oct 03, 2006 12:27 pm
Posts: 162
Location: Inverness UK
The Cron job doesn't support "instant cron" which is execute every seconds
if we want it to execute the file every second we have to create a "daemon" that run at the background.

i wrote a small script to loop every second
create a file and put this in. i call it x.sh
Code:
#!/bin/bash

while true; do
sh /path/to/file/has/commad.sh #execute the file
echo > /path/to/file/has/commad.sh #remove the content
sleep 2 #seconds to wait
done


now to run it from the back ground simply type:

root@machine:~# sh x.sh &

the (&) symbol allows it to run from the background
you can type root@machine:~# ps to see the process id (pid) and "kill" it if you want it to stop.

to make it run on start up, simply move it to /etc/init.d/
make it executable
chmod +x /etc/init.d/x.sh

update-rc.d x.sh start 37 S

done

_________________
Computers are like air conditioners, They stop working properly when you open Windows!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2010 Highlands Linux Users Group