Skip to content

Commit 67a7949

Browse files
committed
privacy: Stream isolation for Tor
According to Tor's extensions to the SOCKS protocol (https://gitweb.torproject.org/torspec.git/tree/socks-extensions.txt) it is possible to perform stream isolation by providing authentication to the proxy. Each set of credentials will create a new circuit, which makes it harder to correlate connections. This patch adds an option, `-proxyrandomize` (on by default) that randomizes credentials for every outgoing connection, thus creating a new circuit. 2015-03-16 15:29:59 SOCKS5 Sending proxy authentication 3842137544:3256031132
1 parent 8f955b9 commit 67a7949

File tree

6 files changed

+129
-79
lines changed

6 files changed

+129
-79
lines changed

src/init.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ std::string HelpMessage(HelpMessageMode mode)
301301
strUsage += HelpMessageOpt("-permitbaremultisig", strprintf(_("Relay non-P2SH multisig (default: %u)"), 1));
302302
strUsage += HelpMessageOpt("-port=<port>", strprintf(_("Listen for connections on <port> (default: %u or testnet: %u)"), 8333, 18333));
303303
strUsage += HelpMessageOpt("-proxy=<ip:port>", _("Connect through SOCKS5 proxy"));
304+
strUsage += HelpMessageOpt("-proxyrandomize", strprintf(_("Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u)"), 1));
304305
strUsage += HelpMessageOpt("-seednode=<ip>", _("Connect to a node to retrieve peer addresses, and disconnect"));
305306
strUsage += HelpMessageOpt("-timeout=<n>", strprintf(_("Specify connection timeout in milliseconds (minimum: 1, default: %d)"), DEFAULT_CONNECT_TIMEOUT));
306307
#ifdef USE_UPNP
@@ -351,7 +352,7 @@ std::string HelpMessage(HelpMessageMode mode)
351352
strUsage += HelpMessageOpt("-flushwallet", strprintf(_("Run a thread to flush wallet periodically (default: %u)"), 1));
352353
strUsage += HelpMessageOpt("-stopafterblockimport", strprintf(_("Stop running after importing blocks from disk (default: %u)"), 0));
353354
}
354-
string debugCategories = "addrman, alert, bench, coindb, db, lock, rand, rpc, selectcoins, mempool, net"; // Don't translate these and qt below
355+
string debugCategories = "addrman, alert, bench, coindb, db, lock, rand, rpc, selectcoins, mempool, net, proxy"; // Don't translate these and qt below
355356
if (mode == HMM_BITCOIN_QT)
356357
debugCategories += ", qt";
357358
strUsage += HelpMessageOpt("-debug=<category>", strprintf(_("Output debugging information (default: %u, supplying <category> is optional)"), 0) + ". " +
@@ -891,10 +892,10 @@ bool AppInit2(boost::thread_group& threadGroup)
891892
}
892893
}
893894

894-
CService addrProxy;
895+
proxyType addrProxy;
895896
bool fProxy = false;
896897
if (mapArgs.count("-proxy")) {
897-
addrProxy = CService(mapArgs["-proxy"], 9050);
898+
addrProxy = proxyType(CService(mapArgs["-proxy"], 9050), GetArg("-proxyrandomize", true));
898899
if (!addrProxy.IsValid())
899900
return InitError(strprintf(_("Invalid -proxy address: '%s'"), mapArgs["-proxy"]));
900901

@@ -904,14 +905,14 @@ bool AppInit2(boost::thread_group& threadGroup)
904905
fProxy = true;
905906
}
906907

907-
// -onion can override normal proxy, -noonion disables tor entirely
908+
// -onion can override normal proxy, -noonion disables connecting to .onion entirely
908909
if (!(mapArgs.count("-onion") && mapArgs["-onion"] == "0") &&
909910
(fProxy || mapArgs.count("-onion"))) {
910-
CService addrOnion;
911+
proxyType addrOnion;
911912
if (!mapArgs.count("-onion"))
912913
addrOnion = addrProxy;
913914
else
914-
addrOnion = CService(mapArgs["-onion"], 9050);
915+
addrOnion = proxyType(CService(mapArgs["-onion"], 9050), GetArg("-proxyrandomize", true));
915916
if (!addrOnion.IsValid())
916917
return InitError(strprintf(_("Invalid -onion address: '%s'"), mapArgs["-onion"]));
917918
SetProxy(NET_TOR, addrOnion);

src/netbase.cpp

Lines changed: 104 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "hash.h"
1313
#include "sync.h"
1414
#include "uint256.h"
15+
#include "random.h"
1516
#include "util.h"
1617
#include "utilstrencodings.h"
1718

@@ -38,7 +39,7 @@ using namespace std;
3839

3940
// Settings
4041
static proxyType proxyInfo[NET_MAX];
41-
static CService nameProxy;
42+
static proxyType nameProxy;
4243
static CCriticalSection cs_proxyInfos;
4344
int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
4445
bool fNameLookup = false;
@@ -285,59 +286,100 @@ bool static InterruptibleRecv(char* data, size_t len, int timeout, SOCKET& hSock
285286
return len == 0;
286287
}
287288

288-
bool static Socks5(string strDest, int port, SOCKET& hSocket)
289+
struct ProxyCredentials
290+
{
291+
std::string username;
292+
std::string password;
293+
};
294+
295+
/** Connect using SOCKS5 (as described in RFC1928) */
296+
bool static Socks5(string strDest, int port, const ProxyCredentials *auth, SOCKET& hSocket)
289297
{
290298
LogPrintf("SOCKS5 connecting %s\n", strDest);
291-
if (strDest.size() > 255)
292-
{
299+
if (strDest.size() > 255) {
293300
CloseSocket(hSocket);
294301
return error("Hostname too long");
295302
}
296-
char pszSocks5Init[] = "\5\1\0";
297-
ssize_t nSize = sizeof(pszSocks5Init) - 1;
298-
299-
ssize_t ret = send(hSocket, pszSocks5Init, nSize, MSG_NOSIGNAL);
300-
if (ret != nSize)
301-
{
303+
// Accepted authentication methods
304+
std::vector<uint8_t> vSocks5Init;
305+
vSocks5Init.push_back(0x05);
306+
if (auth) {
307+
vSocks5Init.push_back(0x02); // # METHODS
308+
vSocks5Init.push_back(0x00); // X'00' NO AUTHENTICATION REQUIRED
309+
vSocks5Init.push_back(0x02); // X'02' USERNAME/PASSWORD (RFC1929)
310+
} else {
311+
vSocks5Init.push_back(0x01); // # METHODS
312+
vSocks5Init.push_back(0x00); // X'00' NO AUTHENTICATION REQUIRED
313+
}
314+
ssize_t ret = send(hSocket, (const char*)begin_ptr(vSocks5Init), vSocks5Init.size(), MSG_NOSIGNAL);
315+
if (ret != (ssize_t)vSocks5Init.size()) {
302316
CloseSocket(hSocket);
303317
return error("Error sending to proxy");
304318
}
305319
char pchRet1[2];
306-
if (!InterruptibleRecv(pchRet1, 2, SOCKS5_RECV_TIMEOUT, hSocket))
307-
{
320+
if (!InterruptibleRecv(pchRet1, 2, SOCKS5_RECV_TIMEOUT, hSocket)) {
308321
CloseSocket(hSocket);
309322
return error("Error reading proxy response");
310323
}
311-
if (pchRet1[0] != 0x05 || pchRet1[1] != 0x00)
312-
{
324+
if (pchRet1[0] != 0x05) {
313325
CloseSocket(hSocket);
314326
return error("Proxy failed to initialize");
315327
}
316-
string strSocks5("\5\1");
317-
strSocks5 += '\000'; strSocks5 += '\003';
318-
strSocks5 += static_cast<char>(std::min((int)strDest.size(), 255));
319-
strSocks5 += strDest;
320-
strSocks5 += static_cast<char>((port >> 8) & 0xFF);
321-
strSocks5 += static_cast<char>((port >> 0) & 0xFF);
322-
ret = send(hSocket, strSocks5.data(), strSocks5.size(), MSG_NOSIGNAL);
323-
if (ret != (ssize_t)strSocks5.size())
324-
{
328+
if (pchRet1[1] == 0x02 && auth) {
329+
// Perform username/password authentication (as described in RFC1929)
330+
std::vector<uint8_t> vAuth;
331+
vAuth.push_back(0x01);
332+
if (auth->username.size() > 255 || auth->password.size() > 255)
333+
return error("Proxy username or password too long");
334+
vAuth.push_back(auth->username.size());
335+
vAuth.insert(vAuth.end(), auth->username.begin(), auth->username.end());
336+
vAuth.push_back(auth->password.size());
337+
vAuth.insert(vAuth.end(), auth->password.begin(), auth->password.end());
338+
ret = send(hSocket, (const char*)begin_ptr(vAuth), vAuth.size(), MSG_NOSIGNAL);
339+
if (ret != (ssize_t)vAuth.size()) {
340+
CloseSocket(hSocket);
341+
return error("Error sending authentication to proxy");
342+
}
343+
LogPrint("proxy", "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password);
344+
char pchRetA[2];
345+
if (!InterruptibleRecv(pchRetA, 2, SOCKS5_RECV_TIMEOUT, hSocket)) {
346+
CloseSocket(hSocket);
347+
return error("Error reading proxy authentication response");
348+
}
349+
if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) {
350+
CloseSocket(hSocket);
351+
return error("Proxy authentication unsuccesful");
352+
}
353+
} else if (pchRet1[1] == 0x00) {
354+
// Perform no authentication
355+
} else {
356+
CloseSocket(hSocket);
357+
return error("Proxy requested wrong authentication method %02x", pchRet1[1]);
358+
}
359+
std::vector<uint8_t> vSocks5;
360+
vSocks5.push_back(0x05); // VER protocol version
361+
vSocks5.push_back(0x01); // CMD CONNECT
362+
vSocks5.push_back(0x00); // RSV Reserved
363+
vSocks5.push_back(0x03); // ATYP DOMAINNAME
364+
vSocks5.push_back(strDest.size()); // Length<=255 is checked at beginning of function
365+
vSocks5.insert(vSocks5.end(), strDest.begin(), strDest.end());
366+
vSocks5.push_back((port >> 8) & 0xFF);
367+
vSocks5.push_back((port >> 0) & 0xFF);
368+
ret = send(hSocket, (const char*)begin_ptr(vSocks5), vSocks5.size(), MSG_NOSIGNAL);
369+
if (ret != (ssize_t)vSocks5.size()) {
325370
CloseSocket(hSocket);
326371
return error("Error sending to proxy");
327372
}
328373
char pchRet2[4];
329-
if (!InterruptibleRecv(pchRet2, 4, SOCKS5_RECV_TIMEOUT, hSocket))
330-
{
374+
if (!InterruptibleRecv(pchRet2, 4, SOCKS5_RECV_TIMEOUT, hSocket)) {
331375
CloseSocket(hSocket);
332376
return error("Error reading proxy response");
333377
}
334-
if (pchRet2[0] != 0x05)
335-
{
378+
if (pchRet2[0] != 0x05) {
336379
CloseSocket(hSocket);
337380
return error("Proxy failed to accept request");
338381
}
339-
if (pchRet2[1] != 0x00)
340-
{
382+
if (pchRet2[1] != 0x00) {
341383
CloseSocket(hSocket);
342384
switch (pchRet2[1])
343385
{
@@ -352,8 +394,7 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
352394
default: return error("Proxy error: unknown");
353395
}
354396
}
355-
if (pchRet2[2] != 0x00)
356-
{
397+
if (pchRet2[2] != 0x00) {
357398
CloseSocket(hSocket);
358399
return error("Error: malformed proxy response");
359400
}
@@ -375,13 +416,11 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
375416
}
376417
default: CloseSocket(hSocket); return error("Error: malformed proxy response");
377418
}
378-
if (!ret)
379-
{
419+
if (!ret) {
380420
CloseSocket(hSocket);
381421
return error("Error reading from proxy");
382422
}
383-
if (!InterruptibleRecv(pchRet3, 2, SOCKS5_RECV_TIMEOUT, hSocket))
384-
{
423+
if (!InterruptibleRecv(pchRet3, 2, SOCKS5_RECV_TIMEOUT, hSocket)) {
385424
CloseSocket(hSocket);
386425
return error("Error reading from proxy");
387426
}
@@ -471,7 +510,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
471510
return true;
472511
}
473512

474-
bool SetProxy(enum Network net, CService addrProxy) {
513+
bool SetProxy(enum Network net, const proxyType &addrProxy) {
475514
assert(net >= 0 && net < NET_MAX);
476515
if (!addrProxy.IsValid())
477516
return false;
@@ -489,15 +528,15 @@ bool GetProxy(enum Network net, proxyType &proxyInfoOut) {
489528
return true;
490529
}
491530

492-
bool SetNameProxy(CService addrProxy) {
531+
bool SetNameProxy(const proxyType &addrProxy) {
493532
if (!addrProxy.IsValid())
494533
return false;
495534
LOCK(cs_proxyInfos);
496535
nameProxy = addrProxy;
497536
return true;
498537
}
499538

500-
bool GetNameProxy(CService &nameProxyOut) {
539+
bool GetNameProxy(proxyType &nameProxyOut) {
501540
LOCK(cs_proxyInfos);
502541
if(!nameProxy.IsValid())
503542
return false;
@@ -513,37 +552,49 @@ bool HaveNameProxy() {
513552
bool IsProxy(const CNetAddr &addr) {
514553
LOCK(cs_proxyInfos);
515554
for (int i = 0; i < NET_MAX; i++) {
516-
if (addr == (CNetAddr)proxyInfo[i])
555+
if (addr == (CNetAddr)proxyInfo[i].proxy)
517556
return true;
518557
}
519558
return false;
520559
}
521560

522-
bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed)
561+
static bool ConnectThroughProxy(const proxyType &proxy, const std::string strDest, int port, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed)
523562
{
524-
proxyType proxy;
525-
if (outProxyConnectionFailed)
526-
*outProxyConnectionFailed = false;
527-
// no proxy needed (none set for target network)
528-
if (!GetProxy(addrDest.GetNetwork(), proxy))
529-
return ConnectSocketDirectly(addrDest, hSocketRet, nTimeout);
530-
531563
SOCKET hSocket = INVALID_SOCKET;
532-
533564
// first connect to proxy server
534-
if (!ConnectSocketDirectly(proxy, hSocket, nTimeout)) {
565+
if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout)) {
535566
if (outProxyConnectionFailed)
536567
*outProxyConnectionFailed = true;
537568
return false;
538569
}
539570
// do socks negotiation
540-
if (!Socks5(addrDest.ToStringIP(), addrDest.GetPort(), hSocket))
541-
return false;
571+
if (proxy.randomize_credentials) {
572+
ProxyCredentials random_auth;
573+
random_auth.username = strprintf("%i", insecure_rand());
574+
random_auth.password = strprintf("%i", insecure_rand());
575+
if (!Socks5(strDest, (unsigned short)port, &random_auth, hSocket))
576+
return false;
577+
} else {
578+
if (!Socks5(strDest, (unsigned short)port, 0, hSocket))
579+
return false;
580+
}
542581

543582
hSocketRet = hSocket;
544583
return true;
545584
}
546585

586+
bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed)
587+
{
588+
proxyType proxy;
589+
if (outProxyConnectionFailed)
590+
*outProxyConnectionFailed = false;
591+
592+
if (GetProxy(addrDest.GetNetwork(), proxy))
593+
return ConnectThroughProxy(proxy, addrDest.ToStringIP(), addrDest.GetPort(), hSocketRet, nTimeout, outProxyConnectionFailed);
594+
else // no proxy needed (none set for target network)
595+
return ConnectSocketDirectly(addrDest, hSocketRet, nTimeout);
596+
}
597+
547598
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed)
548599
{
549600
string strDest;
@@ -554,9 +605,7 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
554605

555606
SplitHostPort(string(pszDest), port, strDest);
556607

557-
SOCKET hSocket = INVALID_SOCKET;
558-
559-
CService nameProxy;
608+
proxyType nameProxy;
560609
GetNameProxy(nameProxy);
561610

562611
CService addrResolved(CNetAddr(strDest, fNameLookup && !HaveNameProxy()), port);
@@ -569,18 +618,7 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
569618

570619
if (!HaveNameProxy())
571620
return false;
572-
// first connect to name proxy server
573-
if (!ConnectSocketDirectly(nameProxy, hSocket, nTimeout)) {
574-
if (outProxyConnectionFailed)
575-
*outProxyConnectionFailed = true;
576-
return false;
577-
}
578-
// do socks negotiation
579-
if (!Socks5(strDest, (unsigned short)port, hSocket))
580-
return false;
581-
582-
hSocketRet = hSocket;
583-
return true;
621+
return ConnectThroughProxy(nameProxy, strDest, port, hSocketRet, nTimeout, outProxyConnectionFailed);
584622
}
585623

586624
void CNetAddr::Init()

src/netbase.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,25 @@ class CService : public CNetAddr
168168
}
169169
};
170170

171-
typedef CService proxyType;
171+
class proxyType
172+
{
173+
public:
174+
proxyType(): randomize_credentials(false) {}
175+
proxyType(const CService &proxy, bool randomize_credentials=false): proxy(proxy), randomize_credentials(randomize_credentials) {}
176+
177+
bool IsValid() const { return proxy.IsValid(); }
178+
179+
CService proxy;
180+
bool randomize_credentials;
181+
};
172182

173183
enum Network ParseNetwork(std::string net);
174184
std::string GetNetworkName(enum Network net);
175185
void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
176-
bool SetProxy(enum Network net, CService addrProxy);
186+
bool SetProxy(enum Network net, const proxyType &addrProxy);
177187
bool GetProxy(enum Network net, proxyType &proxyInfoOut);
178188
bool IsProxy(const CNetAddr &addr);
179-
bool SetNameProxy(CService addrProxy);
189+
bool SetNameProxy(const proxyType &addrProxy);
180190
bool HaveNameProxy();
181191
bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
182192
bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);

src/qt/optionsmodel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ bool OptionsModel::getProxySettings(QNetworkProxy& proxy) const
335335
proxyType curProxy;
336336
if (GetProxy(NET_IPV4, curProxy)) {
337337
proxy.setType(QNetworkProxy::Socks5Proxy);
338-
proxy.setHostName(QString::fromStdString(curProxy.ToStringIP()));
339-
proxy.setPort(curProxy.GetPort());
338+
proxy.setHostName(QString::fromStdString(curProxy.proxy.ToStringIP()));
339+
proxy.setPort(curProxy.proxy.GetPort());
340340

341341
return true;
342342
}

src/rpcmisc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Value getinfo(const Array& params, bool fHelp)
9090
obj.push_back(Pair("blocks", (int)chainActive.Height()));
9191
obj.push_back(Pair("timeoffset", GetTimeOffset()));
9292
obj.push_back(Pair("connections", (int)vNodes.size()));
93-
obj.push_back(Pair("proxy", (proxy.IsValid() ? proxy.ToStringIPPort() : string())));
93+
obj.push_back(Pair("proxy", (proxy.IsValid() ? proxy.proxy.ToStringIPPort() : string())));
9494
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
9595
obj.push_back(Pair("testnet", Params().TestnetToBeDeprecatedFieldRPC()));
9696
#ifdef ENABLE_WALLET

src/rpcnet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ static Array GetNetworksInfo()
371371
obj.push_back(Pair("name", GetNetworkName(network)));
372372
obj.push_back(Pair("limited", IsLimited(network)));
373373
obj.push_back(Pair("reachable", IsReachable(network)));
374-
obj.push_back(Pair("proxy", proxy.IsValid() ? proxy.ToStringIPPort() : string()));
374+
obj.push_back(Pair("proxy", proxy.IsValid() ? proxy.proxy.ToStringIPPort() : string()));
375+
obj.push_back(Pair("proxy_randomize_credentials", proxy.randomize_credentials));
375376
networks.push_back(obj);
376377
}
377378
return networks;

0 commit comments

Comments
 (0)