1.\" Copyright (c) 1998, Matthew Dillon. Terms and conditions are those of 2.\" the BSD Copyright as specified in the file "/usr/src/COPYRIGHT" in 3.\" the source tree. 4.\" 5.\" $Id: security.7,v 1.6 1999/03/02 03:55:34 ghelmer Exp $ 6.\" 7.Dd December 20, 1998 8.Dt SECURITY 7 9.Os 10.Sh NAME 11.Nm security 12.Nd introduction to security under FreeBSD 13.Sh DESCRIPTION 14.Pp 15Security is a function that begins and ends with the system administrator. 16While all 17.Bx 18systems are inherently multi-user capable, the job of building and 19maintaining security mechanisms to keep those users 20.Sq honest 21is probably 22one of the single largest undertakings of the sysadmin. Machines are 23only as secure as you make them, and security concerns are ever competing 24with the human necessity for convenience. 25.Ux 26systems, 27in general, are capable of running a huge number of simultaneous processes 28and many of these processes operate as servers - meaning that external entities 29can connect and talk to them. As yesterday's mini-computers and mainframes 30become today's desktops, and as computers become networked and internetworked, 31security becomes an ever bigger issue. 32.Pp 33Security concerns can be split up into several categories: 34.Bl -enum -offset indent 35.It 36Denial of service attacks 37.It 38User account compromises 39.It 40Root compromise through accessible servers 41.It 42Root compromise via user accounts 43.El 44.Pp 45A denial of service attack is an action that deprives the machine of needed 46resources. Typically, D.O.S. attacks are brute-force mechanisms that attempt 47to crash or otherwise make a machine unusable by overwhelming its servers or 48network stack. Some D.O.S. attacks try to take advantages of bugs in the 49networking stack to crash a machine with a single packet. The latter can 50only be fixed by applying a bug fix to the kernel. Attacks on servers can 51often be fixed by properly specifying options to servers to limit the load 52they incur on the system under adverse conditions. Brute-force network 53attacks are harder to deal with. A spoofed-packet attack, for example, is 54nearly impossible to stop short of cutting your system off from the internet. 55.Pp 56A user account compromise is even more common then a D.O.S. attack. Many 57sysadmins still run standard telnetd, rlogind, rshd, and ftpd servers on their 58machines. These servers, by default, do not operate over encrypted 59connections. The result is that if you have any moderate-sized user base, 60one or more of your users logging into your system from a remote location 61.Po 62which is the most common and convenient way to login to a system 63.Pc 64will 65have his or her password sniffed. The attentive system admin will analyze 66his remote access logs occasionally looking for suspicious source addresses 67even for successful logins. 68.Pp 69One must always assume that once an attacker has access to a user account, 70the attacker can break root. However, the reality is that in a well secured 71and maintained system, access to a user account does not necessarily give the 72attacker access to root. The distinction is important because without access 73to root the attacker cannot generally hide his tracks and may, at best, be 74able to remove that user's files and crash the machine, but not touch anyone 75else's files. 76.Pp 77System administrators must keep in mind that there are several ways to break 78root on a machine. The attacker may know the root password, the attacker 79may find a bug in a root-run server and be able to break root over a network 80connection to that server, or the attacker may know of a bug in an suid-root 81program that allows the attacker to break root once he has broken into a 82user's account. 83.Pp 84Security remedies are always implemented in a multi-layered 85.Sq onion peel 86approach and can be categorized as follows: 87.Bl -enum -offset indent 88.It 89Securing root and staff accounts 90.It 91Securing root - root-run servers and suid/sgid binaries 92.It 93Securing user accounts 94.It 95Securing the password file 96.It 97Securing the kernel core, raw devices, and filesystems 98.It 99Checking file integrity: binaries, configuration files, and so forth 100.It 101Paranoia 102.El 103.Sh SECURING THE ROOT ACCOUNT AND SECURING STAFF ACCOUNTS 104.Pp 105Don't bother securing staff accounts if you haven't secured the root 106account. Most systems have a password assigned to the root account. The 107first thing you do is assume that the password is 108.Sq always 109compromised. 110To secure the root account you make sure that it is not possible to login 111to the root account using the root password from a random user account or 112over the network. If you haven't already, configure telnetd, rlogind, and 113all other servers that handle login operations to refuse root logins, period, 114whether the right password is given or not. Allow direct root logins only 115via the system console. The 116.Sq Pa /etc/ttys 117file comes in handy here and is 118secure by default on most systems, but a good sysadmin always checks to make 119sure. 120.Pp 121Of course, as a sysadmin you have to be able to get to root, so we open up 122a few holes. But we make sure these holes require additional password 123verification to operate. One way to make root accessible is to add appropriate 124staff accounts to the wheel group 125.Pq in Pa /etc/group . 126The staff members placed 127in the wheel group are allowed to 128.Sq su 129to root. You should never give staff 130members native wheel access via their entry in the password file... put staff 131in a 132.Sq staff 133group or something and only add those that really need root to 134the wheel group. Unfortunately the wheel mechanism still allows an intruder to 135break root if the intruder has gotten hold of your password file - he need only 136break the root password and the password of one of the staff accounts that 137happens to be in the wheel group. So while the wheel mechanism is usable, 138it isn't much safer then not having a wheel group at all. 139.Pp 140An indirect way to secure the root account is to secure your staff accounts 141by using an alternative login access method and *'ing out the crypted password 142for the staff accounts. This way an intruder may be able to steal the password 143file but will not be able to break into any staff accounts (or, indirectly, 144root, even if root has a crypted password associated with it). Staff members 145get into their staff accounts through a secure login mechanism such as 146.Xr kerberos 1 147or 148.Xr ssh 1 149.Po 150see 151.Pa /usr/ports/security/ssh 152.Pc 153using a private/public 154key pair. When you use something like kerberos you generally must secure 155the machines which run the kerberos servers and your desktop workstation. 156When you use a public/private key pair with ssh, you must generally secure 157the machine you are logging in FROM 158.Pq typically your workstation , 159but you can 160also add an additional layer of protection to the key pair by password 161protecting the keypair when you create it with 162.Xr ssh-keygen 1 . 163Being able 164to *-out the passwords for staff accounts also guarantees that staff members 165can only login through secure access methods that you have setup. You can 166thus force all staff members to use secure, encrypted connections for 167all their sessions which closes an important hole used by many intruders: That 168of sniffing the network from an unrelated, less secure machine. 169.Pp 170The more indirect security mechanisms also assume that you are logging in 171from a more restrictive server to a less restrictive server. For example, 172if your main box is running all sorts of servers, your workstation shouldn't 173be running any. In order for your workstation to be reasonably secure 174you should run as few servers as possible, up to and including no servers 175at all, and you should run a password-protected screen blanker. 176Of course, given physical access to 177a workstation an attacker can break any sort of security you put on it. 178This is definitely a problem that you should consider but you should also 179consider the fact that the vast majority of break-ins occur remotely, over 180a network, from people who do not have physical access to your workstation or 181servers. 182.Pp 183Using something like kerberos also gives you the ability to disable or 184change the password for a staff account in one place and have it immediately 185effect all the machine the staff member may have an account on. If a staff 186member's account gets compromised, the ability to instantly change his 187password on all machines should not be underrated. With discrete passwords, 188changing a password on N machines can be a mess. You can also impose 189re-passwording restrictions with kerberos: not only can a kerberos ticket 190be made to timeout after a while, but the kerberos system can require that 191the user choose a new password after a certain period of time 192.Pq say, once a month . 193.Sh SECURING ROOT - ROOT-RUN SERVERS AND SUID/SGID BINARIES 194.Pp 195The prudent sysadmin only runs the servers he needs to, no more, no less. Be 196aware that third party servers are often the most bug-prone. For example, 197running an old version of imapd or popper is like giving a universal root 198ticket out to the entire world. Never run a server that you have not checked 199out carefully. Many servers do not need to be run as root. For example, 200the ntalk, comsat, and finger daemons can be run in special user 201.Sq sandboxes . 202A sandbox isn't perfect unless you go to a large amount of trouble, but the 203onion approach to security still stands: If someone is able to break in 204through a server running in a sandbox, they still have to break out of the 205sandbox. The more layers the attacker must break through, the lower the 206likelihood of his success. Root holes have historically been found in 207virtually every server ever run as root, including basic system servers. 208If you are running a machine through which people only login via sshd and 209never login via telnetd or rshd or rlogind, then turn off those services! 210.Pp 211.Bx Free 212now defaults to running ntalkd, comsat, and finger in a sandbox. 213Another program which may be a candidate for running in a sandbox is 214.Xr named 8 . 215The default rc.conf includes the arguments necessary to run 216named in a sandbox in a commented-out form. Depending on whether you 217are installing a new system or upgrading an existing system, the special 218user accounts used by these sandboxes may not be installed. The prudent 219sysadmin would research and implement sandboxes for servers whenever possible. 220.Pp 221There are a number of other servers that typically do not run in sandboxes: 222sendmail, popper, imapd, ftpd, and others. There are alternatives to 223some of these, but installing them may require more work then you are willing 224to put 225.Pq the convenience factor strikes again . 226You may have to run these 227servers as root and rely on other mechanisms to detect break-ins that might 228occur through them. 229.Pp 230The other big potential root hole in a system are the suid-root and sgid 231binaries installed on the system. Most of these binaries, such as rlogin, 232reside in 233.Pa /bin , 234.Pa /sbin , 235.Pa /usr/bin , 236or 237.Pa /usr/sbin . 238While nothing is 100% safe, 239the system-default suid and sgid binaries can be considered reasonably safe. 240Still, root holes are occasionally found in these binaries. A root hole 241was found in Xlib in 1998 that made xterm 242.Pq which is typically suid 243vulnerable. 244It is better to be safe then sorry and the prudent sysadmin will restrict suid 245binaries that only staff should run to a special group that only staff can 246access, and get rid of 247.Pq chmod 000 248any suid binaries that nobody uses. A 249server with no display generally does not need an xterm binary. Sgid binaries 250can be almost as dangerous. If an intruder can break an sgid-kmem binary the 251intruder might be able to read 252.Pa /dev/kmem 253and thus read the crypted password 254file, potentially compromising any passworded account. An intruder that breaks 255the tty group can write to almost any user's tty. If a user is running a terminal 256program or emulator with a talk-back feature, the intruder can potentially 257generate a data stream that causes the user's terminal to echo a command, which 258is then run as that user. 259.Sh SECURING USER ACCOUNTS 260.Pp 261User accounts are usually the most difficult to secure. While you can impose 262Draconian access restrictions on your staff and *-out their passwords, you 263may not be able to do so with any general user accounts you might have. If 264you do have sufficient control then you may win out and be able to secure the 265user accounts properly. If not, you simply have to be more vigilant in your 266monitoring of those accounts. Use of ssh and kerberos for user accounts is 267more problematic, but still a very good solution compared to a crypted 268password. 269.Sh SECURING THE PASSWORD FILE 270.Pp 271The only sure fire way is to *-out as many passwords as you can and 272use ssh or kerberos for access to those accounts. Even though the 273crypted password file 274.Pq Pa /etc/spwd.db 275can only be read by root, it may 276be possible for an intruder to obtain read access to that file even if the 277attacker cannot obtain root-write access. 278.Pp 279Your security scripts should always check for and report changes to 280the password file 281.Po 282see 283.Sq Checking file integrity 284below 285.Pc . 286.Sh SECURING THE KERNEL CORE, RAW DEVICES, AND FILESYSTEMS 287.Pp 288If an attacker breaks root he can do just about anything, but there 289are certain conveniences. For example, most modern kernels have a 290packet sniffing device driver built in. Under 291.Bx Free 292it is called 293the 294.Sq bpf 295device. An intruder will commonly attempt to run a packet sniffer 296on a compromised machine. You do not need to give the intruder the 297capability and most systems should not have the bpf device compiled in. 298.Pp 299But even if you turn off the bpf device, 300you still have 301.Pa /dev/mem 302and 303.Pa /dev/kmem 304to worry about. For that matter, 305the intruder can still write raw devices. 306Also, there is another kernel feature called 307.Xr kldload 8 . 308An enterprising intruder can use a KLD module to install 309his own bpf device or other sniffing device on a running kernel. 310To avoid these problems you have to run 311the kernel at a higher secure level, at least securelevel 1. The securelevel 312can be set with a sysctl on the kern.securelevel variable. Once you have 313set the securelevel to 1, write access to raw devices will be denied and 314special chflags flags, such as 315.Sq schg , 316will be enforced. You must also ensure 317that the 318.Sq schg 319flag is set on critical startup binaries, directories, and 320script files - everything that gets run up to the point where the securelevel 321is set. This might be overdoing it, and upgrading the system is much more 322difficult when you operate at a higher secure level. You may compromise and 323run the system at a higher secure level but not set the schg flag for every 324system file and directory under the sun. 325.Sh CHECKING FILE INTEGRITY: BINARIES, CONFIG FILES, ETC 326.Pp 327When it comes right down to it, you can only protect your core system 328configuration and control files so much before the convenience factor 329rears its ugly head. The last layer of your security onion is perhaps 330the most important - detection. 331.Pp 332The only correct way to check a system's file integrity is via another, 333more secure system. It is fairly easy to setup a 334.Sq secure 335system: you 336simply do not run any services on it. With a secure system in place you 337can then give it access to other system's root spaces via ssh. This may 338seem like a security breech, but you have to put your trust somewhere and 339as long as you don't do something stupid like run random servers it really 340is possible to build a secure machine. When I say 341.Sq secure 342here, I assuming 343physical access security as well, of course. Given a secure machine with 344root access on all your other machines, you can then write security scripts 345ON the secure machine to check the other machines on the system. The most 346common way of checking is to have the security script 347.Xr scp 1 348over a find 349and md5 binary and then ssh a shell command to the remote machine to md5 350all the files in the system 351.Po 352or, at least, the 353.Pa / , 354.Pa /var , 355and 356.Pa /usr 357partitions! 358.Pc . 359The security machine copies the results to a file and diff's them against 360results from a previous run (or compares the results against its own 361binaries), then emails each staff member a daily report of differences. 362.Pp 363Another way to do this sort of check is to NFS export the major filesystems 364from every other machine to the security machine. This is somewhat more 365network intensive but also virtually impossible for an intruder to detect 366or spoof. 367.Pp 368A good security script will also check for changes to user and staff members 369access configuration files: .rhosts, .shosts, .ssh/authorized_keys, and 370so forth... files that might fall outside the purview of the MD5 check. 371.Pp 372A good security script will check for suid and sgid binaries on all 373filesystems and report their absolute existence as well as a diff against 374the previous report or some baseline 375.Pq say, make a baseline once a week . 376While you can turn off the ability to run suid and sgid binaries on certain 377filesystems through the 378.Sq nosuid 379option in fstab/mount, you cannot turn this 380off on root and anyone who breaks root can just install their binary there. 381If you have a huge amount of user disk space, though, it may be useful to 382disallow suid binaries and devices 383.Po 384.Sq nodev 385option 386.Pc 387on the user partitions 388so you do not have to scan them for such. I would scan them anyway, though, 389at least once a week, since the object of this onion layer is detection of 390a break-in. 391.Pp 392Process accounting 393.Po 394see 395.Xr accton 1 396.Pc 397is a relatively low-overhead feature of 398the operating system which I recommend using as a post-break-in evaluation 399mechanism. It is especially useful in tracking down how an intruder has 400actually broken root on a system, assuming the file is still intact after 401the break-in occurs. 402.Pp 403Finally, security scripts should process the log files and the logs themselves 404should be generated in as secured a manner as possible - remote syslog can be 405very useful. An intruder tries to cover his tracks, and log files are critical 406to the sysadmin trying to track down the time and method of the initial break-in. 407.Sh PARANOIA 408.Pp 409A little paranoia never hurts. As a rule, a sysadmin can add any number 410of security features as long as they do not effect convenience, and 411can add security features that do effect convenience with some added 412thought. 413.Sh SPECIAL SECTION ON D.O.S. ATTACKS 414.Pp 415This section covers Denial of Service attacks. A DOS attack is typically 416a packet attack. While there isn't much you can do about modern spoofed 417packet attacks that saturate your network, you can generally limit the damage 418by ensuring that the attacks cannot take down your servers. 419.Bl -enum -offset indent 420.It 421Limiting server forks 422.It 423Limiting springboard attacks (ICMP response attacks, ping broadcast, etc...) 424.It 425Kernel Route Cache 426.El 427.Pp 428A common DOS attack is against a forking server that attempts to cause the 429server to eat processes, file descriptors, and memory until the machine 430dies. Inetd 431.Po 432see 433.Xr inetd 8 434.Pc 435has several options to limit this sort of attack. 436It should be noted that while it is possible to prevent a machine from going 437down it is not generally possible to prevent a service from being disrupted 438by the attack. Read the inetd manual page carefully and pay specific attention 439to the 440.Fl c , 441.Fl C , 442and 443.Fl R 444options. Note that spoofed-IP attacks will circumvent 445the 446.Fl C 447option to inetd, so typically a combination of options must be used. 448Some standalone servers have self-fork-limitation parameters. 449.Pp 450Sendmail has its 451.Fl OMaxDaemonChildren 452option which tends to work much 453better than trying to use sendmail's load limiting options due to the 454load lag. You should specify a 455.Cm MaxDaemonChildren 456parameter when you start 457sendmail high enough to handle your expected load but no so high that the 458computer cannot handle that number of sendmails without falling on its face. 459It is also prudent to run sendmail in queued mode 460.Pq Fl ODeliveryMode=queued 461and to run the daemon 462.Pq Cm sendmail -bd 463separate from the queue-runs 464.Pq Cm sendmail -q15m . 465If you still want realtime delivery you can run the queue 466at a much lower interval, such as 467.Fl q1m , 468but be sure to specify a reasonable 469.Cm MaxDaemonChildren 470option for that sendmail to prevent cascade failures. 471.Pp 472Syslogd can be attacked directly and it is strongly recommended that you use 473the 474.Fl s 475option whenever possible, and the 476.Fl a 477option otherwise. 478.Pp 479You should also be fairly careful 480with connect-back services such as tcpwrapper's reverse-identd, which can 481be attacked directly. You generally do not want to use the reverse-ident 482feature of tcpwrappers for this reason. 483.Pp 484It is a very good idea to protect internal services from external access 485by firewalling them off at your border routers. The idea here is to prevent 486saturation attacks from outside your LAN, not so much to protect internal 487services from network-based root compromise. Always configure an exclusive 488firewall, i.e. 489.So 490firewall everything *except* ports A, B, C, D, and M-Z 491.Sc . 492This 493way you can firewall off all of your low ports except for certain specific 494services such as named 495.Pq if you are primary for a zone , 496ntalkd, sendmail, 497and other internet-accessible services. 498If you try to configure the firewall the other 499way - as an inclusive or permissive firewall, there is a good chance that you 500will forget to 501.Sq close 502a couple of services or that you will add a new internal 503service and forget to update the firewall. You can still open up the 504high-numbered port range on the firewall to allow permissive-like operation 505without compromising your low ports. Also take note that 506.Bx Free 507allows you to 508control the range of port numbers used for dynamic binding via the various 509net.inet.ip.portrange sysctl's 510.Pq sysctl -a \&| fgrep portrange , 511which can also 512ease the complexity of your firewall's configuration. I usually use a normal 513first/last range of 4000 to 5000, and a hiport range of 49152 to 65535, then 514block everything under 4000 off in my firewall 515.Po 516except for certain specific 517internet-accessible ports, of course 518.Pc . 519.Pp 520Another common DOS attack is called a springboard attack - to attack a server 521in a manner that causes the server to generate responses which then overload 522the server, the local network, or some other machine. The most common attack 523of this nature is the ICMP PING BROADCAST attack. The attacker spoofs ping 524packets sent to your LAN's broadcast address with the source IP address set 525to the actual machine they wish to attack. If your border routers are not 526configured to stomp on ping's to broadcast addresses, your LAN winds up 527generating sufficient responses to the spoofed source address to saturate the 528victim, especially when the attacker uses the same trick on several dozen 529broadcast addresses over several dozen different networks at once. Broadcast 530attacks of over a hundred and twenty megabits have been measured. A second 531common springboard attack is against the ICMP error reporting system. By 532constructing packets that generate ICMP error responses, an attacker can 533saturate a server's incoming network and cause the server to saturate its 534outgoing network with ICMP responses. This type of attack can also crash the 535server by running it out of mbuf's, especially if the server cannot drain the 536ICMP responses it generates fast enough. The 537.Bx Free 538kernel has a new kernel 539compile option called ICMP_BANDLIM which limits the effectiveness of these 540sorts of attacks. The last major class of springboard attacks is related to 541certain internal inetd services such as the udp echo service. An attacker 542simply spoofs a UDP packet with the source address being server A's echo port, 543and the destination address being server B's echo port, where server A and B 544are both on your LAN. The two servers then bounce this one packet back and 545forth between each other. The attacker can overload both servers and their 546LANs simply by injecting a few packets in this manner. Similar problems 547exist with the internal chargen port. A competent sysadmin will turn off all 548of these inetd-internal test services. 549.Pp 550Spoofed packet attacks may also be used to overload the kernel route cache. 551Refer to the net.inet.ip.rtexpire, rtminexpire, and rtmaxcache sysctl 552parameters. A spoofed packet attack that uses a random source IP will cause 553the kernel to generate a temporary cached route in the route table, viewable 554with 555.Sq netstat -rna \&| fgrep W3 . 556These routes typically timeout in 1600 557seconds or so. If the kernel detects that the cached route table has gotten 558too big it will dynamically reduce the rtexpire but will never decrease it to 559less then rtminexpire. There are two problems: (1) The kernel does not react 560quickly enough when a lightly loaded server is suddenly attacked, and (2) The 561rtminexpire is not low enough for the kernel to survive a sustained attack. 562If your servers are connected to the internet via a T3 or better it may be 563prudent to manually override both rtexpire and rtminexpire via 564.Xr sysctl 8 . 565Never set either parameter to zero 566.Pq unless you want to crash the machine :-) . 567Setting both parameters to 2 seconds should be sufficient to protect the route 568table from attack. 569 570.Sh SEE ALSO 571.Pp 572.Xr accton 1 , 573.Xr chflags 1 , 574.Xr find 1 , 575.Xr kerberos 1 , 576.Xr md5 1 , 577.Xr ssh 1 , 578.Xr sshd 1 , 579.Xr syslogd 1 , 580.Xr xdm 1 , 581.Xr sysctl 8 582.Sh HISTORY 583The 584.Nm 585manual page was originally written by 586.An Matthew Dillon 587and first appeared 588in 589.Fx 3.1 , 590December 1998. 591