(no commit message)
[ikiwiki.git] / docs / newhandbook / Security / index.mdwn
1
2
3 # Security 
4 ***Much of this chapter has been taken from the security(7) manual page by Matthew Dillon. ***
5
6 [[!toc levels=3]]
7
8
9
10 ## Synopsis 
11
12
13
14 This chapter will provide a basic introduction to system security concepts, some general good rules of thumb, and some advanced topics under DragonFly. A lot of the topics covered here can be applied to system and Internet security in general as well. The Internet is no longer a ***friendly*** place in which everyone wants to be your kind neighbor. Securing your system is imperative to protect your data, intellectual property, time, and much more from the hands of hackers and the like.
15
16
17
18 DragonFly provides an array of utilities and mechanisms to ensure the integrity and security of your system and network.
19
20
21
22 After reading this chapter, you will know:
23
24
25
26
27 * Basic system security concepts, in respect to DragonFly.
28
29
30 * About the various crypt mechanisms available in DragonFly, such as DES and MD5.
31
32
33 * How to set up one-time password authentication.
34
35
36 * How to set up  **KerberosIV** .
37
38
39 * How to set up  **Kerberos5** .
40
41
42 * How to create firewalls using IPFW.
43
44
45 * How to configure IPsec and create a VPN between DragonFly/Windows® machines.
46
47
48 * How to configure and use  **OpenSSH** , DragonFly's SSH implementation.
49
50
51
52 Before reading this chapter, you should:
53
54
55
56
57 * Understand basic DragonFly and Internet concepts.
58
59
60
61
62
63
64
65 CategoryHandbook
66
67 Category
68
69
70
71
72 ## Introduction 
73
74
75
76 Security is a function that begins and ends with the system administrator. While all BSD UNIX® multi-user systems have some inherent security, the job of building and maintaining additional security mechanisms to keep those users ***honest*** is probably one of the single largest undertakings of the sysadmin. Machines are only as secure as you make them, and security concerns are ever competing with the human necessity for convenience. UNIX systems, in general, are capable of running a huge number of simultaneous processes and many of these processes operate as servers -- meaning that external entities can connect and talk to them. As yesterday's mini-computers and mainframes become today's desktops, and as computers become networked and internetworked, security becomes an even bigger issue.
77
78
79
80 Security is best implemented through a layered ***onion*** approach. In a nutshell, what you want to do is to create as many layers of security as are convenient and then carefully monitor the system for intrusions. You do not want to overbuild your security or you will interfere with the detection side, and detection is one of the single most important aspects of any security mechanism. For example, it makes little sense to set the `schg` flags (see [chflags(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#chflags&section1)) on every system binary because while this may temporarily protect the binaries, it prevents an attacker who has broken in from making an easily detectable change that may result in your security mechanisms not detecting the attacker at all.
81
82
83
84 System security also pertains to dealing with various forms of attack, including attacks that attempt to crash, or otherwise make a system unusable, but do not attempt to compromise the `root` account (***break root***). Security concerns can be split up into several categories:
85
86
87
88   1. Denial of service attacks.
89
90   1. User account compromises.
91
92   1. Root compromise through accessible servers.
93
94   1. Root compromise via user accounts.
95
96   1. Backdoor creation.
97
98
99
100 A denial of service attack is an action that deprives the machine of needed resources. Typically, DoS attacks are brute-force mechanisms that attempt to crash or otherwise make a machine unusable by overwhelming its servers or network stack. Some DoS attacks try to take advantage of bugs in the networking stack to crash a machine with a single packet. The latter can only be fixed by applying a bug fix to the kernel. Attacks on servers can often be fixed by properly specifying options to limit the load the servers incur on the system under adverse conditions. Brute-force network attacks are harder to deal with. A spoofed-packet attack, for example, is nearly impossible to stop, short of cutting your system off from the Internet. It may not be able to take your machine down, but it can saturate your Internet connection.
101
102
103
104 A user account compromise is even more common than a DoS attack. Many sysadmins still run standard  **telnetd** ,  **rlogind** ,  **rshd** , and  **ftpd**  servers on their machines. These servers, by default, do not operate over encrypted connections. The result is that if you have any moderate-sized user base, one or more of your users logging into your system from a remote location (which is the most common and convenient way to login to a system) will have his or her password sniffed. The attentive system admin will analyze his remote access logs looking for suspicious source addresses even for successful logins.
105
106
107
108 One must always assume that once an attacker has access to a user account, the attacker can break `root`. However, the reality is that in a well secured and maintained system, access to a user account does not necessarily give the attacker access to `root`. The distinction is important because without access to `root` the attacker cannot generally hide his tracks and may, at best, be able to do nothing more than mess with the user's files, or crash the machine. User account compromises are very common because users tend not to take the precautions that sysadmins take.
109
110
111
112 System administrators must keep in mind that there are potentially many ways to break `root` on a machine. The attacker may know the `root` password, the attacker may find a bug in a root-run server and be able to break `root` over a network connection to that server, or the attacker may know of a bug in a suid-root program that allows the attacker to break `root` once he has broken into a user's account. If an attacker has found a way to break `root` on a machine, the attacker may not have a need to install a backdoor. Many of the `root` holes found and closed to date involve a considerable amount of work by the attacker to cleanup after himself, so most attackers install backdoors. A backdoor provides the attacker with a way to easily regain `root` access to the system, but it also gives the smart system administrator a convenient way to detect the intrusion. Making it impossible for an attacker to install a backdoor may actually be detrimental to your security, because it will not close off the hole the attacker found to break in the first place.
113
114
115
116 Security remedies should always be implemented with a multi-layered ***onion peel*** approach and can be categorized as follows:
117
118
119
120   1. Securing `root` and staff accounts.
121
122   1. Securing `root` -- root-run servers and suid/sgid binaries.
123
124   1. Securing user accounts.
125
126   1. Securing the password file.
127
128   1. Securing the kernel core, raw devices, and filesystems.
129
130   1. Quick detection of inappropriate changes made to the system.
131
132   1. Paranoia.
133
134
135
136 The next section of this chapter will cover the above bullet items in greater depth.
137
138
139
140
141
142
143
144 CategoryHandbook
145
146 CategoryHandbook-security
147
148
149
150
151
152 ## Securing DragonFly 
153
154
155
156  **Command vs. Protocol:**  Throughout this document, we will use  **bold**  text to refer to a command or application. This is used for instances such as ssh, since it is a protocol as well as command.
157
158
159
160 The sections that follow will cover the methods of securing your DragonFly system that were mentioned in the [last section](security-intro.html) of this chapter.
161
162
163
164 ### Securing the root Account and Staff Accounts 
165
166
167
168 First off, do not bother securing staff accounts if you have not secured the `root` account. Most systems have a password assigned to the `root` account. The first thing you do is assume that the password is ***always*** compromised. This does not mean that you should remove the password. The password is almost always necessary for console access to the machine. What it does mean is that you should not make it possible to use the password outside of the console or possibly even with the [su(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#su&section1) command. For example, make sure that your pty's are specified as being insecure in the `/etc/ttys` file so that direct `root` logins via `telnet` or `rlogin` are disallowed. If using other login services such as  **sshd** , make sure that direct `root` logins are disabled there as well. You can do this by editing your `/etc/ssh/sshd_config` file, and making sure that `PermitRootLogin` is set to `NO`. Consider every access method -- services such as FTP often fall through the cracks. Direct `root` logins should only be allowed via the system console.
169
170
171
172 Of course, as a sysadmin you have to be able to get to `root`, so we open up a few holes. But we make sure these holes require additional password verification to operate. One way to make `root` accessible is to add appropriate staff accounts to the `wheel` group (in `/etc/group`). The staff members placed in the `wheel` group are allowed to `su` to `root`. You should never give staff members native `wheel` access by putting them in the `wheel` group in their password entry. Staff accounts should be placed in a `staff` group, and then added to the `wheel` group via the `/etc/group` file. Only those staff members who actually need to have `root` access should be placed in the `wheel` group. It is also possible, when using an authentication method such as Kerberos, to use Kerberos' `.k5login` file in the `root` account to allow a [ksu(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ksu&section1) to `root` without having to place anyone at all in the `wheel` group. This may be the better solution since the `wheel` mechanism still allows an intruder to break `root` if the intruder has gotten hold of your password file and can break into a staff account. While having the `wheel` mechanism is better than having nothing at all, it is not necessarily the safest option.
173
174
175
176 An indirect way to secure staff accounts, and ultimately `root` access is to use an alternative login access method and do what is known as ***starring*** out the encrypted password for the staff accounts. Using the [vipw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#vipw&section8) command, one can replace each instance of an encrypted password with a single `*` character. This command will update the `/etc/master.passwd` file and user/password database to disable password-authenticated logins.
177
178
179
180 A staff account entry such as:
181
182
183
184     
185
186     foobar:R9DT/Fa1/LV9U:1000:1000::0:0:Foo Bar:/home/foobar:/usr/local/bin/tcsh
187
188
189
190
191
192 Should be changed to this:
193
194
195
196     
197
198     foobar:*:1000:1000::0:0:Foo Bar:/home/foobar:/usr/local/bin/tcsh
199
200
201
202
203
204 This change will prevent normal logins from occurring, since the encrypted password will never match `*`. With this done, staff members must use another mechanism to authenticate themselves such as [kerberos(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#kerberos&section1) or [ssh(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=ssh&section=1&manpath=OpenBSD+3.3) using a public/private key pair. When using something like Kerberos, one generally must secure the machines which run the Kerberos servers and your desktop workstation. When using a public/private key pair with ssh, one must generally secure the machine used to login ***from*** (typically one's workstation). An additional layer of protection can be added to the key pair by password protecting the key pair when creating it with [ssh-keygen(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=ssh-keygen&section=1). Being able to ***star*** out the passwords for staff accounts also guarantees that staff members can only login through secure access methods that you have set up. This forces all staff members to use secure, encrypted connections for all of their sessions, which closes an important hole used by many intruders: sniffing the network from an unrelated, less secure machine.
205
206
207
208 The more indirect security mechanisms also assume that you are logging in from a more restrictive server to a less restrictive server. For example, if your main box is running all sorts of servers, your workstation should not be running any. In order for your workstation to be reasonably secure you should run as few servers as possible, up to and including no servers at all, and you should run a password-protected screen blanker. Of course, given physical access to a workstation an attacker can break any sort of security you put on it. This is definitely a problem that you should consider, but you should also consider the fact that the vast majority of break-ins occur remotely, over a network, from people who do not have physical access to your workstation or servers.
209
210
211
212 Using something like Kerberos also gives you the ability to disable or change the password for a staff account in one place, and have it immediately affect all the machines on which the staff member may have an account. If a staff member's account gets compromised, the ability to instantly change his password on all machines should not be underrated. With discrete passwords, changing a password on N machines can be a mess. You can also impose re-passwording restrictions with Kerberos: not only can a Kerberos ticket be made to timeout after a while, but the Kerberos system can require that the user choose a new password after a certain period of time (say, once a month).
213
214
215
216 ### Securing Root-run Servers and SUID/SGID Binaries 
217
218
219
220 The prudent sysadmin only runs the servers he needs to, no more, no less. Be aware that third party servers are often the most bug-prone. For example, running an old version of  **imapd**  or  **popper**  is like giving a universal `root` ticket out to the entire world. Never run a server that you have not checked out carefully. Many servers do not need to be run as `root`. For example, the  **ntalk** ,  **comsat** , and  **finger**  daemons can be run in special user ***sandboxes***. A sandbox is not perfect, unless you go through a large amount of trouble, but the onion approach to security still stands: If someone is able to break in through a server running in a sandbox, they still have to break out of the sandbox. The more layers the attacker must break through, the lower the likelihood of his success. Root holes have historically been found in virtually every server ever run as `root`, including basic system servers. If you are running a machine through which people only login via  **sshd**  and never login via  **telnetd**  or  **rshd**  or  **rlogind** , then turn off those services!
221
222
223
224 DragonFly now defaults to running  **ntalkd** ,  **comsat** , and  **finger**  in a sandbox. Another program which may be a candidate for running in a sandbox is [named(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#named&section8). `/etc/defaults/rc.conf` includes the arguments necessary to run  **named**  in a sandbox in a commented-out form. Depending on whether you are installing a new system or upgrading an existing system, the special user accounts used by these sandboxes may not be installed. The prudent sysadmin would research and implement sandboxes for servers whenever possible.
225
226
227
228 There are a number of other servers that typically do not run in sandboxes:  **sendmail** ,  **popper** ,  **imapd** ,  **ftpd** , and others. There are alternatives to some of these, but installing them may require more work than you are willing to perform (the convenience factor strikes again). You may have to run these servers as `root` and rely on other mechanisms to detect break-ins that might occur through them.
229
230
231
232 The other big potential `root` holes in a system are the suid-root and sgid binaries installed on the system. Most of these binaries, such as  **rlogin** , reside in `/bin`, `/sbin`, `/usr/bin`, or `/usr/sbin`. While nothing is 100% safe, the system-default suid and sgid binaries can be considered reasonably safe. Still, `root` holes are occasionally found in these binaries. A `root` hole was found in `Xlib` in 1998 that made  **xterm**  (which is typically suid) vulnerable. It is better to be safe than sorry and the prudent sysadmin will restrict suid binaries, that only staff should run, to a special group that only staff can access, and get rid of (`chmod 000`) any suid binaries that nobody uses. A server with no display generally does not need an  **xterm**  binary. Sgid binaries can be almost as dangerous. If an intruder can break an sgid-kmem binary, the intruder might be able to read `/dev/kmem` and thus read the encrypted password file, potentially compromising any passworded account. Alternatively an intruder who breaks group `kmem` can monitor keystrokes sent through pty's, including pty's used by users who login through secure methods. An intruder that breaks the `tty` group can write to almost any user's tty. If a user is running a terminal program or emulator with a keyboard-simulation feature, the intruder can potentially generate a data stream that causes the user's terminal to echo a command, which is then run as that user.
233
234
235
236 ### Securing User Accounts 
237
238
239
240 User accounts are usually the most difficult to secure. While you can impose Draconian access restrictions on your staff and ***star*** out their passwords, you may not be able to do so with any general user accounts you might have. If you do have sufficient control, then you may win out and be able to secure the user accounts properly. If not, you simply have to be more vigilant in your monitoring of those accounts. Use of ssh and Kerberos for user accounts is more problematic, due to the extra administration and technical support required, but still a very good solution compared to a crypted password file.
241
242
243
244 ### Securing the Password File 
245
246
247
248 The only sure fire way is to `*` out as many passwords as you can and use ssh or Kerberos for access to those accounts. Even though the encrypted password file (`/etc/spwd.db`) can only be read by `root`, it may be possible for an intruder to obtain read access to that file even if the attacker cannot obtain root-write access.
249
250
251
252 Your security scripts should always check for and report changes to the password file (see the [Checking file integrity](securing-freebsd.html#SECURITY-INTEGRITY) section below).
253
254
255
256 ### Securing the Kernel Core, Raw Devices, and Filesystems 
257
258
259
260 If an attacker breaks `root` he can do just about anything, but there are certain conveniences. For example, most modern kernels have a packet sniffing device driver built in. Under DragonFly it is called the `bpf` device. An intruder will commonly attempt to run a packet sniffer on a compromised machine. You do not need to give the intruder the capability and most systems do not have the need for the `bpf` device compiled in.
261
262
263
264 But even if you turn off the `bpf` device, you still have `/dev/mem` and `/dev/kmem` to worry about. For that matter, the intruder can still write to raw disk devices. Also, there is another kernel feature called the module loader, [kldload(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#kldload&section8). An enterprising intruder can use a KLD module to install his own `bpf` device, or other sniffing device, on a running kernel. To avoid these problems you have to run the kernel at a higher secure level, at least securelevel 1. The securelevel can be set with a `sysctl` on the `kern.securelevel` variable. Once you have set the securelevel to 1, write access to raw devices will be denied and special `chflags` flags, such as `schg`, will be enforced. You must also ensure that the `schg` flag is set on critical startup binaries, directories, and script files -- everything that gets run up to the point where the securelevel is set. This might be overdoing it, and upgrading the system is much more difficult when you operate at a higher secure level. You may compromise and run the system at a higher secure level but not set the `schg` flag for every system file and directory under the sun. Another possibility is to simply mount `/` and `/usr` read-only. It should be noted that being too Draconian in what you attempt to protect may prevent the all-important detection of an intrusion.
265
266
267
268 ### Checking File Integrity: Binaries, Configuration Files, Etc. 
269
270
271
272 When it comes right down to it, you can only protect your core system configuration and control files so much before the convenience factor rears its ugly head. For example, using `chflags` to set the `schg` bit on most of the files in `/` and `/usr` is probably counterproductive, because while it may protect the files, it also closes a detection window. The last layer of your security onion is perhaps the most important -- detection. The rest of your security is pretty much useless (or, worse, presents you with a false sense of safety) if you cannot detect potential incursions. Half the job of the onion is to slow down the attacker, rather than stop him, in order to give the detection side of the equation a chance to catch him in the act.
273
274
275
276 The best way to detect an incursion is to look for modified, missing, or unexpected files. The best way to look for modified files is from another (often centralized) limited-access system. Writing your security scripts on the extra-secure limited-access system makes them mostly invisible to potential attackers, and this is important. In order to take maximum advantage you generally have to give the limited-access box significant access to the other machines in the business, usually either by doing a read-only NFS export of the other machines to the limited-access box, or by setting up ssh key-pairs to allow the limited-access box to ssh to the other machines. Except for its network traffic, NFS is the least visible method -- allowing you to monitor the filesystems on each client box virtually undetected. If your limited-access server is connected to the client boxes through a switch, the NFS method is often the better choice. If your limited-access server is connected to the client boxes through a hub, or through several layers of routing, the NFS method may be too insecure (network-wise) and using ssh may be the better choice even with the audit-trail tracks that ssh lays.
277
278
279
280 Once you give a limited-access box, at least read access to the client systems it is supposed to monitor, you must write scripts to do the actual monitoring. Given an NFS mount, you can write scripts out of simple system utilities such as [find(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#find&section1) and [md5(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=md5&section=1). It is best to physically md5 the client-box files at least once a day, and to test control files such as those found in `/etc` and `/usr/local/etc` even more often. When mismatches are found, relative to the base md5 information the limited-access machine knows is valid, it should scream at a sysadmin to go check it out. A good security script will also check for inappropriate suid binaries and for new or deleted files on system partitions such as `/` and `/usr`.
281
282
283
284 When using ssh rather than NFS, writing the security script is much more difficult. You essentially have to `scp` the scripts to the client box in order to run them, making them visible, and for safety you also need to `scp` the binaries (such as find) that those scripts use. The  **ssh**  client on the client box may already be compromised. All in all, using ssh may be necessary when running over insecure links, but it is also a lot harder to deal with.
285
286
287
288 A good security script will also check for changes to user and staff members access configuration files: `.rhosts`, `.shosts`, `.ssh/authorized_keys` and so forth... files that might fall outside the purview of the `MD5` check.
289
290
291
292 If you have a huge amount of user disk space, it may take too long to run through every file on those partitions. In this case, setting mount flags to disallow suid binaries and devices on those partitions is a good idea. The `nodev` and `nosuid` options (see [mount(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#mount&section8)) are what you want to look into. You should probably scan them anyway, at least once a week, since the object of this layer is to detect a break-in whether or not the break-in is effective.
293
294
295
296 Process accounting (see [accton(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#accton&section8)) is a relatively low-overhead feature of the operating system which might help as a post-break-in evaluation mechanism. It is especially useful in tracking down how an intruder has actually broken into a system, assuming the file is still intact after the break-in occurs.
297
298
299
300 Finally, security scripts should process the log files, and the logs themselves should be generated in as secure a manner as possible -- remote syslog can be very useful. An intruder tries to cover his tracks, and log files are critical to the sysadmin trying to track down the time and method of the initial break-in. One way to keep a permanent record of the log files is to run the system console to a serial port and collect the information on a continuing basis through a secure machine monitoring the consoles.
301
302
303
304 ### Paranoia 
305
306
307
308 A little paranoia never hurts. As a rule, a sysadmin can add any number of security features, as long as they do not affect convenience, and can add security features that ***do*** affect convenience with some added thought. Even more importantly, a security administrator should mix it up a bit -- if you use recommendations such as those given by this document verbatim, you give away your methodologies to the prospective attacker who also has access to this document.
309
310
311
312 ### Denial of Service Attacks 
313
314
315
316 This section covers Denial of Service attacks. A DoS attack is typically a packet attack. While there is not much you can do about modern spoofed packet attacks that saturate your network, you can generally limit the damage by ensuring that the attacks cannot take down your servers.
317
318
319
320   1. Limiting server forks.
321
322   1. Limiting springboard attacks (ICMP response attacks, ping broadcast, etc.).
323
324   1. Kernel Route Cache.
325
326
327
328 A common DoS attack is against a forking server that attempts to cause the server to eat processes, file descriptors, and memory, until the machine dies.  **inetd**  (see [inetd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#inetd&section8)) has several options to limit this sort of attack. It should be noted that while it is possible to prevent a machine from going down, it is not generally possible to prevent a service from being disrupted by the attack. Read the  **inetd**  manual page carefully and pay specific attention to the `-c`, `-C`, and `-R` options. Note that spoofed-IP attacks will circumvent the `-C` option to  **inetd** , so typically a combination of options must be used. Some standalone servers have self-fork-limitation parameters.
329
330
331
332  **Sendmail**  has its `-OMaxDaemonChildren` option, which tends to work much better than trying to use sendmail's load limiting options due to the load lag. You should specify a `MaxDaemonChildren` parameter, when you start  **sendmail** , high enough to handle your expected load, but not so high that the computer cannot handle that number of  **sendmails**  without falling on its face. It is also prudent to run sendmail in queued mode (`-ODeliveryMode=queued`) and to run the daemon (`sendmail -bd`) separate from the queue-runs (`sendmail -q15m`). If you still want real-time delivery you can run the queue at a much lower interval, such as `-q1m`, but be sure to specify a reasonable `MaxDaemonChildren` option for ***that*** sendmail to prevent cascade failures.
333
334
335
336  **Syslogd**  can be attacked directly and it is strongly recommended that you use the `-s` option whenever possible, and the `-a` option otherwise.
337
338
339
340 You should also be fairly careful with connect-back services such as  **tcpwrapper**  s reverse-identd, which can be attacked directly. You generally do not want to use the reverse-ident feature of  **tcpwrappers**  for this reason.
341
342
343
344 It is a very good idea to protect internal services from external access by firewalling them off at your border routers. The idea here is to prevent saturation attacks from outside your LAN, not so much to protect internal services from network-based `root` compromise. Always configure an exclusive firewall, i.e., firewall everything ***except*** ports A, B, C, D, and M-Z. This way you can firewall off all of your low ports except for certain specific services such as  **named**  (if you are primary for a zone),  **ntalkd** ,  **sendmail** , and other Internet-accessible services. If you try to configure the firewall the other way -- as an inclusive or permissive firewall, there is a good chance that you will forget to ***close*** a couple of services, or that you will add a new internal service and forget to update the firewall. You can still open up the high-numbered port range on the firewall, to allow permissive-like operation, without compromising your low ports. Also take note that DragonFly allows you to control the range of port numbers used for dynamic binding, via the various `net.inet.ip.portrange` `sysctl`'s (`sysctl -a | fgrep portrange`), which can also ease the complexity of your firewall's configuration. For example, you might use a normal first/last range of 4000 to 5000, and a hiport range of 49152 to 65535, then block off everything under 4000 in your firewall (except for certain specific Internet-accessible ports, of course).
345
346
347
348 Another common DoS attack is called a springboard attack -- to attack a server in a manner that causes the server to generate responses which overloads the server, the local network, or some other machine. The most common attack of this nature is the ***ICMP ping broadcast attack***. The attacker spoofs ping packets sent to your LAN's broadcast address with the source IP address set to the actual machine they wish to attack. If your border routers are not configured to stomp on ping's to broadcast addresses, your LAN winds up generating sufficient responses to the spoofed source address to saturate the victim, especially when the attacker uses the same trick on several dozen broadcast addresses over several dozen different networks at once. Broadcast attacks of over a hundred and twenty megabits have been measured. A second common springboard attack is against the ICMP error reporting system. By constructing packets that generate ICMP error responses, an attacker can saturate a server's incoming network and cause the server to saturate its outgoing network with ICMP responses. This type of attack can also crash the server by running it out of mbuf's, especially if the server cannot drain the ICMP responses it generates fast enough. The DragonFly kernel has a new kernel compile option called `ICMP_BANDLIM` which limits the effectiveness of these sorts of attacks. The last major class of springboard attacks is related to certain internal  **inetd**  services such as the udp echo service. An attacker simply spoofs a UDP packet with the source address being server A's echo port, and the destination address being server B's echo port, where server A and B are both on your LAN. The two servers then bounce this one packet back and forth between each other. The attacker can overload both servers and their LANs simply by injecting a few packets in this manner. Similar problems exist with the internal  **chargen**  port. A competent sysadmin will turn off all of these inetd-internal test services.
349
350
351
352 Spoofed packet attacks may also be used to overload the kernel route cache. Refer to the `net.inet.ip.rtexpire`, `rtminexpire`, and `rtmaxcache` `sysctl` parameters. A spoofed packet attack that uses a random source IP will cause the kernel to generate a temporary cached route in the route table, viewable with `netstat -rna | fgrep W3`. These routes typically timeout in 1600 seconds or so. If the kernel detects that the cached route table has gotten too big it will dynamically reduce the `rtexpire` but will never decrease it to less than `rtminexpire`. There are two problems:
353
354
355
356   1. The kernel does not react quickly enough when a lightly loaded server is suddenly attacked.
357
358   1. The `rtminexpire` is not low enough for the kernel to survive a sustained attack.
359
360
361
362 If your servers are connected to the Internet via a T3 or better, it may be prudent to manually override both `rtexpire` and `rtminexpire` via [sysctl(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#sysctl&section8). Never set either parameter to zero (unless you want to crash the machine). Setting both parameters to two seconds should be sufficient to protect the route table from attack.
363
364
365
366 ### Access Issues with Kerberos and SSH 
367
368
369
370 There are a few issues with both Kerberos and ssh that need to be addressed if you intend to use them. Kerberos V is an excellent authentication protocol, but there are bugs in the kerberized  **telnet**  and  **rlogin**  applications that make them unsuitable for dealing with binary streams. Also, by default Kerberos does not encrypt a session unless you use the `-x` option.  **ssh**  encrypts everything by default.
371
372
373
374 ssh works quite well in every respect except that it forwards encryption keys by default. What this means is that if you have a secure workstation holding keys that give you access to the rest of the system, and you ssh to an insecure machine, your keys are usable. The actual keys themselves are not exposed, but ssh installs a forwarding port for the duration of your login, and if an attacker has broken `root` on the insecure machine he can utilize that port to use your keys to gain access to any other machine that your keys unlock.
375
376
377
378 We recommend that you use ssh in combination with Kerberos whenever possible for staff logins.  **ssh**  can be compiled with Kerberos support. This reduces your reliance on potentially exposable ssh keys while at the same time protecting passwords via Kerberos. ssh keys should only be used for automated tasks from secure machines (something that Kerberos is unsuited to do). We also recommend that you either turn off key-forwarding in the ssh configuration, or that you make use of the `from=IP/DOMAIN` option that ssh allows in its `authorized_keys` file to make the key only usable to entities logging in from specific machines.
379
380
381
382
383
384
385 ## DES, MD5, and Crypt 
386
387
388
389 ***Parts rewritten and updated by Bill Swingle. ***
390
391
392
393 Every user on a UNIX® system has a password associated with their account. It seems obvious that these passwords need to be known only to the user and the actual operating system. In order to keep these passwords secret, they are encrypted with what is known as a ***one-way hash***, that is, they can only be easily encrypted but not decrypted. In other words, what we told you a moment ago was obvious is not even true: the operating system itself does not ***really*** know the password. It only knows the ***encrypted*** form of the password. The only way to get the ***plain-text*** password is by a brute force search of the space of possible passwords.
394
395
396
397 Unfortunately the only secure way to encrypt passwords when UNIX came into being was based on DES, the Data Encryption Standard. This was not such a problem for users resident in the US, but since the source code for DES could not be exported outside the US, DragonFly had to find a way to both comply with US law and retain compatibility with all the other UNIX variants that still used DES.
398
399
400
401 The solution was to divide up the encryption libraries so that US users could install the DES libraries and use DES but international users still had an encryption method that could be exported abroad. This is how DragonFly came to use MD5 as its default encryption method. MD5 is believed to be more secure than DES, so installing DES is offered primarily for compatibility reasons.
402
403
404
405 ### Recognizing Your Crypt Mechanism 
406
407
408
409 `libcrypt.a` provides a configurable password authentication hash library. Currently the library supports DES, MD5 and Blowfish hash functions. By default DragonFly uses MD5 to encrypt passwords.
410
411
412
413 It is pretty easy to identify which encryption method DragonFly is set up to use. Examining the encrypted passwords in the `/etc/master.passwd` file is one way. Passwords encrypted with the MD5 hash are longer than those encrypted with the DES hash and also begin with the characters `$1$`. Passwords starting with `$2a$` are encrypted with the Blowfish hash function. DES password strings do not have any particular identifying characteristics, but they are shorter than MD5 passwords, and are coded in a 64-character alphabet which does not include the `$` character, so a relatively short string which does not begin with a dollar sign is very likely a DES password.
414
415
416
417 The password format used for new passwords is controlled by the `passwd_format` login capability in `/etc/login.conf`, which takes values of `des`, `md5` or `blf`. See the [login.conf(5)](http://leaf.dragonflybsd.org/cgi/web-man?command#login.conf&section5) manual page for more information about login capabilities.
418
419
420
421
422
423
424 ## One-time Passwords 
425
426
427
428 S/Key is a one-time password scheme based on a one-way hash function. DragonFly uses the MD4 hash for compatibility but other systems have used MD5 and DES-MAC. S/Key ia part of the FreeBSD base system, and is also used on a growing number of other operating systems. S/Key is a registered trademark of Bell Communications Research, Inc.
429
430
431
432 There are three different sorts of passwords which we will discuss below. The first is your usual UNIX® style or Kerberos password; we will call this a ***UNIX password***. The second sort is the one-time password which is generated by the S/Key `key` program or the OPIE [opiekey(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#opiekey&section1) program and accepted by the `keyinit` or [opiepasswd(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=opiepasswd&section=1) programs and the login prompt; we will call this a ***one-time password***. The final sort of password is the secret password which you give to the `key`/`opiekey` programs (and sometimes the `keyinit`/`opiepasswd` programs) which it uses to generate one-time passwords; we will call it a ***secret password*** or just unqualified ***password***.
433
434
435
436 The secret password does not have anything to do with your UNIX password; they can be the same but this is not recommended. S/Key and OPIE secret passwords are not limited to eight characters like old UNIX passwords[(1)](#FTN.AEN8429), they can be as long as you like. Passwords of six or seven word long phrases are fairly common. For the most part, the S/Key or OPIE system operates completely independently of the UNIX password system.
437
438
439
440 Besides the password, there are two other pieces of data that are important to S/Key and OPIE. One is what is known as the ***seed*** or ***key***, consisting of two letters and five digits. The other is what is called the ***iteration count***, a number between 1 and 100. S/Key creates the one-time password by concatenating the seed and the secret password, then applying the MD4/MD5 hash as many times as specified by the iteration count and turning the result into six short English words. These six English words are your one-time password. The authentication system (primarily PAM) keeps track of the last one-time password used, and the user is authenticated if the hash of the user-provided password is equal to the previous password. Because a one-way hash is used it is impossible to generate future one-time passwords if a successfully used password is captured; the iteration count is decremented after each successful login to keep the user and the login program in sync. When the iteration count gets down to 1, S/Key and OPIE must be reinitialized.
441
442
443
444 There are three programs involved in each system which we will discuss below. The `key` and `opiekey` programs accept an iteration count, a seed, and a secret password, and generate a one-time password or a consecutive list of one-time passwords. The `keyinit` and `opiepasswd` programs are used to initialize S/Key and OPIE respectively, and to change passwords, iteration counts, or seeds; they take either a secret passphrase, or an iteration count, seed, and one-time password. The `keyinfo` and `opieinfo` programs examine the relevant credentials files (`/etc/skeykeys` or `/etc/opiekeys`) and print out the invoking user's current iteration count and seed.
445
446
447
448 There are four different sorts of operations we will cover. The first is using `keyinit` or `opiepasswd` over a secure connection to set up one-time-passwords for the first time, or to change your password or seed. The second operation is using `keyinit` or `opiepasswd` over an insecure connection, in conjunction with `key` or `opiekey` over a secure connection, to do the same. The third is using `key`/`opiekey` to log in over an insecure connection. The fourth is using `key` or `opiekey` to generate a number of keys which can be written down or printed out to carry with you when going to some location without secure connections to anywhere.
449
450
451
452 ### Secure Connection Initialization 
453
454
455
456 To initialize S/Key for the first time, change your password, or change your seed while logged in over a secure connection (e.g., on the console of a machine or via  **ssh** ), use the `keyinit` command without any parameters while logged in as yourself:
457
458
459
460     
461
462     % keyinit
463
464     Adding unfurl:
465
466     Reminder - Only use this method if you are directly connected.
467
468     If you are using telnet or rlogin exit with no password and use keyinit -s.
469
470     Enter secret password:
471
472     Again secret password:
473
474     
475
476     ID unfurl s/key is 99 to17757
477
478     DEFY CLUB PRO NASH LACE SOFT
479
480
481
482
483
484 For OPIE, `opiepasswd` is used instead:
485
486
487
488     
489
490     % opiepasswd -c
491
492     [grimreaper] ~ $ opiepasswd -f -c
493
494     Adding unfurl:
495
496     Only use this method from the console; NEVER from remote. If you are using
497
498     telnet, xterm, or a dial-in, type ^C now or exit with no password.
499
500     Then run opiepasswd without the -c parameter.
501
502     Using MD5 to compute responses.
503
504     Enter new secret pass phrase:
505
506     Again new secret pass phrase:
507
508     ID unfurl OTP key is 499 to4268
509
510     MOS MALL GOAT ARM AVID COED
511
512
513
514
515
516 At the Enter new secret pass phrase: or Enter secret password: prompts, you should enter a password or phrase. Remember, this is not the password that you will use to login with, this is used to generate your one-time login keys. The ***ID*** line gives the parameters of your particular instance: your login name, the iteration count, and seed. When logging in the system will remember these parameters and present them back to you so you do not have to remember them. The last line gives the particular one-time password which corresponds to those parameters and your secret password; if you were to re-login immediately, this one-time password is the one you would use.
517
518
519
520 ### Insecure Connection Initialization 
521
522
523
524 To initialize or change your secret password over an insecure connection, you will need to already have a secure connection to some place where you can run `key` or `opiekey`; this might be in the form of a desk accessory on a Macintosh®, or a shell prompt on a machine you trust. You will also need to make up an iteration count (100 is probably a good value), and you may make up your own seed or use a randomly-generated one. Over on the insecure connection (to the machine you are initializing), use the `keyinit -s` command:
525
526
527
528     
529
530     % keyinit -s
531
532     Updating unfurl:
533
534     Old key: to17758
535
536     Reminder you need the 6 English words from the key command.
537
538     Enter sequence count from 1 to 9999: 100
539
540     Enter new key [default to17759]:
541
542     s/key 100 to 17759
543
544     s/key access password:
545
546     s/key access password:CURE MIKE BANE HIM RACY GORE
547
548
549
550
551
552 For OPIE, you need to use `opiepasswd`:
553
554
555
556     
557
558     % opiepasswd
559
560     
561
562     Updating unfurl:
563
564     You need the response from an OTP generator.
565
566     Old secret pass phrase:
567
568             otp-md5 498 to4268 ext
569
570             Response: GAME GAG WELT OUT DOWN CHAT
571
572     New secret pass phrase:
573
574             otp-md5 499 to4269
575
576             Response: LINE PAP MILK NELL BUOY TROY
577
578     
579
580     ID mark OTP key is 499 gr4269
581
582     LINE PAP MILK NELL BUOY TROY
583
584
585
586
587
588 To accept the default seed (which the `keyinit` program confusingly calls a `key`), press  **Return** . Then before entering an access password, move over to your secure connection or S/Key desk accessory, and give it the same parameters:
589
590
591
592     
593
594     % key 100 to17759
595
596     Reminder - Do not use this program while logged in via telnet or rlogin.
597
598     Enter secret password: <secret password>
599
600     CURE MIKE BANE HIM RACY GORE
601
602
603
604
605
606 Or for OPIE:
607
608
609
610     
611
612     % opiekey 498 to4268
613
614     Using the MD5 algorithm to compute response.
615
616     Reminder: Don't use opiekey from telnet or dial-in sessions.
617
618     Enter secret pass phrase:
619
620     GAME GAG WELT OUT DOWN CHAT
621
622
623
624
625
626 Now switch back over to the insecure connection, and copy the one-time password generated over to the relevant program.
627
628
629
630 ### Generating a Single One-time Password 
631
632
633
634 Once you have initialized S/Key, when you login you will be presented with a prompt like this:
635
636
637
638     
639
640     % telnet example.com
641
642     Trying 10.0.0.1...
643
644     Connected to example.com
645
646     Escape character is '^]'.
647
648     
649
650     DragonFly/i386 (example.com) (ttypa)
651
652     
653
654     login: <username>
655
656     s/key 97 fw13894
657
658     Password:
659
660
661
662
663
664 Or for OPIE:
665
666
667
668     
669
670     % telnet example.com
671
672     Trying 10.0.0.1...
673
674     Connected to example.com
675
676     Escape character is '^]'.
677
678     
679
680     DragonFly/i386 (example.com) (ttypa)
681
682     
683
684     login: <username>
685
686     otp-md5 498 gr4269 ext
687
688     Password:
689
690
691
692
693
694 As a side note, the S/Key and OPIE prompts have a useful feature (not shown here): if you press  **Return**  at the password prompt, the prompter will turn echo on, so you can see what you are typing. This can be extremely useful if you are attempting to type in a password by hand, such as from a printout.
695
696
697
698 At this point you need to generate your one-time password to answer this login prompt. This must be done on a trusted system that you can run `key` or `opiekey` on. (There are versions of these for DOS, Windows® and Mac OS® as well.) They need both the iteration count and the seed as command line options. You can cut-and-paste these right from the login prompt on the machine that you are logging in to.
699
700
701
702 On the trusted system:
703
704
705
706     
707
708     % key 97 fw13894
709
710     Reminder - Do not use this program while logged in via telnet or rlogin.
711
712     Enter secret password:
713
714     WELD LIP ACTS ENDS ME HAAG
715
716
717
718
719
720 For OPIE:
721
722
723
724     
725
726     % opiekey 498 to4268
727
728     Using the MD5 algorithm to compute response.
729
730     Reminder: Don't use opiekey from telnet or dial-in sessions.
731
732     Enter secret pass phrase:
733
734     GAME GAG WELT OUT DOWN CHAT
735
736
737
738
739
740 Now that you have your one-time password you can continue logging in:
741
742
743
744     
745
746     login: <username>
747
748     s/key 97 fw13894
749
750     Password: <return to enable echo>
751
752     s/key 97 fw13894
753
754     Password [echo on]: WELD LIP ACTS ENDS ME HAAG
755
756     Last login: Tue Mar 21 11:56:41 from 10.0.0.2 ...
757
758
759
760
761
762 ### Generating Multiple One-time Passwords 
763
764
765
766 Sometimes you have to go places where you do not have access to a trusted machine or secure connection. In this case, it is possible to use the `key` and `opiekey` commands to generate a number of one-time passwords beforehand to be printed out and taken with you. For example:
767
768
769
770     
771
772     % key -n 5 30 zz99999
773
774     Reminder - Do not use this program while logged in via telnet or rlogin.
775
776     Enter secret password: <secret password>
777
778     26: SODA RUDE LEA LIND BUDD SILT
779
780     27: JILT SPY DUTY GLOW COWL ROT
781
782     28: THEM OW COLA RUNT BONG SCOT
783
784     29: COT MASH BARR BRIM NAN FLAG
785
786     30: CAN KNEE CAST NAME FOLK BILK
787
788
789
790
791
792 Or for OPIE:
793
794
795
796     
797
798     % opiekey -n 5 30 zz99999
799
800     Using the MD5 algorithm to compute response.
801
802     Reminder: Don't use opiekey from telnet or dial-in sessions.
803
804     Enter secret pass phrase: <secret password>
805
806     26: JOAN BORE FOSS DES NAY QUIT
807
808     27: LATE BIAS SLAY FOLK MUCH TRIG
809
810     28: SALT TIN ANTI LOON NEAL USE
811
812     29: RIO ODIN GO BYE FURY TIC
813
814     30: GREW JIVE SAN GIRD BOIL PHI
815
816
817
818
819
820 The `-n 5` requests five keys in sequence, the `30` specifies what the last iteration number should be. Note that these are printed out in ***reverse*** order of eventual use. If you are really paranoid, you might want to write the results down by hand; otherwise you can cut-and-paste into `lpr`. Note that each line shows both the iteration count and the one-time password; you may still find it handy to scratch off passwords as you use them.
821
822
823
824 ### Restricting Use of UNIX® Passwords 
825
826
827
828 S/Key can place restrictions on the use of UNIX passwords based on the host name, user name, terminal port, or IP address of a login session. These restrictions can be found in the configuration file `/etc/skey.access`. The [skey.access(5)](http://leaf.dragonflybsd.org/cgi/web-man?command#skey.access&section5) manual page has more information on the complete format of the file and also details some security cautions to be aware of before depending on this file for security.
829
830
831
832 If there is no `/etc/skey.access` file (this is the default), then all users will be allowed to use UNIX passwords. If the file exists, however, then all users will be required to use S/Key unless explicitly permitted to do otherwise by configuration statements in the `skey.access` file. In all cases, UNIX passwords are permitted on the console.
833
834
835
836 Here is a sample `skey.access` configuration file which illustrates the three most common sorts of configuration statements:
837
838
839
840     
841
842     permit internet 192.168.0.0 255.255.0.0
843
844     permit user fnord
845
846     permit port ttyd0
847
848
849
850
851
852 The first line (`permit internet`) allows users whose IP source address (which is vulnerable to spoofing) matches the specified value and mask, to use UNIX passwords. This should not be considered a security mechanism, but rather, a means to remind authorized users that they are using an insecure network and need to use S/Key for authentication.
853
854
855
856 The second line (`permit user`) allows the specified username, in this case `fnord`, to use UNIX passwords at any time. Generally speaking, this should only be used for people who are either unable to use the `key` program, like those with dumb terminals, or those who are uneducable.
857
858
859
860 The third line (`permit port`) allows all users logging in on the specified terminal line to use UNIX passwords; this would be used for dial-ups.
861
862
863
864 Here is a sample `opieaccess` file:
865
866
867
868     
869
870     permit 192.168.0.0 255.255.0.0
871
872
873
874
875
876 This line allows users whose IP source address (which is vulnerable to spoofing) matches the specified value and mask, to use UNIX passwords at any time.
877
878
879
880 If no rules in `opieaccess` are matched, the default is to deny non-OPIE logins.
881
882
883
884 #### Notes 
885
886
887
888 [[!table  data="""
889 |<tablestyle="width:100%"> [one-time-passwords.html#AEN8429 (1)] | Under DragonFly the standard login password may be up to 128 characters in length. |
890
891 """]]
892
893
894
895
896
897 CategoryHandbook
898
899 CategoryHandbook-security
900
901
902
903
904 ## Kerberos5 
905
906
907
908 ***Contributed by Tillman Hodgson. Based on a contribution by Mark Murray. ***
909
910
911
912 The following information only applies to  **Kerberos5** . Users who wish to use the  **KerberosIV**  package may install the [security/krb4](http://pkgsrc.se/security/krb4) port.
913
914
915
916  **Kerberos**  is a network add-on system/protocol that allows users to authenticate themselves through the services of a secure server. Services such as remote login, remote copy, secure inter-system file copying and other high-risk tasks are made considerably safer and more controllable.
917
918
919
920  **Kerberos**  can be described as an identity-verifying proxy system. It can also be described as a trusted third-party authentication system.  **Kerberos**  provides only one function -- the secure authentication of users on the network. It does not provide authorization functions (what users are allowed to do) or auditing functions (what those users did). After a client and server have used  **Kerberos**  to prove their identity, they can also encrypt all of their communications to assure privacy and data integrity as they go about their business.
921
922
923
924 Therefore it is highly recommended that  **Kerberos**  be used with other security methods which provide authorization and audit services.
925
926
927
928 The following instructions can be used as a guide on how to set up  **Kerberos**  as distributed for DragonFly. However, you should refer to the relevant manual pages for a complete description.
929
930
931
932 For purposes of demonstrating a  **Kerberos**  installation, the various namespaces will be handled as follows:
933
934
935
936
937 * The DNS domain (***zone***) will be example.org.
938
939
940 * The  **Kerberos**  realm will be EXAMPLE.ORG.
941
942
943
944  **Note:** Please use real domain names when setting up  **Kerberos**  even if you intend to run it internally. This avoids DNS problems and assures inter-operation with other  **Kerberos**  realms.
945
946
947
948 ### History 
949
950
951
952  **Kerberos**  was created by MIT as a solution to network security problems. The  **Kerberos**  protocol uses strong cryptography so that a client can prove its identity to a server (and vice versa) across an insecure network connection.
953
954
955
956  **Kerberos**  is both the name of a network authentication protocol and an adjective to describe programs that implement the program ( **Kerberos**  telnet, for example). The current version of the protocol is version 5, described in RFC 1510.
957
958
959
960 Several free implementations of this protocol are available, covering a wide range of operating systems. The Massachusetts Institute of Technology (MIT), where  **Kerberos**  was originally developed, continues to develop their  **Kerberos**  package. It is commonly used in the US as a cryptography product, as such it has historically been affected by US export regulations. The MIT  **Kerberos**  is available as a port ([`security/krb5`](http://pkgsrc.se/security/krb5)). Heimdal  **Kerberos**  is another version 5 implementation, and was explicitly developed outside of the US to avoid export regulations (and is thus often included in non-commercial UNIX® variants). The Heimdal  **Kerberos**  distribution is available as a port ([`security/heimdal`](http://pkgsrc.se/security/heimdal)), and a minimal installation of it is included in the base DragonFly install.
961
962
963
964 In order to reach the widest audience, these instructions assume the use of the Heimdal distribution included in DragonFly.
965
966
967
968 ### Setting up a Heimdal KDC 
969
970
971
972 The Key Distribution Center (KDC) is the centralized authentication service that  **Kerberos**  provides -- it is the computer that issues  **Kerberos**  tickets. The KDC is considered ***trusted*** by all other computers in the  **Kerberos**  realm, and thus has heightened security concerns.
973
974
975
976 Note that while running the  **Kerberos**  server requires very few computing resources, a dedicated machine acting only as a KDC is recommended for security reasons.
977
978
979
980 To begin setting up a KDC, ensure that your `/etc/rc.conf` file contains the correct settings to act as a KDC (you may need to adjust paths to reflect your own system):
981
982
983
984     
985
986     kerberos5_server_enable="YES"
987
988     kadmind5_server_enable="YES"
989
990     kerberos_stash="YES"
991
992
993
994
995
996 Next we will set up your  **Kerberos**  config file, `/etc/krb5.conf`:
997
998
999
1000     
1001
1002     [libdefaults]
1003
1004         default_realm = EXAMPLE.ORG
1005
1006     [realms]
1007
1008         EXAMPLE.ORG = {
1009
1010             kdc = kerberos.example.org
1011
1012         }
1013
1014     [domain_realm]
1015
1016         .example.org = EXAMPLE.ORG
1017
1018
1019
1020
1021
1022 Note that this `/etc/krb5.conf` file implies that your KDC will have the fully-qualified hostname of `kerberos.example.org`. You will need to add a CNAME (alias) entry to your zone file to accomplish this if your KDC has a different hostname.
1023
1024
1025
1026  **Note:** For large networks with a properly configured BIND DNS server, the above example could be trimmed to:
1027
1028
1029
1030     
1031
1032     [libdefaults]
1033
1034           default_realm = EXAMPLE.ORG
1035
1036
1037
1038
1039
1040 With the following lines being appended to the `example.org` zonefile:
1041
1042
1043
1044     
1045
1046     _kerberos._udp      IN  SRV     01 00 88 kerberos.example.org.
1047
1048     _kerberos._tcp      IN  SRV     01 00 88 kerberos.example.org.
1049
1050     _kpasswd._udp       IN  SRV     01 00 464 kerberos.example.org.
1051
1052     _kerberos-adm._tcp  IN  SRV     01 00 749 kerberos.example.org.
1053
1054     _kerberos           IN  TXT     EXAMPLE.ORG.
1055
1056
1057
1058
1059
1060 Next we will create the  **Kerberos**  database. This database contains the keys of all principals encrypted with a master password. You are not required to remember this password, it will be stored in a file (`/var/heimdal/m-key`). To create the master key, run `kstash` and enter a password.
1061
1062
1063
1064 Once the master key has been created, you can initialize the database using the `kadmin` program with the `-l` option (standing for ***local***). This option instructs `kadmin` to modify the database files directly rather than going through the `kadmind` network service. This handles the chicken-and-egg problem of trying to connect to the database before it is created. Once you have the `kadmin` prompt, use the `init` command to create your realms initial database.
1065
1066
1067
1068 Lastly, while still in `kadmin`, create your first principal using the `add` command. Stick to the defaults options for the principal for now, you can always change them later with the `modify` command. Note that you can use the `?` command at any prompt to see the available options.
1069
1070
1071
1072 A sample database creation session is shown below:
1073
1074
1075
1076     
1077
1078     # kstash
1079
1080     Master key: xxxxxxxx
1081
1082     Verifying password - Master key: xxxxxxxx
1083
1084     
1085
1086     # kadmin -l
1087
1088     kadmin&gt; init EXAMPLE.ORG
1089
1090     Realm max ticket life [unlimited]:
1091
1092     kadmin&gt; add tillman
1093
1094     Max ticket life [unlimited]:
1095
1096     Max renewable life [unlimited]:
1097
1098     Attributes []:
1099
1100     Password: xxxxxxxx
1101
1102     Verifying password - Password: xxxxxxxx
1103
1104
1105
1106
1107
1108 Now it is time to start up the KDC services. Run `/etc/rc.d/kerberos start` and `/etc/rc.d/kadmind start` to bring up the services. Note that you won't have any kerberized daemons running at this point but you should be able to confirm that the KDC is functioning by obtaining and listing a ticket for the principal (user) that you just created from the command-line of the KDC itself:
1109
1110
1111
1112     
1113
1114     % k5init `***tillman***`
1115
1116     tillman@EXAMPLE.ORG's Password:
1117
1118     
1119
1120     % k5list
1121
1122     Credentials cache: FILE:`/tmp/krb5cc_500`
1123
1124         Principal: tillman@EXAMPLE.ORG
1125
1126     
1127
1128       Issued           Expires          Principal
1129
1130     Aug 27 15:37:58  Aug 28 01:37:58  krbtgt/EXAMPLE.ORG@EXAMPLE.ORG
1131
1132
1133
1134
1135
1136 ### **Kerberos**  enabling a server with Heimdal services 
1137
1138
1139
1140 First, we need a copy of the  **Kerberos**  configuration file, `/etc/krb5.conf`. To do so, simply copy it over to the client computer from the KDC in a secure fashion (using network utilities, such as [scp(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#scp&section1&manpath=OpenBSD+3.3), or physically via a floppy disk).
1141
1142
1143
1144 Next you need a `/etc/krb5.keytab` file. This is the major difference between a server providing  **Kerberos**  enabled daemons and a workstation -- the server must have a `keytab` file. This file contains the servers host key, which allows it and the KDC to verify each others identity. It must be transmitted to the server in a secure fashion, as the security of the server can be broken if the key is made public. This explicitly means that transferring it via a clear text channel, such as FTP, is a very bad idea.
1145
1146
1147
1148 Typically, you transfer to the `keytab` to the server using the `kadmin` program. This is handy because you also need to create the host principal (the KDC end of the `krb5.keytab`) using `kadmin`.
1149
1150
1151
1152 Note that you must have already obtained a ticket and that this ticket must be allowed to use the `kadmin` interface in the `kadmind.acl`. See the section titled ***Remote administration*** in the Heimdal info pages (`info heimdal`) for details on designing access control lists. If you do not want to enable remote `kadmin` access, you can simply securely connect to the KDC (via local console, [ssh(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh&section1&manpath=OpenBSD+3.3) or  **Kerberos**  [telnet(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=telnet&section=1)) and perform administration locally using `kadmin -l`.
1153
1154
1155
1156 After installing the `/etc/krb5.conf` file, you can use `kadmin` from the  **Kerberos**  server. The `add --random-key` command will let you add the servers host principal, and the `ext` command will allow you to extract the servers host principal to its own keytab. For example:
1157
1158
1159
1160     
1161
1162     # kadmin
1163
1164     kadmin&gt; add --random-key host/myserver.example.org
1165
1166     Max ticket life [unlimited]:
1167
1168     Max renewable life [unlimited]:
1169
1170     Attributes []:
1171
1172     kadmin&gt; ext host/myserver.example.org
1173
1174     kadmin&gt; exit
1175
1176
1177
1178
1179
1180 Note that the `ext` command (short for ***extract***) stores the extracted key in `/etc/krb5.keytab` by default.
1181
1182
1183
1184 If you do not have `kadmind` running on the KDC (possibly for security reasons) and thus do not have access to `kadmin` remotely, you can add the host principal (`host/myserver.EXAMPLE.ORG`) directly on the KDC and then extract it to a temporary file (to avoid over-writing the `/etc/krb5.keytab` on the KDC) using something like this:
1185
1186
1187
1188     
1189
1190     # kadmin
1191
1192     kadmin&gt; ext --keytab=/tmp/example.keytab host/myserver.example.org
1193
1194     kadmin&gt; exit
1195
1196
1197
1198
1199
1200 You can then securely copy the keytab to the server computer (using `scp` or a floppy, for example). Be sure to specify a non-default keytab name to avoid over-writing the keytab on the KDC.
1201
1202
1203
1204 At this point your server can communicate with the KDC (due to its `krb5.conf` file) and it can prove its own identity (due to the `krb5.keytab` file). It is now ready for you to enable some  **Kerberos**  services. For this example we will enable the `telnet` service by putting a line like this into your `/etc/inetd.conf` and then restarting the [inetd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#inetd&section8) service with `/etc/rc.d/inetd restart`:
1205
1206
1207
1208     
1209
1210     telnet    stream  tcp     nowait  root    /usr/libexec/telnetd  telnetd -a user
1211
1212
1213
1214
1215
1216 The critical bit is that the `-a` (for authentication) type is set to user. Consult the [telnetd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#telnetd&section8) manual page for more details.
1217
1218
1219
1220 ### Kerberos enabling a client with Heimdal 
1221
1222
1223
1224 Setting up a client computer is almost trivially easy. As far as  **Kerberos**  configuration goes, you only need the  **Kerberos**  configuration file, located at `/etc/krb5.conf`. Simply securely copy it over to the client computer from the KDC.
1225
1226
1227
1228 Test your client computer by attempting to use `kinit`, `klist`, and `kdestroy` from the client to obtain, show, and then delete a ticket for the principal you created above. You should also be able to use  **Kerberos**  applications to connect to  **Kerberos**  enabled servers, though if that does not work and obtaining a ticket does the problem is likely with the server and not with the client or the KDC.
1229
1230
1231
1232 When testing an application like `telnet`, try using a packet sniffer (such as [tcpdump(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#tcpdump&section1)) to confirm that your password is not sent in the clear. Try using `telnet` with the `-x` option, which encrypts the entire data stream (similar to `ssh`).
1233
1234
1235
1236 The core  **Kerberos**  client applications (traditionally named `kinit`, `klist`, `kdestroy`, and `kpasswd`) are installed in the base DragonFly install. Note that DragonFly versions prior to 5.0 renamed them to `k5init`, `k5list`, `k5destroy`, `k5passwd`, and `k5stash` (though it is typically only used once).
1237
1238
1239
1240 Various non-core  **Kerberos**  client applications are also installed by default. This is where the ***minimal*** nature of the base Heimdal installation is felt: `telnet` is the only  **Kerberos**  enabled service.
1241
1242
1243
1244 The Heimdal port adds some of the missing client applications:  **Kerberos**  enabled versions of `ftp`, `rsh`, `rcp`, `rlogin`, and a few other less common programs. The MIT port also contains a full suite of  **Kerberos**  client applications.
1245
1246
1247
1248 ### User configuration files: `.k5login` and `.k5users` 
1249
1250
1251
1252 Users within a realm typically have their  **Kerberos**  principal (such as `tillman@EXAMPLE.ORG`) mapped to a local user account (such as a local account named `tillman`). Client applications such as `telnet` usually do not require a user name or a principal.
1253
1254
1255
1256 Occasionally, however, you want to grant access to a local user account to someone who does not have a matching  **Kerberos**  principal. For example, `tillman@EXAMPLE.ORG` may need access to the local user account `webdevelopers`. Other principals may also need access to that local account.
1257
1258
1259
1260 The `.k5login` and `.k5users` files, placed in a users home directory, can be used similar to a powerful combination of `.hosts` and `.rhosts`, solving this problem. For example, if a `.k5login` with the following contents:
1261
1262
1263
1264     
1265
1266     tillman@example.org
1267
1268     jdoe@example.org
1269
1270
1271
1272
1273
1274 Were to be placed into the home directory of the local user `webdevelopers` then both principals listed would have access to that account without requiring a shared password.
1275
1276
1277
1278 Reading the manual pages for these commands is recommended. Note that the `ksu` manual page covers `.k5users`.
1279
1280
1281
1282 ### Kerberos Tips, Tricks, and Troubleshooting 
1283
1284
1285
1286
1287 * When using either the Heimdal or MIT  **Kerberos**  ports ensure that your `PATH` environment variable lists the  **Kerberos**  versions of the client applications before the system versions.
1288
1289
1290 * Is your time in sync? Are you sure? If the time is not in sync (typically within five minutes) authentication will fail.
1291
1292
1293 * MIT and Heimdal inter-operate nicely. Except for `kadmin`, the protocol for which is not standardized.
1294
1295
1296 * If you change your hostname, you also need to change your `host/` principal and update your keytab. This also applies to special keytab entries like the `www/` principal used for Apache's [`www/mod_auth_kerb`](http://pkgsrc.se/www/mod_auth_kerb).
1297
1298
1299 * All hosts in your realm must be resolvable (both forwards and reverse) in DNS (or `/etc/hosts` as a minimum). CNAMEs will work, but the A and PTR records must be correct and in place. The error message isn't very intuitive: ***`Kerberos5 refuses authentication because Read req failed: Key table entry not found`***.
1300
1301
1302 * Some operating systems that may being acting as clients to your KDC do not set the permissions for `ksu` to be setuid `root`. This means that `ksu` does not work, which is a good security idea but annoying. This is not a KDC error.
1303
1304
1305 * With MIT  **Kerberos** , if you want to allow a principal to have a ticket life longer than the default ten hours, you must use `modify_principal` in `kadmin` to change the maxlife of both the principal in question and the `krbtgt` principal. Then the principal can use the `-l` option with `kinit` to request a ticket with a longer lifetime.
1306
1307
1308 *  **Note:** If you run a packet sniffer on your KDC to add in troubleshooting and then run `kinit` from a workstation, you will notice that your TGT is sent immediately upon running `kinit` -- even before you type your password! The explanation is that the  **Kerberos**  server freely transmits a TGT (Ticket Granting Ticket) to any unauthorized request; however, every TGT is encrypted in a key derived from the user's password. Therefore, when a user types their password it is not being sent to the KDC, it is being used to decrypt the TGT that `kinit` already obtained. If the decryption process results in a valid ticket with a valid time stamp, the user has valid  **Kerberos**  credentials. These credentials include a session key for establishing secure communications with the  **Kerberos**  server in the future, as well as the actual ticket-granting ticket, which is actually encrypted with the  **Kerberos**  server's own key. This second layer of encryption is unknown to the user, but it is what allows the  **Kerberos**  server to verify the authenticity of each TGT.
1309
1310
1311 * You have to keep the time in sync between all the computers in your realm. NTP is perfect for this. For more information on NTP, see [network-ntp.html Section 19.12].
1312
1313
1314 * If you want to use long ticket lifetimes (a week, for example) and you are using  **OpenSSH**  to connect to the machine where your ticket is stored, make sure that  **Kerberos**  `TicketCleanup` is set to `no` in your `sshd_config` or else your tickets will be deleted when you log out.
1315
1316
1317 * Remember that host principals can have a longer ticket lifetime as well. If your user principal has a lifetime of a week but the host you are connecting to has a lifetime of nine hours, you will have an expired host principal in your cache and the ticket cache will not work as expected.
1318
1319
1320 * When setting up a `krb5.dict` file to prevent specific bad passwords from being used (the manual page for `kadmind` covers this briefly), remember that it only applies to principals that have a password policy assigned to them. The `krb5.dict` files format is simple: one string per line. Creating a symbolic link to `/usr/share/dict/words` might be useful.
1321
1322
1323
1324 ### Differences with the MIT port 
1325
1326
1327
1328 The major difference between the MIT and Heimdal installs relates to the `kadmin` program which has a different (but equivalent) set of commands and uses a different protocol. This has a large implications if your KDC is MIT as you will not be able to use the Heimdal `kadmin` program to administer your KDC remotely (or vice versa, for that matter).
1329
1330
1331
1332 The client applications may also take slightly different command line options to accomplish the same tasks. Following the instructions on the MIT  **Kerberos**  web site (http://web.mit.edu/Kerberos/www/) is recommended. Be careful of path issues: the MIT port installs into `/usr/local/` by default, and the ***normal*** system applications may be run instead of MIT if your `PATH` environment variable lists the system directories first.
1333
1334
1335
1336  **Note:** With the MIT [`security/krb5`](http://pkgsrc.se/security/krb5) port that is provided by DragonFly, be sure to read the `/usr/local/share/doc/krb5/README.FreeBSD` file installed by the port if you want to understand why logins via `telnetd` and `klogind` behave somewhat oddly. Most importantly, correcting the ***incorrect permissions on cache file*** behavior requires that the `login.krb5` binary be used for authentication so that it can properly change ownership for the forwarded credentials.
1337
1338
1339
1340 ### Mitigating limitations found in Kerberos 
1341
1342
1343
1344 #### Kerberos is an all-or-nothing approach 
1345
1346
1347
1348 Every service enabled on the network must be modified to work with  **Kerberos**  (or be otherwise secured against network attacks) or else the users credentials could be stolen and re-used. An example of this would be  **Kerberos**  enabling all remote shells (via `rsh` and `telnet`, for example) but not converting the POP3 mail server which sends passwords in plaintext.
1349
1350
1351
1352 #### Kerberos is intended for single-user workstations 
1353
1354
1355
1356 In a multi-user environment,  **Kerberos**  is less secure. This is because it stores the tickets in the `/tmp` directory, which is readable by all users. If a user is sharing a computer with several other people simultaneously (i.e. multi-user), it is possible that the user's tickets can be stolen (copied) by another user.
1357
1358
1359
1360 This can be overcome with the `-c` filename command-line option or (preferably) the `KRB5CCNAME` environment variable, but this is rarely done. In principal, storing the ticket in the users home directory and using simple file permissions can mitigate this problem.
1361
1362
1363
1364 #### The KDC is a single point of failure 
1365
1366
1367
1368 By design, the KDC must be as secure as the master password database is contained on it. The KDC should have absolutely no other services running on it and should be physically secured. The danger is high because  **Kerberos**  stores all passwords encrypted with the same key (the ***master*** key), which in turn is stored as a file on the KDC.
1369
1370
1371
1372 As a side note, a compromised master key is not quite as bad as one might normally fear. The master key is only used to encrypt the  **Kerberos**  database and as a seed for the random number generator. As long as access to your KDC is secure, an attacker cannot do much with the master key.
1373
1374
1375
1376 Additionally, if the KDC is unavailable (perhaps due to a denial of service attack or network problems) the network services are unusable as authentication can not be performed, a recipe for a denial-of-service attack. This can alleviated with multiple KDCs (a single master and one or more slaves) and with careful implementation of secondary or fall-back authentication (PAM is excellent for this).
1377
1378
1379
1380 #### Kerberos Shortcomings 
1381
1382
1383
1384  **Kerberos**  allows users, hosts and services to authenticate between themselves. It does not have a mechanism to authenticate the KDC to the users, hosts or services. This means that a trojanned `kinit` (for example) could record all user names and passwords. Something like [`security/tripwire`](http://pkgsrc.se/security/tripwire) or other file system integrity checking tools can alleviate this.
1385
1386
1387
1388 ### Resources and further information 
1389
1390
1391
1392
1393 * [The  **Kerberos**  FAQ](http://www.faqs.org/faqs/Kerberos-faq/general/preamble.html)
1394
1395
1396 * [Designing an Authentication System: a Dialogue in Four Scenes](http://web.mit.edu/Kerberos/www/dialogue.html)
1397
1398
1399 * [RFC 1510, The  **Kerberos**  Network Authentication Service (V5)](http://www.ietf.org/rfc/rfc1510.txt?number=1510)
1400
1401
1402 * [MIT  **Kerberos**  home page](http://web.mit.edu/Kerberos/www/)
1403
1404
1405 * [Heimdal  **Kerberos**  home page](http://www.pdc.kth.se/heimdal/)
1406
1407
1408
1409
1410
1411
1412
1413 CategoryHandbook
1414
1415 CategoryHandbook-security
1416
1417
1418
1419
1420
1421
1422
1423 ## Firewalls 
1424
1425
1426
1427 ***Contributed by Gary Palmer and Alex Nash. ***
1428
1429
1430
1431 Firewalls are an area of increasing interest for people who are connected to the Internet, and are even finding applications on private networks to provide enhanced security. This section will hopefully explain what firewalls are, how to use them, and how to use the facilities provided in the DragonFly kernel to implement them.
1432
1433
1434
1435  **Note:** People often think that having a firewall between your internal network and the ***Big Bad Internet*** will solve all your security problems. It may help, but a poorly set up firewall system is more of a security risk than not having one at all. A firewall can add another layer of security to your systems, but it cannot stop a really determined cracker from penetrating your internal network. If you let internal security lapse because you believe your firewall to be impenetrable, you have just made the crackers job that much easier.
1436
1437
1438
1439 ### What Is a Firewall? 
1440
1441
1442
1443 There are currently two distinct types of firewalls in common use on the Internet today. The first type is more properly called a ***packet filtering router***. This type of firewall utilizes a multi-homed machine and a set of rules to determine whether to forward or block individual packets. A multi-homed machine is simply a device with multiple network interfaces. The second type, known as a ***proxy server***, relies on daemons to provide authentication and to forward packets, possibly on a multi-homed machine which has kernel packet forwarding disabled.
1444
1445
1446
1447 Sometimes sites combine the two types of firewalls, so that only a certain machine (known as a ***bastion host***) is allowed to send packets through a packet filtering router onto an internal network. Proxy services are run on the bastion host, which are generally more secure than normal authentication mechanisms.
1448
1449
1450
1451 DragonFly comes with a kernel packet filter (known as IPFW), which is what the rest of this section will concentrate on. Proxy servers can be built on DragonFly from third party software, but there is such a variety of proxy servers available that it would be impossible to cover them in this section.
1452
1453
1454
1455 #### Packet Filtering Routers 
1456
1457
1458
1459 A router is a machine which forwards packets between two or more networks. A packet filtering router is programmed to compare each packet to a list of rules before deciding if it should be forwarded or not. Most modern IP routing software includes packet filtering functionality that defaults to forwarding all packets. To enable the filters, you need to define a set of rules.
1460
1461
1462
1463 To decide whether a packet should be passed on, the firewall looks through its set of rules for a rule which matches the contents of the packet's headers. Once a match is found, the rule action is obeyed. The rule action could be to drop the packet, to forward the packet, or even to send an ICMP message back to the originator. Only the first match counts, as the rules are searched in order. Hence, the list of rules can be referred to as a ***rule chain***.
1464
1465
1466
1467 The packet-matching criteria varies depending on the software used, but typically you can specify rules which depend on the source IP address of the packet, the destination IP address, the source port number, the destination port number (for protocols which support ports), or even the packet type (UDP, TCP, ICMP, etc).
1468
1469
1470
1471 #### Proxy Servers 
1472
1473
1474
1475 Proxy servers are machines which have had the normal system daemons ( **telnetd** ,  **ftpd** , etc) replaced with special servers. These servers are called ***proxy servers***, as they normally only allow onward connections to be made. This enables you to run (for example) a proxy  **telnet**  server on your firewall host, and people can  **telnet**  in to your firewall from the outside, go through some authentication mechanism, and then gain access to the internal network (alternatively, proxy servers can be used for signals coming from the internal network and heading out).
1476
1477
1478
1479 Proxy servers are normally more secure than normal servers, and often have a wider variety of authentication mechanisms available, including ***one-shot*** password systems so that even if someone manages to discover what password you used, they will not be able to use it to gain access to your systems as the password expires immediately after the first use. As they do not actually give users access to the host machine, it becomes a lot more difficult for someone to install backdoors around your security system.
1480
1481
1482
1483 Proxy servers often have ways of restricting access further, so that only certain hosts can gain access to the servers. Most will also allow the administrator to specify which users can talk to which destination machines. Again, what facilities are available depends largely on what proxy software you choose.
1484
1485
1486
1487 ### What Does IPFW Allow Me to Do? 
1488
1489
1490
1491 IPFW, the software supplied with DragonFly, is a packet filtering and accounting system which resides in the kernel, and has a user-land control utility, [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8). Together, they allow you to define and query the rules used by the kernel in its routing decisions.
1492
1493
1494
1495 There are two related parts to IPFW. The firewall section performs packet filtering. There is also an IP accounting section which tracks usage of the router, based on rules similar to those used in the firewall section. This allows the administrator to monitor how much traffic the router is getting from a certain machine, or how much WWW traffic it is forwarding, for example.
1496
1497
1498
1499 As a result of the way that IPFW is designed, you can use IPFW on non-router machines to perform packet filtering on incoming and outgoing connections. This is a special case of the more general use of IPFW, and the same commands and techniques should be used in this situation.
1500
1501
1502
1503 ### Enabling IPFW on DragonFly 
1504
1505
1506
1507 As the main part of the IPFW system lives in the kernel, you will need to add one or more options to your kernel configuration file, depending on what facilities you want, and recompile your kernel. See "Reconfiguring your Kernel" ([kernelconfig.html Chapter 9]) for more details on how to recompile your kernel.
1508
1509
1510
1511  **Warning:** IPFW defaults to a policy of `deny ip from any to any`. If you do not add other rules during startup to allow access, ***you will lock yourself out*** of the server upon rebooting into a firewall-enabled kernel. We suggest that you set `firewall_type=open` in your `/etc/rc.conf` file when first enabling this feature, then refining the firewall rules in `/etc/rc.firewall` after you have tested that the new kernel feature works properly. To be on the safe side, you may wish to consider performing the initial firewall configuration from the local console rather than via  **ssh** . Another option is to build a kernel using both the `IPFIREWALL` and `IPFIREWALL_DEFAULT_TO_ACCEPT` options. This will change the default rule of IPFW to `allow ip from any to any` and avoid the possibility of a lockout.
1512
1513
1514
1515 There are currently four kernel configuration options relevant to IPFW:
1516
1517
1518
1519 `options IPFIREWALL`:: Compiles into the kernel the code for packet filtering.`options IPFIREWALL_VERBOSE`:: Enables code to allow logging of packets through [syslogd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#syslogd&section8). Without this option, even if you specify that packets should be logged in the filter rules, nothing will happen.`options IPFIREWALL_VERBOSE_LIMIT=10`:: Limits the number of packets logged through [syslogd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=syslogd&section=8) on a per entry basis. You may wish to use this option in hostile environments in which you want to log firewall activity, but do not want to be open to a denial of service attack via syslog flooding.
1520
1521 When a chain entry reaches the packet limit specified, logging is turned off for that particular entry. To resume logging, you will need to reset the associated counter using the [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8) utility:
1522
1523     
1524
1525     # ipfw zero 4500
1526
1527
1528
1529 Where 4500 is the chain entry you wish to continue logging.`options IPFIREWALL_DEFAULT_TO_ACCEPT`:: This changes the default rule action from ***deny*** to ***allow***. This avoids the possibility of locking yourself out if you happen to boot a kernel with `IPFIREWALL` support but have not configured your firewall yet. It is also very useful if you often use [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8) as a filter for specific problems as they arise. Use with care though, as this opens up the firewall and changes the way it works.
1530
1531
1532
1533 ### Configuring IPFW 
1534
1535
1536
1537 The configuration of the IPFW software is done through the [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8) utility. The syntax for this command looks quite complicated, but it is relatively simple once you understand its structure.
1538
1539
1540
1541 There are currently four different command categories used by the utility: addition/deletion, listing, flushing, and clearing. Addition/deletion is used to build the rules that control how packets are accepted, rejected, and logged. Listing is used to examine the contents of your rule set (otherwise known as the chain) and packet counters (accounting). Flushing is used to remove all entries from the chain. Clearing is used to zero out one or more accounting entries.
1542
1543
1544
1545 #### Altering the IPFW Rules 
1546
1547
1548
1549 The syntax for this form of the command is:
1550
1551
1552
1553 `ipfw` [-N] command [index] action [log] protocol addresses [options]
1554
1555
1556
1557 There is one valid flag when using this form of the command:
1558
1559
1560
1561 -N:: Resolve addresses and service names in output.
1562
1563
1564
1565 The ***command*** given can be shortened to the shortest unique form. The valid ***commands*** are:
1566
1567
1568
1569 add:: Add an entry to the firewall/accounting rule listdelete:: Delete an entry from the firewall/accounting rule list
1570
1571
1572
1573 Previous versions of IPFW used separate firewall and accounting entries. The present version provides packet accounting with each firewall entry.
1574
1575
1576
1577 If an ***index*** value is supplied, it is used to place the entry at a specific point in the chain. Otherwise, the entry is placed at the end of the chain at an index 100 greater than the last chain entry (this does not include the default policy, rule 65535, deny).
1578
1579
1580
1581 The `log` option causes matching rules to be output to the system console if the kernel was compiled with `IPFIREWALL_VERBOSE`.
1582
1583
1584
1585 Valid ***actions*** are:
1586
1587
1588
1589 reject:: Drop the packet, and send an ICMP host or port unreachable (as appropriate) packet to the source.allow:: Pass the packet on as normal. (aliases: `pass`, `permit`, and `accept`)deny:: Drop the packet. The source is not notified via an ICMP message (thus it appears that the packet never arrived at the destination).count:: Update packet counters but do not allow/deny the packet based on this rule. The search continues with the next chain entry.
1590
1591
1592
1593 Each ***action*** will be recognized by the shortest unambiguous prefix.
1594
1595
1596
1597 The ***protocols*** which can be specified are:
1598
1599
1600
1601 all:: Matches any IP packeticmp:: Matches ICMP packetstcp:: Matches TCP packetsudp:: Matches UDP packets
1602
1603
1604
1605 The ***address*** specification is:
1606
1607
1608
1609 from `***address/mask***` [`***port***`] to `***address/mask***` [`***port***`] [via `***interface***`]
1610
1611
1612
1613 You can only specify `***port***` in conjunction with ***protocols*** which support ports (UDP and TCP).
1614
1615
1616
1617 The `via` is optional and may specify the IP address or domain name of a local IP interface, or an interface name (e.g. `ed0`) to match only packets coming through this interface. Interface unit numbers can be specified with an optional wildcard. For example, `ppp*` would match all kernel PPP interfaces.
1618
1619
1620
1621 The syntax used to specify an `***address/mask***` is:
1622
1623
1624
1625     
1626
1627     `***address***`
1628
1629
1630
1631
1632
1633  or
1634
1635
1636
1637     
1638
1639     `***address***`/`***mask-bits***`
1640
1641
1642
1643
1644
1645  or
1646
1647
1648
1649     
1650
1651     `***address***`:`***mask-pattern***`
1652
1653
1654
1655
1656
1657 A valid hostname may be specified in place of the IP address. ' **mask-bits** ' is a decimal number representing how many bits in the address mask should be set. e.g. specifying `192.216.222.1/24` will create a mask which will allow any address in a class C subnet (in this case, `192.216.222`) to be matched. ' **mask-pattern** ' is an IP address which will be logically AND'ed with the address given. The keyword `any` may be used to specify ***any IP address***.
1658
1659
1660
1661 The port numbers to be blocked are specified as:
1662
1663
1664
1665 `***port***` [,`***port***` [,`***port***` [...]]]
1666
1667
1668
1669  to specify either a single port or a list of ports, or
1670
1671
1672
1673 `***port***`-`***port***`
1674
1675
1676
1677  to specify a range of ports. You may also combine a single range with a list, but the range must always be specified first.
1678
1679
1680
1681 The ***options*** available are:
1682
1683
1684
1685 frag:: Matches if the packet is not the first fragment of the datagram.in:: Matches if the packet is on the way in.out:: Matches if the packet is on the way out.ipoptions `***spec***`:: Matches if the IP header contains the comma separated list of options specified in `***spec***`. The supported IP options are: `ssrr` (strict source route), `lsrr` (loose source route), `rr` (record packet route), and `ts` (time stamp). The absence of a particular option may be specified with a leading `!`.established:: Matches if the packet is part of an already established TCP connection (i.e. it has the RST or ACK bits set). You can optimize the performance of the firewall by placing ***established*** rules early in the chain.setup:: Matches if the packet is an attempt to establish a TCP connection (the SYN bit is set but the ACK bit is not).tcpflags `***flags***`:: Matches if the TCP header contains the comma separated list of `***flags***`. The supported flags are `fin`, `syn`, `rst`, `psh`, `ack`, and `urg`. The absence of a particular flag may be indicated by a leading `!`.icmptypes `***types***`:: Matches if the ICMP type is present in the list `***types***`. The list may be specified as any combination of ranges and/or individual types separated by commas. Commonly used ICMP types are: `0` echo reply (ping reply), `3` destination unreachable, `5` redirect, `8` echo request (ping request), and `11` time exceeded (used to indicate TTL expiration as with [traceroute(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#traceroute&section8)).
1686
1687
1688
1689 #### Listing the IPFW Rules 
1690
1691
1692
1693 The syntax for this form of the command is:
1694
1695
1696
1697 `ipfw` [-a] [-c] [-d] [-e] [-t] [-N] [-S] list
1698
1699
1700
1701 There are seven valid flags when using this form of the command:
1702
1703
1704
1705 -a:: While listing, show counter values. This option is the only way to see accounting counters.-c:: List rules in compact form.-d:: Show dynamic rules in addition to static rules.-e:: If `-d` was specified, also show expired dynamic rules.-t:: Display the last match times for each chain entry. The time listing is incompatible with the input syntax used by the [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8) utility.-N:: Attempt to resolve given addresses and service names.-S:: Show the set each rule belongs to. If this flag is not specified, disabled rules will not be listed.
1706
1707
1708
1709 #### Flushing the IPFW Rules 
1710
1711
1712
1713 The syntax for flushing the chain is:
1714
1715
1716
1717 `ipfw` flush
1718
1719
1720
1721 This causes all entries in the firewall chain to be removed except the fixed default policy enforced by the kernel (index 65535). Use caution when flushing rules; the default deny policy will leave your system cut off from the network until allow entries are added to the chain.
1722
1723
1724
1725 #### Clearing the IPFW Packet Counters 
1726
1727
1728
1729 The syntax for clearing one or more packet counters is:
1730
1731
1732
1733 `ipfw` zero [`***index***`]
1734
1735
1736
1737 When used without an `***index***` argument, all packet counters are cleared. If an `***index***` is supplied, the clearing operation only affects a specific chain entry.
1738
1739
1740
1741 ### Example Commands for  **ipfw**  
1742
1743
1744
1745 This command will deny all packets from the host `evil.crackers.org` to the telnet port of the host `nice.people.org`:
1746
1747
1748
1749     
1750
1751     # ipfw add deny tcp from evil.crackers.org to nice.people.org 23
1752
1753
1754
1755
1756
1757 The next example denies and logs any TCP traffic from the entire `crackers.org` network (a class C) to the `nice.people.org` machine (any port).
1758
1759
1760
1761     
1762
1763     # ipfw add deny log tcp from evil.crackers.org/24 to nice.people.org
1764
1765
1766
1767
1768
1769 If you do not want people sending X sessions to your internal network (a subnet of a class C), the following command will do the necessary filtering:
1770
1771
1772
1773     
1774
1775     # ipfw add deny tcp from any to my.org/28 6000 setup
1776
1777
1778
1779
1780
1781 To see the accounting records:
1782
1783
1784
1785     
1786
1787     # ipfw -a list
1788
1789
1790
1791
1792
1793  or in the short form
1794
1795
1796
1797     
1798
1799     # ipfw -a l
1800
1801
1802
1803
1804
1805 You can also see the last time a chain entry was matched with:
1806
1807
1808
1809     
1810
1811     # ipfw -at l
1812
1813
1814
1815
1816
1817 ### Building a Packet Filtering Firewall 
1818
1819 <!-- XXX: AFAIK pf is vastly superior to ipfw, so this should be focused on pf, not ipfw -->
1820
1821
1822  **Note:** The following suggestions are just that: suggestions. The requirements of each firewall are different and we cannot tell you how to build a firewall to meet your particular requirements.
1823
1824
1825
1826 When initially setting up your firewall, unless you have a test bench setup where you can configure your firewall host in a controlled environment, it is strongly recommend you use the logging version of the commands and enable logging in the kernel. This will allow you to quickly identify problem areas and cure them without too much disruption. Even after the initial setup phase is complete, I recommend using the logging for `deny' as it allows tracing of possible attacks and also modification of the firewall rules if your requirements alter.
1827
1828
1829
1830  **Note:** If you use the logging versions of the `accept` command, be aware that it can generate ***large*** amounts of log data. One log entry will be generated for every packet that passes through the firewall, so large FTP/http transfers, etc, will really slow the system down. It also increases the latencies on those packets as it requires more work to be done by the kernel before the packet can be passed on.  **syslogd**  will also start using up a lot more processor time as it logs all the extra data to disk, and it could quite easily fill the partition `/var/log` is located on.
1831
1832
1833
1834 You should enable your firewall from `/etc/rc.conf.local` or `/etc/rc.conf`. The associated manual page explains which knobs to fiddle and lists some preset firewall configurations. If you do not use a preset configuration, `ipfw list` will output the current ruleset into a file that you can pass to `rc.conf`. If you do not use `/etc/rc.conf.local` or `/etc/rc.conf` to enable your firewall, it is important to make sure your firewall is enabled before any IP interfaces are configured.
1835
1836
1837
1838 The next problem is what your firewall should actually ***do***! This is largely dependent on what access to your network you want to allow from the outside, and how much access to the outside world you want to allow from the inside. Some general rules are:
1839
1840
1841
1842
1843 * Block all incoming access to ports below 1024 for TCP. This is where most of the security sensitive services are, like finger, SMTP (mail) and telnet.
1844
1845
1846 * Block ***all*** incoming UDP traffic. There are very few useful services that travel over UDP, and what useful traffic there is, is normally a security threat (e.g. Suns RPC and NFS protocols). This has its disadvantages also, since UDP is a connectionless protocol, denying incoming UDP traffic also blocks the replies to outgoing UDP traffic. This can cause a problem for people (on the inside) using external archie (prospero) servers. If you want to allow access to archie, you will have to allow packets coming from ports 191 and 1525 to any internal UDP port through the firewall.  **ntp**  is another service you may consider allowing through, which comes from port 123.
1847
1848
1849 * Block traffic to port 6000 from the outside. Port 6000 is the port used for access to X11 servers, and can be a security threat (especially if people are in the habit of doing `xhost +` on their workstations). X11 can actually use a range of ports starting at 6000, the upper limit being how many X displays you can run on the machine. The upper limit as defined by RFC 1700 (Assigned Numbers) is 6063.
1850
1851
1852 * Check what ports any internal servers use (e.g. SQL servers, etc). It is probably a good idea to block those as well, as they normally fall outside the 1-1024 range specified above.
1853
1854
1855
1856 Another checklist for firewall configuration is available from CERT at http://www.cert.org/tech_tips/packet_filtering.html
1857
1858
1859
1860 As stated above, these are only ***guidelines***. You will have to decide what filter rules you want to use on your firewall yourself. We cannot accept ANY responsibility if someone breaks into your network, even if you follow the advice given above.
1861
1862
1863
1864 ### IPFW Overhead and Optimization 
1865
1866
1867
1868 Many people want to know how much overhead IPFW adds to a system. The answer to this depends mostly on your rule set and processor speed. For most applications dealing with Ethernet and small rule sets, the answer is ***negligible***. For those of you that need actual measurements to satisfy your curiosity, read on.
1869
1870
1871
1872 The following measurements were made using FreeBSD 2.2.5-STABLE on a 486-66. (While IPFW has changed slightly in later releases of DragonFly, it still performs with similar speed.) IPFW was modified to measure the time spent within the `ip_fw_chk` routine, displaying the results to the console every 1000 packets.
1873
1874
1875
1876 Two rule sets, each with 1000 rules, were tested. The first set was designed to demonstrate a worst case scenario by repeating the rule:
1877
1878
1879
1880     
1881
1882     # ipfw add deny tcp from any to any 55555
1883
1884
1885
1886
1887
1888 This demonstrates a worst case scenario by causing most of IPFW's packet check routine to be executed before finally deciding that the packet does not match the rule (by virtue of the port number). Following the 999th iteration of this rule was an `allow ip from any to any`.
1889
1890
1891
1892 The second set of rules were designed to abort the rule check quickly:
1893
1894
1895
1896     
1897
1898     # ipfw add deny ip from 1.2.3.4 to 1.2.3.4
1899
1900
1901
1902
1903
1904 The non-matching source IP address for the above rule causes these rules to be skipped very quickly. As before, the 1000th rule was an `allow ip from any to any`.
1905
1906
1907
1908 The per-packet processing overhead in the former case was approximately 2.703 ms/packet, or roughly 2.7 microseconds per rule. Thus the theoretical packet processing limit with these rules is around 370 packets per second. Assuming 10 Mbps Ethernet and a ~1500 byte packet size, we would only be able to achieve 55.5% bandwidth utilization.
1909
1910
1911
1912 For the latter case each packet was processed in approximately 1.172 ms, or roughly 1.2 microseconds per rule. The theoretical packet processing limit here would be about 853 packets per second, which could consume 10 Mbps Ethernet bandwidth.
1913
1914
1915
1916 The excessive number of rules tested and the nature of those rules do not provide a real-world scenario -- they were used only to generate the timing information presented here. Here are a few things to keep in mind when building an efficient rule set:
1917
1918
1919
1920
1921 * Place an `established` rule early on to handle the majority of TCP traffic. Do not put any `allow tcp` statements before this rule.
1922
1923
1924 * Place heavily triggered rules earlier in the rule set than those rarely used (***without changing the permissiveness of the firewall***, of course). You can see which rules are used most often by examining the packet counting statistics with `ipfw -a l`.
1925
1926
1927
1928
1929
1930
1931
1932 CategoryHandbook
1933
1934 CategoryHandbook-security
1935
1936
1937
1938
1939
1940
1941 ## OpenSSL 
1942
1943
1944
1945 [OpenSSL](http://www.openssl.org/) provides a general-purpose cryptography library, as well as the Secure Sockets Layer v2/v3 (SSLv2/SSLv3) and Transport Layer Security v1 (TLSv1) network security protocols.
1946
1947
1948
1949 However, one of the algorithms (specifically IDEA) included in OpenSSL is protected by patents in the USA and elsewhere, and is not available for unrestricted use. IDEA is included in the OpenSSL sources in DragonFly, but it is not built by default. If you wish to use it, and you comply with the license terms, enable the `MAKE_IDEA` switch in `/etc/make.conf` and rebuild your sources using `make world`.
1950
1951
1952
1953 Today, the RSA algorithm is free for use in USA and other countries. In the past it was protected by a patent.
1954
1955
1956
1957
1958
1959 ## VPN over IPsec 
1960
1961 <!-- XXX: someone revise this and add words about pf. I've no clue about this stuff -->
1962
1963
1964 ***Written by Nik Clayton. ***
1965
1966
1967
1968 Creating a VPN between two networks, separated by the Internet, using DragonFly gateways.
1969
1970
1971
1972 ### Understanding IPsec 
1973
1974
1975
1976 ***Written by Hiten M. Pandya. ***
1977
1978
1979
1980 This section will guide you through the process of setting up IPsec, and to use it in an environment which consists of DragonFly and  **Microsoft® Windows® 2000/XP**  machines, to make them communicate securely. In order to set up IPsec, it is necessary that you are familiar with the concepts of building a custom kernel (see [kernelconfig.html Chapter 9]).
1981
1982
1983
1984 ***IPsec*** is a protocol which sits on top of the Internet Protocol (IP) layer. It allows two or more hosts to communicate in a secure manner (hence the name). The DragonFly IPsec ***network stack*** is based on the [KAME](http://www.kame.net/) implementation, which has support for both protocol families, IPv4 and IPv6.
1985
1986
1987
1988 IPsec consists of two sub-protocols:
1989
1990
1991
1992
1993 * ***Encapsulated Security Payload (ESP)***, protects the IP packet data from third party interference, by encrypting the contents using symmetric cryptography algorithms (like Blowfish, 3DES).
1994
1995
1996 * ***Authentication Header (AH)***, protects the IP packet header from third party interference and spoofing, by computing a cryptographic checksum and hashing the IP packet header fields with a secure hashing function. This is then followed by an additional header that contains the hash, to allow the information in the packet to be authenticated.
1997
1998
1999
2000 ESP and AH can either be used together or separately, depending on the environment.
2001
2002
2003
2004 IPsec can either be used to directly encrypt the traffic between two hosts (known as ***Transport Mode***); or to build ***virtual tunnels*** between two subnets, which could be used for secure communication between two corporate networks (known as ***Tunnel Mode***). The latter is more commonly known as a ***Virtual Private Network (VPN)***. The [ipsec(4)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipsec&section4) manual page should be consulted for detailed information on the IPsec subsystem in DragonFly.
2005
2006
2007
2008 To add IPsec support to your kernel, add the following options to your kernel configuration file:
2009
2010
2011
2012     
2013
2014     options   IPSEC        #IP security
2015
2016     options   IPSEC_ESP    #IP security (crypto; define w/ IPSEC)
2017
2018     
2019
2020
2021
2022
2023
2024 If IPsec debugging support is desired, the following kernel option should also be added:
2025
2026
2027
2028     
2029
2030     options   IPSEC_DEBUG  #debug for IP security
2031
2032     
2033
2034
2035
2036
2037
2038 ### The Problem 
2039
2040
2041
2042 There's no standard for what constitutes a VPN. VPNs can be implemented using a number of different technologies, each of which have their own strengths and weaknesses. This article presents a number of scenarios, and strategies for implementing a VPN for each scenario.
2043
2044
2045
2046 ### Scenario #1: Two networks, connected to the Internet, to behave as one 
2047
2048
2049
2050 This is the scenario that caused me to first investigating VPNs. The premise is as follows:
2051
2052
2053
2054
2055 * You have at least two sites
2056
2057
2058 * Both sites are using IP internally
2059
2060
2061 * Both sites are connected to the Internet, through a gateway that is running DragonFly.
2062
2063
2064 * The gateway on each network has at least one public IP address.
2065
2066
2067 * The internal addresses of the two networks can be public or private IP addresses, it doesn't matter. You can be running NAT on the gateway machine if necessary.
2068
2069
2070 * The internal IP addresses of the two networks ***do not collide***. While I expect it is theoretically possible to use a combination of VPN technology and NAT to get this to work, I expect it to be a configuration nightmare.
2071
2072
2073
2074 If you find that you are trying to connect two networks, both of which, internally, use the same private IP address range (e.g., both of them use `192.168.1.x`), then one of the networks will have to be renumbered.
2075
2076
2077
2078 The network topology might look something like this:
2079
2080
2081
2082 security/ipsec-network.png
2083
2084
2085
2086 Notice the two public IP addresses. I'll use the letters to refer to them in the rest of this article. Anywhere you see those letters in this article, replace them with your own public IP addresses. Note also that internally, the two gateway machines have .1 IP addresses, and that the two networks have different private IP addresses (`192.168.1.x` and `192.168.2.x` respectively). All the machines on the private networks have been configured to use the `.1` machine as their default gateway.
2087
2088
2089
2090 The intention is that, from a network point of view, each network should view the machines on the other network as though they were directly attached the same router -- albeit a slightly slow router with an occasional tendency to drop packets.
2091
2092
2093
2094 This means that (for example), machine `192.168.1.20` should be able to run
2095
2096
2097
2098     
2099
2100     ping 192.168.2.34
2101
2102
2103
2104
2105
2106 and have it work, transparently. Windows machines should be able to see the machines on the other network, browse file shares, and so on, in exactly the same way that they can browse machines on the local network.
2107
2108
2109
2110 And the whole thing has to be secure. This means that traffic between the two networks has to be encrypted.
2111
2112
2113
2114 Creating a VPN between these two networks is a multi-step process. The stages are as follows:
2115
2116
2117
2118   1. Create a ***virtual*** network link between the two networks, across the Internet. Test it, using tools like [ping(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ping&section8), to make sure it works.
2119
2120   1. Apply security policies to ensure that traffic between the two networks is transparently encrypted and decrypted as necessary. Test this, using tools like [tcpdump(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#tcpdump&section1), to ensure that traffic is encrypted.
2121
2122   1. Configure additional software on the DragonFly gateways, to allow Windows machines to see one another across the VPN.
2123
2124
2125
2126 #### Step 1: Creating and testing a ***virtual*** network link 
2127
2128
2129
2130 Suppose that you were logged in to the gateway machine on network #1 (with public IP address `A.B.C.D`, private IP address `192.168.1.1`), and you ran `ping 192.168.2.1`, which is the private address of the machine with IP address `W.X.Y.Z`. What needs to happen in order for this to work?
2131
2132
2133
2134   1. The gateway machine needs to know how to reach `192.168.2.1`. In other words, it needs to have a route to `192.168.2.1`.
2135
2136   1. Private IP addresses, such as those in the `192.168.x` range are not supposed to appear on the Internet at large. Instead, each packet you send to `192.168.2.1` will need to be wrapped up inside another packet. This packet will need to appear to be from `A.B.C.D`, and it will have to be sent to `W.X.Y.Z`. This process is called ***encapsulation***.
2137
2138   1. Once this packet arrives at `W.X.Y.Z` it will need to ***unencapsulated***, and delivered to `192.168.2.1`.
2139
2140
2141
2142 You can think of this as requiring a ***tunnel*** between the two networks. The two ***tunnel mouths*** are the IP addresses `A.B.C.D` and `W.X.Y.Z`, and the tunnel must be told the addresses of the private IP addresses that will be allowed to pass through it. The tunnel is used to transfer traffic with private IP addresses across the public Internet.
2143
2144
2145
2146 This tunnel is created by using the generic interface, or `gif` devices on DragonFly. As you can imagine, the `gif` interface on each gateway host must be configured with four IP addresses; two for the public IP addresses, and two for the private IP addresses.
2147
2148
2149
2150 Support for the gif device must be compiled in to the DragonFly kernel on both machines. You can do this by adding the line:
2151
2152
2153
2154     
2155
2156     pseudo-device gif
2157
2158
2159
2160
2161
2162 to the kernel configuration files on both machines, and then compile, install, and reboot as normal.
2163
2164
2165
2166 Configuring the tunnel is a two step process. First the tunnel must be told what the outside (or public) IP addresses are, using [gifconfig(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#gifconfig&section8). Then the private IP addresses must be configured using [ifconfig(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=ifconfig&section=8).
2167
2168
2169
2170 On the gateway machine on network #1 you would run the following two commands to configure the tunnel.
2171
2172
2173
2174     
2175
2176     gifconfig gif0 A.B.C.D W.X.Y.Z
2177
2178     ifconfig gif0 inet 192.168.1.1 192.168.2.1 netmask 0xffffffff
2179
2180     
2181
2182
2183
2184
2185
2186 On the other gateway machine you run the same commands, but with the order of the IP addresses reversed.
2187
2188
2189
2190     
2191
2192     gifconfig gif0 W.X.Y.Z A.B.C.D
2193
2194     ifconfig gif0 inet 192.168.2.1 192.168.1.1 netmask 0xffffffff
2195
2196     
2197
2198
2199
2200
2201
2202 You can then run:
2203
2204
2205
2206     
2207
2208     gifconfig gif0
2209
2210
2211
2212
2213
2214 to see the configuration. For example, on the network #1 gateway, you would see this:
2215
2216
2217
2218     
2219
2220     # gifconfig gif0
2221
2222     gif0: flags=8011&lt;UP,POINTTOPOINT,MULTICAST&gt; mtu 1280
2223
2224     inet 192.168.1.1 --&gt; 192.168.2.1 netmask 0xffffffff
2225
2226     physical address inet A.B.C.D --&gt; W.X.Y.Z
2227
2228     
2229
2230
2231
2232
2233
2234 As you can see, a tunnel has been created between the physical addresses `A.B.C.D` and `W.X.Y.Z`, and the traffic allowed through the tunnel is that between `192.168.1.1` and `192.168.2.1`.
2235
2236
2237
2238 This will also have added an entry to the routing table on both machines, which you can examine with the command `netstat -rn`. This output is from the gateway host on network #1.
2239
2240
2241
2242     
2243
2244     # netstat -rn
2245
2246     Routing tables
2247
2248     
2249
2250     Internet:
2251
2252     Destination      Gateway       Flags    Refs    Use    Netif  Expire
2253
2254     ...
2255
2256     192.168.2.1      192.168.1.1   UH        0        0    gif0
2257
2258     ...
2259
2260     
2261
2262
2263
2264
2265
2266 As the ***Flags*** value indicates, this is a host route, which means that each gateway knows how to reach the other gateway, but they do not know how to reach the rest of their respective networks. That problem will be fixed shortly.
2267
2268
2269
2270 It is likely that you are running a firewall on both machines. This will need to be circumvented for your VPN traffic. You might want to allow all traffic between both networks, or you might want to include firewall rules that protect both ends of the VPN from one another.
2271
2272
2273
2274 It greatly simplifies testing if you configure the firewall to allow all traffic through the VPN. You can always tighten things up later. If you are using [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8) on the gateway machines then a command like
2275
2276
2277
2278     
2279
2280     ipfw add 1 allow ip from any to any via gif0
2281
2282
2283
2284
2285
2286 will allow all traffic between the two end points of the VPN, without affecting your other firewall rules. Obviously you will need to run this command on both gateway hosts.
2287
2288
2289
2290 This is sufficient to allow each gateway machine to ping the other. On `192.168.1.1`, you should be able to run
2291
2292
2293
2294     
2295
2296     ping 192.168.2.1
2297
2298
2299
2300
2301
2302 and get a response, and you should be able to do the same thing on the other gateway machine.
2303
2304
2305
2306 However, you will not be able to reach internal machines on either network yet. This is because of the routing -- although the gateway machines know how to reach one another, they do not know how to reach the network behind each one.
2307
2308
2309
2310 To solve this problem you must add a static route on each gateway machine. The command to do this on the first gateway would be:
2311
2312
2313
2314     
2315
2316     route add 192.168.2.0 192.168.2.1 netmask 0xffffff00
2317
2318     
2319
2320
2321
2322
2323
2324 This says ***In order to reach the hosts on the network `192.168.2.0`, send the packets to the host `192.168.2.1`***. You will need to run a similar command on the other gateway, but with the `192.168.1.x` addresses instead.
2325
2326
2327
2328 IP traffic from hosts on one network will now be able to reach hosts on the other network.
2329
2330
2331
2332 That has now created two thirds of a VPN between the two networks, in as much as it is ***virtual*** and it is a ***network***. It is not private yet. You can test this using [ping(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ping&section8) and [tcpdump(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=tcpdump&section=1). Log in to the gateway host and run
2333
2334
2335
2336     
2337
2338     tcpdump dst host 192.168.2.1
2339
2340
2341
2342
2343
2344 In another log in session on the same host run
2345
2346
2347
2348     
2349
2350     ping 192.168.2.1
2351
2352
2353
2354
2355
2356 You will see output that looks something like this:
2357
2358
2359
2360     
2361
2362     16:10:24.018080 192.168.1.1 &gt; 192.168.2.1: icmp: echo request
2363
2364     16:10:24.018109 192.168.1.1 &gt; 192.168.2.1: icmp: echo reply
2365
2366     16:10:25.018814 192.168.1.1 &gt; 192.168.2.1: icmp: echo request
2367
2368     16:10:25.018847 192.168.1.1 &gt; 192.168.2.1: icmp: echo reply
2369
2370     16:10:26.028896 192.168.1.1 &gt; 192.168.2.1: icmp: echo request
2371
2372     16:10:26.029112 192.168.1.1 &gt; 192.168.2.1: icmp: echo reply
2373
2374     
2375
2376
2377
2378
2379
2380 As you can see, the ICMP messages are going back and forth unencrypted. If you had used the `-s` parameter to [tcpdump(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#tcpdump&section1) to grab more bytes of data from the packets you would see more information.
2381
2382
2383
2384 Obviously this is unacceptable. The next section will discuss securing the link between the two networks so that it all traffic is automatically encrypted.
2385
2386
2387
2388  **Summary:** 
2389
2390
2391
2392
2393 * Configure both kernels with ***pseudo-device gif***.
2394
2395
2396 * Edit `/etc/rc.conf` on gateway host #1 and add the following lines (replacing IP addresses as necessary).
2397
2398       
2399
2400       gifconfig_gif0="A.B.C.D W.X.Y.Z"
2401
2402       ifconfig_gif0="inet 192.168.1.1 192.168.2.1 netmask 0xffffffff"
2403
2404       static_routes="vpn"
2405
2406       route_vpn="192.168.2.0 192.168.2.1 netmask 0xffffff00"
2407
2408   
2409
2410
2411 * Edit your firewall script (`/etc/rc.firewall`, or similar) on both hosts, and add
2412
2413       
2414
2415       ipfw add 1 allow ip from any to any via gif0
2416
2417   
2418
2419
2420 * Make similar changes to `/etc/rc.conf` on gateway host #2, reversing the order of IP addresses.
2421
2422
2423
2424 #### Step 2: Securing the link 
2425
2426
2427
2428 To secure the link we will be using IPsec. IPsec provides a mechanism for two hosts to agree on an encryption key, and to then use this key in order to encrypt data between the two hosts.
2429
2430
2431
2432 The are two areas of configuration to be considered here.
2433
2434
2435
2436   1. There must be a mechanism for two hosts to agree on the encryption mechanism to use. Once two hosts have agreed on this mechanism there is said to be a ***security association*** between them.
2437
2438   1. There must be a mechanism for specifying which traffic should be encrypted. Obviously, you don't want to encrypt all your outgoing traffic -- you only want to encrypt the traffic that is part of the VPN. The rules that you put in place to determine what traffic will be encrypted are called ***security policies***.
2439
2440
2441
2442 Security associations and security policies are both maintained by the kernel, and can be modified by userland programs. However, before you can do this you must configure the kernel to support IPsec and the Encapsulated Security Payload (ESP) protocol. This is done by configuring a kernel with:
2443
2444
2445
2446     
2447
2448     options IPSEC
2449
2450     options IPSEC_ESP
2451
2452     
2453
2454
2455
2456
2457
2458 and recompiling, reinstalling, and rebooting. As before you will need to do this to the kernels on both of the gateway hosts.
2459
2460
2461
2462 You have two choices when it comes to setting up security associations. You can configure them by hand between two hosts, which entails choosing the encryption algorithm, encryption keys, and so forth, or you can use daemons that implement the Internet Key Exchange protocol (IKE) to do this for you.
2463
2464
2465
2466 I recommend the latter. Apart from anything else, it is easier to set up.
2467
2468
2469
2470 Editing and displaying security policies is carried out using [setkey(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#setkey&section8). By analogy, `setkey` is to the kernel's security policy tables as [route(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=route&section=8) is to the kernel's routing tables. `setkey` can also display the current security associations, and to continue the analogy further, is akin to `netstat -r` in that respect.
2471
2472
2473
2474 There are a number of choices for daemons to manage security associations with DragonFly. This article will describe how to use one of these, racoon. racoon is in the FreeBSD ports collection, in the security/ category, and is installed in the usual way.
2475
2476
2477
2478 racoon must be run on both gateway hosts. On each host it is configured with the IP address of the other end of the VPN, and a secret key (which you choose, and must be the same on both gateways).
2479
2480
2481
2482 The two daemons then contact one another, confirm that they are who they say they are (by using the secret key that you configured). The daemons then generate a new secret key, and use this to encrypt the traffic over the VPN. They periodically change this secret, so that even if an attacker were to crack one of the keys (which is as theoretically close to unfeasible as it gets) it won't do them much good -- by the time they've cracked the key the two daemons have chosen another one.
2483
2484
2485
2486 racoon's configuration is stored in `${PREFIX}/etc/racoon`. You should find a configuration file there, which should not need to be changed too much. The other component of racoon's configuration, which you will need to change, is the ***pre-shared key***.
2487
2488
2489
2490 The default racoon configuration expects to find this in the file `${PREFIX}/etc/racoon/psk.txt`. It is important to note that the pre-shared key is ***not*** the key that will be used to encrypt your traffic across the VPN link, it is simply a token that allows the key management daemons to trust one another.
2491
2492
2493
2494 `psk.txt` contains a line for each remote site you are dealing with. In this example, where there are two sites, each `psk.txt` file will contain one line (because each end of the VPN is only dealing with one other end).
2495
2496
2497
2498 On gateway host #1 this line should look like this:
2499
2500
2501
2502     
2503
2504     W.X.Y.Z            secret
2505
2506
2507
2508
2509
2510 That is, the ***public*** IP address of the remote end, whitespace, and a text string that provides the secret. Obviously, you shouldn't use ***secret*** as your key -- the normal rules for choosing a password apply.
2511
2512
2513
2514 On gateway host #2 the line would look like this
2515
2516
2517
2518     
2519
2520     A.B.C.D            secret
2521
2522
2523
2524
2525
2526 That is, the public IP address of the remote end, and the same secret key. `psk.txt` must be mode `0600` (i.e., only read/write to `root`) before racoon will run.
2527
2528
2529
2530 You must run racoon on both gateway machines. You will also need to add some firewall rules to allow the IKE traffic, which is carried over UDP to the ISAKMP (Internet Security Association Key Management Protocol) port. Again, this should be fairly early in your firewall ruleset.
2531
2532
2533
2534     
2535
2536     ipfw add 1 allow udp from A.B.C.D to W.X.Y.Z isakmp
2537
2538     ipfw add 1 allow udp from W.X.Y.Z to A.B.C.D isakmp
2539
2540     
2541
2542
2543
2544
2545
2546 Once racoon is running you can try pinging one gateway host from the other. The connection is still not encrypted, but racoon will then set up the security associations between the two hosts -- this might take a moment, and you may see this as a short delay before the ping commands start responding.
2547
2548
2549
2550 Once the security association has been set up you can view it using [setkey(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#setkey&section8). Run
2551
2552
2553
2554     
2555
2556     setkey -D
2557
2558
2559
2560
2561
2562 on either host to view the security association information.
2563
2564
2565
2566 That's one half of the problem. They other half is setting your security policies.
2567
2568
2569
2570 To create a sensible security policy, let's review what's been set up so far. This discussions hold for both ends of the link.
2571
2572
2573
2574 Each IP packet that you send out has a header that contains data about the packet. The header includes the IP addresses of both the source and destination. As we already know, private IP addresses, such as the `192.168.x.y` range are not supposed to appear on the public Internet. Instead, they must first be encapsulated inside another packet. This packet must have the public source and destination IP addresses substituted for the private addresses.
2575
2576
2577
2578 So if your outgoing packet started looking like this:
2579
2580
2581
2582 security/ipsec-out-pkt.png
2583
2584
2585
2586 Then it will be encapsulated inside another packet, looking something like this:
2587
2588
2589
2590 security/ipsec-encap-pkt.png
2591
2592
2593
2594 This encapsulation is carried out by the `gif` device. As you can see, the packet now has real IP addresses on the outside, and our original packet has been wrapped up as data inside the packet that will be put out on the Internet.
2595
2596
2597
2598 Obviously, we want all traffic between the VPNs to be encrypted. You might try putting this in to words, as:
2599
2600
2601
2602 ***If a packet leaves from `A.B.C.D`, and it is destined for `W.X.Y.Z`, then encrypt it, using the necessary security associations.***
2603
2604
2605
2606 ***If a packet arrives from `W.X.Y.Z`, and it is destined for `A.B.C.D`, then decrypt it, using the necessary security associations.***
2607
2608
2609
2610 That's close, but not quite right. If you did this, all traffic to and from `W.X.Y.Z`, even traffic that was not part of the VPN, would be encrypted. That's not quite what you want. The correct policy is as follows
2611
2612
2613
2614 ***If a packet leaves from `A.B.C.D`, and that packet is encapsulating another packet, and it is destined for `W.X.Y.Z`, then encrypt it, using the necessary security associations.***
2615
2616
2617
2618 ***If a packet arrives from `W.X.Y.Z`, and that packet is encapsulating another packet, and it is destined for `A.B.C.D`, then encrypt it, using the necessary security associations.***
2619
2620
2621
2622 A subtle change, but a necessary one.
2623
2624
2625
2626 Security policies are also set using [setkey(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#setkey&section8). [setkey(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=setkey&section=8) features a configuration language for defining the policy. You can either enter configuration instructions via stdin, or you can use the `-f` option to specify a filename that contains configuration instructions.
2627
2628
2629
2630 The configuration on gateway host #1 (which has the public IP address `A.B.C.D`) to force all outbound traffic to `W.X.Y.Z` to be encrypted is:
2631
2632
2633
2634     
2635
2636     spdadd A.B.C.D/32 W.X.Y.Z/32 ipencap -P out ipsec esp/tunnel/A.B.C.D-W.X.Y.Z/require;
2637
2638     
2639
2640
2641
2642
2643
2644 Put these commands in a file (e.g., `/etc/ipsec.conf`) and then run
2645
2646
2647
2648     
2649
2650     # setkey -f /etc/ipsec.conf
2651
2652
2653
2654
2655
2656 `spdadd` tells [setkey(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#setkey&section8) that we want to add a rule to the secure policy database. The rest of this line specifies which packets will match this policy. `A.B.C.D/32` and `W.X.Y.Z/32` are the IP addresses and netmasks that identify the network or hosts that this policy will apply to. In this case, we want it to apply to traffic between these two hosts. `ipencap` tells the kernel that this policy should only apply to packets that encapsulate other packets. `-P out` says that this policy applies to outgoing packets, and `ipsec` says that the packet will be secured.
2657
2658
2659
2660 The second line specifies how this packet will be encrypted. `esp` is the protocol that will be used, while `tunnel` indicates that the packet will be further encapsulated in an IPsec packet. The repeated use of `A.B.C.D` and `W.X.Y.Z` is used to select the security association to use, and the final `require` mandates that packets must be encrypted if they match this rule.
2661
2662
2663
2664 This rule only matches outgoing packets. You will need a similar rule to match incoming packets.
2665
2666
2667
2668     
2669
2670     spdadd W.X.Y.Z/32 A.B.C.D/32 ipencap -P in ipsec esp/tunnel/W.X.Y.Z-A.B.C.D/require;
2671
2672
2673
2674
2675
2676 Note the `in` instead of `out` in this case, and the necessary reversal of the IP addresses.
2677
2678
2679
2680 The other gateway host (which has the public IP address `W.X.Y.Z`) will need similar rules.
2681
2682
2683
2684     
2685
2686     spdadd W.X.Y.Z/32 A.B.C.D/32 ipencap -P out ipsec esp/tunnel/W.X.Y.Z-A.B.C.D/require;
2687
2688            spdadd A.B.C.D/32 W.X.Y.Z/32 ipencap -P in ipsec esp/tunnel/A.B.C.D-W.X.Y.Z/require;
2689
2690
2691
2692
2693
2694 Finally, you need to add firewall rules to allow ESP and IPENCAP packets back and forth. These rules will need to be added to both hosts.
2695
2696
2697
2698     
2699
2700     ipfw add 1 allow esp from A.B.C.D to W.X.Y.Z
2701
2702     ipfw add 1 allow esp from W.X.Y.Z to A.B.C.D
2703
2704     ipfw add 1 allow ipencap from A.B.C.D to W.X.Y.Z
2705
2706     ipfw add 1 allow ipencap from W.X.Y.Z to A.B.C.D
2707
2708     
2709
2710
2711
2712
2713
2714 Because the rules are symmetric you can use the same rules on each gateway host.
2715
2716
2717
2718 Outgoing packets will now look something like this:
2719
2720
2721
2722 security/ipsec-crypt-pkt.png
2723
2724
2725
2726 When they are received by the far end of the VPN they will first be decrypted (using the security associations that have been negotiated by racoon). Then they will enter the `gif` interface, which will unwrap the second layer, until you are left with the innermost packet, which can then travel in to the inner network.
2727
2728
2729
2730 You can check the security using the same [ping(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ping&section8) test from earlier. First, log in to the `A.B.C.D` gateway machine, and run:
2731
2732
2733
2734     
2735
2736     tcpdump dst host 192.168.2.1
2737
2738
2739
2740
2741
2742 In another log in session on the same host run
2743
2744
2745
2746     
2747
2748     ping 192.168.2.1
2749
2750
2751
2752
2753
2754 This time you should see output like the following:
2755
2756
2757
2758     
2759
2760     XXX tcpdump output
2761
2762
2763
2764
2765
2766 Now, as you can see, [tcpdump(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#tcpdump&section1) shows the ESP packets. If you try to examine them with the `-s` option you will see (apparently) gibberish, because of the encryption.
2767
2768
2769
2770 Congratulations. You have just set up a VPN between two remote sites.
2771
2772
2773
2774  **Summary** 
2775
2776
2777
2778
2779 * Configure both kernels with:
2780
2781       
2782
2783       options IPSEC
2784
2785       options IPSEC_ESP
2786
2787   
2788
2789
2790 * Install [`security/racoon`](http://pkgsrc.se/security/racoon). Edit `${PREFIX}/etc/racoon/psk.txt` on both gateway hosts, adding an entry for the remote host's IP address and a secret key that they both know. Make sure this file is mode 0600.
2791
2792
2793 * Add the following lines to `/etc/rc.conf` on each host:
2794
2795       
2796
2797       ipsec_enable="YES"
2798
2799       ipsec_file="/etc/ipsec.conf"
2800
2801   
2802
2803
2804 * Create an `/etc/ipsec.conf` on each host that contains the necessary spdadd lines. On gateway host #1 this would be:
2805
2806       
2807
2808       spdadd A.B.C.D/32 W.X.Y.Z/32 ipencap -P out ipsec
2809
2810         esp/tunnel/A.B.C.D-W.X.Y.Z/require;
2811
2812       spdadd W.X.Y.Z/32 A.B.C.D/32 ipencap -P in ipsec
2813
2814         esp/tunnel/W.X.Y.Z-A.B.C.D/require;
2815
2816   
2817
2818   On gateway host #2 this would be:
2819
2820       
2821
2822       spdadd W.X.Y.Z/32 A.B.C.D/32 ipencap -P out ipsec
2823
2824         esp/tunnel/W.X.Y.Z-A.B.C.D/require;
2825
2826       spdadd A.B.C.D/32 W.X.Y.Z/32 ipencap -P in ipsec
2827
2828         esp/tunnel/A.B.C.D-W.X.Y.Z/require;
2829
2830   
2831
2832
2833 * Add firewall rules to allow IKE, ESP, and IPENCAP traffic to both hosts:
2834
2835       
2836
2837       ipfw add 1 allow udp from A.B.C.D to W.X.Y.Z isakmp
2838
2839       ipfw add 1 allow udp from W.X.Y.Z to A.B.C.D isakmp
2840
2841       ipfw add 1 allow esp from A.B.C.D to W.X.Y.Z
2842
2843       ipfw add 1 allow esp from W.X.Y.Z to A.B.C.D
2844
2845       ipfw add 1 allow ipencap from A.B.C.D to W.X.Y.Z
2846
2847       ipfw add 1 allow ipencap from W.X.Y.Z to A.B.C.D
2848
2849   
2850
2851
2852
2853 The previous two steps should suffice to get the VPN up and running. Machines on each network will be able to refer to one another using IP addresses, and all traffic across the link will be automatically and securely encrypted.
2854
2855
2856
2857 ----
2858
2859
2860
2861 ## OpenSSH 
2862
2863
2864
2865 ***Contributed by Chern Lee. ***
2866
2867
2868
2869  **OpenSSH**  is a set of network connectivity tools used to access remote machines securely. It can be used as a direct replacement for `rlogin`, `rsh`, `rcp`, and `telnet`. Additionally, any other TCP/IP connections can be tunneled/forwarded securely through SSH.  **OpenSSH**  encrypts all traffic to effectively eliminate eavesdropping, connection hijacking, and other network-level attacks.
2870
2871
2872
2873  **OpenSSH**  is maintained by the OpenBSD project, and is based upon SSH v1.2.12 with all the recent bug fixes and updates. It is compatible with both SSH protocols 1 and 2.
2874
2875
2876
2877 ### Advantages of Using OpenSSH 
2878
2879
2880
2881 Normally, when using [telnet(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#telnet&section1) or [rlogin(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=rlogin&section=1), data is sent over the network in an clear, un-encrypted form. Network sniffers anywhere in between the client and server can steal your user/password information or data transferred in your session.  **OpenSSH**  offers a variety of authentication and encryption methods to prevent this from happening.
2882
2883
2884
2885 ### Enabling sshd 
2886
2887
2888
2889 Be sure to make the following addition to your `rc.conf` file:
2890
2891
2892
2893     
2894
2895     sshd_enable="YES"
2896
2897
2898
2899
2900
2901 This will load [sshd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#sshd&section8&manpath=OpenBSD+3.3), the daemon program for  **OpenSSH** , the next time your system initializes. Alternatively, you can simply run directly the  **sshd**  daemon by typing `rcstart sshd` on the command line.
2902
2903
2904
2905 ### SSH Client 
2906
2907
2908
2909 The [ssh(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh&section1&manpath=OpenBSD+3.3) utility works similarly to [rlogin(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=rlogin&section=1).
2910
2911
2912
2913     
2914
2915     # ssh user@example.com
2916
2917     Host key not found from the list of known hosts.
2918
2919     Are you sure you want to continue connecting (yes/no)? yes
2920
2921     Host 'example.com' added to the list of known hosts.
2922
2923     user@example.com's password: *******
2924
2925
2926
2927
2928
2929 The login will continue just as it would have if a session was created using `rlogin` or `telnet`. SSH utilizes a key fingerprint system for verifying the authenticity of the server when the client connects. The user is prompted to enter `yes` only when connecting for the first time. Future attempts to login are all verified against the saved fingerprint key. The SSH client will alert you if the saved fingerprint differs from the received fingerprint on future login attempts. The fingerprints are saved in `~/.ssh/known_hosts`, or `~/.ssh/known_hosts2` for SSH v2 fingerprints.
2930
2931
2932
2933 By default,  **OpenSSH**  servers are configured to accept both SSH v1 and SSH v2 connections. The client, however, can choose between the two. Version 2 is known to be more robust and secure than its predecessor.
2934
2935
2936
2937 The [ssh(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh&section1&manpath=OpenBSD+3.3) command can be forced to use either protocol by passing it the `-1` or `-2` argument for v1 and v2, respectively.
2938
2939
2940
2941 ### Secure Copy 
2942
2943
2944
2945 The [scp(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#scp&section1&manpath=OpenBSD+3.3) command works similarly to [rcp(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=rcp&section=1); it copies a file to or from a remote machine, except in a secure fashion.
2946
2947
2948
2949     
2950
2951     #  scp user@example.com:/COPYRIGHT COPYRIGHT
2952
2953     user@example.com's password: *******
2954
2955     COPYRIGHT            100% |*****************************|  4735
2956
2957     00:00
2958
2959     #
2960
2961
2962
2963
2964
2965 Since the fingerprint was already saved for this host in the previous example, it is verified when using [scp(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#scp&section1&manpath=OpenBSD+3.3) here.
2966
2967
2968
2969 The arguments passed to [scp(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#scp&section1&manpath=OpenBSD+3.3) are similar to [cp(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=cp&section=1), with the file or files in the first argument, and the destination in the second. Since the file is fetched over the network, through SSH, one or more of the file arguments takes on the form `user@host:<path_to_remote_file>`. The `user@` part is optional. If omitted, it will default to the same username as you are currently logged in as, unless configured otherwise.
2970
2971
2972
2973 ### Configuration 
2974
2975
2976
2977 The system-wide configuration files for both the  **OpenSSH**  daemon and client reside within the `/etc/ssh` directory.
2978
2979
2980
2981 `ssh_config` configures the client settings, while `sshd_config` configures the daemon.
2982
2983
2984
2985 Additionally, the `sshd_program` (`/usr/sbin/sshd` by default), and `sshd_flags` `rc.conf` options can provide more levels of configuration.
2986
2987
2988
2989 Each user can have a personal configuration file in `~/.ssh/config`. The file can configure various client options, and can include host-specific options. With the following configuration file, a user could type `ssh shell` which would be equivalent to `ssh -X user@shell.example.com`.
2990
2991
2992
2993     
2994
2995     Host shell
2996
2997      Hostname shell.example.com
2998
2999      Username user
3000
3001      Protocol 2
3002
3003      ForwardX11 yes
3004
3005
3006
3007
3008
3009 ### ssh-keygen 
3010
3011
3012
3013 Instead of using passwords, [ssh-keygen(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-keygen&section1&manpath=OpenBSD+3.3) can be used to generate RSA keys to authenticate a user:
3014
3015
3016
3017     
3018
3019     % ssh-keygen -t rsa1
3020
3021     Initializing random number generator...
3022
3023     Generating p:  .++ (distance 66)
3024
3025     Generating q:  ..............................++ (distance 498)
3026
3027     Computing the keys...
3028
3029     Key generation complete.
3030
3031     Enter file in which to save the key (/home/user/.ssh/identity):
3032
3033     Enter passphrase:
3034
3035     Enter the same passphrase again:
3036
3037     Your identification has been saved in /home/user/.ssh/identity.
3038
3039     ...
3040
3041
3042
3043
3044
3045 [ssh-keygen(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-keygen&section1&manpath=OpenBSD+3.3) will create a public and private key pair for use in authentication. The private key is stored in `~/.ssh/identity`, whereas the public key is stored in `~/.ssh/identity.pub`. The public key must be placed in `~/.ssh/authorized_keys` of the remote machine in order for the setup to work.
3046
3047
3048
3049 This will allow connection to the remote machine based upon RSA authentication instead of passwords.
3050
3051
3052
3053  **Note:** The `-t rsa1` option will create RSA keys for use by SSH protocol version 1. If you want to use RSA keys with the SSH protocol version 2, you have to use the command `ssh-keygen -t rsa`.
3054
3055
3056
3057 If a passphrase is used in [ssh-keygen(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-keygen&section1&manpath=OpenBSD+3.3), the user will be prompted for a password each time in order to use the private key.
3058
3059
3060
3061 A SSH protocol version 2 DSA key can be created for the same purpose by using the `ssh-keygen -t dsa` command. This will create a public/private DSA key for use in SSH protocol version 2 sessions only. The public key is stored in `~/.ssh/id_dsa.pub`, while the private key is in `~/.ssh/id_dsa`.
3062
3063
3064
3065 DSA public keys are also placed in `~/.ssh/authorized_keys` on the remote machine.
3066
3067
3068
3069 [ssh-agent(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-agent&section1&manpath=OpenBSD+3.3) and [ssh-add(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=ssh-add&section=1&manpath=OpenBSD+3.3) are utilities used in managing multiple passworded private keys.
3070
3071
3072
3073  **Warning:** The various options and files can be different according to the  **OpenSSH**  version you have on your system, to avoid problems you should consult the [ssh-keygen(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-keygen&section1&manpath=OpenBSD+3.3) manual page.
3074
3075
3076
3077 ### SSH Tunneling 
3078
3079
3080
3081  **OpenSSH**  has the ability to create a tunnel to encapsulate another protocol in an encrypted session.
3082
3083
3084
3085 The following command tells [ssh(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh&section1&manpath=OpenBSD+3.3) to create a tunnel for  **telnet** :
3086
3087
3088
3089     
3090
3091     % ssh -2 -N -f -L 5023:localhost:23 user@foo.example.com
3092
3093     %
3094
3095
3096
3097
3098
3099 The `ssh` command is used with the following options:
3100
3101
3102
3103 `-2`
3104
3105  :: Forces `ssh` to use version 2 of the protocol. (Do not use if you are working with older SSH servers)
3106
3107 `-N`
3108
3109  :: Indicates no command, or tunnel only. If omitted, `ssh` would initiate a normal session.
3110
3111 `-f`
3112
3113  :: Forces `ssh` to run in the background.
3114
3115 `-L`
3116
3117  :: Indicates a local tunnel in `***localport:remotehost:remoteport***` fashion.
3118
3119 `user@foo.example.com`
3120
3121  :: The remote SSH server.
3122
3123
3124
3125 An SSH tunnel works by creating a listen socket on `localhost` on the specified port. It then forwards any connection received on the local host/port via the SSH connection to the specified remote host and port.
3126
3127
3128
3129 In the example, port `***5023***` on `localhost` is being forwarded to port `***23***` on `localhost` of the remote machine. Since `***23***` is  **telnet** , this would create a secure  **telnet**  session through an SSH tunnel.
3130
3131
3132
3133 This can be used to wrap any number of insecure TCP protocols such as SMTP, POP3, FTP, etc.
3134
3135
3136
3137  **Example 10-1. Using SSH to Create a Secure Tunnel for SMTP** 
3138
3139
3140
3141     
3142
3143     % ssh -2 -N -f -L 5025:localhost:25 user@mailserver.example.com
3144
3145     user@mailserver.example.com's password: *****
3146
3147     % telnet localhost 5025
3148
3149     Trying 127.0.0.1...
3150
3151     Connected to localhost.
3152
3153     Escape character is '^]'.
3154
3155     220 mailserver.example.com ESMTP
3156
3157
3158
3159
3160
3161 This can be used in conjunction with an [ssh-keygen(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-keygen&section1&manpath=OpenBSD+3.3) and additional user accounts to create a more seamless/hassle-free SSH tunneling environment. Keys can be used in place of typing a password, and the tunnels can be run as a separate user.
3162
3163
3164
3165 #### Practical SSH Tunneling Examples 
3166
3167
3168
3169 ##### Secure Access of a POP3 Server 
3170
3171
3172
3173 At work, there is an SSH server that accepts connections from the outside. On the same office network resides a mail server running a POP3 server. The network, or network path between your home and office may or may not be completely trustable. Because of this, you need to check your e-mail in a secure manner. The solution is to create an SSH connection to your office's SSH server, and tunnel through to the mail server.
3174
3175
3176
3177     
3178
3179     % ssh -2 -N -f -L 2110:mail.example.com:110 user@ssh-server.example.com
3180
3181     user@ssh-server.example.com's password: ******
3182
3183
3184
3185
3186
3187 When the tunnel is up and running, you can point your mail client to send POP3 requests to `localhost` port 2110. A connection here will be forwarded securely across the tunnel to `mail.example.com`.
3188
3189
3190
3191 ##### Bypassing a Draconian Firewall 
3192
3193
3194
3195 Some network administrators impose extremely draconian firewall rules, filtering not only incoming connections, but outgoing connections. You may be only given access to contact remote machines on ports 22 and 80 for SSH and web surfing.
3196
3197
3198
3199 You may wish to access another (perhaps non-work related) service, such as an Ogg Vorbis server to stream music. If this Ogg Vorbis server is streaming on some other port than 22 or 80, you will not be able to access it.
3200
3201
3202
3203 The solution is to create an SSH connection to a machine outside of your network's firewall, and use it to tunnel to the Ogg Vorbis server.
3204
3205
3206
3207     
3208
3209     % ssh -2 -N -f -L 8888:music.example.com:8000 user@unfirewalled-system.example.org
3210
3211     user@unfirewalled-system.example.org's password: *******
3212
3213
3214
3215
3216
3217 Your streaming client can now be pointed to `localhost` port 8888, which will be forwarded over to `music.example.com` port 8000, successfully evading the firewall.
3218