2020-10-20 13:36:10 +02:00
---
title: 'FreeBSD - Jail - Secure Jumphost'
description: "FreeBSD"
date: "2020-10-20"
---
The goal is to create a limited jail using rbash and securing it so it can only accept secure SSH sessions. It should only be used as an SSH jumphost to connect further. It should therefor not be possible to create, use or install other code in this limited environment.
2020-10-20 14:07:46 +02:00
All commands are executed as root inside the jail, unless specified otherwise.
2020-10-20 13:36:10 +02:00
# FreeBSD jail
Create a jail and connect to the console.
```
2020-10-20 14:07:46 +02:00
[simon@host ~]$ sudo ezjail-admin create bastion 'bridge0|10.0.0.10'
[simon@host ~]$ sudo ezjail-admin console bastion
2020-10-20 13:36:10 +02:00
```
Install `bash` .
```
2020-10-20 14:07:46 +02:00
# pkg install bash
2020-10-20 13:36:10 +02:00
```
# OpenSSH-Portable
Install `openssh-portable` .
```
2020-10-20 14:07:46 +02:00
# pkg install openssh-portable
2020-10-20 13:36:10 +02:00
```
Configure `rc.conf` .
```
2020-10-20 14:07:46 +02:00
# sysrc sshd_enable=NO
# sysrc openssh_enable=YES
2020-10-20 13:36:10 +02:00
```
Check only what the current best practices are regarding the full OpenSSH daemon configuration.
For example check; https://infosec.mozilla.org/guidelines/openssh
Make sure the daemon only listens to the assigned IP for this jail. And make sure the firewall running on the host accepts incoming and outgoing SSH connections.
```
2020-10-20 14:07:46 +02:00
# cat /usr/local/etc/sshd
2020-10-20 13:36:10 +02:00
...
ListenAddress 10.0.0.10
...
```
Stop and start the services.
```
2020-10-20 14:07:46 +02:00
# service sshd stop
# service openssh start
2020-10-20 13:36:10 +02:00
```
# User
Create a default `user` and make sure the `user` has the `/usr/local/bin/rbash` shell configured.
```
2020-10-20 14:07:46 +02:00
# mkdir /usr/home/user/bin
2020-10-20 13:36:10 +02:00
```
Symlink the only required binaries into this directory.
```
2020-10-20 14:07:46 +02:00
# ln -s /usr/local/bin/ssh /usr/home/user/bin/ssh
2020-10-20 13:36:10 +02:00
```
Create bash profile.
```
2020-10-20 14:07:46 +02:00
# cat /usr/home/user/.bash_profile
2020-10-20 13:36:10 +02:00
PATH=$HOME/bin
export PATH
```
Make sure the permissions are so that the user cannot modify its own `.(bash_)profile` files.
```
2020-10-20 14:38:58 +02:00
# chown root:user .bash_profile .profile
2020-10-20 13:36:10 +02:00
```
Remove also all unused < shell > rc files like cshrc, shrc, etc.
```
2020-10-20 14:07:46 +02:00
# rm .cshrc .shrc ...
2020-10-20 13:36:10 +02:00
```
2020-10-20 14:07:46 +02:00
Create `.ssh` folder and fill `authorized_keys` file (optional).
2020-10-20 13:36:10 +02:00
```
2020-10-20 14:07:46 +02:00
# mkdir /usr/home/user/.ssh
# echo "your_public_key_here" >> /usr/home/user/.ssh/authorized_keys
# chown -R user:user /usr/home/user/.ssh
# chmod -R 700 /usr/home/user/.ssh
2020-10-20 13:36:10 +02:00
```
User directory can look like this.
```
2020-10-20 14:07:46 +02:00
[user@bastion ~]$ ls -al
2020-10-20 13:36:10 +02:00
total 3
drwxr-xr-x 4 user user 5 Oct 20 11:24 .
drwxr-xr-x 4 root wheel 4 Oct 19 11:59 ..
-rw-r--r-- 1 root user 43 Oct 19 14:09 .bash_profile
drwx------ 2 user user 5 Oct 19 12:40 .ssh
drwxr-xr-x 2 user user 3 Oct 19 14:21 bin
```
# Result
2020-10-20 14:40:33 +02:00
- FreeBSD Jail with latest packaged version of OpenSSH-Portable
2020-10-20 13:36:10 +02:00
- Commands are unavailable and absolute paths are not allowed.
- The `$PATH` variable is read-only.
- The `.bash_profile` file is read-only for the user.
- Only some bash functions + the `ssh` binary is available for the user.
```
[user@bastion ~]$ ls
-rbash: ls: command not found
[user@bastion ~]$ /bin/ls
-rbash: /bin/ls: restricted: cannot specify `/' in command names
[user@bastion ~]$ export PATH=/usr/bin
-rbash: PATH: readonly variable
[user@bastion ~]$
! break continue else fg in pushd shopt true while
./ builtin coproc enable fi jobs pwd source type {
: caller declare esac for kill read ssh typeset }
[ case dirs eval function let readarray suspend ulimit
[[ cd disown exec getopts local readonly test umask
]] command do exit hash logout return then unalias
alias compgen done export help mapfile select time unset
bg complete echo false history popd set times until
bind compopt elif fc if printf shift trap wait
```