Pages

Monday, 2 September 2013

ProxyChains 及 Tsocks

现在我们可以用 FireFox 及 Chrome 通过 SSH Tunneling 来访问之前无法访问的网站了,但有些程序本身不支持 Socks5 代理,那么我们可以通过 ProxyChains 及 Tsocks 这两个工具来实现。

ProxyChains

ProxyChains 是一个类似于 windows 下 freecap 的软件,可结合 ssh tunneling 功能来实现翻墙。
安装
sudo apt-get install proxychains
配置,修改 /etc/proxychains.conf
# proxychains.conf  VER 2.0
#
#        HTTP, SOCKS4, SOCKS5 tunneling proxifier.
#

# The option below identifies how the ProxyList is treated.
# only one option should be uncommented at time,
# otherwise the last appearing option will be accepted
#
# Dynamic - Each connection will be done via chained proxies
# all proxies chained in the order as they appear in the list
# at least one proxy must be online to play in chain
# (dead proxies are skipped)
# otherwise EINTR is returned to the app
#
# Strict - Each connection will be done via chained proxies
# all proxies chained in the order as they appear in the list
# all proxies must be online to play in chain
# otherwise EINTR is returned to the app
#
# Random - Each connection will be done via random proxy
# (or proxy chain, see  chain_len) from the list
# this option is good for scans

dynamic_chain
#strict_chain
#random_chain

# Make sense only if random_chain
chain_len = 2

# Quiet mode (no output)
#quiet_mode

# Write stats about good proxies to proxychains.stats
#write_stats

#Some timeouts in milliseconds
#
tcp_read_time_out 15000
tcp_connect_time_out 10000

[ProxyList]
# ProxyList format
#       type  host  port [user pass]
#       (values separated by 'tab' or 'blank')
#
#
#        Examples:
#
#             socks5 192.168.67.78 1080 lamer  secret
#  http 192.168.89.3 8080 justu hidden
#   socks4 192.168.1.49 1080
#         http 192.168.39.93 8080
#
#
#       proxy types: http, socks4, socks5
#        ( auth types supported: "basic"-http  "user/pass"-socks )
#
#http  10.0.0.5 3128
socks5 127.0.0.1 7070
注意选 dynamic_chain
如何使用?
比如我们可以通过 proxychains 来运行 Twitter 客户端 pino ,如下。
proxychains pino &
就这么简单。

tsocks

安装 tsocks
sudo apt-get install tsocks
配置tsocks,让它使用我们上面建立的socks5代理,用root权限编辑/etc/tsocks.conf,修改以下几行:
server = 127.0.0.1
# Server type defaults to 4 so we need to specify it as 5 for this one
server_type = 5
The port defaults to 1080 but I've stated it here for clarity
server_port = 7070
tsocks 的使用方法和 proxychains 差不多,也是把 tsocks 放在软件名称之前就可以了。
tsocks pino &
另外通过 proxychains 和 tsocks 来安装来自于 PPA 的软件可以起到增速的作用。
sudo tsocks apt-get install software-name
-------------------------------------------------------

在终端下间接使用Socks5代理的几种方法(privoxy,tsocks,proxychains)

 
不知道为什么今天PPA又只能使用代理访问了。好像终端不支持Socks5代理 。没办法,咱转换吧。

(以下方法几乎可以使linux下面大多数软件实现代理通信,即使软件本身不支持设置代理。)

1.使用tsocks转接

简单介绍下配置方法
我们可以这样测试一下
效果那是立竿见影啊!遗憾的是似乎已经停止开发了,而且ssh和ftp不工作,其官方网站推荐了另外一个替代品,Dante,这个配置有点复杂,后面我会给出一个更好的替代品。

2.使用privoxy转换Socks5->HTTP代理

在最后一行加入
注意后面有个点
1080是本机的Socks代理端口
然后用service把privoxy给restart一下即可
默认监听的端口是本机的8118
如果要监听所有端口或者更换所有端口

改为
即可
这个虽然有点杀鸡用牛刀的意思,而且privoxy的鉴权不好实现。但是还是个好方法。

3.使用proxychains完美替代

配置非常简单,软件有3种代理模式
# Dynamic - Each connection will be done via chained proxies
# all proxies chained in the order as they appear in the list
# at least one proxy must be online to play in chain
# (dead proxies are skipped)
# otherwise EINTR is returned to the app
动态模式
按照列表内指定的代理的顺序执行代理操作,遇到不可用代理时会自动切换
至少需要一个可用代理
# Strict - Each connection will be done via chained proxies
# all proxies chained in the order as they appear in the list
# all proxies must be online to play in chain
# otherwise EINTR is returned to the app
严格模式
代理顺序严格按照列表顺序来,列表内代理必须全部处于可用状态
# Random - Each connection will be done via random proxy
# (or proxy chain, see chain_len) from the list
# this option is good for scans
随机模式
随机选择代理代理列表里面的代理,代理个数由chain_len决定(如果你的代理个数少于chain_len个数,会报错)
[ProxyList]配置节指定了代理列表。支持socks4/5 http等
还支持带认证的代理
比如
type host port [user pass]
socks5 192.168.67.78 1080 lamer secret
贴一下我的配置文件
使用方法和tsocks是一样的,比如
遗憾的是,以上几款都不支持基于用户名和密码的鉴权!
本机用用可以.
---------

tsocks, a transparent SOCKS proxying library mac版

tsocks

a transparent SOCKS proxying library
This project is originally hosted by sourceforge http://tsocks.sourceforge.net
Porting to macOS by Zou Guangxian zouguangxian@163.com based on http://marc-abramowitz.com/archives/2006/01/29/building-tsocks-on-mac-os-x, with fixes by Mikhail Zakharov zmey20000@yahoo.com

Install

    $ git clone https://github.com/mezantrop/tsocks.git
    $ cd tsocks
    $ autoconf -f
    $ ./configure --prefix=/usr/local --libdir=/usr/local/lib --with-conf=/usr/local/etc/tsocks.conf --enable-debug
    $ make
    $ make install

Usage

    $ grep '^[^#]' /usr/local/etc/tsocks.conf 
    local = 192.168.0.0/255.255.0.0
    local = 172.16.0.0/255.240.0.0
    local = 10.0.0.0/255.0.0.0
    server = 127.0.0.1
    server_type = 5
    server_port = 8135

    $ TSOCKS_DEBUG=2 tsocks git pull
or
    $ source /usr/local/bin/tsocks on 
    $ telnet example.org
or
    $ . /usr/local/bin/tsocks on 
    $ telnet example.org

Notes for macOS

  • Install OpenSSH from sources (or use Homebrew https://brew.sh port) as the default ssh will not work with the preloaded library:
    brew install openssh
  • To permanently sockify all connections using preloaded library, set essential environmental variables on login:
    launchctl setenv DYLD_FORCE_FLAT_NAMESPACE 1
    launchctl setenv DYLD_INSERT_LIBRARIES /usr/local/lib/libtsocks.dylib
frm https://github.com/zouguangxian/tsocks