SSH tunneling over HTTP Proxy

sudo apt update && sudo apt install gcc

sudo vim http-injector-unix-client.c

/*
MIT License

Copyright (c) [2020] [Ardika Rommy Sanjaya]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stddef.h>

#if (__STDC_VERSION__ >= 199901L)
#include <stdint.h>
#endif

// https://creativeandcritical.net/str-replace-c
char *repl_str(const char *str, const char *from, const char *to) {

	/* Adjust each of the below values to suit your needs. */

	/* Increment positions cache size initially by this number. */
	size_t cache_sz_inc = 16;
	/* Thereafter, each time capacity needs to be increased,
	 * multiply the increment by this factor. */
	const size_t cache_sz_inc_factor = 3;
	/* But never increment capacity by more than this number. */
	const size_t cache_sz_inc_max = 1048576;

	char *pret, *ret = NULL;
	const char *pstr2, *pstr = str;
	size_t i, count = 0;
	#if (__STDC_VERSION__ >= 199901L)
	uintptr_t *pos_cache_tmp, *pos_cache = NULL;
	#else
	ptrdiff_t *pos_cache_tmp, *pos_cache = NULL;
	#endif
	size_t cache_sz = 0;
	size_t cpylen, orglen, retlen, tolen, fromlen = strlen(from);

	/* Find all matches and cache their positions. */
	while ((pstr2 = strstr(pstr, from)) != NULL) {
		count++;

		/* Increase the cache size when necessary. */
		if (cache_sz < count) {
			cache_sz += cache_sz_inc;
			pos_cache_tmp = realloc(pos_cache, sizeof(*pos_cache) * cache_sz);
			if (pos_cache_tmp == NULL) {
				goto end_repl_str;
			} else pos_cache = pos_cache_tmp;
			cache_sz_inc *= cache_sz_inc_factor;
			if (cache_sz_inc > cache_sz_inc_max) {
				cache_sz_inc = cache_sz_inc_max;
			}
		}

		pos_cache[count-1] = pstr2 - str;
		pstr = pstr2 + fromlen;
	}

	orglen = pstr - str + strlen(pstr);

	/* Allocate memory for the post-replacement string. */
	if (count > 0) {
		tolen = strlen(to);
		retlen = orglen + (tolen - fromlen) * count;
	} else	retlen = orglen;
		ret = malloc(retlen + 1);
	if (ret == NULL) {
		goto end_repl_str;
	}

	if (count == 0) {
		/* If no matches, then just duplicate the string. */
		strcpy(ret, str);
	} else {
		/* Otherwise, duplicate the string whilst performing
		 * the replacements using the position cache. */
		pret = ret;
		memcpy(pret, str, pos_cache[0]);
		pret += pos_cache[0];
		for (i = 0; i < count; i++) {
			memcpy(pret, to, tolen);
			pret += tolen;
			pstr = str + pos_cache[i] + fromlen;
			cpylen = (i == count-1 ? orglen : pos_cache[i+1]) - pos_cache[i] - fromlen;
			memcpy(pret, pstr, cpylen);
			pret += cpylen;
		}
		ret[retlen] = '\0';
	}

end_repl_str:
	/* Free the cache and return the post-replacement string,
	 * which will be NULL in the event of an error. */
	free(pos_cache);
	return ret;
}

/* connecting to http proxy */

void printHelp() {
	printf("ssh [email protected] -o ProxyCommand=\"./http-injector-client -x 192.168.43.172:44533 -P \'CONNECT 103.129.220.168:22 HTTP/1.1[crlf*2]\'\"\n\n");
	printf("-x: HTTP Proxy.\n");
	printf("-P: HTTP Payload.\n");
	printf("-s: Buffer size (Optional, default: 1023).\n");
}

void *reader(void *args) {
	int fd = *(int *) args;
	char ch;
	while (1) {
    		fread(&ch, 1, 1, stdin);
		send(fd, (void *) &ch, 1, 0);
	}
}

void *writer(void *args) {
	int fd = *(int *) args;
	char ch;
	while (1) {
		recv(fd, (void *) &ch, 1, 0);
		fwrite(&ch, 1, 1, stdout);
		fflush(stdout);
	}
}


int main(int argc, char* argv[]) {

	int fd;
	struct sockaddr_in remote; 

	char *proxy_host = NULL;
	int proxy_port = -1;

	char *payload = NULL;

	int buf_size = 1024;
	int opt;
  	while ((opt = getopt (argc, argv, "x:h:s:P:")) != -1) {
		switch (opt) {
			case 'x':
				proxy_host = strtok(optarg, ":");
				if (proxy_host != NULL) {
					proxy_port = atoi(strtok(NULL, ":"));
				}
				break;
			case 'h':
				printHelp();
				break;
			case 's':
				buf_size = atoi(optarg);
				break;
			case 'P':
				payload = optarg;
				payload = repl_str(payload, "[cr]", "\r");
				payload = repl_str(payload, "[lf]", "\n");
				payload = repl_str(payload, "[crlf]", "\r\n");
				payload = repl_str(payload, "[crlf*2]", "\r\n\r\n");
				payload = repl_str(payload, "[lfcr]", "\n\r");
				break;
		}
	}
	
	if (proxy_host == NULL || proxy_port < 0 || payload == NULL) {
		printHelp();
		exit(-1);
	}

	char buffer[buf_size];
	int sent, read = 0;
	pthread_t worker;


	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		printf("\nFailed to open socket file descriptor.\n");
		return -1;
	}

	remote.sin_family = AF_INET;
	remote.sin_port = htons(proxy_port);

	if (inet_pton(AF_INET, proxy_host, &remote.sin_addr) <= 0) {
        	printf("\nInvalid address/Address not supported.\n");
		close(fd);
        	return -1;
    	}

	if (connect(fd, (struct sockaddr *) &remote, sizeof(remote)) < 0) {
        	printf("\nConnection Failed.\n");
		close(fd);
        	return -1;
	}

	if ((sent = send(fd, payload, strlen(payload), 0)) < 0) {
		printf("\nFailed to send HTTP payload.\n");
		close(fd);
		return -1;
	}

	memset(buffer, '\0', buf_size);
	while (read == 0) {
		read = recv(fd, buffer, buf_size, 0);
		for (int i = 0; i < read; i++) {
			if (buffer[read - 4] == '\r' && buffer[read - 3] == '\n' && buffer[read - 2] == '\r' && buffer[read - 1] == '\n') {
				read == 0;
				break;
			}
		}
	}

	pthread_create(&worker, NULL, reader, (void*) &fd); 
	writer((void*) &fd);

	close(fd);
	return 0;
}

gcc http-injector-unix-client.c -o http-injector-client -lpthread

ssh [email protected] -o "ProxyCommand=./http-injector-client -x 192.168.43.173:44355 -P 'CONNECT 103.129.220.168:22 HTTP/1.1[crlf*2]'"

*) Parameters

  • -x: HTTP Proxy

  • -P: HTTP request payload (Request connection to ssh server)

  • -s: Buffer size (Optional), default value is 1024.

Compile and Install Linux Kernel on Ubuntu 20.04

sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison

wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.7.10.tar.xz

tar xvf linux-5.7.10.tar.xz

cd linux-5.7.10

cp /boot/config-$(uname -r) .config

make menuconfig

make

sudo make modules_install

sudo make install

sudo update-initramfs -c -k 5.7.10

sudo update-grub

*) used for trying new kernel api from linux (io_uring).

Nginx HTTP Load Balancer

$ sudo vim /etc/nginx/nginx.conf

http {

  # round robin
  upstream rumahpemuridan {
      server 127.0.0.1:8081 weight=3;
      server 127.0.0.1:8082 max_conns=2;
      queue 100 timeout=70;
  }

  server {
    listen 80;
    server_name rumahpemuridan.com;
    location / {
      proxy_pass http://rumahpemuridan;
    }
  }
}

Install Nginx and Let's Encrypt on Ubuntu 20.04

Prerequisites

  • You have domain name pointing to your server public IP.

Installation process

  • sudo apt update

  • sudo apt install nginx

  • sudo systemctl enable nginx

  • sudo apt install certbot

  • sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

  • sudo mkdir -p /var/lib/letsencrypt/.well-known

  • sudo chgrp www-data /var/lib/letsencrypt

  • sudo chmod g+s /var/lib/letsencrypt

  • sudo vim /etc/nginx/snippets/letsencrypt.conf

location ^~ /.well-known/acme-challenge/ {
  allow all;
  root /var/lib/letsencrypt/;
  default_type "text/plain";
  try_files $uri =404;
}
  • sudo vim /etc/nginx/snippets/ssl.conf
ssl_dhparam /etc/ssl/certs/dhparam.pem;

ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
ssl_prefer_server_ciphers on;

ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 30s;

add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
  • sudo vim /etc/nginx/sites-available/dev.cmsnesia.com.conf
server {
  listen 80;
  listen [::]:80;
  server_name dev.cmsnesia.com;
  
  root /var/www/html
  
  include snippets/letsencrypt.conf;
}
  • sudo ln -s /etc/nginx/sites-available/dev.cmsnesia.com.conf /etc/nginx/sites-enabled/

  • sudo systemctl restart nginx

  • sudo certbot certonly --agree-tos --email [email protected] --webroot -w /var/lib/letsencrypt/ -d dev.cmsnesia.com

  • sudo vim /etc/nginx/sites-available/dev.cmsnesia.com.conf

server {
    listen 80;
    listen [::]:80;
    server_name dev.cmsnesia.com;
  
    root /var/www/html
    include snippets/letsencrypt.conf;
    return 301 https://dev.cmsnesia.com$request_uri; # redirect http to https
}

server {
    listen 443 ssl http2;
    server_name dev.cmsnesia.com;

    ssl_certificate /etc/letsencrypt/live/dev.cmsnesia.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dev.cmsnesia.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/dev.cmsnesia.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;
}

  • sudo vim /etc/cron.d/certbot
    0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook "systemctl reload nginx"
    
  • sudo certbot renew --dry-run