Background
This article was supposed to be posted a half year ago. However, I had been super super busy with my final semester study and my first formal job. So, everything’s going very well (thanks if you are interested in), and I finally got some time to record this experience.
The background is that the China’s Great Firewall is an incredible and magnificent project, which filters the network flow in a bidirectional way around the whole People’s Republic of China. However, the market in China is accordingly very large and many services do not have any alternatives around the world. For instance, in this article I am going to talk about the music service provider (Netease music) in China, which is driven by a very active community.
The issue for me when accessing its service from abroad is that the most songs are not available from my oversea ip address, and you may want to say using a proxy. Exactly, but, the Great Firewall can detects common VPN connections in few minutes, and the vpn will be banned for you in short. Therefore, the usual way we being use is SOCKS5, to be more specific - Shadowsocks which is an obfuscated version of SOCKS5.
There is an app called Transocks, providing SOCKS5 China’s proxies to people from overseas. The service is great as a free-to-use software, but the problem is that it supports mobile devices only, and I simply want to make it work with PC. So, this is my intention.
Procedure
Here’s an record how I make it.
Detecting the protocol
First of all, when I want to make use of the existing service, I have to find out which protocol it is using.
When opening the apk file as a zip, I can easily see that in assets/armeabi-v7a,
there are ss-local and ss-tunnel which are the essential libraries that drive the Shadowsocks.
Acting as man-in-the-middle
I have known that the proxy is shadowsocks, so I need to know how the app gets the login token. The most obvious way is hijacking the network communications.
Based on my experience, Fiddler 4 (Free) and Charles Proxy (Commercial) capture HTTPS communication. The prerequisite is that you will need to install a root certificate on your mobile phone of which the tutorials can be found via Google Search.
After using the right tool and right configuration for you mobile proxy (to your PC’s Fiddler or Charles Proxy), you are able to view the plain text of every HTTPS request and response.
The process is roughly as follows:
- Send register request with a UUID, get username and password.
- Send login request using the username and password, get login status.
- Send login status verification request, get user info and status.
- Send IP list request, get IP list with payloads.
- Send password request by passing server id, get shadowsocks password.
Note that, the password request requires a client side certification.
Crack the client-side certification
From the apk file, we can find a keystore in path assets/transocksv1.bks.
But the issue is that we do not know the password for this Android keystore.
So, I used Xposed framework to hook the Android system function java.security.KeyStore.load().
Here’s the reference code: https://git.io/fNGtv.
Then, I easily find out the password is 123456 which is simply stupid.
Extract certificates
Bks is Android keystore file format, and it’s not easy to extract key file outside the Android runtime.
Therefore I found a tool called Portecle doing this.
It can load the Android keystore file and extract the certificates, and we are able to use this certificates via any programming languages.
Here’s the reference code in Python:
r = requests.post(
"https://" + ipaddr + "/api/1/user-line-association/" + str(userid) + "/" + str(
ipchannel) + "?mac=" + new_uuid + "&device=Android&org=transocks", headers=headers, data=payload,
verify=False, cert=(CERT_FILE, CERT_FILE_DEC))
print(repr(r.json()))
Test with the shadowsocks password
Using sslocal -c configuration.json where the configuration.json is the response content from previous get password request.
Here’s an example:
{
"local_address":"127.0.0.1",
"method":"aes-128-cfb",
"server":"122.226.102.117",
"local_port":"1080",
"password":"6gJ6Z7TDs4IRv4AvFe",
"server_port":11426,
"timeout":60,
"alias":""
}
Disclaimer
I am not posting or publishing any detailed communication protocol or API.
As well, I will NOT release anything like Transocks PC client.
Since the tooling no longer works after all these 9 years, I’ve made the repository public: https://github.com/MewX/Transocks-PC just for future reference.
BTW, the apk was encrypted by Tencent apk protection which was not easy to decompile or dump.
Therefore, I used hook.
BTW2, I found there’s a PC client which simply uses a different client-side certificate, and they can make money by testing the certificate only. Thank Transocks for the service!
背景
这篇文章原本半年前就该发了。 不过前阵子忙着最后一学期的学业,又入职了人生第一份正式工作,真是忙到飞起。 总之,现在一切都挺顺利的(如果你关心的话,谢谢啦),我也终于抽出空来把这次经历记录下来。
事情的背景是,中国的防火长城是一项无比惊人且宏伟的工程,它在整个中华人民共和国境内对网络流量进行双向过滤。 但与此同时,国内市场规模庞大,很多服务在全世界范围内都找不到替代品。 比如我这篇文章要聊的国内音乐平台(网易云音乐),它的社区氛围就非常活跃。
身在海外访问网易云音乐时,我遇到的问题是:海外 IP 下绝大多数歌曲都听不了。你可能会说,用代理不就行了? 没错,但防火长城几分钟内就能识别出常规 VPN 连接,很快就会把你的 VPN 给封掉。 因此,我们平时常用的方案是 SOCKS5,更具体地说,是 Shadowsocks——一种经过混淆处理的 SOCKS5 协议。
有一款叫穿梭(Transocks)的 App,专门给海外华人提供回国 SOCKS5 代理。 作为一款免费软件,它的服务很赞,但问题是它只支持手机端,而我单纯想在电脑上也能用。 这就是我折腾这件事的初衷。
折腾过程
下面记录一下我是怎么搞定的。
探测协议
首先,既然想复用现有的服务,我得先弄清楚它底层用的是什么协议。
把 apk 文件当成 zip 压缩包打开后,一眼就能在 assets/armeabi-v7a 目录里看到 ss-local 和 ss-tunnel,这正是驱动 Shadowsocks 的核心组件。
中间人抓包
既然已经确定代理用的是 Shadowsocks,接下来就需要弄明白 App 是怎么拿到登录凭证的。 最直接的办法就是劫持网络通信。
以我的经验,Fiddler 4(免费)和 Charles Proxy(商业软件)都可以抓取 HTTPS 流量。 前提是你得先在手机上安装根证书,具体教程用 Google 一搜一大把。
用对工具并把手机代理正确配置好(指向电脑上的 Fiddler 或 Charles Proxy)之后,你就能看到每一条 HTTPS 请求和响应的明文了。
整个通信流程大致如下:
- 携带 UUID 发送注册请求,拿到用户名和密码。
- 用用户名和密码发送登录请求,获取登录状态。
- 发送登录状态校验请求,获取用户信息及状态。
- 发送 IP 列表请求,获取带负载信息的线路 IP 列表。
- 传入服务器 ID 发送获取密码请求,拿到 Shadowsocks 密码。
注意:获取密码这一步请求需要客户端证书。
破解客户端证书
在 apk 文件里,我们可以在 assets/transocksv1.bks 路径下找到一个密钥库文件。
但问题在于,我们不知道这个 Android KeyStore 的密码。
于是,我用 Xposed 框架 Hook 了 Android 系统方法 java.security.KeyStore.load()。
参考代码在这里:https://git.io/fNGtv。
然后轻轻松松就拿到了密码——竟然是 123456,简直蠢爆了。
提取证书
Bks 是 Android 的密钥库文件格式,在 Android 运行时之外不太方便直接提取私钥文件。
所以我找了个叫 Portecle 的工具来干这事。
它可以加载 Android 密钥库文件并导出里面的证书,这样我们就能在任意编程语言中使用这套证书了。
以下是 Python 参考代码:
r = requests.post(
"https://" + ipaddr + "/api/1/user-line-association/" + str(userid) + "/" + str(
ipchannel) + "?mac=" + new_uuid + "&device=Android&org=transocks", headers=headers, data=payload,
verify=False, cert=(CERT_FILE, CERT_FILE_DEC))
print(repr(r.json()))
测试 Shadowsocks 密码
使用 sslocal -c configuration.json 来测试,其中 configuration.json 的内容就是上一步获取密码请求返回的响应体。
示例如下:
{
"local_address":"127.0.0.1",
"method":"aes-128-cfb",
"server":"122.226.102.117",
"local_port":"1080",
"password":"6gJ6Z7TDs4IRv4AvFe",
"server_port":11426,
"timeout":60,
"alias":""
}
免责声明
我不会公开或发布任何具体的通信协议和 API 细节。
同时,我也不会发布任何类似 Transocks PC 客户端 的软件。
一晃九年过去了,这套工具早已失效,所以我把这个仓库公开了: https://github.com/MewX/Transocks-PC,权当留个参考。
顺便一提,这个 apk 加了腾讯应用加固,不好直接反编译或脱壳,所以我才用了 hook 的方式。
再顺便一提,我发现他们其实出了 PC 客户端,只不过换了另一个客户端证书——单靠校验这个证书就能收费赚钱。 感谢穿梭提供的服务!