xref: /freebsd/usr.sbin/bsdinstall/scripts/hardening (revision 4d1ba6febfa7c7808027fd1ef60b3bffadd17853)
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# $FreeBSD$
280e3f233fSBartek Rutkowski
29*4d1ba6feSAlfonso S. Siciliano: ${BSDDIALOG_OK=0}
300e3f233fSBartek Rutkowski
3172121342SDimitry Andricecho -n > $BSDINSTALL_TMPETC/rc.conf.hardening
321d01cb0dSSteven Kreuzerecho -n > $BSDINSTALL_TMPETC/sysctl.conf.hardening
33c3afb29bSDag-Erling Smørgravecho -n > $BSDINSTALL_TMPBOOT/loader.conf.hardening
340e3f233fSBartek Rutkowski
350e3f233fSBartek Rutkowskiexec 3>&1
36*4d1ba6feSAlfonso S. SicilianoFEATURES=$( bsddialog --backtitle "FreeBSD Installer" \
3795525572SBartek Rutkowski    --title "System Hardening" --nocancel --separate-output \
380e3f233fSBartek Rutkowski    --checklist "Choose system security hardening options:" \
390e3f233fSBartek Rutkowski    0 0 0 \
402434a052SBartek Rutkowski	"0 hide_uids" "Hide processes running as other users" ${hide_uids:-off} \
412434a052SBartek Rutkowski	"1 hide_gids" "Hide processes running as other groups" ${hide_gids:-off} \
42d8061effSSteve Wills	"2 hide_jail" "Hide processes running in jails" ${hide_jail:-off} \
43d8061effSSteve Wills	"3 read_msgbuf" "Disable reading kernel message buffer for unprivileged users" ${read_msgbuf:-off} \
44d8061effSSteve Wills	"4 proc_debug" "Disable process debugging facilities for unprivileged users" ${proc_debug:-off} \
45d8061effSSteve Wills	"5 random_pid" "Randomize the PID of newly created processes" ${random_pid:-off} \
46391aafd7SBartek Rutkowski	"6 clear_tmp" "Clean the /tmp filesystem on system startup" ${clear_tmp:-off} \
47391aafd7SBartek Rutkowski	"7 disable_syslogd" "Disable opening Syslogd network socket (disables remote logging)" ${disable_syslogd:-off} \
48391aafd7SBartek Rutkowski	"8 disable_sendmail" "Disable Sendmail service" ${disable_sendmail:-off} \
49391aafd7SBartek Rutkowski	"9 secure_console" "Enable console password prompt" ${secure_console:-off} \
50c3afb29bSDag-Erling Smørgrav	"10 disable_ddtrace" "Disallow DTrace destructive-mode" ${disable_ddtrace:-off} \
510e3f233fSBartek Rutkowski2>&1 1>&3 )
52*4d1ba6feSAlfonso S. Sicilianoretval=$?
530e3f233fSBartek Rutkowskiexec 3>&-
540e3f233fSBartek Rutkowski
55*4d1ba6feSAlfonso S. Sicilianoif [ $retval -ne $BSDDIALOG_OK ]; then
56*4d1ba6feSAlfonso S. Siciliano	exit 1
57*4d1ba6feSAlfonso S. Sicilianofi
58*4d1ba6feSAlfonso S. Siciliano
590e3f233fSBartek Rutkowskifor feature in $FEATURES; do
60fbc57e2dSEd Maste	case "$feature" in
61fbc57e2dSEd Maste	hide_uids)
620e3f233fSBartek Rutkowski		echo security.bsd.see_other_uids=0 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
63fbc57e2dSEd Maste		;;
64fbc57e2dSEd Maste	hide_gids)
650e3f233fSBartek Rutkowski		echo security.bsd.see_other_gids=0 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
66fbc57e2dSEd Maste		;;
67fbc57e2dSEd Maste	hide_jail)
68d8061effSSteve Wills		echo security.bsd.see_jail_proc=0 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
69fbc57e2dSEd Maste		;;
70fbc57e2dSEd Maste	read_msgbuf)
710e3f233fSBartek Rutkowski		echo security.bsd.unprivileged_read_msgbuf=0 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
72fbc57e2dSEd Maste		;;
73fbc57e2dSEd Maste	proc_debug)
740e3f233fSBartek Rutkowski		echo security.bsd.unprivileged_proc_debug=0 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
75fbc57e2dSEd Maste		;;
76fbc57e2dSEd Maste	random_pid)
77f78bd12dSAllan Jude		echo kern.randompid=1 >> $BSDINSTALL_TMPETC/sysctl.conf.hardening
78fbc57e2dSEd Maste		;;
79fbc57e2dSEd Maste	clear_tmp)
800e3f233fSBartek Rutkowski		echo 'clear_tmp_enable="YES"' >> $BSDINSTALL_TMPETC/rc.conf.hardening
81fbc57e2dSEd Maste		;;
82fbc57e2dSEd Maste	disable_syslogd)
830e3f233fSBartek Rutkowski		echo 'syslogd_flags="-ss"' >> $BSDINSTALL_TMPETC/rc.conf.hardening
84fbc57e2dSEd Maste		;;
85fbc57e2dSEd Maste	disable_sendmail)
860e3f233fSBartek Rutkowski		echo 'sendmail_enable="NONE"' >> $BSDINSTALL_TMPETC/rc.conf.hardening
87fbc57e2dSEd Maste		;;
88fbc57e2dSEd Maste	secure_console)
8982ec242fSBartek Rutkowski		sed "s/unknown	off secure/unknown	off insecure/g" $BSDINSTALL_CHROOT/etc/ttys > $BSDINSTALL_TMPETC/ttys.hardening
90fbc57e2dSEd Maste		;;
91fbc57e2dSEd Maste	disable_ddtrace)
92c3afb29bSDag-Erling Smørgrav		echo 'security.bsd.allow_destructive_dtrace=0' >> $BSDINSTALL_TMPBOOT/loader.conf.hardening
93fbc57e2dSEd Maste		;;
94fbc57e2dSEd Maste	esac
950e3f233fSBartek Rutkowskidone
96