Basic of Firewalling and Iptables
Total Page:16
File Type:pdf, Size:1020Kb
Basic of Firewalling and iptables Antonio Merola & Arrigo Triulzi Internet is a tremendous bin of information and this is a great advantage, but likewise is quite easy to get lost or waste lots of time to collect info about topic we are interested in. This is where magazines like this come to play; with this Starter Kit firewall issue we want give you everything concerning, packet filtering and firewalling starting from scratch, basically you have in your hands the fastest way to learn about basic of firewalling and iptables! firewall is a software box used to filter The header contains instructions about the packets according to rules to match; data carried by the packet, these instructions A these rules are intended to protect hosts may include for instance the Protocol as in the behind this box, with this box we are defining case of an IP packet carrying on another pack- a perimeter. For instance, at your home DSL et (transport protocol) as TCP, UDP or ICMP connection, quite surely you have a box acts as packet (ICMP is embedded as a transport pro- a router/firewall between you and your service tocol as TCP/UDP, although it is a layer three provider network, this because you want to be protocol as the IP protocol itself). safe while connecting to an untrusted network The payload, also called the body or as internet. data of the packet is the actual data that the Of course a firewall is not a heal-all for packet is delivering to the destination; the security, because a single security technol- trailer typically contains some bits that tell the ogy will give a single point of failure, anyway receiving device that it has reached the end a firewall is a must to secure networks. So let’s of the packet. It may also have some type of start understanding key concepts that we will error checking such as the Cyclic Redundancy meet while firewalling, first of all with a packets Check (CRC). Packet filtering/firewalling is review. Basic packets review What you will learn... Dealing with TCP/IP networks we deal quite • you will learn about stateless and stateful filter- often with packets. To better understand what ing, along with iptables basics. a packet is made of, we start with the concept that depending on the type of network, pack- What you should know... ets may be referred by another name: frame, block, cell, segment, datagram etc., anyway • the reader should have at lea basic knowledge regardless this, they are quite often composed about the TCP-IP protocol. of three part: header, payload and trailer. 42 www.en.hakin9.org hakin9 starter kit 2/2007 Basic of firewalling and iptables surely linked to some kind of con- that carry on TCP packets, no UDP traffic to ports in the 1-1023 range. nection with values defined inside or ICMP. Now in the same way it has As a matter of fact we would have packets header’s fields, some kind to look the second e third byte in the lots of open ports to handle these of comparison that let decide what TCP header looking for binary value kind of dynamic port allocation and allow and what not. Let’s start un- of 00000000 01010000 (decimal we would be unable to recognize derstanding the so-called stateless number 80, http port identifier) has packets not belonging to legitimate filtering. showed in Figure 2. sessions. Now a question: is this In the same way we can filter only kind of filtering useful?, of course the Stateless filtering certain networks, only packets with answer is yes. We can perform a so First firewall generations were called flag SYN or ACK set, etc. or packets called absolute filtering, to be more stateless filter. Stateless filtering is directed on certain ports and so on, precise the filtering we want apply the ability to filter packets taking de- basically all the fields of the packets regardless of our security rules, cisions based on values discovered header might be compared. for instance is good choice to drop inside header’s fields. In Figure 1 an A limit of these kind of filtering is inbound traffic with source ip belong- IP packet header is displayed. that it isn’t able to keep the state of ing to internal networks or belonging Now let’s suppose we want a connection and isn’t able to look in- to private addressing space or to our packet filter, allow only outgo- side the payload in order to perform IANA unallocated address space ing web traffic; we already know a kind of advanced filtering. such as 0.x.x.x – 1.x.x.x – 2.x.x.x that this means only TCP packets This limit plays a problem han- etc. or more, drop critical ports as directed to the TCP destination dling complex protocols that deals TCP/UDP 135-139 and so on. port number 80. Well, our stateless with dynamic port allocation as in the Almost every router implements filter has to look inside IP packets case of FTP or RPC protocols. the so called ACL (Access Control and according to the field in byte As a matter of fact to let an ac- List) that let you specify this type of position number 9 in the IP header tive ftp session go throughout the filtering; of course with a firewall is it has only to allow TCP packet, in packet filter, would be necessary possible to perform this filtering and this case only IP packets with the allow all traffic from source port 20 later on you are going to see how you binary value of 00000110 (decimal to all destination ports greater than can do it with iptables. It’s now time number 6, tcp protocol value identi- 1023, while in the case of passive ftp to understand the other side of filter- fier) which means only IP packets mode would be necessary allow all ing called Stateful. Stateful filtering As an improved mechanism this kind of filtering is able to keep a ses- sion table in order to apply decision based on communications already logged in this table. As a rule of thumb, as soon as a connection is initiated it is compared with the filter rules; if there is an allow/permit for that connection, an entry is created in this table; this table is called quite Figure 1. IP protocol header, note that one tick mark represents one bit position often connection table. This be- cause further packets related are matched with the entries into the connection table in order to verify if they are intended as a part of the connection or are uncorrelated packets to discard. Once the ses- sion has ended, its entry in the table is removed. Basically the packet filter store the four values representing a TCP/ IP session: source ip and port, des- tination ip and port; advanced or let’s say modern stateful packets filters are able to save also others Figure 2. TCP protocol header, note that one tick mark represents one bit position value as the sequence number of hakin9 starter kit 2/2007 www.en.hakin9.org 43 the connection etc., appropriate latter filtering is also well known as overview on a topic. I think the best behavior is to better know how proxying or application firewall such way to do this is invoking google and stateful filter handle the connection as the relatively new XML firewall wikipedia; just type www.google.com table. As a note, only lately pack- concept, a specialized box able to and google in this way: define: ets filter are able to handle stateful perform security with XML (Exten- firewall, (see frame Definition of connection with also some kind of sible Markup Language) messaging firewall on the web), while from ICMP messages, where a reply exchange. We are arriving with that www.wikipedia.org you should get message packet is allowed only as kind of filtering called Deep Packet something like showed in frame Fire- a response to a request message Inspection (DPI) capable to check wall (networking) – from Wikipedia, packet. Another restriction for this also for non-protocol compliance the free encyclopedia kind of filtering is linked to the inabil- along with predefined rule to decide ity of protocol or payload checking. if allow or deny packets, basically iptables Complex protocol behavior as in a layer 2-7 filtering inspection; nowa- On most Linux systems, iptables the case of FTP or RPC etc. a bit days included in network appliance is a software installed under /sbin/ unsuitable with stateless filtering, this is a technology also used to iptables it’s widely used and that is let’s foresee we only want to allow perform a sort of basic intrusion why is also present in many com- http port 80 traffic, it’s impossible prevention, as result of the predicted mercial firewall; lots of DSL box are with this filtering to check that actu- convergence of Firewall and Intru- custom Linux with iptables. For an ally http is going through the port sion Prevention Systems. easy approach to iptables and just 80. So in the first case the packet Now that we now know what to avoid writing the umpteenth ipta- filter has to be able to look inside the we are talking about, we can look bles tutorial we preferred to analyse payload to correctly allocate the port up on internet the word firewall and a typical home DSL configuration, to open during the session, while in check others opinions on the topic; after a proper introduction.