Tor is a free software that prevents people from learning your location or browsing habits by letting you communicate anonymously on the Internet. This article outlines the process of installing TOR on a Centos/RHEL 7 server and shows how to communicate programmatically through TOR network by using cURL in PHP.
Over the years, I have seen TOR installed in a variety of different environments, but I think it's important to mention that outside of using Tor Browser for anonymous web browsing, there are various other ways of using TOR. In the following article, I show how to use the TOR network in a server setup, where TOR is installed as a service; which allows for programmatically configuration of Internet application in such a way that they can communicate with the outside world by sending and receiving the Internet traffic through TOR network.
For applications that require enhanced traffic anonymity, this is likely the easiest way to achieve the goal.
In Centos 7, run the following two commands to install TOR service:
- yum install tor (to install Tor service)
- systemctl start tor (to start Tor service)
Tor will create a proxy at http://localhost:9050 which can receive and forward traffic from my server to Tor network nodes.
In the next step, I wrote a simple PHP Curl function, which emulates a Chrome browser and retrieves the content of any Internet page through Tor service. This is what it looked like:
The next step was to execute the above function in PHP. But to test that I am truly programmatically accessing the remote content through Tor service, I’ve decided to test it on an API that shows which IP address am I connecting to Internet from. The API that shows external IP address is available from https://api.ipify.org?format=json So, I have executed the function first without going through Tor service, like this. Note that the zero passed to a function will ensure that I will not use Tor:
It worked. My current local IP address is the output of the function from the server:
Checking the IP’s location at https://www.iplocation.net/ confirmed that it’s my home address:
Now, decided to run the same command, but by leveraging the Tor service, like this:
Note the '1' at the end, which enables the Tor proxy in PHP Curl function.
That leads to a different result, the IP address shown in the result is as follows:
So, to see where Tor is taking me, I’ve also checked this IP’s location at https://www.iplocation.net/, this was the output:
This is a solid proof that our application traffic is going through a Tor node located in Paris, France.
This example shows, that it’s fairly easy to programmatically anonymize the traffic through Tor and if we had a requirement to send the information to and from our application, we could easily do so by using TOR’s safety services and anonymity of TOR nodes.
--
Here is the code of the PHP cURL/TOR function for those who'd like to further experiment with it:
function get_data($url,$useTor) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
if ($useTor==1) {
curl_setopt($ch, CURLOPT_PROXY, 'http://localhost:9050');
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
}
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
It can be used like this:
$useTor = 1; // 1 - use TOR | 0 - do not use TOR
$url = "https://www.bing.com";
$html = get_data($url,$useTor);