How to setup a Tor hidden service?
Tags: todayilearned, tor, python, webdev
Published: 2020-06-17 08:04:49

I'm gonna out on a limb here and assume that you know the basics of how a web server works when you clicked on the title of this post. Basic knowledge of vim would be helpful too.

For help if you get stuck, look at the **Note** at the bottom of this post.

This tutorial is specifically aimed for Unix users but anyone can follow this in all honesty. Yeah, even the Windows users!👌🏻

Alright, so first of all you wanna make sure that you have a web server up and running. You can use Apache2 but for better security and anonymity, I'd recommend that you use Nginx. It's pretty cool :P

### Step 1 - Setting up the Nginx server
To check for its installation, type `nginx` into the terminal. And head to `localhost:8080` in your preferred web browser.

If you don't have it installed, you can go ahead and install it using `brew install nginx` on your macOS or `sudo apt-get install nginx` on your WSL or Linux distro with Debian packages.

Once you have it installed, run it using `nginx` and head to the `localhost:8080` in your browser. If it shows something similar to :

![ss](https://user-images.githubusercontent.com/41021374/84872867-0ac28e80-b0a0-11ea-966b-2c9031c5a521.jpeg)

Once you are sure that Nginx is installed and runs fine, move on to the next step.

### Step 2 - Configuring the Nginx config file *( Optional )*

Let's say you are already hosting something on localhost:8080 and don't want to set up Nginx on that port, you'd want to change the Nginx config file in that case. 

`arthtyagi@MacBook-Pro ~ % cd /usr/local/etc/nginx `
`arthtyagi@MacBook-Pro ~ % vim nginx.conf`

( Note: If you don't like how your vim is, check [this vimconfig](https://github.com/arthtyagi/vimconfig) out. )

``` vim
 84     #server {
 85     #    listen       8000;
 86     #    listen       somename:8080;
 87     #    server_name  somename  alias  another.alias;
 88 
 89     #    location / {
 90     #        root   html;
 91     #        index  index.html index.htm;
 92     #    }
 93     #}

```
Uncomment these lines to open port 8000 for Nginx and while you are at it, don't forget to comment out port 8080 lines cause you don't want Nginx listening on two ports, that just doesn't work.

Once you're satisfied with your configuration, move on to Step 3.

### Step 3 - Serving static content.

`arthtyagi@MacBook-Pro ~ % cd /usr/local/var/www`
`arthtyagi@MacBook-Pro ~ % vim index.html`

This opens the index.html file in vim, edit it however you want. Add css, put the files appropriately though. If an issue occurs while serving static content ( probably won't ), feel free to lookup [Stackoverflow](https://stackoverflow.com).

Now make sure your Nginx server is up and running and visit, `localhost:8080` in your browser.

### Step 4 - Serving on the Dark Web as a .onion site

First, you wanna make sure that you have `tor` installed on your system.

Type in `tor` in your terminal, if it initiates a tor connection, you've got tor installed. Else, you might really wanna install it. Use `brew install tor` to install tor on your MacOS.

For Debian/Ubuntu/WSL with Debian/Ubuntu refer to [this](https://2019.www.torproject.org/docs/debian.html.en).

Once you have made sure it's installed, move on to Step 5.

### Step 5 - Configuring the torrc

1. Head into the tor directory. For MacOS, it's available here :
`arthtyagi@MacBook-Pro ~ % cd /usr/local/etc/tor`

2. If you list the files present, you'll find that there is a file named `torrc.sample` present in the tor directory. Now you'd want to change `torrc.sample` to `torrc`. You can do that by simply opening the `torrc.sample` in vim and using the vim command, `:!mv torrc.sample torrc`.

3. Edit the torrc file using `vim torrc`.

Add these lines :
``` vim
 79 HiddenServiceDir /usr/local/etc/tor/hidden_http_service/
 80 HiddenServicePort 80 127.0.0.1:8080
```

### Final Step : Run the Service 

`arthtyagi@MacBook-Pro ~ % tor `

Your site is serving over Tor relay now. But where?

`arthtyagi@MacBook-Pro ~ % cat /usr/local/etc/tor/hidden_http_service/`

You will see a .onion address, that's where your onion service is live at.

### Additional Step : Read into these

[Operational Security](https://2019.www.torproject.org/docs/tor-onion-service.html.en#OpSec) and [Next Gen v3](https://2019.www.torproject.org/docs/tor-onion-service.html.en#four) options.

**NOTE :** 
1. To stop the Nginx server, use `sudo nginx -s stop`.
2. To stop Tor, use `killall tor`.
3. To save and exit in vim, use `:x`.

Either way, I'm glad you read this to the end ( hopefully lol ).