10e3f233fSBartek Rutkowski#!/bin/sh
20e3f233fSBartek Rutkowski#-
30e3f233fSBartek Rutkowski# Copyright (c) 2016 Bartek Rutkowski
40e3f233fSBartek Rutkowski# All rights reserved.
50e3f233fSBartek Rutkowski#
60e3f233fSBartek Rutkowski# Redistribution and use in source and binary forms, with or without
70e3f233fSBartek Rutkowski# modification, are permitted provided that the following conditions
80e3f233fSBartek Rutkowski# are met:
90e3f233fSBartek Rutkowski# 1. Redistributions of source code must retain the above copyright
100e3f233fSBartek Rutkowski#    notice, this list of conditions and the following disclaimer.
110e3f233fSBartek Rutkowski# 2. Redistributions in binary form must reproduce the above copyright
120e3f233fSBartek Rutkowski#    notice, this list of conditions and the following disclaimer in the
130e3f233fSBartek Rutkowski#    documentation and/or other materials provided with the distribution.
140e3f233fSBartek Rutkowski#
150e3f233fSBartek Rutkowski# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
160e3f233fSBartek Rutkowski# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170e3f233fSBartek Rutkowski# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
180e3f233fSBartek Rutkowski# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
190e3f233fSBartek Rutkowski# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
200e3f233fSBartek Rutkowski# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
210e3f233fSBartek Rutkowski# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
220e3f233fSBartek Rutkowski# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
230e3f233fSBartek Rutkowski# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
240e3f233fSBartek Rutkowski# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250e3f233fSBartek Rutkowski# SUCH DAMAGE.
260e3f233fSBartek Rutkowski#
270e3f233fSBartek Rutkowski
28cc42ef53SBrad DavisBSDCFG_SHARE="/usr/share/bsdconfig"
29cc42ef53SBrad Davis. $BSDCFG_SHARE/common.subr || exit 1
30cc42ef53SBrad Davis
314d1ba6feSAlfonso S. Siciliano: ${BSDDIALOG_OK=0}
320e3f233fSBartek Rutkowski
3372121342SDimitry Andricecho -n > $BSDINSTALL_TMPETC/rc.conf.hardening
341d01cb0dSSteven Kreuzerecho -n > $BSDINSTALL_TMPETC/sysctl.conf.hardening
35c3afb29bSDag-Erling Smørgravecho -n > $BSDINSTALL_TMPBOOT/loader.conf.hardening
360e3f233fSBartek Rutkowski
37c0e249d3SLars Kellogg-Stedmanexec 5>&1
38cc42ef53SBrad DavisFEATURES=$( bsddialog --backtitle "$OSNAME Installer" \
3995525572SBartek Rutkowski    --title "System Hardening" --nocancel --separate-output \
400e3f233fSBartek Rutkowski    --checklist "Choose system security hardening options:" \
410e3f233fSBartek Rutkowski    0 0 0 \
422434a052SBartek Rutkowski	"0 hide_uids" "Hide processes running as other users" ${hide_uids:-off} \
432434a052SBartek Rutkowski	"1 hide_gids" "Hide processes running as other groups" ${hide_gids:-off} \
44d8061effSSteve Wills	"2 hide_jail" "Hide processes running in jails" ${hide_jail:-off} \
45d8061effSSteve Wills	"3 read_msgbuf" "Disable reading kernel message buffer for unprivileged users" ${read_msgbuf:-off} \
46d8061effSSteve Wills	"4 proc_debug" "Disable process debugging facilities for unprivileged users" ${proc_debug:-off} \
47d8061effSSteve Wills	"5 random_pid" "Randomize the PID of newly created processes" ${random_pid:-off} \
48391aafd7SBartek Rutkowski	"6 clear_tmp" "Clean the /tmp filesystem on system startup" ${clear_tmp:-off} \
49391aafd7SBartek Rutkowski	"7 disable_syslogd" "Disable opening Syslogd network socket (disables remote logging)" ${disable_syslogd:-off} \
50fe06db18SBaptiste Daroussin	"8 secure_console" "Enable console password prompt" ${secure_console:-off} \
51fe06db18SBaptiste Daroussin	"9 disable_ddtrace" "Disallow DTrace destructive-mode" ${disable_ddtrace:-off} \
52c0e249d3SLars Kellogg-Stedman2>&1 1>&5 )
534d1ba6feSAlfonso S. Sicilianoretval=$?
54c0e249d3SLars Kellogg-Stedmanexec 5>&-
550e3f233fSBartek Rutkowski
564d1ba6feSAlfonso S. Sicilianoif [ $retval -ne $BSDDIALOG_OK ]; then
574d1ba6feSAlfonso S. Siciliano	exit 1
584d1ba6feSAlfonso S. Sicilianofi
594d1ba6feSAlfonso S. Siciliano
600e3f233fSBartek Rutkowskifor feature in $FEATURES; do
61fbc57e2dSEd Maste	case "$feature" in
62fbc57e2dSEd Maste	hide_uids)
630e3f233fSBartek Rutkowski		echo security.bsd.see_other_uids=0 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
64fbc57e2dSEd Maste		;;
65fbc57e2dSEd Maste	hide_gids)
660e3f233fSBartek Rutkowski		echo security.bsd.see_other_gids=0 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
67fbc57e2dSEd Maste		;;
68fbc57e2dSEd Maste	hide_jail)
69d8061effSSteve Wills		echo security.bsd.see_jail_proc=0 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
70fbc57e2dSEd Maste		;;
71fbc57e2dSEd Maste	read_msgbuf)
720e3f233fSBartek Rutkowski		echo security.bsd.unprivileged_read_msgbuf=0 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
73fbc57e2dSEd Maste		;;
74fbc57e2dSEd Maste	proc_debug)
750e3f233fSBartek Rutkowski		echo security.bsd.unprivileged_proc_debug=0 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
76fbc57e2dSEd Maste		;;
77fbc57e2dSEd Maste	random_pid)
78f78bd12dSAllan Jude		echo kern.randompid=1 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
79fbc57e2dSEd Maste		;;
80fbc57e2dSEd Maste	clear_tmp)
810e3f233fSBartek Rutkowski		echo 'clear_tmp_enable="YES"' >> $BSDINSTALL_TMPETC/rc.conf.hardening
82fbc57e2dSEd Maste		;;
83fbc57e2dSEd Maste	disable_syslogd)
840e3f233fSBartek Rutkowski		echo 'syslogd_flags="-ss"' >> $BSDINSTALL_TMPETC/rc.conf.hardening
85fbc57e2dSEd Maste		;;
86fbc57e2dSEd Maste	secure_console)
8782ec242fSBartek Rutkowski		sed "s/unknown	off secure/unknown	off insecure/g" $BSDINSTALL_CHROOT/etc/ttys > $BSDINSTALL_TMPETC/ttys.hardening
88fbc57e2dSEd Maste		;;
89fbc57e2dSEd Maste	disable_ddtrace)
90c3afb29bSDag-Erling Smørgrav		echo 'security.bsd.allow_destructive_dtrace=0' >> $BSDINSTALL_TMPBOOT/loader.conf.hardening
91fbc57e2dSEd Maste		;;
92fbc57e2dSEd Maste	esac
930e3f233fSBartek Rutkowskidone
94