Question

Using IPTABLES to redirect http traffic to a transparent proxy

Asked by: manav08

We have a proxy server which is hosted at 192.168.102.3, running on port 8080.
This proxy is running Squid.
We have another linux server which provides DHCP, DNS, etc. for the 192.168.102.x network. What I want to do is, use IPTABLES on this server to direct all http traffic to our proxy.
Here are the 2 commands I am using.

iptables -t nat -I PREROUTING -i eth0 -p tcp --dport 80 -s ! 192.168.102.3 -j DNAT --to-destination 192.168.102.3:8080
iptables -t nat -I PREROUTING -i eth0 -p tcp --dport 80 -d 192.168.102.1 -RETURN

This does work as it redirects traffic to the proxy but the problem is, that squid doesn;t pick up the URL and comes up with an error.
Example:
http://www.google.com/ - is detected as - /
http://www.google.com/search?hl=en&rls=com.microsoft%3A*&q=test - is detected as - /search?hl=en&rls=com.microsoft%3A*&q=test


Basically it strikes of the main URL completely and hence an error comes up saying INVALID URL. Anyone know how to fix it??
             

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-02-05 at 20:06:15ID24118280
Topics

IP Tables/IP Chains

,

Bourne-Again Shell (bash)

Participating Experts
1
Points
200
Comments
19

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. iptables
    hai all, i am having a machine with 2 NIC. one connected to (eth1)a server and the other(eth0) to intranet. just a testing purpose. my iptable file looks like this. i am actually redirecting the http traffic to my machine. but it is n't working properly. ...
  2. IPTABLES: NAT and FORWARD
    Hi, I am trying to setup a spam filter gateway. We have iptables firewall running on Linux box. Our firewall is connected to the public internet and ip addresses behind firewall are public too. So I want to redirect email traffic to the spam filter gateway using iptables. ...
  3. Iptable
    I use FC2 my eth0(192.168.0.8) connect to router (192.168.0.2 which connect to internet) ,My eth1 (192.168.2.18) conect to client pcs 1 How can I use iptable to sharing internet to my client pcs 2 only repquest form client pc that is port 80 force to send to squid Than...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: BlazPosted on 2009-02-05 at 23:26:24ID: 23567460

 

by: manav08Posted on 2009-02-06 at 05:34:16ID: 23569542

I am using Squid 3.0 and the following are not available.

httpd_accel_host virtual
httpd_accel_port 80
httpd_accel_with_proxy on
httpd_accel_uses_host_header on

Please advise

 

by: manav08Posted on 2009-02-06 at 05:43:34ID: 23569599

I think the answer to my question might be in

http://kuscsik.blogspot.com/2008/01/transparent-proxy-with-squid-3-on.html

I am unable to access the server now, so I will have to try it on Monday when I get back to work.
Interesting that in SQUID 3.0 they have made it more simple than the previous versions.
Hope this works or I will be back on experts-exchange on Monday.

 

by: manav08Posted on 2009-02-10 at 16:03:28ID: 23607001

Hi Blaz,

Got the transparent proxy going now. Slight problem now my access.log file shows all traffic coming from 192.168.102.1 whereas earlier it was showing the actual IP addresses. Any ideas?

 

by: BlazPosted on 2009-02-11 at 00:49:27ID: 23609248

With the DNAT rules above this should not happen.

Do you have any SNAT rules or MASQUERADE rule on the 192.168.102.1 box?

 

by: manav08Posted on 2009-02-11 at 02:50:20ID: 23609890

No. This is a box that I actually inherited. Is there any way to check??

 

by: BlazPosted on 2009-02-11 at 03:36:08ID: 23610169

This command will list all NAT rules on the box
/sbin/iptables -t nat -L

 

by: manav08Posted on 2009-02-11 at 04:08:17ID: 23610457

Here you go -

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DNATP1     all  --  anywhere             some internet IP
DNATP2     all  --  anywhere             some internet IP
DNAT       tcp  --  anywhere             some internet IP
 
 
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
ACCEPT     ipv6-crypt--  anywhere             anywhere            MARK match 0x0
 
ACCEPT     ipv6-auth--  anywhere             anywhere            MARK match 0x0
MASQUERADE  all  --  anywhere             anywhere
MASQUERADE  all  --  anywhere             anywhere            ctstate DNAT MARK
match 0xc
MASQUERADE  all  --  anywhere             anywhere            ctstate DNAT MARK
match 0xd
MASQUERADE  all  --  anywhere             anywhere            ctstate DNAT MARK
match 0xe
MASQUERADE  all  --  anywhere             anywhere            ctstate DNAT MARK
match 0x11
MASQUERADE  all  --  anywhere             anywhere            ctstate DNAT MARK
match 0x12
MASQUERADE  all  --  anywhere             anywhere
MASQUERADE  all  --  anywhere             anywhere
 
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
 
Chain DNATP1 (1 references)
target     prot opt source               destination
DNAT       tcp  --  anywhere             anywhere            tcp dpt:http to:192.168.11.10
DNAT       tcp  --  anywhere             anywhere            tcp dpt:https to:192.168.11.10
DNAT       tcp  --  anywhere             anywhere            tcp dpt:snpp to:192.168.11.10
 
Chain DNATP2 (1 references)
target     prot opt source               destination
DNAT       udp  --  anywhere             anywhere            udp dpt:datametrics to:192.168.11.17
DNAT       udp  --  anywhere             anywhere            udp dpt:sa-msg-port to:192.168.11.17

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:

Select allOpen in new window

 

by: BlazPosted on 2009-02-11 at 04:35:37ID: 23610655

In the POSTROUTING chain you can see that all the traffic is MASQUREADEd. You should exclude all the traffic exiting on internal LAN from MASQUERADEing.

You can do this by inserting the rule:
iptables -t nat -I POSTROUTING -o eth0 -j ACCEPT

or by modifying the rule which now probably looks like:
iptables -t nat -A POSTROUTING -j MASQUERADE
to:
iptables -t nat -A POSTROUTING ! -o eth0 -j MASQUERADE

 

by: manav08Posted on 2009-02-11 at 06:45:57ID: 23611843

Thanks I will try this tomorrow and let you know how I went.

 

by: manav08Posted on 2009-02-11 at 15:05:54ID: 23617657

Hi Blaz,

This didn't work - iptables -t nat -I POSTROUTING -o eth0 -j ACCEPT
When I did it my proxy stopped working altogether.

2.  iptables -t nat -A POSTROUTING ! -o eth0 -j MASQUERADE

This made no difference and the request still seem to be coming from 192.168.102.1


Also did you mean, run this in the following order -

iptables -t nat -I POSTROUTING -o eth0 -j ACCEPT
iptables -t nat -A POSTROUTING ! -o eth0 -j MASQUERADE

If this is what you meant I haven't tried. I juest ran them individually. Please comment.

 

by: BlazPosted on 2009-02-11 at 23:04:37ID: 23619778

I meant those two rules as alternatives.

Let me explain:
All the traffic from your internal lan (eth0) goes to your firewall (102.1) and when forwarding to internet you have to assign the firewalls public IP to the packets. You acomplish this with the command MASQUERADE (or you could have done it with a SNAT - source NAT). To the internet all the packets from your network seem to come from your firewall (a single machine).

The problem is that currently you are now forwarding traffic to an internal machine (the proxy) and obviously your rules do the MASQUERADING to that packets also (appearing to come from your firewall). This is not neccessary. You should make sure the MASQUERADING rules do not affect the traffic from your LAN to your proxy.

You have many MASQUREADING rules could you post them all? There may be some tricky rules at your site...
Could you also describe the machine - what interfaces does it have, how do you connect to the internet.

You could also change
iptables -t nat -I POSTROUTING -o eth0 -j ACCEPT

to:
iptables -t nat -I POSTROUTING -s 192.168.102.0/24 -o eth0 -d 192.168.102.3 -j ACCEPT
to catch only the traffic from your LAN to your proxy

 

by: manav08Posted on 2009-02-12 at 19:35:40ID: 23629663

Hi Blaz,

Posted all the MASQUERADING rules for you above.

"You could also change
iptables -t nat -I POSTROUTING -o eth0 -j ACCEPT

to:
iptables -t nat -I POSTROUTING -s 192.168.102.0/24 -o eth0 -d 192.168.102.3 -j ACCEPT
to catch only the traffic from your LAN to your proxy"

This still didn't fix the problem

 

by: BlazPosted on 2009-02-12 at 23:07:48ID: 23630417

No, that output is a bit short. I would be interested in the original rules in a script. This could be script /etc/sysconfig/iptables or rules listed in /etc/rc.local or a script called from there (maybe there is a call to iptables-restore).

At least post againt the output of:
/sbin/iptables -t nat -L -v -x


Additionally the output of the command would be usefull:
/sbin/ip rule list

There are several MASQURADE rules that MARK packets. Usually this is to change routing preferences.

 

by: manav08Posted on 2009-02-13 at 20:14:02ID: 23639180

Your request 1:
/sbin/iptables -t nat -L -v -x

Note I have reverted all the changes to original since what you suggested didn't work for me. At this point in time even these 2 commands are not active

iptables -t nat -I PREROUTING -i eth0 -p tcp --dport 80 -s ! 192.168.102.3 -j DNAT --to-destination 192.168.102.3:8080
iptables -t nat -I PREROUTING -i eth0 -p tcp --dport 80 -d 192.168.102.1 -RETURN

Chain PREROUTING (policy ACCEPT 12751 packets, 1307348 bytes)
    pkts      bytes target     prot opt in     out     source               dest
ination
     403    24051 DNATP1     all  --  any    any     anywhere              some internet IP
      57     7199 DNATP2     all  --  any    any     anywhere              some internet IP
      15      712 DNAT       tcp  --  any    any     anywhere              some internet IP  tcp dpt:smtp to:192.168.11.30
 
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               dest
ination
       0        0 ACCEPT     ipv6-crypt--  any    any     anywhere             anywhere            MARK match 0x0
       0        0 ACCEPT     ipv6-auth--  any    any     anywhere             anywhere            MARK match 0x0
    1500    99691 MASQUERADE  all  --  any    any     anywhere             anywhere
       0        0 MASQUERADE  all  --  any    eth2  anywhere             anywhere            ctstate DNAT MARK match 0xc
       0        0 MASQUERADE  all  --  any    eth3  anywhere             anywhere            ctstate DNAT MARK match 0xd
       0        0 MASQUERADE  all  --  any    eth0  anywhere             anywhere            ctstate DNAT MARK match 0xe
       0        0 MASQUERADE  all  --  any    ppp1    anywhere             anywhere            ctstate DNAT MARK match 0x11
       0        0 MASQUERADE  all  --  any    ppp0    anywhere             anywhere
       0        0 MASQUERADE  all  --  any    eth1    anywhere             anywhere
 
Chain OUTPUT (policy ACCEPT 316 packets, 20674 bytes)
    pkts      bytes target     prot opt in     out     source               destination
 
Chain DNATP1 (1 references)
    pkts      bytes target     prot opt in     out     source               destination
     227    11140 DNAT       tcp  --  any    any     anywhere             anywhere            tcp dpt:http to:192.168.11.30
       0        0 DNAT       tcp  --  any    any     anywhere             anywhere            tcp dpt:snpp to:192.168.11.30
     119     5712 DNAT       tcp  --  any    any     anywhere             anywhere            tcp dpt:https to:192.168.11.30
 
Chain DNATP2 (1 references)
    pkts      bytes target     prot opt in     out     source               destination
       0        0 DNAT       udp  --  any    any     anywhere             anywhere            udp dpt:datametrics to:192.168.11.17
       0        0 DNAT       udp  --  any    any     anywhere             anywhere            udp dpt:sa-msg-port to:192.168.11.17

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:

Select allOpen in new window

 

by: manav08Posted on 2009-02-13 at 20:16:39ID: 23639186

Your request 2:
/sbin/ip rule list

Note I have reverted all the changes to original since what you suggested didn't work for me. At this point in time even these 2 commands are not active

iptables -t nat -I PREROUTING -i eth0 -p tcp --dport 80 -s ! 192.168.102.3 -j DNAT --to-destination 192.168.102.3:8080
iptables -t nat -I PREROUTING -i eth0 -p tcp --dport 80 -d 192.168.102.1 -RETURN

0:      from all lookup local
1024:   from all fwmark        7 lookup 1
1025:   from all fwmark       12 lookup 2
2048:   from OURINTERNETIP_1 lookup 1
2049:   from OURINTERNETIP_2 lookup 2
32766:  from all lookup main
32767:  from all lookup default

                                              
1:
2:
3:
4:
5:
6:
7:

Select allOpen in new window

 

by: BlazPosted on 2009-02-16 at 06:25:09ID: 23649963

Could you describe your connection to the internet - you have two xDSLs with two public IPs or something like that?
What are the interfaces on the machine (eth0, eth2, ...)

The output of the following commands:
/sbin/iptables -t mangle -L -v -x
/sbin/ip rule list 1
/sbin/ip rule list 2
/sbin/ip rule list default

 

by: manav08Posted on 2009-02-17 at 03:24:51ID: 23658228

Hi Blaz,

I wont have time to check this all out now as the project has been deferred for the moment. I will reul.  helpfopen the question once I get around to it. For now I will award the points to you. You were extremely helpful so I have raised the points to 200.

Regards

Manav

 

by: BlazPosted on 2009-02-17 at 04:41:22ID: 23658747

I must admit that the simptoms you describe confuse me somewhat.

You have some complicated firewall (and you don't know how it was set up) - marking of packets, routing depending on marking etc.

For example there are several MASQUERADING rules but it seems that only the first one is used (all traffic -> masquerade). The traffic you forward to your proxy should not be masqueraded and you will get the correct source IP on the proxy. Why it didn't work when implementing my suggested rules I don't know but they are in the right direction. With some iptables logging I believe you could figure it out.

I hope that my questions helped you understand your firewall a bit more.

Good luck

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...