Skip to content

Commit c35ecb5

Browse files
committed
Add optional port multiplexer role
1 parent 666f728 commit c35ecb5

File tree

8 files changed

+144
-8
lines changed

8 files changed

+144
-8
lines changed

.templates/inventory/group_vars/all.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,9 @@ default_role_fail2ban:
162162
default_role_system_settings:
163163
timezone: Europe/Rome
164164
locale: "en_GB.UTF-8"
165+
166+
167+
default_role_port_multiplexer:
168+
sslh_route_to_ssh_port: 22
169+
sslh_route_to_http_port: 80
170+
sslh_route_to_tls_port: 4443

playbooks/prepare-machine.yml

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,77 +29,94 @@
2929
# ===================================
3030
- name: Set system settings
3131
when: role_system_settings is defined
32+
tags: system_settings
3233
block:
3334
- name: Include required vars
3435
set_fact:
3536
args: "{{ default_role_system_settings | combine(role_system_settings | default({}), recursive=True) }}"
3637

3738
- include_role: name=system-settings
38-
tags: system_settings
3939

4040
# ==========
4141
# Multi User
4242
# ==========
4343
- name: Users management role
4444
when: role_users is defined
45+
tags: users
4546
block:
4647
- name: Include required vars
4748
set_fact:
4849
args: "{{ default_role_users | combine(role_users | default({}), recursive=True) }}"
4950

5051
- include_role: name=blackandred.server_multi_user
51-
tags: users
5252

5353
# ==============
5454
# Basic Software
5555
# ==============
5656
- name: Basic Software role
5757
when: role_basic_software is defined
58+
tags: basic_software
5859
block:
5960
- name: Include required vars
6061
set_fact:
6162
args: "{{ default_role_basic_software | combine(role_basic_software | default({}), recursive=True) }}"
6263

6364
- include_role: name=blackandred.server_basic_software
64-
tags: basic_software
6565

6666
# ========
6767
# Tweaking
6868
# ========
6969
- name: Tweaking role
7070
when: role_tune is defined
71+
tags: tune
7172
block:
7273
- name: Include required vars
7374
set_fact:
7475
args: "{{ default_role_tune | combine(role_tune | default({}), recursive=True) }}"
7576

7677
- include_role: name=infrastructure-ansible-tweak-os
77-
tags: tune
7878

7979
# ====
8080
# Logs
8181
# ====
8282
- name: Logs role
8383
when: role_logs is defined
84+
tags: logs
8485
block:
8586
- name: Include required vars
8687
set_fact:
8788
args: "{{ default_role_logs | combine(role_logs | default({}), recursive=True) }}"
8889

8990
- include_role: name=infrastructure-ansible-logs
90-
tags: logs
9191

92+
# =========================
93+
# Security - IPS - Fail2Ban
94+
# =========================
9295
- name: Fail2ban role
9396
when: role_fail2ban is defined
97+
tags: fail2ban
9498
block:
9599
- name: Include required vars
96100
set_fact:
97101
args: "{{ default_role_fail2ban | combine(role_fail2ban | default({}), recursive=True) }}"
98102

99103
- name: Touch /var/log/auth.log
100104
become: yes
101-
path: /var/log/auth.log
102-
state: touch
105+
file:
106+
path: /var/log/auth.log
107+
state: touch
103108

104109
- include_role: name=oefenweb.fail2ban
105-
tags: fail2ban
110+
111+
# ==========================================
112+
# Security - optional ports to mask SSH port
113+
# ==========================================
114+
- name: Multiplexer role
115+
when: role_port_multiplexer is defined
116+
tags: port-multiplexer
117+
block:
118+
- name: Include required vars
119+
set_fact:
120+
args: "{{ default_role_port_multiplexer | combine(role_port_multiplexer | default({}), recursive=True) }}"
121+
122+
- include_role: name=port-multiplexer

roles/port-multiplexer/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Port multiplexer
2+
================
3+
4+
Sets up multiple protocols like VPN, SSH, HTTPS on single port.
5+
6+
- Additional layer of security by hiding services behind regular 443 port, no one would expect it
7+
- Misleads the eavesdropping by mixing different services on one port (privacy)
8+
- Proxies transparently, so the source IP address is preserved and fail2ban can be still used together to provide maximum security
9+
10+
**Before using this role make sure that nothing is listening on 443 port on 0.0.0.0 (does not collide with 127.0.0.1:443)**
11+
12+
13+
Read more:
14+
https://confluence.jaytaala.com/display/TKB/Transparent+SSLH%3A+using+a+single+port+to+transparently+route+incoming+traffic+for+Apache%2C+OpenVPN%2C+and+SSH
15+
16+
17+
18+
Alternative solutions
19+
---------------------
20+
21+
- Port knocking

roles/port-multiplexer/tasks/main.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
3+
- tags: port-multiplexer
4+
block:
5+
- name: Install sslh
6+
become: yes
7+
package:
8+
name: sslh
9+
state: present
10+
11+
- name: Detect gateway interface
12+
become: yes
13+
shell: "ip route | head -n 1 | awk '/default/ { print $3 }'"
14+
register: gw_interface_out
15+
16+
- name: Set gateway interface
17+
set_fact:
18+
gateway_interface: "{{ gw_interface_out.stdout }}"
19+
20+
- name: Create iptables script for transparent proxying
21+
become: yes
22+
template:
23+
src: "{{ item.path }}"
24+
dest: "/{{ item.path }}"
25+
mode: u+rwx,g+rx,o+rx
26+
with_items:
27+
- { path: usr/local/bin/sslh-transparent, mode: u+rwx,g+rx,o+rx }
28+
- { path: etc/default/sslh, mode: u+rwx,g+r,o+r }
29+
- { path: etc/systemd/system/sslh-transparent.service, mode: u+rw,g+r,o+r }
30+
- { path: etc/sslh.cfg, mode: u+rw,g+r,o+r }
31+
32+
- name: Enable services
33+
become: yes
34+
systemd:
35+
name: "{{ item }}"
36+
state: started
37+
with_items:
38+
- sslh
39+
- sslh-transparent
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RUN=no
2+
DAEMON=/usr/sbin/sslh
3+
DAEMON_OPTS="--user sslh -F /etc/sslh.cfg --pidfile /var/run/sslh/sslh.pid"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
verbose: 0;
2+
foreground: true;
3+
inetd: false;
4+
numeric: false;
5+
transparent: true;
6+
timeout: 2;
7+
user: "nobody";
8+
chroot: "/tmp";
9+
syslog_facility: "auth";
10+
11+
listen:
12+
(
13+
{ host: "0.0.0.0"; port: "443"; }
14+
);
15+
16+
protocols:
17+
(
18+
{ name: "ssh"; service: "ssh"; host: "127.0.0.1"; port: "{{ sslh_route_to_ssh_port | default('22') }}"; keepalive: true; fork: true; tfo_ok: true },
19+
{ name: "http"; host: "127.0.0.1"; port: "{{ sslh_route_to_http_port | default('80') }}"; },
20+
{ name: "tls"; host: "127.0.0.1"; port: "{{ sslh_route_to_tls_port | default('443') }}"; tfo_ok: true }
21+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=sslh transparent (see /usr/local/sbin/ssl-transparent)
3+
Wants=network-online.target
4+
After=network-online.target
5+
6+
[Service]
7+
Type=simple
8+
ExecStart=/usr/local/sbin/sslh-transparent
9+
10+
[Install]
11+
WantedBy=multi-user.target
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
sysctl -w net.ipv4.conf.default.route_localnet=1
4+
sysctl -w net.ipv4.conf.all.route_localnet=1
5+
6+
# DROP martian packets as they would have been if route_localnet was zero
7+
# Note: packets not leaving the server aren't affected by this, thus sslh will still work
8+
iptables -t raw -A PREROUTING ! -i lo -d 127.0.0.0/8 -j DROP
9+
iptables -t mangle -A POSTROUTING ! -o lo -s 127.0.0.0/8 -j DROP
10+
11+
# Mark all connections made by ssl for special treatment (here sslh is run as user "sslh")
12+
iptables -t nat -A OUTPUT -m owner --uid-owner sslh -p tcp --tcp-flags FIN,SYN,RST,ACK SYN -j CONNMARK --set-xmark 0x01/0x0f
13+
14+
# Outgoing packets that should go to sslh instead have to be rerouted, so mark them accordingly (copying over the connection mark)
15+
iptables -t mangle -A OUTPUT ! -o lo -p tcp -m connmark --mark 0x01/0x0f -j CONNMARK --restore-mark --mask 0x0f
16+
17+
ip rule add fwmark 0x1 lookup 100
18+
ip route add local 0.0.0.0/0 dev lo table 100

0 commit comments

Comments
 (0)