Search code examples
bashshellcurltortorsocks

Bash TorSocks Issue


In theory, my goal with this script is to randomly generate .onion urls and check whether they exist or not. Basically, the status variable is sending a HEAD request then returning the response. head grabs the first line from the response then grep searches for the HTTP header within that first line sending the output to /dev/null. If all goes well, status should be set to true. I use an if statement to check if status is true. If it is true, then I output "this service exists" while if status is not true, "this service does not exist" is outputted.

The problem is, torsocks returns an error if the onion url does not exist.

The error is:

[Jul 03 17:09:23] ERROR torsocks[4422]: General SOCKS server failure (in socks5_recv_connect_reply() at socks5.c:516)

I verified that the command does indeed work on legitimate existing onion urls meaning that the error is something to do with torsocks quite obviously.

Anyone know of a solution or possible fix? I could not find anything helpful.

Here's my code:

#!/bin/bash

url="http://"$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-z0-9' | fold -w 16 | head -n 1)".onion";
echo "[i] Scanning $url";
status=$(torsocks curl -s --head $url | head -n 1 | grep "HTTP/1.[01] [23].." > /dev/null);
if $status; then
    echo "[+] Valid hidden service $url";
else
    echo "[-] Invalid hidden service $url";
fi

All help is greatly appreciated.


Solution

  • You're never assigning status any non-empty value at all.

    value=$(something >/dev/null)
    

    ...throws away the output of something (that's what the >/dev/null does), and thus will always unconditionally assign an empty string to value (since $(...) -- command substitution -- captures output, not exit status).

    Moreover, you aren't throwing away error messages, because those are on stderr (FD 2), not stdout (FD 1).


    Making some guesses at intent, perhaps you want instead:

    if torsocks curl --fail --head "$url" >/dev/null 2>&1; then
        echo "[+] Valid hidden service $url";
    else
        echo "[-] Invalid hidden service $url";
    fi