I have this code:
<?php
$ip = '127.0.0.1';
$port = '9051';
$auth = 'PASSWORD';
$command = 'signal NEWNYM';
$fp = fsockopen($ip,$port,$error_number,$err_string,10);
if(!$fp) { echo "ERROR: $error_number : $err_string";
return false;
} else {
fwrite($fp,"AUTHENTICATE \"".$auth."\"
");
$received = fread($fp,512);
fwrite($fp,$command."
");
$received = fread($fp,512);
}
fclose($fp);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://whatismyip.org");
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9050");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$response = curl_exec($ch);
?>
The problem is it doesn't work. I am running on windows not linux. And I downloaded the tor browser. I understand there was supposed to be some configuration where I set a password and a port. However that did not happen during the install.
Is there anyone familiar with tor. What am I missing?
By default Tor does not listen on the control port, and you will also need to configure a password or specify it use CookieAuthentication. If you are going to be scripting with Tor, you may want to download the expert bundle instead of the browser bundle.
These values need to go in the torrc
file in the Tor Data
directory. See the default sample torrc.
First, generate a hashed password (note piping to more
is necessary on Windows or you won't see any output):
C:\Path\To\Tor>tor.exe --hash-password PASSWORD|more
Copy the output that looks like 16:*BUNCH_OF_HEX_DIGITS*
Next, you need to edit your config and add or uncomment the line with ControlPort 9051
and add your hashed password.
ControlPort 9051
HashedControlPassword 16:YOUR_HASHED_PASSWORD_HERE
Then restart the daemon, make sure it is using the config file with these values, and try your code again.
See the docs on the ControlPort and HashedControlPassword settings.
Perhaps overkill for what you are doing, but you may also be interested in this PHP Tor library I made which you can use to interact with the Tor control port.
Using my library your code would look like:
<?php
use Dapphp\TorUtils\ControlClient;
use Dapphp\TorUtils\ProtocolError;
$tc = new ControlClient();
try {
$tc->connect(); // connect to 127.0.0.1:9051
$tc->authenticate(); // uses cookie authentication, can also use $tc->authenticate('password_here');
} catch (\Exception $ex) {
echo "Failed to create Tor control connection: " . $ex->getMessage() . "
";
exit;
}
try {
echo "Sending NEWNYM signal to controller...";
$tc->signal(ControlClient::SIGNAL_NEWNYM);
echo "OK";
} catch (ProtocolError $pe) {
echo $pe->getMessage();
}
$tc->quit(); // close control connection
Note that in C:\Path\To\Tor>tor.exe --hash-password PASSWORD|more
, "PASSWORD" needs to be replaced like the password you like. Suppose you use "my_password" as your password. Then use: C:\Path\To\Tor>tor.exe --hash-password my_password|more
to generate a hashed password In your code to switch identity, use $auth = '"my_password"'; Yes, with double quotes