Networking Forums

Networking Forums > Computer Networking > Linux Networking > using iptables to block OUTBOUND port 25?

Reply
Thread Tools Display Modes

using iptables to block OUTBOUND port 25?

 
 
deja3-user@bitrealm.com
Guest
Posts: n/a

 
      02-16-2008, 01:30 PM
Using linux as a gateway router, how does one do this? I'd like to
prevent internal computers from making outbound port 25 connections,
but still allow them to the linux box on port 25.

So, I have eth0 as the outside network and eth1 as the inside
network. Computers on the eth1 network need to be able to hit the
linux box on port 25, but those computers should NOT be allowed to go
out to the internet on port 25. This is the only port I want to block
for now.

eth0: public ip...5.6.7.8/255.255.252.0
eth1: internal ips: 172.16.0.0/255.255.255.0
 
Reply With Quote
 
 
 
 
Chris Davies
Guest
Posts: n/a

 
      02-16-2008, 02:36 PM
deja3-(E-Mail Removed) wrote:
> Using linux as a gateway router, how does one do this? I'd like to
> prevent internal computers from making outbound port 25 connections,
> but still allow them to the linux box on port 25.


Assuming (but not stated) that you want your linux box to be able to
make outbound port 25 connections, you just need this:

iptables -A FORWARD -o eth0 -p tcp --dport 25 -j REJECT


Alternatively you can do interesting things like forcing everything
that attempts to talk to port 25 to actually end up talking to your own
port 25:

iptables -t nat A PREROUTING -i eth1 -p tcp --dport 25 -j REDIRECT

You need to be careful of this (redirect), though, and bear in mind
that it will break any attempt by a client to use Authenticated SMTP
to a remote server. I do it on my home network so that I have record of
all outbound email. However, I don't block outbound Authenticated SMTP
on port 587, nor do I block SMTP/SSL on port 465. YMMV.

Chris
 
Reply With Quote
 
deja3-user@bitrealm.com
Guest
Posts: n/a

 
      02-16-2008, 03:15 PM
On Feb 16, 7:36 am, Chris Davies <chris-use...@roaima.co.uk> wrote:
> deja3-u...@bitrealm.com wrote:
> > Using linux as a gateway router, how does one do this? I'd like to
> > prevent internal computers from making outbound port 25 connections,
> > but still allow them to the linux box on port 25.

>
> Assuming (but not stated) that you want your linux box to be able to
> make outbound port 25 connections, you just need this:
>
> iptables -A FORWARD -o eth0 -p tcp --dport 25 -j REJECT
>
> Alternatively you can do interesting things like forcing everything
> that attempts to talk to port 25 to actually end up talking to your own
> port 25:
>
> iptables -t nat A PREROUTING -i eth1 -p tcp --dport 25 -j REDIRECT
>
> You need to be careful of this (redirect), though, and bear in mind
> that it will break any attempt by a client to use Authenticated SMTP
> to a remote server. I do it on my home network so that I have record of
> all outbound email. However, I don't block outbound Authenticated SMTP
> on port 587, nor do I block SMTP/SSL on port 465. YMMV.
>
> Chris


The linux box needs to be able to send/receive from the internet, but
I do not wish to allow internal users to go out to the internet. Any
attempt at an outbound connection should be denied, not redirected.
There are several worm type variants that run their own smtp server,
and I want to make sure that I am not contributing to the spam bot net.
 
Reply With Quote
 
deja3-user@bitrealm.com
Guest
Posts: n/a

 
      02-16-2008, 03:49 PM
On Feb 16, 8:15 am, deja3-u...@bitrealm.com wrote:
> On Feb 16, 7:36 am, Chris Davies <chris-use...@roaima.co.uk> wrote:
>
>
>
> > deja3-u...@bitrealm.com wrote:
> > > Using linux as a gateway router, how does one do this? I'd like to
> > > prevent internal computers from making outbound port 25 connections,
> > > but still allow them to the linux box on port 25.

>
> > Assuming (but not stated) that you want your linux box to be able to
> > make outbound port 25 connections, you just need this:

>
> > iptables -A FORWARD -o eth0 -p tcp --dport 25 -j REJECT

>
> > Alternatively you can do interesting things like forcing everything
> > that attempts to talk to port 25 to actually end up talking to your own
> > port 25:

>
> > iptables -t nat A PREROUTING -i eth1 -p tcp --dport 25 -j REDIRECT

>
> > You need to be careful of this (redirect), though, and bear in mind
> > that it will break any attempt by a client to use Authenticated SMTP
> > to a remote server. I do it on my home network so that I have record of
> > all outbound email. However, I don't block outbound Authenticated SMTP
> > on port 587, nor do I block SMTP/SSL on port 465. YMMV.

>
> > Chris

>

I tried the line you suggested above:

iptables -A FORWARD -o eth0 -p tcp --dport 25 -j REJECT

And it seems to be doing what I need it to do. Thanks for your
assistance!

Now, can I have it log to a file whenever this rule matches so I can
tell which machines are mis-configured or contain a spam bot? The log
should contain the date/time as well as the source/destination ips if
possible.

 
Reply With Quote
 
Chris Davies
Guest
Posts: n/a

 
      02-16-2008, 11:08 PM
deja3-(E-Mail Removed) wrote:
> I tried the line you suggested above:
> iptables -A FORWARD -o eth0 -p tcp --dport 25 -j REJECT


> And it seems to be doing what I need it to do. Thanks for your
> assistance!


You asked for something to block outbound SMTP connections. This
suggestion that you took from my post does NOT do that. Please ensure
you're using the right example.


> Now, can I have it log to a file whenever this rule matches so I can
> tell which machines are mis-configured or contain a spam bot?


Suggest you "man iptables" and look for the LOG option.

Chris
 
Reply With Quote
 
Andrew Gideon
Guest
Posts: n/a

 
      02-17-2008, 09:55 PM
On Sun, 17 Feb 2008 00:08:21 +0000, Chris Davies wrote:

>> Now, can I have it log to a file whenever this rule matches so I can
>> tell which machines are mis-configured or contain a spam bot?

>
> Suggest you "man iptables" and look for the LOG option.


Agreed.

But if you really want to know what the other system is doing, and you're
willing to invest a little, there's an option to consider. Using the
PREROUTING rule described earlier, pass the SMTP attempts on to an SMTP
server (not necessarily on port 25) on your routing box. This would be
set to accept but not forward the traffic.

You can then manually review the messages sitting in that mail server's
mqueue. Anything you want to pass on can be passed on with -qI....

Like I wrote, though, there's an investment here.

- Andrew
 
Reply With Quote
 
David Brown
Guest
Posts: n/a

 
      02-18-2008, 05:58 AM
Andrew Gideon wrote:
> On Sun, 17 Feb 2008 00:08:21 +0000, Chris Davies wrote:
>
>>> Now, can I have it log to a file whenever this rule matches so I can
>>> tell which machines are mis-configured or contain a spam bot?

>> Suggest you "man iptables" and look for the LOG option.

>
> Agreed.
>
> But if you really want to know what the other system is doing, and you're
> willing to invest a little, there's an option to consider. Using the
> PREROUTING rule described earlier, pass the SMTP attempts on to an SMTP
> server (not necessarily on port 25) on your routing box. This would be
> set to accept but not forward the traffic.
>
> You can then manually review the messages sitting in that mail server's
> mqueue. Anything you want to pass on can be passed on with -qI....
>
> Like I wrote, though, there's an investment here.
>
> - Andrew


I'd be surprised if there is much to gain here. I've done similar
blocking on our network (the router/firewall was not Linux, but the
principle is the same). There are not many potential sources of stmp
traffic, and it makes sense to block almost all of them. It's important
to see when the traffic is being blocked (so you know who to blame
and/or disconnect from your network), but the traffic itself is pretty
worthless.

On a small network, the only outgoing smtp traffic should be from your
mail server to your ISP's relay (or from individual machines to a single
specific external relay if you have no internal email server). Anything
else is either a misconfigured email client (the user will quickly see
they've got a problem), a misconfigured email server (again, the admin
for it will see they have a problem), or a spam bot. It doesn't matter
what the spam bot is sending out - all that matters is that its traffic
is blocked and that the admins are notified quickly of the blocking. It
helps to have more than one admin - the one time someone in our office
accidentally installed a trojan mailer, I was out of the office - the
next day, I had 50,000 emails from our router complaining about illegal
traffic...
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
iptables - opening an inbound port but allowing access to all machines outbound Allan M. Bruce Linux Networking 1 06-12-2006 08:18 AM
Redirect outbound traffic with IPTABLEs TravisT Linux Networking 5 05-02-2006 02:32 PM
iptables rule to block external, but accept local port (8009) connection OtisUsenet Linux Networking 1 02-14-2006 08:10 AM
IPTables filtering what should be allowed outbound traffic SmittyBroham Linux Networking 6 10-03-2005 05:42 PM
Kernel 2.4.22 iptables not NAT'ting outbound PPTP connections Carl Farrington Linux Networking 4 01-18-2004 09:45 PM



1 2 3 4 5 6 7 8 9 10 11