Difference between revisions of "SSH Port"

From SME Server
Jump to navigationJump to search
m (ref to AutoBlock and Fail2ban)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{Outdated}}
 +
 +
{{Note box|As of SME9 there is the [http://wiki.contribs.org/AutoBlock AutoBlock] feature and there is the [http://wiki.contribs.org/Fail2ban Fail2ban] contrib.}}
 +
 +
{{Note box | 2013/2/17: SSH password harvesting attacks have been reported on non-standard ports: [http://bsdly.blogspot.ca/2013/02/theres-no-protection-in-high-ports.html There's No Protection In High Ports Anymore]<br /><br />Do not rely solely on changing the listening port when securing ssh on your server. }}
 +
 
== Changing the default ssh port on SME 7 ==
 
== Changing the default ssh port on SME 7 ==
  
Line 7: Line 13:
 
Based on: [http://no.longer.valid/phpwiki/index.php/Changing%20the%20default%20ssh%20port Changing the default ssh port] written by cc_skavenger. Use his howto if you are running SME 5.6 - 6.x!
 
Based on: [http://no.longer.valid/phpwiki/index.php/Changing%20the%20default%20ssh%20port Changing the default ssh port] written by cc_skavenger. Use his howto if you are running SME 5.6 - 6.x!
  
[[#Procedure:_SME_7.1.3|Updated 4/11/07 for SME 7.1.3 (possibly 7.1.x?)]]
+
* Update 7/25/07 to point out that this howto is not needed as of SME 7.2
 +
* Updated 5/28/07 to correct minor typos and improve clarity
 +
* [[#Procedure:_SME_7.1.3|Updated 4/11/07 for SME 7.1.3 (possibly 7.1.x?)]]
  
  
Line 14: Line 22:
 
In order to change the default port used by the sshd server in SME 7.0 you must change two configuration files on the SME server:
 
In order to change the default port used by the sshd server in SME 7.0 you must change two configuration files on the SME server:
  
*1. you must tell sshd what port to listen on in /etc/ssh/sshd_config and
+
# you must tell sshd what port to listen on in /etc/ssh/sshd_config and
*2. (pre 7.1x) you must configure /etc/rc.d/init.d/masq to allow inbound traffic on your new sshd port
+
# (pre 7.1x) you must configure /etc/rc.d/init.d/masq to allow inbound traffic on your new sshd port
 +
 
 +
As of SME 7.1.3 (and possibly from 7.1 onwards?) you can change this port completely by modifying configuration database entries (there is no longer a need to create a custom template).
  
As of SME 7.1.3 (and possibly from 7.1 onwards?) you can change this port completely by modifying configuration database entries (there is no longer an need to create a custom template).
 
  
 +
SME 7 uses the special db values 'TCPPort' and UDPPort' to control the iptables configuration instead of in the 'masq' templates as was done on 5.6 - 6.0x. See http://wiki.contribs.org/DB_Variables_Configuration#IPTables_firewall_.28masq.29 for more information.
  
SME 7 keeps the firewall information in the 'configuration' database instead of in the 'masq' templates as was done on 5.6 - 6.0x; this howto demonstrates
+
This howto demonstrates
  
*1. (pre 7.1) how to use a custom template fragment to modify sshd_config  
+
# (pre 7.1) how to use a custom template fragment to modify sshd_config  
*2. how to change the configuration database to open the desired non-standard port for your sshd server
+
# how to change the configuration database to open the desired non-standard port for your sshd server
*3. how to use 'expand-template' to re-generate the new sshd_config and masq files
+
# how to use 'expand-template' to re-generate the new sshd_config and masq files
*4. how to force the sshd and firewall services to recognize the new configurations
+
# how to force the sshd and firewall services to recognize the new configurations
  
 
I have inserted the procedure for SME 7.1.3 here and left the old procedure in place below for anyone still running 7.0
 
I have inserted the procedure for SME 7.1.3 here and left the old procedure in place below for anyone still running 7.0
 +
 +
=== Procedure: SME 7.2 ===
 +
 +
# Login to server-manager on your SME
 +
# Select "Remote access" (under "Security")
 +
# Enter the desired port number in "TCP port for secure shell access"
 +
# Click "Save"
  
 
=== Procedure: SME 7.1.3 ===
 
=== Procedure: SME 7.1.3 ===
 
====  Summary Version: ====
 
====  Summary Version: ====
 
+
(note: these commands will still work in 7.2)
 
  config setprop sshd TCPPort <newport>
 
  config setprop sshd TCPPort <newport>
 
  signal-event remoteaccess-update
 
  signal-event remoteaccess-update
  /etc/init.d/sshd restart
+
  service sshd restart
 
See [[#Conclusions|Conclusions]]
 
See [[#Conclusions|Conclusions]]
  
Line 102: Line 119:
 
This Howto was developed in response to the recommendations in this article at [http://isc.sans.org/diary.php?storyid=846 isc.sans.org]. The article briefly suggests taking 3 steps to secure your server against ssh attacks:
 
This Howto was developed in response to the recommendations in this article at [http://isc.sans.org/diary.php?storyid=846 isc.sans.org]. The article briefly suggests taking 3 steps to secure your server against ssh attacks:
  
*1. Run ssh on a non-standard port
+
# Run ssh on a non-standard port
*2. Choose good passphrases, and enforce them with PAM or other wrappers.
+
# Choose good passphrases, and enforce them with PAM or other wrappers.
*3. Monitor your logs, then consciously look at blocking and/or reporting abusive netblocks.
+
# Monitor your logs, then consciously look at blocking and/or reporting abusive netblocks.
  
 
Now you know how to run ssh on a non-standard port, at least. Don't be fooled into thinking that this will bring long-term securiy, however! There is a discussion of this issue in the forums here on contribs that concludes that moving ssh to another port will only help until the attackers upgrade their tools. Public/private key security is recommended. (See [http://forums.contribs.org/index.php?topic=29505.msg123499#msg123499 Guessing passwords])
 
Now you know how to run ssh on a non-standard port, at least. Don't be fooled into thinking that this will bring long-term securiy, however! There is a discussion of this issue in the forums here on contribs that concludes that moving ssh to another port will only help until the attackers upgrade their tools. Public/private key security is recommended. (See [http://forums.contribs.org/index.php?topic=29505.msg123499#msg123499 Guessing passwords])
  
 
----
 
----
[[Category:Howto]]
+
[[Category:Obsolete]]

Latest revision as of 06:17, 6 October 2014

Time.png Outdated:
The information on this page maybe no longer relevant.



Important.png Note:
As of SME9 there is the AutoBlock feature and there is the Fail2ban contrib.



Important.png Note:
2013/2/17: SSH password harvesting attacks have been reported on non-standard ports: There's No Protection In High Ports Anymore

Do not rely solely on changing the listening port when securing ssh on your server.


Changing the default ssh port on SME 7

Author: mmccarn

References: sans.org article on securing ssh and Guessing passwords

Based on: Changing the default ssh port written by cc_skavenger. Use his howto if you are running SME 5.6 - 6.x!


Introduction

In order to change the default port used by the sshd server in SME 7.0 you must change two configuration files on the SME server:

  1. you must tell sshd what port to listen on in /etc/ssh/sshd_config and
  2. (pre 7.1x) you must configure /etc/rc.d/init.d/masq to allow inbound traffic on your new sshd port

As of SME 7.1.3 (and possibly from 7.1 onwards?) you can change this port completely by modifying configuration database entries (there is no longer a need to create a custom template).


SME 7 uses the special db values 'TCPPort' and UDPPort' to control the iptables configuration instead of in the 'masq' templates as was done on 5.6 - 6.0x. See http://wiki.contribs.org/DB_Variables_Configuration#IPTables_firewall_.28masq.29 for more information.

This howto demonstrates

  1. (pre 7.1) how to use a custom template fragment to modify sshd_config
  2. how to change the configuration database to open the desired non-standard port for your sshd server
  3. how to use 'expand-template' to re-generate the new sshd_config and masq files
  4. how to force the sshd and firewall services to recognize the new configurations

I have inserted the procedure for SME 7.1.3 here and left the old procedure in place below for anyone still running 7.0

Procedure: SME 7.2

  1. Login to server-manager on your SME
  2. Select "Remote access" (under "Security")
  3. Enter the desired port number in "TCP port for secure shell access"
  4. Click "Save"

Procedure: SME 7.1.3

Summary Version:

(note: these commands will still work in 7.2)

config setprop sshd TCPPort <newport>
signal-event remoteaccess-update
service sshd restart

See Conclusions

Annotated Version (same as "Summary Version", but with explanations & verification)

  • Check, then modify the TCPPort setting for sshd
config getprop sshd TCPPort

(should return '22')

config setprop sshd TCPPort <newport>
config getprop sshd TCPPort

(should now return <newport>)

  • Regenerate the firewall rules and sshd configuration
signal-event remoteaccess-update

(this modifies the firewall rules to allow inbound traffic on <newport> and recreates /etc/ssh/sshd_config)

  • Check that sshd_config was updated and restart sshd
grep Port /etc/ssh/sshd_config

(should return 'Port <newport>')

service sshd restart

(restart sshd, causing it to recognize the new values in /etc/ssh/sshd_config)

Existing connections will remain unaffected, so that you *can* safely execute this procedure remotely. If you do change the sshd port remotely, be sure to open a connection on <newport> before you disconnect your existing session!

Please read the Conclusions section below for important security information. SSH should NOT be open for password access from the Internet!

Procedure: SME 7.0

  • Modify the firewall and sshd settings to allow inbound traffic on the new port (only the 2nd command is required, the first and third commands are included only to allow you to verify that the database was updated properly).
db configuration getprop sshd TCPPort

(should return '22')

db configuration setprop sshd TCPPort <newport>

(where <newport> is the same port number you entered into 10Port above. There is no visible result of this command)

db configuration getprop sshd TCPPort

(should now return <newport>)

  • Now expand the templates:
/sbin/e-smith/expand-template /etc/ssh/sshd_config
/sbin/e-smith/expand-template /etc/rc.d/init.d/masq
  • Verify the results:
grep Port /etc/ssh/sshd_config

(should return 'Port <newport>')

grep sshd /etc/rc.d/init.d/masq

(should return ' # sshd: TCPPort <newport>, AllowHosts: 0.0.0.0/0, DenyHosts:')

While the actual port assignment comes on the next line in /etc/rc.d/init.d/masq, both lines are pulled from the configuration database - if this line looks OK, the actual invocation should look fine, too!

  • Restart the services:
/etc/rc.d/init.d/sshd reload
/etc/rc.d/init.d/masq restart

Be sure to test your modifications before you have to rely on them! Make sure you can connect to your server using the new port value from both inside and outside of your network.

Conclusions

This Howto was developed in response to the recommendations in this article at isc.sans.org. The article briefly suggests taking 3 steps to secure your server against ssh attacks:

  1. Run ssh on a non-standard port
  2. Choose good passphrases, and enforce them with PAM or other wrappers.
  3. Monitor your logs, then consciously look at blocking and/or reporting abusive netblocks.

Now you know how to run ssh on a non-standard port, at least. Don't be fooled into thinking that this will bring long-term securiy, however! There is a discussion of this issue in the forums here on contribs that concludes that moving ssh to another port will only help until the attackers upgrade their tools. Public/private key security is recommended. (See Guessing passwords)