I'm running a personal ssh server on a nonstandard port. If someone tries to log into my ssh server thru the standard port 22, it seems that the server sends "Connection refused" message.
$ ssh localhost
ssh: connect to host localhost port 22: Connection refused
How can I make it so that it doesn't send such message and it behaves like it's not running?
You've already done it -- the connection refused response is sent by the operating system when an attempt is made to connect to a port which doesn't have anything listening on it.
The server is not sending anything, it's the client telling you the server refused the connection. That's the expected behaviour if a TCP port is closed when you try to connect.
If you want your system to silently drop packets without sending a "this port is closed" TCP answer (a so-called stealth port), you need to use a firewall on the system and/or in between it and the client.
What you see is most likely your client's reaction to an ICMP port-unreachable sent by the server upon receiving the first TCP SYN-packet for a port that does not accept connections. This is correct behavior and allows the client to quickly tell you so.
If you actually mean stealth as in "this host is non-existing, dead or offline and doesn't send anything at all", configure your firewall to drop (as opposed to reject) any packets to this port. Assuming netfilter/iptables:
This leads to the client hanging for a while as it cannot decide if a response is in transit or non-existant.
Note: This will actually kill all connections instantly, including the running ones. Molly switch alert!
The server network stack is sending packets in response to the client TCP connect attempt.
When a client program sends a TCP SYN packet to request a connection, a reply packet with the ACK and RST flags set, according to examination of captured packets, is classified as a connection refused error. No service is listening on the port, and the port is closed.
furthermore DROP is not a "polite target", as it makes the net obscure to clients. DROP should be used with care.
if you really want a port to be a nice attacker enemy you could be interested in the tarpit iptables module
if you want to disorient your attacker you can join the REJECT target and play with exotics combination of ICMP error codes and --reject-type:
You can completely stealth your
ssh
port withfwknop
in conjunction with setting a default deny policy iniptables
to silently drop packets. Allnmap
will see are the public services.See my guide for fwknop & the accompanying pages for secure
ssh
ciphers.