Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving original destination from iptables after REDIRECT

Tags:

linux

iptables

I'm writing an application proxy for generic use.

I want to use this as a transparent proxy, where my original plan is to use iptables with a REDIRECT rule forward all connections to my application proxy.

The problem here is of course, that my application proxy lose the information about the intended destination.

Is it possible to query iptables to retrieve the originally intended recipient? Any other possible solution to this problem is also appreciated!

like image 674
Dog eat cat world Avatar asked Jun 29 '11 10:06

Dog eat cat world


1 Answers

Perhaps this is what you were looking for?

http://www.network-builders.com/iptables-redirect-original-destination-ip-t69515.html

Read the SO_ORIGINAL_DST option of the TCP socket.
Or look up the connection tracking table in /proc/net/ip_conntrack.

#include <linux/netfilter_ipv4.h>

struct sockaddr_in addr;
socklen_t addr_sz = sizeof(addr);
memset(&addr, 0, addr_sz);
addr.sin_family = AF_INET;
getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, &addr, &addr_sz);

  I think you should be able to convert that to something similar for python.

like image 75
Casper Avatar answered Oct 08 '22 09:10

Casper