We raise the site in Tor in a couple of seconds

Hacker

Professional
Messages
1,046
Reputation
9
Reaction score
743
Points
113
To many people who are not very well versed in the IT field, it seems that it is very difficult and hemorrhoid to create and raise your own website in the torus. Nevertheless, this article is recognized to dispel this myth and prove that even a person who first installed Linux on a USB flash drive is already able to make himself a simple site that will work and function normally.

All we need is C / Go compilers, a Linux operating system (some UNIX like it will do), a make program, and a tor package. With all these means, we can start cooking.

Here, the code will be painted accordingly, which will help automate the creation of the site and making changes to the torus configuration file. This code will be written in C language.

In the Go programming language, we will create a server program directly.

This article is not documentation of this kind that I will explain the code I have written.
Here you yourself will have to analyze the code and try to understand what is written.

If you do not know programming languages, but want to learn at least some, then do not waste time and learn. Fortunately, there is plenty of information on the Internet in many languages.
If you already know at least the basics of my chosen languages, then welcome to the world where you will analyze the code yourself.
If you are in NULL programming or do not know the languages of my choice, then do not worry, I hope it will not be a difficult task to learn the two key combinations Ctrl + C and Ctrl + V.

Well, let's get started.
The first thing we need to do is think about the architecture of the program.
I personally split the program into five component parts:
Code:
1. [Preparation | backgr.c] Create directories and initial files
2. [Configuration | confgr.c] Add torrc and start tor services
3. [Results | result.c] Get the hostname of the file
4. [Launch | wakeup.c] Raising a site in the tor network
5. [Bundle | main.c] A bunch of the above programs


In addition to the .c files, there will also be .h header files:
Code:
1. [Constants | macro.h]
2. [Data types | types.h]
3. [Bundle of functions | onion.h]

The code of these header files is tiny, so we can look at it right away:

File: macro.h
Code:
#pragma once

#define BUFF_SIZE     1024
#define MAX_LENGTH     128

#define PORT         "80 "
#define IP_PORT        "127.0.0.1:80"

#define HIDDEN_DIR  "HiddenServiceDir "
#define HIDDEN_PORT "HiddenServicePort "

#define MAIN_DIR    "www"
#define TORRC_DIR   "/etc/tor/torrc"

#define CHOICE_DIR  "onion"
#define PRIVATE_DIR "/var/lib/tor/onion/"

File: types.h
Code:
#pragma once

typedef enum {false, true} bool;

File: onion.h
Code:
extern void backgr(void);
extern void confgr(void);
extern void result(void);
extern void wakeup(void);

The last file in our collection is the Makefile,
which simply contains instructions for compiling .c files:

File: Makefile
Code:
CC = gcc
FL = *.o *.i *.s main

.PHONY: default build clean

default: build

build: main.o backgr.o confgr.o result.o wakeup.o
$(CC) -save-temps main.o backgr.o confgr.o result.o wakeup.o -o main

clean:
rm -rf $(FL)

main.o: main.c
$(CC) -c main.c -o main.o
backgr.o: backgr.c
$(CC) -c backgr.c -o backgr.o
confgr.o: confgr.c
$(CC) -c confgr.c -o confgr.o
result.o: result.c
$(CC) -c result.c -o result.o
wakeup.o: wakeup.c
$(CC) -c wakeup.c -o wakeup.o

main.c:
$(error "main.c not found")
backgr.c:
$(error "backgr.c not found")
confgr.c:
$(error "confgr.c not found")
result.c:
$(error "result.c not found")
wakeup.c:
$(error "wakeup.c not found")
And now let's go directly to our C files.
There will be a lot of code, so try to stock up on some tea and cookies:

Let's start in order with the backgr.c file .
The tasks of this file are as follows:
- Check for the presence of directories
- Create directories if they are absent
- Check for the presence of files
- Create files if they do not exist:
Generate html and go files.

File: backgr.c
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>

#include "macro.h"
#include "types.h"

#define DEBUG

static bool dirIsExist(const char * const name) {
    DIR *dir = opendir(name);
    if (dir != NULL) {
        closedir(dir);
        #ifdef DEBUG
            printf("[DIR '%s' IS EXIST]\n", name);
        #endif
        return true;
    }
    #ifdef DEBUG
        printf("[DIR '%s' IS NOT EXIST]\n", name);
    #endif
    return false;
}

static bool fileIsExist(const char * const name) {
    FILE *file = fopen(name, "r");
    if (file != NULL) {
        fclose(file);
        #ifdef DEBUG
            printf("[FILE '%s' IS EXIST]\n", name);
        #endif
        return true;
    }
    #ifdef DEBUG
        printf("[FILE '%s' IS NOT EXIST]\n", name);
    #endif
    return false;
}

static void createDir(const char * const dir) {
    char command[6 + MAX_LENGTH];
    sprintf(command, "mkdir %s", dir);
    system(command);
    #ifdef DEBUG
        printf("[DIR '%s' CREATED]\n", dir);
    #endif
}

static bool createFile(const char * const name, const char * const content) {
    FILE *file = fopen(name, "w");
    if (file != NULL) {
        fputs(content, file);
        fclose(file);
        #ifdef DEBUG
            printf("[FILE '%s' CREATED]\n", name);
        #endif
        return true;
    }
    #ifdef DEBUG
        printf("[FILE '%s' NOT CREATED]\n", name);
    #endif
    return false;
}

extern void backgr(void) {
    chdir("/");

    if (!dirIsExist(MAIN_DIR))
        createDir(MAIN_DIR);

    #ifdef DEBUG
        printf("[CHDIR '%s']\n", MAIN_DIR);
    #endif
    chdir(MAIN_DIR);

    if (!dirIsExist(CHOICE_DIR))
        createDir(CHOICE_DIR);

    if (!fileIsExist("main.go"))
        createFile("main.go",
            "package main\n"
            "\n"
            "import (\n"
            "    \"fmt\"\n"
            "    \"net/http\"\n"
            ")\n"
            "\n"
            "func index(w http.ResponseWriter, r *http.Request) {\n"
            "    http.ServeFile(w, r, \""CHOICE_DIR"/index.html\")\n"
            "}\n"
            "\n"
            "func main() {\n"
            "    fmt.Println(\"Server is listening ...\")\n"
            "\n"
            "    http.HandleFunc(\"/\", index)\n"
            "    http.ListenAndServe(\""IP_PORT"\", nil)\n"
            "}\n"
        );

    #ifdef DEBUG
        printf("[CHDIR '%s']\n", CHOICE_DIR);
    #endif
    chdir(CHOICE_DIR);

    if (!fileIsExist("index.html"))
        createFile("index.html",
            "<!DOCTYPE html>\n"
            "<html>\n"
            "<head>\n"
            "    <title>[ CryATor-group ]</title>\n"
            "    <meta charset=\"utf-8\">\n"
            "</head>\n"
            "<body>\n"
            "    <h3>Generated by ZXONS</h3>\n"
            "    <p>hello, world</p>\n"
            "</body>\n"
            "</html>\n"
        );
}

Confgr.c file
Tasks:
- Check the torrc configuration file for the following two lines:
HiddenServiceDir / var / lib / tor / onion /
HiddenServicePort 80 127.0.0.1:80
- Write these lines to the torrc configuration file if they are absent
- Start tor services 'a : sys temctl start tor.service
- Restart tor services'a: systemctl restart tor.service

File: confgr.c
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "macro.h"
#include "types.h"

#define DEBUG

static bool exist[2] = {false, false};

static bool hiddenServiceExist(const char * const hs[2]) {
FILE * const torrc = fopen(TORRC_DIR, "r");
char buffer[BUFF_SIZE / 2];

if (torrc != NULL) {
while (fgets(buffer, BUFF_SIZE, torrc) != NULL) {
if      (strcmp(buffer, hs[0]) == 0) exist[0] = true;
else if (strcmp(buffer, hs[1]) == 0) exist[1] = true;

if (exist[0] && exist[1]) break;
}
fclose(torrc);
}

if (exist[0] && exist[1]) {
#ifdef DEBUG
printf("[DATA '%s' and '%s' FOUND]\n", HIDDEN_DIR, HIDDEN_PORT);
#endif
return true;
}

#ifdef DEBUG
printf("[DATA '%s' or '%s' NOT FOUND]\n", HIDDEN_DIR, HIDDEN_PORT);
#endif
return false;
}

static void appendHiddenService(const char * const hs[2]) {
FILE * const torrc = fopen(TORRC_DIR, "a");
if (torrc != NULL) {
if (!exist[0]) {
#ifdef DEBUG
printf("[DATA '%s' ADDED]\n", HIDDEN_DIR);
#endif
fputs(hs[0], torrc);
}
if (!exist[1]) {
#ifdef DEBUG
printf("[DATA '%s' ADDED]\n", HIDDEN_PORT);
#endif
fputs(hs[1], torrc);
}
fclose(torrc);
}
}

extern void confgr(void) {
const char * const hidden_service[2] = {
[0] = HIDDEN_DIR  PRIVATE_DIR  "\n",
[1] = HIDDEN_PORT PORT IP_PORT "\n"
};

if (!hiddenServiceExist(hidden_service))
appendHiddenService(hidden_service);

#ifdef DEBUG
printf( "[SYS 'systemctl start tor.service']\n"\
"[SYS 'systemctl restart tor.service']\n");
#endif

system("systemctl start tor.service");
system("systemctl restart tor.service");

sleep(1);
}

Result.c file
Task:
- Copying the hostname file to the / www directory

File: result.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "macro.h"

#define DEBUG

extern void result(void) {
#ifdef DEBUG
printf("[CHDIR '%s']\n", "../");
#endif
chdir("../");

#ifdef DEBUG
printf("[COPY '%s' to '%s']\n", PRIVATE_DIR"hostname", ".");
#endif
system("cp "PRIVATE_DIR"hostname .");
}

The wakeup.c file
Tasks:
- Compile the Go language file
- Run the compiled program

File: wakeup.c
Code:
Code:
#include <stdio.h>
#include <stdlib.h>

#define DEBUG

extern void wakeup(void) {
#ifdef DEBUG
printf("[SYS 'go build main.go']\n");
#endif
system("go build main.go");

#ifdef DEBUG
printf("[SYS './main']\n");
#endif
system("./main");
}

File: main.c
Code:
#include <stdio.h>
#include "onion.h"

int main(void) {
backgr();
confgr();
result();
wakeup();

return 0;
}

All of the above files must be in the same directory .
Compiling and running our program will look like this:
Code:
$ make build
$ sudo ./main

When compiled, the result will be the following:
Code:
gcc -c main.c -o main.o
gcc -c backgr.c -o backgr.o
gcc -c confgr.c -o confgr.o
gcc -c result.c -o result.o
gcc -c wakeup.c -o wakeup.o
gcc -save-temps main.o backgr.o confgr.o result.o wakeup.o -o main

The program must be run as a root user, since it works with objects outside the normal user's directory.

When we first run the program itself, we will see the following:
Code:
[DIR 'www' IS NOT EXIST]
[DIR 'www' CREATED]
[CHDIR 'www']
[DIR 'onion' IS NOT EXIST]
[DIR 'onion' CREATED]
[FILE 'main.go' IS NOT EXIST]
[FILE 'main.go' CREATED]
[CHDIR 'onion']
[FILE 'index.html' IS NOT EXIST]
[FILE 'index.html' CREATED]
[DATA 'HiddenServiceDir ' or 'HiddenServicePort ' NOT FOUND]
[DATA 'HiddenServiceDir ' ADDED]
[DATA 'HiddenServicePort ' ADDED]
[SYS 'systemctl start tor.service']
[SYS 'systemctl restart tor.service']
[CHDIR '../']
[COPY '/var/lib/tor/onion/hostname' to '.']
[SYS 'go build main.go']
[SYS './main']

Server is listening ...

Now we actually have two directories:
1) / var / lib / tor / onion / - the directory where the hostname and private_key files are located. The hostname
file contains just the generated name of your .onion site.
The private_key file contains the private RSA key of your site.
And this file must be stored like the apple of your eye, otherwise if you lose it, you will lose the hostname.

2) / www / - the directory in which the generated Go file is located, the compiled Go file, also contains the hostname of the site and, accordingly, the onion directory (in our case), which will contain all your html / css / js files, is located here.

If you now try to go to the hostname, for example, in the tor browser, then the site you generated will be displayed, which you can later decorate yourself.

When you restart the program, the created files, as well as other data similar to the key and, accordingly, the hostname will not be overwritten, so you can use this compiled file as a launch of your site.
Code:
[DIR 'www' IS EXIST]
[CHDIR 'www']
[DIR 'onion' IS EXIST]
[FILE 'main.go' IS EXIST]
[CHDIR 'onion']
[FILE 'index.html' IS EXIST]
[DATA 'HiddenServiceDir ' and 'HiddenServicePort ' FOUND]
[SYS 'systemctl start tor.service']
[SYS 'systemctl restart tor.service']
[CHDIR '../']
[COPY '/var/lib/tor/onion/hostname' to '.']
[SYS 'go build main.go']
[SYS './main']
Server is listening ...

That's it, the article has come to an end. If you want to improve your new resulting generated site so that there is not just a message at the "hello, world" level, but something worthwhile, then learn to typeset and accordingly learn some server programming language (in this example, this is Go Of course, one could write in pure C, but I don't really want to lay out a couple of hundred lines of code).
 
Top