{"id":4,"date":"2007-01-09T22:38:06","date_gmt":"2007-01-09T22:38:06","guid":{"rendered":"http:\/\/patchlog.com\/general\/freebsd-securelevel-setup\/"},"modified":"2007-03-28T11:53:57","modified_gmt":"2007-03-28T11:53:57","slug":"freebsd-securelevel-setup","status":"publish","type":"post","link":"https:\/\/patchlog.com\/general\/freebsd-securelevel-setup\/","title":{"rendered":"freebsd securelevel setup"},"content":{"rendered":"<p><!--digg-->FreeBSD securelevel is a security mechanism implemented in the kernel that restricts certain tasks depending on the level that is active. Not even the super user will be able to bypass this mechanism if well setup.<\/p>\n<p>From <a href=\"http:\/\/nixdoc.net\/man-pages\/FreeBSD\/man8\/securelevel.8.html\" title=\"freebsd securelevel man page\">securelevel man page<\/a> :<\/p>\n<blockquote><p><strong>-1<\/strong> Permanently insecure mode - always run the system in level 0 mode.<br \/>\nThis is the default initial value.<\/p>\n<p><strong>0<\/strong> Insecure mode - immutable and append-only flags may be turned off. All devices may be read or written subject to their permissions.<\/p>\n<p><strong>1<\/strong> Secure mode - the system immutable and system append-only flags may not be turned off; disks for mounted file systems, <em>\/dev\/mem<\/em>, and <em>\/dev\/kmem<\/em> may not be opened for writing; kernel modules (see <a href=\"http:\/\/nixdoc.net\/man-pages\/FreeBSD\/man4\/kld.4.html\">kld(4)<\/a>) may not be loaded or unloaded.<\/p>\n<p><strong>2<\/strong> Highly secure mode - same as secure mode, plus disks may not be opened for writing (except by <a href=\"http:\/\/nixdoc.net\/man-pages\/FreeBSD\/man2\/mount.2.html\">mount(2)<\/a>) whether mounted or not. This level precludes tampering with file systems by unmounting them, but also inhibits running <a href=\"http:\/\/nixdoc.net\/man-pages\/FreeBSD\/man8\/newfs.8.html\">newfs(8)<\/a> while the system is multiuser.<\/p>\n<p>In addition, kernel time changes are restricted to less than or equal to one second.  Attempts to change the time by more than this will log the message ``Time adjustment clamped to +1 second''.<\/p>\n<p><strong>3<\/strong> Network secure mode - same as highly secure mode, plus IP packet filter rules (see <a href=\"http:\/\/nixdoc.net\/man-pages\/FreeBSD\/man8\/ipfw.8.html\">ipfw(8)<\/a> and <a href=\"http:\/\/nixdoc.net\/man-pages\/FreeBSD\/man4\/ipfirewall.4.html\">ipfirewall(4)<\/a>) cannot be changed and <a href=\"http:\/\/nixdoc.net\/man-pages\/FreeBSD\/man4\/dummynet.4.html\">dummynet(4)<\/a> configuration cannot be adjusted.<\/p><\/blockquote>\n<p><!--more-->\u00c2\u00a0To see the current securelevel you can use :<\/p>\n<blockquote><p># <a href=\"http:\/\/nixdoc.net\/man-pages\/FreeBSD\/man8\/sysctl.8.html\" target=\"_blank\" title=\"get or set kernel state\">sysctl<\/a> kern.securelevel<\/p><\/blockquote>\n<p>This will display something like:<\/p>\n<blockquote><p>kern.securelevel: -1<\/p><\/blockquote>\n<p>on a system with default configuration.<\/p>\n<p>To raise the securelevel you use sysctl like this:<\/p>\n<blockquote><p># sysctl kern.securelevel=2<\/p><\/blockquote>\n<p>Only the superuser can raise the securelevel, and no one can lower the securelevel, so if you just raised your securelevel and you want it back to -1 you have to make sure securelevel is not active at boot and then reboot.<\/p>\n<p>If you want to activate securelevel at boot you have to edit \/etc\/rc.conf  and add something like this:<\/p>\n<blockquote><p>kern_securelevel_enable=\"YES\" # kernel security level<br \/>\nkern_securelevel=\"2\" # range: -1..3 as above<\/p><\/blockquote>\n<p>But activating securelevel at boot is useless unless you protect your boot files. If an attacker gets root access to your server he will be able to modify the boot files is such a way that after a reboot the securelevel will be deactivated. So before you go and protect any files against changes you should protect \/etc\/rc.conf and the scripts that run on system startup.<\/p>\n<blockquote><p><a href=\"http:\/\/nixdoc.net\/man-pages\/FreeBSD\/man1\/chflags.1.html\" title=\"change file flags\" target=\"_blank\">chflags<\/a> schg \/etc\/rc.*<\/p><\/blockquote>\n<p>This will block any changes on those files unless you remove the \"schg\" flag . But if you enable securelevel &gt;= 1 then no one will be able to remove the schg flag.<\/p>\n<p><strong>Warning!: if you do this you will not be able to lower the securelevel once it was raised because you will not be able to modify \/etc\/rc.conf. This may create a problem especially if you are connected from a remote location and you need to do a change.<\/strong><\/p>\n<p>In order to escape from a secure level when needed I came up with the idea of using a password that the root user can write in a file and that password will be hashed with a sha algorithm and then compared to a sha hash stored on disk. If the hashes match then the securelevel will not be activated at boot.<\/p>\n<p>To make this work you have to edit \/etc\/rc.d\/securelevel and add the following lines right before the last line:<\/p>\n<blockquote><p>master= \"$(<a href=\"http:\/\/nixdoc.net\/man-pages\/FreeBSD\/man1\/cat.1.html\" title=\"concatenate and print files\">cat<\/a> \/usr\/local\/.master)\"<\/p>\n<p>test=\"$(cat \/usr\/local\/etc\/.my_test_passwd |openssl sha1)\"<\/p>\n<p>if [ \"$master\" = \"$test\" ] ; then<\/p>\n<p>kern_securelevel_enable=\"NO\"<br \/>\nkern_securelevel=\"-1\"<\/p>\n<p>fi<\/p><\/blockquote>\n<p>Save the file and then you go to create \/usr\/local\/.master like this:<\/p>\n<blockquote><p>cat &lt;&lt;EOF | openssl sha1 &gt;  \/usr\/local\/.master<\/p><\/blockquote>\n<p>Type in your password and then on a single line type EOF and hit enter. This will create a sha1 hash on your password and store that hash in  \/usr\/local\/.master<\/p>\n<p>Now every time you want to lower the securelevel just  put your plain password in \/usr\/local\/etc\/.my_test_passwd and reboot. After reboot the securelevel should be -1<\/p>\n<p>Some may argue that this solution lowers the security introduced by securelevel because if you put your password in .my_test_passwd and attacker may capture it but it's the only way I know how to do this  if I work on a server where I don't have physical access.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>FreeBSD securelevel is a security mechanism implemented in the kernel that restricts certain tasks depending on the level that is active. Not even the super user will be able to bypass this mechanism if well setup. From securelevel man page : -1 Permanently insecure mode &#8211; always run the system in level 0 mode. This &hellip; <a href=\"https:\/\/patchlog.com\/general\/freebsd-securelevel-setup\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">freebsd securelevel setup<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3,1,5],"tags":[352,360,354],"class_list":["post-4","post","type-post","status-publish","format-standard","hentry","category-freebsd","category-general","category-security","tag-freebsd","tag-patches","tag-security"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pofPh-4","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":0,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"wp:attachment":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}