⬆️ ⬇️

Anonymous chat TorChat and its improvement

TorChat



TorChat is an anonymous, cross-platform instant messenger that uses the Tor network and encrypts correspondence. This article describes the protocol used by TorChat, and the improvements made to the TorChat implementation in Python.



Introduction, description of Tor and hidden service



Tor's anonymity system, which often pops up in the media, serves to anonymously visit and create websites. Anyone can get a “domain” of the form test3unszyhvy7um.onion and in a few seconds this site will be available for all users of the Tor network to visit. To create a domain, an RSA key is created, from the public part of which the hash sum is calculated (in the case of this domain, it is equal to test3unszyhvy7um). Tor remembers matching a domain name to a public key in the DHT. This site is called hidden service. Tor creates a TCP connection between the client and hidden service. Through such a connection, you can skip different protocols: HTTP (S), SSH, IRC, Bitcoin, and others. A description of one of these protocols, TorChat, is in the next section.



The algorithm for connecting to hidden service is not directly related to the topic of the article, its consideration deserves a separate article. So far I will note important points. The hidden service domain name cannot be selected without access to the private part of the RSA key. It is impossible to listen to the channel between the client and hidden service or replace the data on this channel. It is impossible to find the IP address where the hidden service is running or the IP address of its client.

')

TorChat protocol



This section is based on the tc_client.py file. There are enough comments in the file, but there is no protocol in text form.



Clients interact directly through Tor, there are no servers in the TorChat system. The client starts its own Tor process or uses an already running Tor, controlling it via the Control port.



Each user has his own hidden service with a domain name like abc.onion, on which he listens to port 11009. The first part of the name (abc) has a length of 16 characters, may consist of characters 234567abcdefghijklmnopqrstuvwxyz (base32) and serves as a TorChat ID. Each user has the ability to connect to other users through their TorChat ID.



Tor guarantees that only the creator, the holder of the corresponding key, can control this domain name. However, there is no information about who connects to the hidden service, so the authentication includes the creation of a reverse connection. So let's say Alice (alice.onion) connects to Bob (bob.onion). To do this, Alice sends Bob a message like "ping alice <random string from Alice>". Bob sends Alice "ping bob <random string from Bob>" and "pong <random string from Alice>". Alice replies "pong <random string from Bob>". The parties compare sent and received random strings. Matching the lines confirms that the incoming connection is really from who it "presents". Thus, both have a pair of sockets (incoming and outgoing) and confidence that the incoming socket is from the same person the outgoing is sent to. Socket messages are transmitted only in one direction (with the exception of file transfers that are sent in the opposite direction, so as not to compete with text transmission).



TorChat protocol message scheme:

 <command> <encoded> \ n


The command can contain only lowercase Latin letters and underscores. Encoded consists of any characters except the end of line character. The end of line character is replaced by “\” and “n”. Previously, "\" is replaced by "\" and "\".



List of commands:





Implementations



I managed to find 4 implementations of the TorChat client.



Python torchat_py from Prof7bit (Bernd Kreuss, Hannover, Germany), 2007. The first implementation. Now it is in the torchat_py repository on github.

torchat2 on Lazarus + Free Pascal by Prof7bit. New implementation, 2012. Simplified to run multiple instances on the same machine. The kernel is completely separate from the GUI, which allows you to run, including without a GUI. Implemented a plugin for the Purple library, which is used by IM-clients Pidgin and Finch. Uses just one execution thread per program. Python's creates multiple threads for each contact.

TorChat for Max OS X from Julien-Pierre Avérous, France. In 2010, it was written in C ++, in 2013, the code was uploaded to github , then from C ++, they moved to Objective-C. It is possible to make private notes about the interlocutor or block the interlocutor. There is a multiplayer chat.

jTorchat in Java from daux2a. Written in 2012. File transfer is not implemented. Added a broadcast mode that allows you to send messages to all users of the TorChat network, even those who are not in the contact list. Implemented a random chat request from the network.



Representation in official distributions


The distributions of Gentoo, Debian, OpenSuse, Fedora and Windows have been studied.

At the moment TorChat is included only in the Debian distribution.

Package Page: packages.debian.org/wheezy/torchat



Python implementation



Consider the implementation of TorChat in Python version 0.9.9.553 .



When sending messages to a recipient who is not online, these messages are stored locally and sent with the [delayed] prefix when the recipient appears on the network at the same time as the sender. The sender is notified [delayed messages have been sent].



If you leave TorChat turned on for a week and do not use it, you’ll get about 50 megabytes of outgoing and 100 incoming traffic. Creating a new account occurs instantly (RSA key generation time), the first activation takes half a minute. Subsequent activations occur in a couple of seconds. Apparently, during the first activation, time is spent on the “first acquaintance” of the Tor program with the Tor network.



When creating a new account TorChat, it is automatically added to the contacts to itself under the nickname self. This is useful for many reasons. Firstly, the status of this contact shows whether our account is online. Secondly, you can quickly copy your TorChat ID (right mouse button - Copy ID to clipboard). Third, by “chatting” with you, you can estimate the network latency. Usually ping about 1 second. Fourthly, this contact is convenient to use in plugins, for example in a conference (see below).



The portable.txt file is in the program folder. If it is, the program folder is used to store configs. Otherwise, the folder ~ / .torchat or ~ / .torchat_ <account name> is used. The account name is supplied as a command line argument. The config includes the buddy-list.txt and torchat.ini files and the Tor folder with an RSA key.



Of interest are the modules tc_client.py (kernel), tc_gui.py (GUI), dlg_settings.py (settings window), config.py (settings storage).



In the tc_client.py file there are classes:



The tc_gui.py file contains the wxPython code. Of the interesting classes: ChatWindow (chat window), MainWindow (main window, contains a link to BuddyList).



There is only one class in the dlg_settings.py file, Dialog, which is responsible for the settings window. Adding your items to the settings window from the plugin is done by changing the addPluginSettings method (the method is added to my fork).



The config.py file contains the set and get wrappers for the ConfigParser from the standard Python library. In the same file are the values ​​of the default settings (config_defaults). Settings are stored in the file torchat.ini.



The contact list is stored in the buddy-list.txt file in a table format (torchat_id [local nickname]).



Translations are implemented as lang_xx.py files, where xx is the language code. The files are in the translations folder. Each text for translation is stored in a variable referenced from the rest of the program. Non-standard option, but convenient when writing plug-ins: you just need to create the necessary variables in the appropriate modules. TorChat is translated into many languages, including Russian (2011, translator: SB14.org, RusInfo.cc).



Python implementation improvements



People complained that TorChat lacks some functions. Some of them, such as the ability to run multiple instances, are very simple to implement. It is strange that the author did not immediately make them. Others (for example, multi-user chats) are not so simple, but they can be implemented without changing the protocol (the method was described in Habré). Finally, there are things that require protocol changes, such as voice communication. I'm not at all sure that its implementation is possible, taking into account the delays of the Tor network. Finally, there are things that probably will not work. These things include video calling.



When a critical mass of requests to TorChat had accumulated, I began to think about how to get these functions. Unfortunately, the main developer is currently inactive, he is not in TorChat and cannot be contacted by mail. I didn’t want to write on Pascal, Java, Objective-C or Max OS X, so I relied on the old Python implementation, contrary to the author’s preferences. Python and the implementation of TorChat in Python have proven to be very convenient for writing plugins and making improvements. An additional argument was that it was the Python implementation that was included in at least one distribution kit, Debian.



When I began to study the source code, at first it seemed to me that it was not for nothing that they said that the code was of poor quality. Apparently, the description of the protocol contributed to this exclusively in the source code. However, then I looked and saw that the protocol and implementation were carried out conscientiously. I daresay that the author, when he wrote this code a few years ago, did not have much experience with Python. In some places you can meet <>, which speaks of the "Baysik" or "Pascal" past. However, the code is written to fame.



I forked the repository on github and pretty quickly did everything I wanted. What was done:





What's next?







UPD . Interview with Bernd Kreuss, author of the stick, August 2013.

Source: https://habr.com/ru/post/201696/



All Articles