Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
iptables: how do I redirect port 22 to another port?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
Robert S
Guru
Guru


Joined: 15 Aug 2004
Posts: 463
Location: Canberra Australia

PostPosted: Sun May 11, 2008 10:26 am    Post subject: iptables: how do I redirect port 22 to another port? Reply with quote

I'm getting huge numbers of attempted logins through port 22. How do I change the port using iptables? I am using NAT at present. I have tried the following:
Code:
iptables -t nat -A PREROUTING -i ppp0  -p tcp --dport 2222 -j REDIRECT --to-ports 22

but it is possible to log in remotely through port 22 AND port 2222 (2222 is a hypothetical port).

I have a number of iptables rules to block failed login attempts (eg. recent module, app-admin/sshguard) and I don't want to interfere with the function of these if possible.

How do I set this up so logins through port 22 are blocked?
Back to top
View user's profile Send private message
bunder
Bodhisattva
Bodhisattva


Joined: 10 Apr 2004
Posts: 5947

PostPosted: Sun May 11, 2008 10:43 am    Post subject: Reply with quote

Quote:
How do I set this up so logins through port 22 are blocked?


you can tell the router to forward 2222 (outside) to 22 (inside)... any request coming to 22 (outside) should get dropped as it's not forwarded to anything on the inside. (ie: drop the 22->22 rule and replace it with 2222->22)

cheers
_________________
Neddyseagoon wrote:
The problem with leaving is that you can only do it once and it reduces your influence.

banned from #gentoo since sept 2017
Back to top
View user's profile Send private message
manaka
Apprentice
Apprentice


Joined: 23 Jul 2007
Posts: 178
Location: Spain

PostPosted: Mon May 12, 2008 6:00 pm    Post subject: Reply with quote

You can try something like this... The example uses DNAT as target (a personal choice). But should work just fine with REDIRECT...



Code:

# Connections to port 22 are dropped ;-)
iptables -t nat -A PREROUTING -i ppp0 -p tcp --dport 22 -j DROP
# Connections to port 2222 are redirected to port 22 ;-)
iptables -t nat -A PREROUTING -i ppp0 -p tcp --dport 2222 -j DNAT --to :22

# Connections to port 22 are accepted
# This rule is tried *after* NAT prerouting, so you are actually accepting connections to
# the original 2222 port ;-)
iptables -t filter -A INPUT -i ppp0 -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT

_________________
Javier Miqueleiz

"Listen to your heart. It knows all things, because it came from the Soul of the World, and it will one day return there."
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum