Enable DPDK for Libpcap

Install required tools and libraries

sudo apt install git build-essential bison flex libnuma-dev libnl-3-dev libnl-genl-3-dev

Clone latest DPDK source code from Github

git clone https://github.com/DPDK/dpdk.git

Compile and Install

cd dpdk

Get a native target environment automatically

make defconfig O=mybuild

Or get a specific target environment

make config T=x86_64-native-linux-gcc O=mybuild

Customize the target configuration in the generated .config file. Example for enabling the shared library

sed -ri 's,(CONFIG_RTE_BUILD_SHARED_LIB=).*,\1y,' mybuild/.config

make O=mybuild

sudo make install O=mybuild

*) Any kernel modules to be used, e.g. igb_uio, kni, must be compiled with the same kernel as the one running on the target

Install libpcap with dpdk enabled

cd ../

git clone https://github.com/the-tcpdump-group/libpcap.git

cd libpcap

`./configure –with-dpdk=/usr/local

make

sudo make install

A Strict Firewall that Only Allows SSH

export SERVER_IP="x.x.x.x"

Flushing all rules

iptables -F

iptables -X

Setting default filter policy

iptables -P INPUT DROP

iptables -P OUTPUT DROP

iptables -P FORWARD DROP

Allow incoming and outgoing ssh only

iptables -A INPUT -p tcp -s 0/0 -d $SERVER_IP --sport 513:65535 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A OUTPUT -p tcp -s $SERVER_IP -d 0/0 --sport 22 --dport 513:65535 -m state --state ESTABLISHED -j ACCEPT

Make sure nothing comes or goes out

iptables -A INPUT -j DROP

iptables -A OUTPUT -j DROP

Save and load rules

iptables-save > /etc/iptables.rules

vim /etc/network/if-pre-up.d/iptablesload

#!/bin/sh
iptables-restore < /etc/iptables.rules
exit 0

vim /etc/network/if-post-down.d/iptablessave

#!/bin/sh
iptables-save -c > /etc/iptables.rules
if [ -f /etc/iptables.downrules ]; then
   iptables-restore < /etc/iptables.downrules
fi
exit 0

chmod +x /etc/network/if-post-down.d/iptablessave

chmod +x /etc/network/if-pre-up.d/iptablesload

Install Haraka SMTP Server On Ubuntu 20.04

sudo -i

Install NVM (Node Version Manager)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"

[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Install Node (LTS)

nvm install --lts

Install Haraka

apt install build-essential

npm -g config set user root

npm install -g Haraka

cd /var/mail && haraka -i .

vim /var/mail/config/smtp.ini

; Server public IP
public_ip=x.x.x.x

; Daemonize
daemonize=true
daemon_log_file=/var/log/haraka.log
daemon_pid_file=/var/run/haraka.pid

; Spooling
; Save memory by spooling large messages to disk
spool_dir=/var/spool/haraka

haraka -c /var/mail

Allows incoming SMTP request on port 25 for server IP address x.x.x.x

iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d x.x.x.x --dport 25 -m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A OUTPUT -p tcp -s x.x.x.x --sport 25 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

Allow outgoing SMTP requst for server IP address x.x.x.

iptables -A OUTPUT -p tcp -s x.x.x.x --sport 1024:65535 -d 0/0 --dport 25 -m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A INPUT -p tcp -s 0/0 --sport 25 -d x.x.x.x --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

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.