Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 29278 | Differences between
and this patch

Collapse All | Expand All

(-)../cvs/htdocs/doc/en/gentoo-security.xml (-186 / +99 lines)
Lines 45-50 Link Here
45
People who are using Gentoo Linux in a server based environment and/or feel the need for more or paranoid security.
45
People who are using Gentoo Linux in a server based environment and/or feel the need for more or paranoid security.
46
</p>
46
</p>
47
47
48
<note>
49
If you are interested in even more Gentoo security stuff after reading this guide then have a look at the <uri link="http://www.gentoo.org/proj/en/hardened/">Hardened Gentoo Project</uri>
50
</note>
51
48
</body>
52
</body>
49
</section>
53
</section>
50
54
Lines 75-81 Link Here
75
</section>
79
</section>
76
80
77
<section>
81
<section>
78
<title>Enhancements for Next Release of this Guide:</title>
82
<title>Enhancements for future releases of this Guide:</title>
79
<body>
83
<body>
80
84
81
<p>
85
<p>
Lines 365-385 Link Here
365
<chapter>
369
<chapter>
366
<title>Tightening the security after/during installation</title>
370
<title>Tightening the security after/during installation</title>
367
<section>
371
<section>
368
<title>/etc/make.conf</title>
372
<title>USE flags</title>
369
<body>
373
<body>
370
374
371
<p>
375
<p>
372
The make.conf file contains all the options and extra libraries that you want to have support for when building ebuilds. In this file you must make sure that if the ebuild supports any security library like PAM (Pluggable Authentication Modules), tcp wrappers or SSL (Secure Socket Layer) it will add support for it. Your global USE variable should contain pam, tcpd and ssl.
376
The <path>make.conf</path> file contains user defined USE flags and <path>/etc/make.profile/make.defaults</path> contains the default USE flags for Gentoo Linux. For this guide the important flags are pam (Pluggable Authentication Modules), tcp (TCP wrappers) and ssl (Secure Socket Layer). These are all in the default USE flags.
373
</p>
374
375
<p>
376
So add something like this:
377
</p>
377
</p>
378
378
379
<pre caption="USE settings to be added">
380
USE="tcpd pam ssl"
381
</pre>
382
383
</body>
379
</body>
384
</section>
380
</section>
385
381
Lines 560-647 Link Here
560
556
561
<p>
557
<p>
562
<uri link="http://metalog.sourceforge.net">Metalog</uri> by Frank Dennis is not able to log to a remote server, but it does have advantages when it comes to performance and logging flexibility.
558
<uri link="http://metalog.sourceforge.net">Metalog</uri> by Frank Dennis is not able to log to a remote server, but it does have advantages when it comes to performance and logging flexibility.
563
</p>
559
It can log by program name, urgency, program name,by facility (like syslogd) and comes with regular expression matching and it can launch external scripts when specific patterns are found. It is very good for taking action when needed.
564
565
<p>
566
It can log by program name or by facility (like syslogd) and comes with regular expression matching and execution of commands. Very good for taking action when needed.
567
</p> 
560
</p> 
568
561
569
<pre caption="/etc/metalog/metalog.conf">
570
maxsize  = 1000000
571
maxtime  = 86400
572
maxfiles = 7
573
minimum  = 7
574
575
Kernel messages :
576
577
  facility = "kern"
578
  logdir   = "/var/log/kernel"
579
580
Auth messages :
581
  facility = "auth"
582
  logdir   = "/var/log/auth"
583
584
Critical :
585
  facility = "critical"
586
  command  = "/usr/local/sbin/pwdfail.sh"  
587
588
Crond :
589
590
  program  = "crond"
591
  logdir   = "/var/log/crond"
592
  
593
Password failures :
594
595
  regex    = "(password|login|authentication)\s+(fail|invalid)"
596
  regex    = "(failed|invalid)\s+(password|login|authentication)"
597
  regex    = "ILLEGAL ROOT LOGIN"
598
  logdir   = "/var/log/pwdfail"
599
  command  = "/usr/local/sbin/pwdfail.sh"
600
601
SSH Server :
602
603
  program  = "sshd"
604
  logdir   = "/var/log/sshd"
605
606
Mail :
607
608
  facility = "ftp-mail-news"
609
  logdir   = "/var/log/mail"
610
611
Snort:
612
  program   = "snort"
613
  command  = "/usr/local/sbin/pwdfail.sh"
614
615
Everything important :
616
617
  facility = "*"
618
  logdir   = "/var/log/everything"
619
620
Everything very important :
621
622
  facility = "*"
623
  logdir   = "/var/log/critical"
624
625
</pre>
626
627
<p>
562
<p>
628
This is basically a standard configuration with a few modifications, like a minimum logging level at 7, which means that everything will be logged.
563
The standard configuration is basically enough. If you want to be notified by email whenever a password failure occurs use one of the following scripts.
629
</p>
564
</p>
630
565
631
<p>
566
<p>
632
pwdfail.sh for postfix.
567
For postfix:
633
</p>
568
</p>
634
569
635
<pre caption = "postfix' pwdfail.sh">
570
<pre caption = "/usr/local/sbin/mail_pwd_failures.sh for postfix">
636
#! /bin/sh
571
#! /bin/sh
637
echo "$3" | mail -s "Warning (program : $2)" root
572
echo "$3" | mail -s "Warning (program : $2)" root
638
</pre>
573
</pre>
639
574
640
<p>
575
<p>
641
pwdfail.sh for qmail.
576
For qmail:
642
</p>
577
</p>
643
578
644
<pre caption = "qmail's pwdfail.sh">
579
<pre caption = "/user/local/sbin/mail_pwd_failures.sh for qmail">
645
#!/bin/sh
580
#!/bin/sh
646
echo "To: root
581
echo "To: root
647
Subject:Failure (Warning: $2) 
582
Subject:Failure (Warning: $2) 
Lines 650-661 Link Here
650
</pre>
585
</pre>
651
586
652
<p>
587
<p>
653
More information can be found in the <uri link="http://metalog.sourceforge.net">metalog</uri> website.
588
Remember to make the script executable by issuing <c>chmod +x /usr/local/sbin/mail_pwd_failures.sh</c>
589
</p>
590
591
<p>
592
Then uncomment the command line under Password failures in metalog.conf like:
654
</p>
593
</p>
655
594
595
<pre caption="/etc/metalog/metalog.conf">
596
command  = "/usr/local/sbin/mail_pwd_failures.sh"
597
</pre>
656
598
657
</body>
599
</body>
658
</section>
600
</section>
601
659
<section>
602
<section>
660
603
661
<title>Syslog-ng</title>
604
<title>Syslog-ng</title>
Lines 854-860 Link Here
854
</pre>
797
</pre>
855
798
856
<p>
799
<p>
857
Here we set the default settings and a specific setting for the user <e>kn</e>. Limits are part of the shadow package and only applies for the shadow login program. It is not necessary to set any limitations in this file, if you have set the PAM setting in your make.conf and configured PAM properly.
800
Here we set the default settings and a specific setting for the user <e>kn</e>. Limits are part of the shadow package and only apply to the shadow login program. It is not necessary to set any limitations in this file if you haven't disable pam in your <path>make.conf</path> and configured PAM properly.
858
</p>
801
</p>
859
802
860
</body>
803
</body>
Lines 1095-1101 Link Here
1095
<p>
1038
<p>
1096
PAM is a suite of shared libraries that provide an alternative way of making authentication in programs. The PAM settings of Gentoo Linux is pretty reasonable, but there is always room for improvement.
1039
PAM is a suite of shared libraries that provide an alternative way of making authentication in programs. The PAM settings of Gentoo Linux is pretty reasonable, but there is always room for improvement.
1097
</p>
1040
</p>
1098
<note>This chapter will have no effect if you did not include the PAM in your USE option in <path>/etc/make.conf</path></note>
1099
1041
1100
<p>Install cracklib</p>
1042
<p>Install cracklib</p>
1101
1043
Lines 1152-1158 Link Here
1152
<p>
1094
<p>
1153
Is a way of controlling access to services normally run by inetd (which Gentoo does not have) but it can also be used by xinetd and other services.
1095
Is a way of controlling access to services normally run by inetd (which Gentoo does not have) but it can also be used by xinetd and other services.
1154
</p>
1096
</p>
1155
<note>The use in make.conf should contain tcpd and the service should be executing tcpd in its server argument (in xinetd). See the chapter on xinetd for more information</note>
1097
<note>The service should be executing tcpd in its server argument (in xinetd). See the chapter on xinetd for more information</note>
1156
1098
1157
<pre caption="/etc/hosts.deny">
1099
<pre caption="/etc/hosts.deny">
1158
ALL:PARANOID
1100
ALL:PARANOID
Lines 1819-1825 Link Here
1819
</p>
1761
</p>
1820
1762
1821
<p>
1763
<p>
1822
If you have added ssl to your <path>/etc/make.conf</path> before installing apache, you should have access to a ssl enabled server. Just add the following line to enable it.
1764
If you did not disable ssl in your <path>/etc/make.conf</path> before installing apache, you should have access to a ssl enabled server. Just add the following line to enable it.
1823
</p>
1765
</p>
1824
1766
1825
<pre caption="/etc/conf.d/apache">
1767
<pre caption="/etc/conf.d/apache">
Lines 2117-2127 Link Here
2117
<body>
2059
<body>
2118
2060
2119
<p>
2061
<p>
2120
People often think that a firewall is the ultimate security, but they are wrong. In most cases a mis configured firewall gives worse security than not having one at all. A firewall is also a piece of software and should be treated the same way as any other service, because is just as likely to have bugs (security holes).
2062
People often think that a firewall provides the ultimate security, but they are wrong. In most cases a misconfigured firewall gives worse security than not having one at all. A firewall is also a piece of software and should be treated the same way as any other piece of software, because is just as likely to contain bugs.
2121
</p>
2063
</p>
2122
2064
2123
<p>
2065
<p>
2124
So think before implementing one! Do you really need one ? If you think you need one write a policy on how it should work, what type of firewall and who should operate it.
2066
So think before implementing one! Do you really need one? If you think you need one write a policy on how it should work, what type of firewall and who should operate it. But first read this guide.
2125
</p>
2067
</p>
2126
2068
2127
<p>
2069
<p>
Lines 2155-2174 Link Here
2155
<body>
2097
<body>
2156
2098
2157
<p>
2099
<p>
2158
All network traffic is in the form of packets. Large amounts of traffic also split up into small packets for easy handling and then reassembled when arriving at it's destination. Every packet contains information on how and where is should be delivered. And these information is exactly what a packing filtering firewall uses. Filtering is based on:
2100
All network traffic is in the form of packets. Large amounts of traffic also split up into small packets for easy handling and then reassembled when arriving at its destination. In the packet header every packet contains information on how and where it should be delivered. And these informations are exactly what a packing filtering firewall uses. Filtering is based on:
2159
</p>
2101
</p>
2160
2102
2161
<ul>
2103
<ul>
2162
2104
2163
<li>Allow or disallow packets based on source/destination IP address.</li>
2105
<li>Allow or disallow packets based on source/destination IP address.</li>
2164
<li>Allow or disallow packets based on source/destination port.</li>
2106
<li>Allow or disallow packets based on source/destination port.</li>
2165
<li>Allow or disallow packets according to protocol.</li>
2107
<li>Allow or disallow packets based on protocol.</li>
2166
<li>Allow or disallow packets according to flags within a specific protocol</li>
2108
<li>Allow or disallow packets based on flags within a specific protocol.</li>
2167
2109
2168
</ul>
2110
</ul>
2169
2111
2170
<p>
2112
<p>
2171
Basically filtering on all data within the header of a packet and not it's content.
2113
Basically filtering is based on all data within the header of a packet and not its content.
2172
</p>
2114
</p>
2173
2115
2174
<p>
2116
<p>
Lines 2176-2184 Link Here
2176
</p>
2118
</p>
2177
2119
2178
<ul>
2120
<ul>
2179
<li>Address information in a packet can potentially be a bogus address or as we say <e>spoofed</e> by the sender</li>
2121
<li>Address information in a packet can potentially be a bogus IP address or as we say <e>spoofed</e> by the sender.</li>
2180
<li>Data or requests within the allowed packet may contain unwanted data that the attacker can use to exploit known bugs in the services on or behind the firewall</li>
2122
<li>Data or requests within the allowed packet may contain unwanted data that the attacker can use to exploit known bugs in the services on or behind the firewall.</li>
2181
<li>Usually single point of failure</li>
2123
<li>Usually single point of failure.</li>
2182
</ul>
2124
</ul>
2183
2125
2184
<p>
2126
<p>
Lines 2186-2194 Link Here
2186
</p>
2128
</p>
2187
2129
2188
<ul>
2130
<ul>
2189
<li>Simple and easy to implement</li>
2131
<li>Simple and easy to implement.</li>
2190
<li>Can give warnings on a possible attack before they happened (by detecting portscans)</li>
2132
<li>Can give warnings of a possible attack before it happens (ie. by detecting portscans).</li>
2191
<li>Good for stopping SYN attacks</li>
2133
<li>Good for stopping SYN attacks.</li>
2192
</ul>
2134
</ul>
2193
2135
2194
<p>
2136
<p>
Lines 2209-2228 Link Here
2209
<body>
2151
<body>
2210
2152
2211
<p>
2153
<p>
2212
Or circuit level gateways is a firewall that validates connections before allowing data to be exchanged. This means that is simply does not allow or deny packets based based on the header of the packet but determines whether the connection between both ends is valid according to configurable rules before it opens a session and allows data to and from the allowed source address. Filtering is based on:
2154
Or circuit level gateways is a firewall that validates connections before allowing data to be exchanged. This means that it simply does not allow or deny packets based on the packet header but determines whether the connection between both ends is valid according to configurable rules before it opens a session and allows data to be exchanged. Filtering is based on:
2213
</p>
2155
</p>
2214
2156
2215
<ul>
2157
<ul>
2216
<li>Source/destination IP address</li>
2158
<li>Source/destination IP address.</li>
2217
<li>Source/destination port</li>
2159
<li>Source/destination port.</li>
2218
<li>A period of time</li>
2160
<li>A period of time.</li>
2219
<li>Protocol</li>
2161
<li>Protocol.</li>
2220
<li>User</li>
2162
<li>User.</li>
2221
<li>Password</li>
2163
<li>Password.</li>
2222
</ul>
2164
</ul>
2223
2165
2224
<p>
2166
<p>
2225
All traffic is validated, monitored and traffic without is disallowed.
2167
All traffic is validated, monitored and unwanted traffic can be dropped.
2226
</p>
2168
</p>
2227
2169
2228
<p>
2170
<p>
Lines 2243-2256 Link Here
2243
<body>
2185
<body>
2244
2186
2245
<p>
2187
<p>
2246
The application level gateway is a proxy for application, exchanging data with remote systems on behalf of the clients. It is kept away from the public safely behind a DMZ or firewall with no connection from the outside. Filtering is based on:
2188
The application level gateway is a proxy for applications, exchanging data with remote systems on behalf of the clients. It is kept away from the public safely behind a DMZ (De-Militarized Zone: the portion of a private network that is visible through the firewall) or a firewall allowing no connections from the outside. Filtering is based on:
2247
</p>
2189
</p>
2248
2190
2249
<ul>
2191
<ul>
2250
<li>Allow or disallow based on source/destination IP address</li>
2192
<li>Allow or disallow based on source/destination IP address.</li>
2251
<li>Based on the packets content</li>
2193
<li>Based on the packets content.</li>
2252
<li>Can even alter the packet content on the fly</li>
2194
<li>Limiting file access based on file type or extension.</li>
2253
<li>Limiting file access based on file type or extension</li>
2254
</ul>
2195
</ul>
2255
2196
2256
<p>
2197
<p>
Lines 2258-2267 Link Here
2258
</p>
2199
</p>
2259
2200
2260
<ul>
2201
<ul>
2261
<li>Can cache files, increasing network performance</li>
2202
<li>Can cache files, increasing network performance.</li>
2262
<li>Detailed logging of all connections</li>
2203
<li>Detailed logging of all connections.</li>
2263
<li>Scales perfectly (some proxy servers can "share" the cached data)</li>
2204
<li>Scales perfectly (some proxy servers can "share" the cached data).</li>
2264
<li>No direct access from the outside</li>
2205
<li>No direct access from the outside.</li>
2206
<li>Can even alter the packet content on the fly.</li>
2265
</ul>
2207
</ul>
2266
2208
2267
<p>
2209
<p>
Lines 2269-2279 Link Here
2269
</p>
2211
</p>
2270
2212
2271
<ul>
2213
<ul>
2272
<li>The setup is complex</li>
2214
<li>Configuration is complex.</li>
2273
</ul>
2215
</ul>
2274
2216
2275
<p>
2217
<p>
2276
Application gateways are considered to be the most secure solution since it does not have to run as root and is not public to the Internet.
2218
Application gateways are considered to be the most secure solution since it does not have to run as root and the hosts behind it are not reachable from the Internet.
2277
</p>
2219
</p>
2278
2220
2279
<p>
2221
<p>
Lines 2292-2298 Link Here
2292
<body>
2234
<body>
2293
2235
2294
<p>
2236
<p>
2295
In order to get iptables working, it has to be enabled in the kernel. I have added them as modules (the iptables command will load them as they are needed) and recompiled my kernel. After you have compiled it (or while compiling the kernel) you have to add the iptables commands. Just <c>emerge iptables</c> and it should work.
2237
In order to get <c>iptables</c> working, it has to be enabled in the kernel. I have added them as modules (the <c>iptables</c> command will load them as they are needed) and recompiled my kernel. For more information on how to configure your kernel for <c>iptables</c> look go to the <uri link="http://iptables-tutorial.frozentux.net/chunkyhtml/kernelsetup.html">Iptables Tutorial Chapter 2: Preparations</uri>. After you have compiled your new kernel (or while compiling the kernel) you have to add the <c>iptables</c> command. Just <c>emerge iptables</c> and it should work.
2296
</p>
2238
</p>
2297
2239
2298
<p>
2240
<p>
Lines 2300-2340 Link Here
2300
</p>
2242
</p>
2301
2243
2302
<p>
2244
<p>
2303
Iptables is a stateful packet filter which means that it provides greater control and greater security than ipchains (Linux version 2.2) which is not stateful. You are properly asking yourself what is the stateful part? and what is the difference?
2245
Iptables is the new and heavily improved packet filter in the Linux 2.4.x kernel. It is the successor of the previous ipchains packet filter in the Linux 2.2.x kernel. One of the major improvements is that <c>iptables</c> is able to perform stateful packet filtering. With stateful packet filtering it is possible to keep track of each established TCP connection.
2304
</p>
2246
</p>
2305
2247
2306
<p>
2248
<p>
2307
We all know that a TCP connection consists of a series of packets. Each packet contains information about source IP address, the destination IP address and a sequence number so the packets can be reassembled. And we all know that TCP is connection-oriented and UDP is connectionless right? these are the states that is stored .. now you are probably asking yourself and so what? Well I'm getting to that.
2249
A TCP connection consists of a series of packets containing information about source IP address, destination IP address, sequence number so the packets can be reassembled and not to forget data. TCP is a connection-oriented protocol in contrast to UDP which is connectionless.
2308
</p>
2250
</p>
2309
2251
2310
<p>
2252
<p>
2311
Now imagine that you have a stateless firewall (ipchains) and do not want to allow external computers to originate a connection to your internal services but how can the firewall determine whether a packet is part of an ongoing connection or not? A stateless firewall cannot distinguish between an existing connection and one that is part of a new connection by looking at the SYN flag!. 
2253
By examining the TCP packet header a stateful packet filter can determine if a received TCP packet is part of an already established connection or not and decide either to accept or drop the packet.
2312
</p>
2254
</p>
2313
2255
2314
<p>
2256
<p>
2315
Lets say an attacker is sending hand crafted packets (a packet created by the attacker) where he alters the SYN flags or any other flags. This is quite often what attackers do. Send packets right through the firewall altering routing tables or compromising services running on the firewall which are supposed to be safe behind a filter. A stateful firewall will keep track of all connections and can thus detect if a received packet does not belong to an existing connection. When a packet not belonging to any existing connection is found it is marked as invalid and can be discarded. This will also stop the possibility of "stealth scans" since the connection was invalid.
2257
With a stateless packet filter it is possible to fool the packet filter to accept packets that should be dropped by manipulating the TCP packet headers. This could be done by manipulating the SYN flag or other flags in the TCP header. With stateful packet filtering it is possible to drop such packets as they are not part of an already established connection. This will also stop the possibility of "stealth scans" since such packets will not be part of an already established connection.
2316
</p>
2258
</p>
2317
2259
2318
<p>
2260
<p>
2319
I could probably find a dozen other reasons but I think you got the picture .. simple rules result in a smaller firewall configuration that is easier to maintain.
2261
Iptables provides several other features like NAT (Network Address Translation) and rate limiting. Rate limiting is extremely useful when trying to prevent certain DoS (Denial of Service) attacks like SYN floods.
2320
</p>
2262
</p>
2321
2263
2322
<p>
2264
<p>
2323
Iptables provides several other features like rate limiting. This features is extremely useful when trying to prevent certain DoS (Denial of Service) attacks like a SYN attack. Now what is a SYN attack?
2265
A TCP connection is established by a so called three-way handshake. When establishing a TCP connection the client-side sends a packet to the server with the SYN flag set. When the server-side receives the SYN packet it responds by sending a SYN+ACK packet back to the client-side. When the SYN+ACK is received the client-side responds with a third ACK packet in effect acknowledging the connection.
2324
</p>
2266
</p>
2325
2267
2326
<p>
2268
<p>
2327
Again .. when creating a connection with TCP it uses three-way handshake to establish the connection like this:
2269
A SYN flood attack is performed by sending the SYN packet but failing to respond to the SYN+ACK packet. The client-side can forge a packet with a fake source IP address because it does not need a reply. The server-side system will add an entry to a queue of half-open connections when it receives the SYN packet and then wait for the final ACK packet before deleting the entry from the queue. The queue has a limitied number of slots and if all the slots are filled it is unable to open any further connections. If the ACK packet is not received before a specified timeout period the entry will automatically be deleted from the queue. The timeout settings vary but will typically be 30-60 seconds or even more. The client-side initiates the attack by forging a lot of SYN packets with different source IP addresses and sends them to the target IP address as fast as possible and thereby filling up the queue of half-open connections and thus preventing other clients from establishing legitimate with the server.
2328
</p>
2270
</p>
2329
2271
2330
<fig link="http://www.ibiblio.org/pub/Linux/distributions/gentoo/images/synack.jpg" short="Three-way handshake"/>
2331
2332
<p>
2272
<p>
2333
A SYN attack is when it only sends a SYN packet (header only contains SYN flag) and does not continue sending the last two packets to establish the connection. A SYN packet does not require a valid source IP address because it does not need a reply. So the connection will hang until it times out. Now if the attacker sends a lot of SYN packets with a bogus source IP address, the computer waits for the response, which never comes. Depending on the timeout settings in your system, this connection could stay open for 30-60 seconds (or longer). When the connection limit table is completely occupied and unable to communicate with anyone.
2273
This is where the rate limit becomes handy. It is possible to limit the number of accepted SYN packets from a single source by using the <c>-m limit --limit 1/s</c>. This will limit the SYN packets to one per source and therefor restricting the SYN flood on our resources.
2334
</p>
2335
2336
<p>
2337
This is where the rate limit becomes handy. It is possible to limit the number of SYN packets from a single source but using the <c>-m limit --limit 1/s</c>. This will limit the SYN packets to one per source and therefor restricting the SYN flood on our resources.
2338
</p>
2274
</p>
2339
2275
2340
<p>
2276
<p>
Lines 2342-2352 Link Here
2342
</p>
2278
</p>
2343
2279
2344
<p>
2280
<p>
2345
When iptables is loaded in the kernel it has 5 hooks where you can place your rules. They are called INPUT, OUTPUT FORWARD, PREROUTING and POSTROUTING. These lists are called chains because they work by added a rules and checks the rules one at the time as they where added. If one rule deny a packet it will be dropped and does not continue down the chain.
2281
When <c>iptables</c> is loaded in the kernel it has 5 hooks where you can place your rules. They are called INPUT, OUTPUT, FORWARD, PREROUTING and POSTROUTING. Each of these is called a chain and consists of a list of rules. Each rule says if the packet header looks like this, then here is what to do with the packet. If the rule does not match the packet the next rule in the chain is consulted.
2346
</p>
2282
</p>
2347
2283
2348
<p>
2284
<p>
2349
You can place rules directly to the 5 main chains or create chains and add them to as a rule to an existing chain. Lets see how this is done
2285
You can place rules directly in the 5 main chains or create new chains and add them to as a rule to an existing chain. <c>iptables</c> supports the following options.
2350
</p>
2286
</p>
2351
2287
2352
<table border="0">
2288
<table border="0">
Lines 2434-2440 Link Here
2434
</table>
2370
</table>
2435
2371
2436
<p>
2372
<p>
2437
First we will try to block all ICMP packages to our machine, just to get familiar with iptables.
2373
First we will try to block all ICMP packages to our machine, just to get familiar with <c>iptables</c>.
2438
</p>
2374
</p>
2439
2375
2440
<pre caption="Block all ICMP packages">
2376
<pre caption="Block all ICMP packages">
Lines 2442-2452 Link Here
2442
</pre>
2378
</pre>
2443
2379
2444
<p>
2380
<p>
2445
First we specify the chain it should be appended to. Next to specify the protocol and then the rule. The rule can be a ACCEPT, DROP, REJECT, LOG, QUEUE, MASQUERADE, a loaded module or a user defined chain. In this case we use DROP which will drop the packet without responding to the client.
2381
First we specify the chain it should be appended to next the protocol and then the target. The target can be the name of a user specified chain or one of the special targets ACCEPT, DROP, REJECT, LOG, QUEUE, MASQUERADE. In this case we use DROP which will drop the packet without responding to the client.
2446
</p>
2382
</p>
2447
2383
2448
<p>
2384
<p>
2449
Now try <c>ping localhost</c>. It will not be able to get any response since it is blocking the entire ICMP protocol incoming to our machine. It will not be able to ping other machines either since it is not allowed to get the ICMP packets returning from the host. Now flush the chain to get ICMP flowing again.
2385
Now try <c>ping localhost</c>. It will not be able to get any response since <c>iptables</c> will drop all incoming ICMP messages. It will not be able to ping other machines either since the ICMP reply packet will be dropped. Now flush the chain to get ICMP flowing again.
2450
</p>
2386
</p>
2451
2387
2452
<pre caption="Flush all rules">
2388
<pre caption="Flush all rules">
Lines 2454-2460 Link Here
2454
</pre>
2390
</pre>
2455
2391
2456
<p>
2392
<p>
2457
Now lets look at the stateful part in iptables. If we wanted to have a stateful inspection of packets incoming on eth0 we could enable it by issuing:
2393
Now lets look at the stateful packet filtering in <c>iptables</c>. If we wanted to have a stateful inspection of packets incoming on eth0 we could enable it by issuing:
2458
</p>
2394
</p>
2459
2395
2460
<pre caption="Accept packets that originate from an already established connection">
2396
<pre caption="Accept packets that originate from an already established connection">
Lines 2462-2468 Link Here
2462
</pre>
2398
</pre>
2463
2399
2464
<p>
2400
<p>
2465
This will accept any packet already established or related in the INPUT chain. And you could drop any packet that is not in the state table by issuing <c>iptables -A INPUT -i eth0 -m state --state INVALID -j DROP</c> just before. This enables the stateful part in iptables by loading the extension state. If you wanted connection from the outside to connect to you machine you could use the <c>--state NEW</c>. Iptables contain some modules for different purposes. Some of them are: 
2401
This will accept any packet from an already established connection or related in the INPUT chain. And you could drop any packet that is not in the state table by issuing <c>iptables -A INPUT -i eth0 -m state --state INVALID -j DROP</c> just before. This enables the stateful packet filtering in <c>iptables</c> by loading the extension state. If you wanted to allow others to connect to you machine you could use the <c>--state NEW</c>. Iptables contain some modules for different purposes. Some of them are: 
2466
</p>
2402
</p>
2467
2403
2468
<table border="0">
2404
<table border="0">
Lines 2490-2496 Link Here
2490
Lets try to create a user defined chain and apply it to one of the existing chains:
2426
Lets try to create a user defined chain and apply it to one of the existing chains:
2491
</p>
2427
</p>
2492
2428
2493
<pre caption="Creating a userdefined chain">
2429
<pre caption="Creating a user defined chain">
2494
<codenote>Create a new chain with one rule</codenote>
2430
<codenote>Create a new chain with one rule</codenote>
2495
# <i>iptables -X mychain</i>
2431
# <i>iptables -X mychain</i>
2496
# <i>iptables -N mychain</i>
2432
# <i>iptables -N mychain</i>
Lines 2515-2526 Link Here
2515
</p>
2451
</p>
2516
2452
2517
<ul>
2453
<ul>
2518
  <li>Connections to the firewall is only allowed through SSH (port 22)</li>
2454
  <li>Connections to the firewall are only allowed through SSH (port 22).</li>
2519
  <li>The local network should have access to HTTP, HTTPS and SSH (DNS should also be allowed)</li>
2455
  <li>The local network should have access to HTTP, HTTPS and SSH (DNS should also be allowed).</li>
2520
  <li>ICMP traffic can contain payload and should not be allowed. Of course we have to allow some ICMP traffic.</li>
2456
  <li>ICMP traffic can contain payload and should not be allowed. Of course we have to allow some ICMP traffic.</li>
2521
  <li>Port scan should be detected and logged</li>
2457
  <li>Port scans should be detected and logged.</li>
2522
  <li>SYN attack should be avoided</li>
2458
  <li>SYN attacks should be avoided.</li>
2523
  <li>All other traffic should be dropped and logged</li>
2459
  <li>All other traffic should be dropped and logged.</li>
2524
</ul>
2460
</ul>
2525
2461
2526
<pre caption="/etc/init.d/firewall">
2462
<pre caption="/etc/init.d/firewall">
Lines 2738-2752 Link Here
2738
</p>
2674
</p>
2739
2675
2740
<ol>
2676
<ol>
2741
  <li>Create your firewall policy before implementing it</li>
2677
  <li>Create your firewall policy before implementing it.</li>
2742
  <li>Keep it simple</li>
2678
  <li>Keep it simple.</li>
2743
  <li>Know how the protocol works (read the <uri link="http://www.ietf.org/">RFC (Request For Comments)</uri>)</li>
2679
  <li>Know how the protocol works (read the <uri link="http://www.ietf.org/">RFC (Request For Comments)</uri>).</li>
2744
  <li>Keep in mind that a firewall it just another piece of software running as root</li>
2680
  <li>Keep in mind that a firewall it just another piece of software running as root.</li>
2745
  <li>Test your firewall</li>
2681
  <li>Test your firewall.</li>
2746
</ol>
2682
</ol>
2747
2683
2748
<p>
2684
<p>
2749
If you think that iptables is hard to understand or takes to long to setup a decent firewall you could use <uri link="http://www.shorewall.net">Shorewall</uri>. It basically uses iptables to generate firewall rules, but concentrates on rules and not specific protocols.
2685
If you think that <c>iptables</c> is hard to understand or takes to long to setup a decent firewall you could use <uri link="http://www.shorewall.net">Shorewall</uri>. It basically uses <c>iptables</c> to generate firewall rules, but concentrates on rules and not specific protocols.
2750
</p>
2686
</p>
2751
2687
2752
</body>
2688
</body>
Lines 2757-2767 Link Here
2757
<body>
2693
<body>
2758
2694
2759
<p>
2695
<p>
2760
Squid is a very powerful proxy server and it can filter traffic based on: time, regular expression path/uri, source and destination IP addresses, domain, browser, authenticated username, mime-type and port number(protocol). I probably forgot some features, but it can be hard to cover the entire feature list.
2696
Squid is a very powerful proxy server and it can filter traffic based on: time, regular expressions on path/URI, source and destination IP addresses, domain, browser, authenticated username, MIME type and port number (protocol). I probably forgot some features, but it can be hard to cover the entire feature list.
2761
</p> 
2697
</p> 
2762
2698
2763
<p>
2699
<p>
2764
In the following example I have added a banner filter instead of a filter based on porn sites. The reason for this is that Gentoo.org should <c>not</c> be listed as some porn site. And I do not want to waste my time trying to find some good sites for you.
2700
In the following example I have added a banner filter instead of a filter based on porn sites. The reason for this is that Gentoo.org should <e>not</e> be listed as some porn site. And I do not want to waste my time trying to find some good sites for you.
2765
</p>
2701
</p>
2766
2702
2767
<p>
2703
<p>
Lines 2769-2778 Link Here
2769
</p>
2705
</p>
2770
2706
2771
<ul>
2707
<ul>
2772
  <li>Surfing (HTTP/HTTPS) is allowed during work hours (mon-fri 8-17 and sat 8-13) if they are here late they should work, not surf</li>
2708
  <li>Surfing (HTTP/HTTPS) is allowed during work hours (mon-fri 8-17 and sat 8-13) if they are here late they should work, not surf.</li>
2773
  <li>Download is not allowed (.exe, .com, .arj, .zip, .asf, .avi, .mpg, .mpeg etc.)</li>
2709
  <li>Download is not allowed (.exe, .com, .arj, .zip, .asf, .avi, .mpg, .mpeg etc).</li>
2774
  <li>We don't like banners so they are filtered and replaced with a transparent gif (this is where you get creative!)</li>
2710
  <li>We do not like banners so they are filtered and replaced with a transparent gif (this is where you get creative!).</li>
2775
  <li>Every other connection to and from the Internet is not allowed</li>
2711
  <li>All other connections to and from the Internet are not allowed.</li>
2776
</ul>
2712
</ul>
2777
2713
2778
<p>
2714
<p>
Lines 2913-2919 Link Here
2913
</pre>
2849
</pre>
2914
2850
2915
<p>
2851
<p>
2916
And the last part. We want this file to be displayed when it removes a banner. It is basically a half html file with a 4x4 transparent gif image.
2852
And as the last part we want this file to be displayed when a banner is removed. It is basically a half html file with a 4x4 transparent gif image.
2917
</p>
2853
</p>
2918
2854
2919
<pre caption="/etc/squid/errors/NOTE_ADS_FILTERED">
2855
<pre caption="/etc/squid/errors/NOTE_ADS_FILTERED">
Lines 2927-2952 Link Here
2927
</pre>
2863
</pre>
2928
2864
2929
<note>
2865
<note>
2930
Don't close the &lt;HTML&gt; &lt;BODY&gt; tags. This will be done by squid.
2866
Do not close the &lt;HTML&gt; &lt;BODY&gt; tags. This will be done by squid.
2931
</note>
2867
</note>
2932
2868
2933
<p>
2869
<p>
2934
As you can see, squid has a lot of possibilities and it is very effective in both filtering and proxying. It can even use alternative squid proxies to scale on very large networks. The configuration I have listed here is mostly suited for a small network with 1-20 users.
2870
As you can see, squid has a lot of possibilities and it is very effective at both filtering and proxying. It can even use alternative squid proxies to scale on very large networks. The configuration I have listed here is mostly suited for a small network with 1-20 users.
2935
</p>
2871
</p>
2936
2872
2937
<p>
2873
<p>
2938
But combining the packet filter (iptables) and the application gateway (squid) is probably the best solution, even if squid is located somewhere safe and nobody could access it from the outside. We still need to be concerned on attack from the inside.
2874
But combining the packet filter (<c>iptables</c>) and the application gateway (squid) is probably the best solution, even if squid is located somewhere safe and nobody can access it from the outside. We still need to be concerned by attacks from the inside.
2939
</p>
2875
</p>
2940
2876
2941
<p>
2877
<p>
2942
Now you have to add the proxy server into the settings of your clients browsers. The gateway will prevent the users from having any contact with the outside unless they use the proxy.
2878
Now you have to configure your clients browsers to use the proxy server. The gateway will prevent the users from having any contact with the outside unless they use the proxy.
2943
</p>
2879
</p>
2944
<note>
2880
<note>
2945
In Mozilla this is done in Edit->Preferences->Advanced->Proxies.
2881
In Mozilla this is done in Edit->Preferences->Advanced->Proxies.
2946
</note>
2882
</note>
2947
2883
2948
<p>
2884
<p>
2949
It can also be done transparently by using iptables to forward all traffic out to a squid proxy. This can be done by adding a forwarding/prerouting rule on the gateway:
2885
It can also be done transparently by using <c>iptables</c> to forward all outbound traffic to a squid proxy. This can be done by adding a forwarding/prerouting rule on the gateway:
2950
</p>
2886
</p>
2951
2887
2952
<pre caption="Enable portforwarding to our proxyserver">
2888
<pre caption="Enable portforwarding to our proxyserver">
Lines 2967-2979 Link Here
2967
</p>
2903
</p>
2968
2904
2969
<ol>
2905
<ol>
2970
  <li>A firewall can be a risk itself. A badly configured firewall is worse than not having one at all.</li>
2906
  <li>A firewall can be a risk in itself. A badly configured firewall is worse than not having one at all.</li>
2971
  <li>How to setup a basic gateway and a transparent proxy.</li>
2907
  <li>How to setup a basic gateway and a transparent proxy.</li>
2972
  <li>The key to a good firewall is to know the protocol you want do allow.</li>
2908
  <li>The key to a good firewall is to know the protocol you want do allow.</li>
2973
  <li>That IP traffic does not always contain legitimate data. For an example ICMP packets with payload.</li>
2909
  <li>That IP traffic does not always contain legitimate data ie. ICMP packets can contain payload.</li>
2974
  <li>How to prevent SYN attack.</li>
2910
  <li>How to prevent SYN attack.</li>
2975
  <li>Filtering HTTP traffic by removing offensive pictures and downloads of viruses.</li>
2911
  <li>Filtering HTTP traffic by removing offensive pictures and downloads of viruses.</li>
2976
  <li>Combining packet filters and application gateways gives a better control.</li>
2912
  <li>Combining packet filters and application gateways provides better control.</li>
2977
</ol>
2913
</ol>
2978
2914
2979
<p>
2915
<p>
Lines 3371-3397 Link Here
3371
</section>
3307
</section>
3372
</chapter>
3308
</chapter>
3373
3309
3374
3375
3376
<chapter>
3377
<title>FAQ</title>
3378
3379
<section>
3380
<title>Questions and answers</title>
3381
<body>
3382
3383
<table border="0">
3384
  <tr>
3385
    <th>Question</th><th>Answer</th>
3386
  </tr>
3387
  <tr>
3388
    <ti>Where can I find the latest version of this guide?</ti><ti>The lastest version should be available on <uri>http://gentoo.org</uri> and the development version at <uri>http://gentoo.insecurity.dk</uri></ti>
3389
  </tr>
3390
</table>
3391
3392
</body>
3393
</section>
3394
</chapter>
3395
3396
3397
</guide>
3310
</guide>

Return to bug 29278