-
2009-06-17
Making SOCKS proxy transparent - [UNIX/LINUX]
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://joshuafan.blogbus.com/logs/41143488.html
If we have a limited connectivity to the world from current location, but still can connect to a shell account fully open to the world (or open to the other non-public network), than dynamic port forwarding available in
sshcan save us. This feature (accessible by-Doption) in fact makes ssh acting as SOCKS server. OK, but what can we do if our application doesn't support SOCKS proxy? It's important question, because vast majority of software is unaware of such protocol. In Linux we have a greattsocks(http://tsocks.sourceforge.net/), shell wrapper which transparently allow an application to use SOCKS proxy. In Windows there isFreeCap(http://www.freecap.ru/eng/), which does the same thing, but in a different way. Nice, but what if we have dozens of machines to set up. Teaching all users how to use any of mentioned application can be also really inconvenient. Making SOCKS proxy transparent will solve (almost?) all our problems. Is it feasible? YES, but you must have an access to superuser account on a gateway server (it can be also any other server, but gateway is used here for simplicity).I'm assuming that you already have SOCKS server bound to localhost on standard port 1080 (e.g. you started
sshwith-D1080).-
Install
libevent(http://www.monkey.org/~provos/libevent/). It can be already available in your distribution repository. This will be used for compiling in next step, so you must get development package (usuallylibevent-dev). -
Download
transocks_ev(http://oss.tiggerswelt.net/transocks_ev/ - usesvnor simplywgetfiles) and build it usingmake. -
Run
transocks_evwith following arguments:-p 12345 -H localhost -s 1080 -S localhost.-Htells where to bindtransocks_ev,-pwhere to listen for incoming connections,-sand-Spoint SOCKS server. -
Change
iptablesconfiguration. This step requires superuser powers. Below you have example script (heavily based on tranSOCKS_ev's README) with some common alternatives:#!/bin/sh
IPTABLES="/sbin/iptables"
TRANSOCKS_PORT="12345"
SOCKS_HOST="192.168.0.1"
SOCKS_PORT="1080"
# Create our own chain
$IPTABLES -t nat -N TRANSOCKS
# Do not try to redirect local traffic
$IPTABLES -t nat -I TRANSOCKS -o lo -j RETURN
# Do not redirect LAN traffic and some other reserved addresses.
$IPTABLES -t nat -A TRANSOCKS -d 0.0.0.0/8 -j RETURN $IPTABLES -t nat -A TRANSOCKS -d 10.0.0.0/8 -j RETURN
$IPTABLES -t nat -A TRANSOCKS -d 127.0.0.0/8 -j RETURN
$IPTABLES -t nat -A TRANSOCKS -d 169.254.0.0/16 -j RETURN
$IPTABLES -t nat -A TRANSOCKS -d 172.16.0.0/12 -j RETURN
$IPTABLES -t nat -A TRANSOCKS -d 192.168.0.0/16 -j RETURN
$IPTABLES -t nat -A TRANSOCKS -d 224.0.0.0/4 -j RETURN
$IPTABLES -t nat -A TRANSOCKS -d 240.0.0.0/4 -j RETURN
# Do not redirect traffic for the SOCKS server (not needed if server is already excluded by above rules)
$IPTABLES -t nat -I TRANSOCKS -p tcp -d $SOCKS_HOST --dport $SOCKS_PORT -j RETURN
## Redirect only specified addresses.
#$IPTABLES -t nat -A TRANSOCKS -m iprange ! --dst-range 123.45.6.78-123.45.6.90 -j RETURN
# Redirect all traffic that gets to the end of our chain
$IPTABLES -t nat -A TRANSOCKS -p tcp -j REDIRECT --to-port $TRANSOCKS_PORT
# Filter (i.e. just branch into the TRANSOCKS-chain) all traffic that is routed over this host
$IPTABLES -t nat -A PREROUTING -j TRANSOCKS
## Filter all traffic from the own host (BE CAREFUL HERE IF THE SOCKS SERVER RUNS ON THIS MACHINE!)
#$IPTABLES -t nat -A OUTPUT -j TRANSOCKS -
Now all hosts with your machine as a gateway use SOCKS proxy accordingly to
iptablesrules. Transparently! -
Open another beer bottle and enjoy.

随机文章:
zt Fedora 9 中安装vmware tools 2008-11-23XP+UBUNTU双系统上重装XP后修复GRUB方法 2008-06-16BusyBox 简化嵌入式 Linux 系统 2008-05-12zt 几种Linux嵌入式开发环境的简单介绍 2008-03-17跟我一起写 Makefile 2007-10-29
收藏到:Del.icio.us
-







