Maria Sophia wrote:
> Best is a set of nodes for profile1, another set for profile 2, etc.
> That way, it looks "normal" to the social (or throwaway email) web site.
I'm getting tired after investing a few hours fixing the problems with the
Firefox Tor browser when it comes to choosing exit nodes on the fly.
The code I hacked out below works on my Windows system, but if we want to
port it to Linux, we likely will have to port it to Python first.
The only reason it was written first in Powershell was that the initial
tests (version 1.0) used Powershell, and then it just grew from there.
Anyway, if the Firefox Tor browser is running, this will kill it and then
pop up a query for us to choose which exit nodes we want and then the
script will run the Firefox Tor browser using the selected exit nodes.
As far as I'm aware, this functionality doesn't exist on the net anywhere
but in this post as I didn't want to write it but I couldn't find it.
=== cut here for typical output ===
torset.bat
----------------------------------------
Tor exit node selector
----------------------------------------
1 for Profile 1 (two possible exit nodes)
2 for Profile 2 (one exit node)
3 for Profile 3 (one exit node)
Select profile [1,2,3] or Q to quit: 1
Setting Profile 1...
Ensuring clean state by stopping any existing Tor instances...
Tor Browser is not running. Starting Tor Browser automatically...
Waiting for Tor Browser to initialize and open ControlPort (9151)...
Tor Browser port is open.
Authentication: 250 OK
Waiting for Tor bootstrap to complete...
Tor bootstrap complete (100).
Exit configuration: 250 ExitNodes={US}
SETCONF response: 250 OK
NEWNYM response: 250 OK
New exit configuration: 250 ExitNodes=$688604204AF0C0473DFFA8CD2E186D1EEAEE6916,$B8FA90A8B7DA9B90A09AA04CD2E81E263AA8DA4C
ExitNodes verification: Match
General-purpose circuits
-------------------------
Circuit: 8
Exit fingerprint: 634B891DAF74FB17C94C4AA73CB394CE6B2531F1
Profile comparison: Not in profile
Circuit: 18
Exit fingerprint: 688604204AF0C0473DFFA8CD2E186D1EEAEE6916
Profile comparison: Match
Circuit inspection complete.
Tor control session completed successfully.
torset completed successfully.
Press any key to continue . . .
=== cut here for typical output ===
:: ---------------------------------------------------------------------
:: torset.bat
:: Manually set known-good Tor exit nodes for specific web-login profiles
:: Should be ported to python so that it can be ported to Linux as needed
:: ---------------------------------------------------------------------
:: Usage notes:
:: 1. Obtain desired US Tor exit relays:
:: https://metrics.torproject.org/rs.html#search/country:us%20flag:exit
:: 2. Enter their fingerprints in the profile definitions below.
:: 3. Run this script. It cleans up stale sessions, starts Tor, and sets nodes.
:: 4. Allow Tor a bit of time to establish new circuits.
:: 5. Verify the exit relay and exit IP before logging in.
:: ---------------------------------------------------------------------
:: v2p4 20261108 Handled stale process cleanup cleanly in native batch
:: v2p3 20261108 Added automated cleanup of stale Tor/Firefox instances
:: v2p2 20261108 Added full bootstrap completion polling on cold start
:: v2p1 20261108 Fixed $tcp variable scope in try/finally block
:: v2p0 20261108 Added automated Tor Browser startup and port polling
:: v1p9 20261108 Added ControlPort error handling and response validation
:: v1p8 20261108 Cleaned up comments and clarified explanations
:: v1p7 20261108 Identify general-purpose ckts and compare exit fingerprints
:: v1p6 20261108 Display multi-line Tor circuit status via GETINFO
:: v1p5 20261108 Request new Tor circuits after profile selection
:: v1p4 20261108 Added multi-profile exit-node selection menu
:: v1p3 20261108 Set Profile 1 ExitNodes (two fingerprints) via SETCONF
:: v1p2 20261108 Added GETINFO status/bootstrap-phase
:: v1p1 20261108 Retrieve current ExitNodes configuration via GETCONF
:: v1p0 20261108 Tested Tor ControlPort via TCP cookie authentication
:: ---------------------------------------------------------------------
:: Summary:
:: 1. Stop any stale Tor Browser/Firefox instances for a clean start.
:: 2. Launch Tor Browser automatically if not running.
:: 3. Wait for Tor's ControlPort (9151) and 100% bootstrap completion.
:: 4. Connect to Tor's local ControlPort (9151).
:: 5. Authenticate using Tor's control_auth_cookie.
:: 6. Read the current ExitNodes configuration.
:: 7. Apply the selected profile's permitted exit fingerprints.
:: 8. Request new circuits using SIGNAL NEWNYM.
:: 9. Confirm the selected ExitNodes configuration.
:: 10. Check Tor's bootstrap status and inspect circuits.
::
:: Stop and report an error if a required operation fails.
:: This does not prove which circuit a particular website uses.
:: ---------------------------------------------------------------------
@echo off
:: Profile selection
echo ----------------------------------------
echo Tor exit node selector
echo ----------------------------------------
echo.
echo 1 for Profile 1 (two possible exit nodes)
echo 2 for Profile 2 (one exit node)
echo 3 for Profile 3 (one exit node)
echo.
choice /c 123Q /n /m "Select profile [1,2,3] or Q to quit: "
if errorlevel 4 exit /b
if errorlevel 3 goto profile3
if errorlevel 2 goto profile2
goto profile1
:profile1
set "PROFILE=Profile 1"
set "EXITNODES=$688604204AF0C0473DFFA8CD2E186D1EEAEE6916,$B8FA90A8B7DA9B90A09AA04CD2E81E263AA8DA4C"
goto run
:profile2
set "PROFILE=Profile 2"
set "EXITNODES=$BD610AAEAE179EC259B022A178A75C70042407FB"
goto run
:profile3
set "PROFILE=Profile 3"
set "EXITNODES=$9C7B1DB2C5520B21E181EB945A01E9ABA42654A2"
goto run
:run
echo.
echo Setting %PROFILE%...
:: Clean up any existing stale Tor/Firefox instances first
echo Ensuring clean state by stopping any existing Tor instances...
taskkill /f /im firefox.exe >nul 2>&1
timeout /t 2 /nobreak >nul
:: begin powershell block, later to be converted to python
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$ErrorActionPreference = 'Stop'; ^
$tcp = $null; ^
try { ^
$path = 'C:\app\browser\tbb\Tor Browser\Browser\TorBrowser\Data\Tor\control_auth_cookie'; ^
$torExe = 'C:\app\browser\tbb\Tor Browser\Browser\firefox.exe'; ^
^
if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { ^
Write-Host 'Tor Browser is not running. Starting Tor Browser automatically...'; ^
if (-not (Test-Path -LiteralPath $torExe)) { throw 'Tor Browser executable not found at: ' + $torExe }; ^
Start-Process -FilePath $torExe; ^
Write-Host 'Waiting for Tor Browser to initialize and open ControlPort (9151)...'; ^
$sw = [Diagnostics.Stopwatch]::StartNew(); ^
$ready = $false; ^
while ($sw.ElapsedMilliseconds -lt 45000) { ^
if (Test-Path -LiteralPath $path) { ^
try { ^
$testTcp = New-Object System.Net.Sockets.TcpClient; ^
$testTcp.Connect('127.0.0.1', 9151); ^
$testTcp.Close(); ^
$ready = $true; ^
break; ^
} catch { } ^
}; ^
Start-Sleep -Seconds 2; ^
}; ^
$sw.Stop(); ^
if (-not $ready) { throw 'Timed out waiting for Tor Browser to start and open ControlPort 9151.' }; ^
Write-Host 'Tor Browser port is open.'; ^
}; ^
^
if ([string]::IsNullOrWhiteSpace($env:EXITNODES)) { throw 'No exit nodes defined for this profile.' }; ^
$cookie = [BitConverter]::ToString([IO.File]::ReadAllBytes($path)).Replace('-', ''); ^
$tcp = New-Object System.Net.Sockets.TcpClient; ^
$tcp.ReceiveTimeout = 15000; ^
$tcp.SendTimeout = 15000; ^
$tcp.Connect('127.0.0.1', 9151); ^
$stream = $tcp.GetStream(); ^
$stream.ReadTimeout = 15000; ^
$writer = New-Object System.IO.StreamWriter($stream); ^
$reader = New-Object System.IO.StreamReader($stream); ^
$writer.NewLine = [char]13 + [char]10; ^
$writer.AutoFlush = $true; ^
^
function Read-TorReply { ^
$lines = New-Object 'System.Collections.Generic.List[string]'; ^
while ($true) { ^
$line = $reader.ReadLine(); ^
if ($null -eq $line) { throw 'Tor closed the ControlPort connection unexpectedly.' }; ^
[void]$lines.Add($line); ^
if ($line -match '^250\+') { ^
do { ^
$dataLine = $reader.ReadLine(); ^
if ($null -eq $dataLine) { throw 'Tor closed the connection during a data response.' }; ^
[void]$lines.Add($dataLine); ^
} while ($dataLine -ne '.'); ^
continue ^
}; ^
if ($line -match '^\d{3}-') { continue }; ^
break ^
}; ^
return $lines.ToArray() ^
}; ^
^
function Send-TorCommand($command) { ^
$writer.WriteLine($command); ^
$reply = @(Read-TorReply); ^
if ($reply.Count -eq 0 -or $reply[0] -notmatch '^250[ +\-]') { ^
throw ('Tor rejected command: ' + $command + ' -- ' + ($reply -join ' ')) ^
}; ^
return ,$reply ^
}; ^
^
$auth = Send-TorCommand ('AUTHENTICATE ' + $cookie); ^
Write-Host ('Authentication: ' + $auth[0]); ^
^
Write-Host 'Waiting for Tor bootstrap to complete...'; ^
$bootstrapComplete = $false; ^
$bSw = [Diagnostics.Stopwatch]::StartNew(); ^
while ($bSw.ElapsedMilliseconds -lt 60000) { ^
try { ^
$bootstrap = Send-TorCommand 'GETINFO status/bootstrap-phase'; ^
foreach ($line in $bootstrap) { ^
if ($line -match 'PROGRESS=100') { ^
$bootstrapComplete = $true; ^
break; ^
} ^
}; ^
if ($bootstrapComplete) { break }; ^
} catch { } ^
Start-Sleep -Seconds 2; ^
}; ^
$bSw.Stop(); ^
if (-not $bootstrapComplete) { throw 'Timed out waiting for Tor to finish bootstrapping.' }; ^
Write-Host 'Tor bootstrap complete (100%).'; ^
^
$config = Send-TorCommand 'GETCONF ExitNodes'; ^
Write-Host ('Exit configuration: ' + ($config -join ' ')); ^
$setconf = Send-TorCommand ('SETCONF ExitNodes=' + $env:EXITNODES); ^
Write-Host ('SETCONF response: ' + $setconf[-1]); ^
$newnym = Send-TorCommand 'SIGNAL NEWNYM'; ^
Write-Host ('NEWNYM response: ' + $newnym[-1]); ^
Start-Sleep -Seconds 3; ^
^
$config = Send-TorCommand 'GETCONF ExitNodes'; ^
Write-Host ('New exit configuration: ' + ($config -join ' ')); ^
$actualLine = $null; ^
foreach ($item in $config) { ^
if ($item -match '^250 ExitNodes=') { $actualLine = $item; break } ^
}; ^
if (-not $actualLine) { throw 'Could not verify the new ExitNodes configuration.' }; ^
^
$actualNodes = @(); ^
$actualText = $actualLine -replace '^250 ExitNodes=', ''; ^
foreach ($fp in ($actualText -split ',')) { ^
$actualNodes += $fp.Trim().TrimStart('$').ToUpperInvariant() ^
}; ^
^
$expectedNodes = @(); ^
foreach ($fp in ($env:EXITNODES -split ',')) { ^
$expectedNodes += $fp.Trim().TrimStart('$').ToUpperInvariant() ^
}; ^
if ($actualNodes.Count -ne $expectedNodes.Count) { throw 'ExitNodes verification failed: fingerprint count differs.' }; ^
foreach ($fp in $expectedNodes) { ^
if (-not ($actualNodes -contains $fp)) { throw ('ExitNodes verification failed: missing fingerprint ' + $fp) } ^
}; ^
Write-Host 'ExitNodes verification: Match'; ^
^
$allowed = @{}; ^
foreach ($fp in $expectedNodes) { ^
$allowed[$fp] = $true ^
}; ^
^
$circuitReply = Send-TorCommand 'GETINFO circuit-status'; ^
Write-Host ''; ^
Write-Host 'General-purpose circuits'; ^
Write-Host '-------------------------'; ^
$found = $false; ^
foreach ($line in $circuitReply) { ^
if ($line -notmatch '^\d+\s+BUILT\s+') { continue }; ^
if ($line -notmatch 'PURPOSE=GENERAL') { continue }; ^
$fields = $line -split '\s+'; ^
if ($fields.Count -lt 3) { continue }; ^
$hops = $fields[2] -split ','; ^
$last = ($hops[-1] -split '~')[0].TrimStart('$').ToUpperInvariant(); ^
$found = $true; ^
Write-Host ''; ^
Write-Host ('Circuit: ' + $fields[0]); ^
Write-Host ('Exit fingerprint: ' + $last); ^
if ($allowed.ContainsKey($last)) { ^
Write-Host 'Profile comparison: Match' ^
} else { ^
Write-Host 'Profile comparison: Not in profile' ^
}; ^
}; ^
if (-not $found) { ^
Write-Host 'No built general-purpose circuits found.' ^
}; ^
Write-Host ''; ^
Write-Host 'Circuit inspection complete.'; ^
Write-Host 'Tor control session completed successfully.'; ^
} catch { ^
Write-Host ''; ^
Write-Host ('Error: ' + $_.Exception.Message) -ForegroundColor Red; ^
exit 1 ^
} finally { ^
if ($tcp) { $tcp.Close() } ^
}"
:: end of powershell block
if errorlevel 1 (
echo.
echo torset did not complete successfully.
) else (
echo.
echo torset completed successfully.
)
pause
:: ---------------------------------------------------------------------
:: end of torset.bat
:: ---------------------------------------------------------------------
--
Usenet should be a discussion where everyone adds value to the topic.