LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 03-30-2014, 07:07 AM   #1
shreyasjoshi15@gmail.com
LQ Newbie
 
Registered: Mar 2014
Posts: 13

Rep: Reputation: Disabled
Iptables to change the destination IP address of facebook to google.com


I am trying to do the post-routing, where I will change the destination IP. I did the following to get the IP address of the
facebook

# host www.facebook.com
www.facebook.com is an alias for star.c10r.facebook.com.
star.c10r.facebook.com has address 31.13.79.65
star.c10r.facebook.com has IPv6 address 2a03:2880:f002:201:face:b00c:0:1

# host www.google.com
www.google.com has address 74.125.236.81
www.google.com has IPv6 address 2404:6800:4001:802::1014

Now, I want whenever the user in my pc tries to access www.facebook.com, the google.com should be connected instead.
Here, I am trying to the destination NAtting. However, I don't see the results. I can open facebook as well as google.

#iptables -t nat -I PREROUTING -d 31.13.79.65 -j DNAT --to-destination 74.125.236.81
#iptables-save

Please correct me here, what is the problem with my rule?
 
Old 03-30-2014, 07:30 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,339

Rep: Reputation: Disabled
Quote:
Originally Posted by shreyasjoshi15@gmail.com View Post
I am trying to do the post-routing, where I will change the destination IP.
You mean pre-routing NAT.

Quote:
Originally Posted by shreyasjoshi15@gmail.com View Post
I did the following to get the IP address of the
facebook

# host www.facebook.com
www.facebook.com is an alias for star.c10r.facebook.com.
star.c10r.facebook.com has address 31.13.79.65
star.c10r.facebook.com has IPv6 address 2a03:2880:f002:201:face:b00c:0:1
That is only one of several addresses "www.facebook.com" will resolve to. Most web sites that see heavy traffic use DNS round-robin balancing, often combined with region-specific DNS resolution (the hostname will resolve to a different IP address depending on the client IP).

Quote:
Originally Posted by shreyasjoshi15@gmail.com View Post
Now, I want whenever the user in my pc tries to access www.facebook.com, the google.com should be connected instead.
Here, I am trying to the destination NAtting. However, I don't see the results. I can open facebook as well as google.

#iptables -t nat -I PREROUTING -d 31.13.79.65 -j DNAT --to-destination 74.125.236.81
#iptables-save

Please correct me here, what is the problem with my rule?
Besides the fact that you've only NATed one address out of many used by Facebook, you can't really redirect web requests like that.

Even if you manage to successfully NAT all the addresses involved, the client web browser will still send the following request to the web server:
Code:
GET / HTTP 1.1
host: www.facebook.com
[ other headers may follow ]
Many web servers will return an error message if you ask for a domain the server isn't configured to serve, or it may serve a page from the "default virtual host", which may be an entirely different site than the one you wished to redirect the clients to.

It seems Google's web server will respond to requests for non-hosted sites with a "302" redirect to "http://www.google.com/". This is by no means typical behaviour for a web server, but it means that in this particular case an IP redirect should in fact work.

If you want to reliably redirect web traffic in general, you'll have to use a proxy server.

Last edited by Ser Olmy; 03-30-2014 at 07:32 AM.
 
Old 03-30-2014, 07:37 AM   #3
shreyasjoshi15@gmail.com
LQ Newbie
 
Registered: Mar 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
Then, what is the usage of Pre-routing DNAT? I am trying to make a practical example on Pre-routing DNAT. I thought this example will server, but as you told it will not work here. I actually made a firewall for one of the adult website, whenever a user tries to acess it , it gets rejected. I used dport, sport. Can't I combine dport, sport for http, and https with the pre-routing DNAT? I tried something like that, but it fails.

#iptables -t nat -I PREROUTING -p tcp --dport 80 -d 31.13.79.65 -j DNAT --to-destination 74.125.236.81
#iptables -t nat -I PREROUTING -p tcp --dport 443 -d 31.13.79.65 -j DNAT --to-destination 74.125.236.81


also, I added sport for the rules. It didn't work. I understand your DNS concern, but I am changing the destination IP. This is done pre-routing. So, before the packet leaves my pc, the destination ip is changed to some other ip. How, the DNS resolution into the picture here.

Last edited by shreyasjoshi15@gmail.com; 03-30-2014 at 07:42 AM.
 
Old 03-30-2014, 07:42 AM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,339

Rep: Reputation: Disabled
I didn't say destination NAT doesn't work. It most certainly does.

What I did say, was:
  • Redirecting a single IP address may not be sufficient to handle all traffic to a given hostname, as a hostname can resolve to multiple IP adresses.
  • For HTTP and HTTPS, a reference to the hostname is included in the application-layer traffic (which is why virtual hosting works in the first place), so simply redirecting that traffic to a different server may yield unpredictable results, including error messages.
 
Old 03-30-2014, 12:18 PM   #5
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Using iptables to do that might not be the best approach. The /etc/hosts file could be used to do local DNS and assign googles IP address to facebook. But if google and facebook are hosted at the same IP address, you'd still get facebook. You could swap the destination ip address, but facebook has many ip addresses, which would take many rules and need maintenance to stay current. You can setup a webpage with rules to redirect to google, via html content or .htaccess on the webserver. Or a variety of other methods and products designed for this type of thing.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent! Change destination email address in monit and sendmail configuration files jenniekingsland Linux - Newbie 7 11-28-2018 11:57 AM
change destination ip address/port of packet using libipq to be of local process fzansari Linux - Networking 0 03-05-2013 02:45 AM
How to change the target IP address using IPTables ltarc3 Linux - Networking 6 10-04-2011 08:54 PM
Iptables / routing to destination address through interface Xeta Linux - Networking 7 05-10-2006 03:17 PM
Change the destination URL by IPTABLES swmok Linux - Networking 4 09-14-2004 11:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 08:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration