Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I connect to a Tor hidden service using cURL in PHP?

Tags:

php

curl

proxy

tor

I'm trying to connect to a Tor hidden service using the following PHP code:

$url = 'http://jhiwjjlqpyawmpjx.onion/' $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1:9050/"); curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); $output = curl_exec($ch); $curl_error = curl_error($ch); curl_close($ch);  print_r($output); print_r($curl_error); 

When I run it, I get the following error:

Couldn't resolve host name

However, when I run the following command from my command line in Ubuntu:

curl -v --socks5-hostname localhost:9050 http://jhiwjjlqpyawmpjx.onion 

I get a response as expected.

The PHP cURL documentation says this:

--socks5-hostname Use  the  specified  SOCKS5 proxy (and let the proxy resolve the host name). 

I believe the reason it works from the command line is because Tor (the proxy) is resolving the .onion hostname, which it recognizes. When running the PHP code above, my guess is that cURL or PHP is trying to resolve the .onion hostname and doesn't recognize it. I've searched for a way to tell cURL/PHP to let the proxy resolve the hostname, but I can't find a way.

There is a very similar Stack Overflow question, cURL request using socks5 proxy fails when using PHP, but it works through the command line.

like image 372
frosty Avatar asked Mar 16 '13 03:03

frosty


People also ask

How can I connect to Tor hidden service using Curl in PHP?

php $ch = curl_init('http://jhiwjjlqpyawmpjx.onion'); // Tormail URL curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); curl_setopt($ch, CURLOPT_PROXY, "localhost:8118"); // Default privoxy port curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); curl_exec($ch); curl_close($ch); ?>

How does curl work in PHP?

cURL is a PHP library and command-line tool (similar to wget) that allows you to send and receive files over HTTP and FTP. You can use proxies, pass data over SSL connections, set cookies, and even get files that are protected by a login.

Is PHP Curl secure?

Curl is as secure as a normal HTTP request.


2 Answers

You need to set option CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME, which sadly wasn't defined in old PHP versions, circa pre-5.6; if you have earlier in but you can explicitly use its value, which is equal to 7:

curl_setopt($ch, CURLOPT_PROXYTYPE, 7); 
like image 155
dr.scre Avatar answered Sep 18 '22 16:09

dr.scre


I use Privoxy and cURL to scrape Tor pages:

<?php     $ch = curl_init('http://jhiwjjlqpyawmpjx.onion'); // Tormail URL     curl_setopt($ch, CURLOPT_HEADER, 1);     curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);     curl_setopt($ch, CURLOPT_PROXY, "localhost:8118"); // Default privoxy port     curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);     curl_exec($ch);     curl_close($ch); ?> 

After installing Privoxy you need to add this line to the configuration file (/etc/privoxy/config). Note the space and '.' a the end of line.

forward-socks4a / localhost:9050 . 

Then restart Privoxy.

/etc/init.d/privoxy restart 
like image 45
FattyPotatoes Avatar answered Sep 17 '22 16:09

FattyPotatoes