Wednesday, 26 March 2014

Basics :
Encapsulation and De-Encapsulation Process

Note: We are talking about TCP/IP model NOT OSI model which uses 7 layers


Application Layer ----> Transport Layer
All transmissions start at the application Layer . The application on the local computer (client) initiates a request destined for the application installed on some other remote host (server). That request CANNOT be sent directly on to the wire (layer 1). Instead, it must be processed by ALL the layers in between (layers 4 through 2) before it reaches the media. This way, the request from the application is sent down to the transport layer (layer 4) for processing. Application will choose one of the two common protocols used in the layer 4: either TCP, for reliable connections, or UDP for un-reliable connections.

Transport Layer ----> Internet Layer
The transport layer, upon receiving the request from the application layer, will process it according to what it has been designed to do, and is going to stick a so called header to the incoming data (for the details regarding all headers please, see the next lesson). This header will be understood and processed by the layer 4 at the receiving host (remote computer). This segment (as we call it a 'segment' now), is sent down to layer 3 for processing.

Internet Layer ----> Network Interface (Data-Link Layer)
The internet layer will process the incoming data from the layer 4 (which now consists of data or payload from layer 5 + layer 4 header) and will stick its own, layer 3 header, to the payload coming from the upper layers. Once it's done, it will send the packet (as we call it a 'packet' at this layer) down to the layer 2 for processing.

Data-Link Layer ==> Network Interface (Physical Layer)
The Layer 2, similarly to the previous layers (layer 4 and layer 3), is going to process the incoming packet according to the functions designed for this layer. Then, it will attach its own layer 2 header, and is going to send the frame (the name given to the layer 2 protocol data unit) down to the layer 1.

Physical Layer = Signaling
The layer 1 is going to transmit bits, 1s and 0s. Bits is the name given to protocol data unit at this layer according to the OSI model which is the reference for all models). Technically, Layer 1 is common for any networking model and follows IEEE specifications.

The process described above, attaching the controlling information in the forms of the headers, is called encapsulation.


Once the bits arrive at the destination computer (host), the reverse process takes place which is called de-encapsulation.

Network Interface (Physical Layer) ----> Data-Link Layer
The layer 1 is going to accept incoming bits and send them up to the layer 2 for processing.

Network Interface (Data-Link Layer) ----> Internet Layer
The layer 2 piece of software can properly interpret the header information (control information) initially attached by the sender's layer 2 process. So, it reads the layer 2 header, then strips this off, and the content of the frame (without the layer 2 header it's called a packet now), is sent up to the layer 3 for processing.

Internet Layer ----> Transport Layer
The layer 3 is going to perform similar actions that the layer 2 just did. It reads and processes the layer 3 header in the packet. Notice, that only layer 3 process understands the layer 3 header. Then, it removes the layer 3 header and sends the content of the packet (data without layer 2 and layer 3 headers is called a segment) up to the layer 4.

Transport Layer ----> Application Layer
I'm sure you have already guessed what is going to happen next. Yes, the transport layer will read the layer 4 header, which consists of the instructions what to do next. Then, upon stripping off the layer 4 header it will send what was originally created (data request from the client software) to the appropriate process/application at the receiving host (server application).

When the server sends the reply back to the client, the whole encapsulation process will occur again. The client receives the reply from the server, and De-encapsulates the incoming data like explained above.


And this whole process is repeated back and forth until all data has been exchanged.

Classless Inter-Domain Routing (CIDR)
Another term you need to familiarize yourself with is Classless Inter-Domain Routing
(CIDR). It’s basically the method that Internet service providers (ISPs) use to allocate a
number of addresses to a company, a home—their customers. They provide addresses in a
certain block size, something I’ll talk about in greater detail soon.
When you receive a block of addresses from an ISP, what you get will look something
like this: 192.168.10.32/28. This is telling you what your subnet mask is. The slash notation
(/) means how many bits are turned on (1s). Obviously, the maximum could only be
/32 because a byte is 8 bits and there are 4 bytes in an IP address: (4 n 8 = 32). But keep in
mind that regardless of the class of address, the largest subnet mask available relevant to
the Cisco exam objectives can only be a /30 because you’ve got to keep at least 2 bits for
host bits.

Take, for example, a Class A default subnet mask, which is 255.0.0.0. This tells us that
the first byte of the subnet mask is all ones (1s), or 11111111. When referring to a slash
notation, you need to count all the 1-bits to figure out your mask. The 255.0.0.0 is considered
a /8 because it has 8 bits that are 1s—that is, 8 bits that are turned on.
A Class B default mask would be 255.255.0.0, which is a /16 because 16 bits are ones
(1s): 11111111.11111111.00000000.00000000.

Source: www.google.com 

Thursday, 20 March 2014

ARP SPOOFING (Security Class)


1. Introduction
This article introduces the OSI model of internet communication, and describes ARP spoofing. It’s used to attack hosts in a Local Area Network (LAN). Passive and active sniffing are described. Finally, the mitigation of ARP spoofing is briefly discussed.
2. The OSI model
This model describes the structure of internet communication. The idea was to prepare a reference model in order to allow different manufacturers to create networking solutions that can cooperate with each other. The OSI model is comprised of seven layers:
Layer 7: Application
Layer 6: Presentation
Layer 5: Session
Layer 4: Transport
Layer 3: Network
Layer 2: Data link
Layer 1: Physical
I’ll discuss how data goes from the sender to the receiver. From the sender’s perspective, the data goes from layer 7 to layer 1. From the receiver’s perspective, the data goes from layer 1 to layer 7.
I’ll explain what happens when data goes from layer 7 to layer 1. At each stage (from layer 7 to layer 6, from layer 6 to layer 5, and so on) the data of the upper layer is includes into the data of the lower layer (encapsulation). On the receiver’s side, the reverse process occurs and is called decapsulation. That’s how the different OSI layers interact. In reality, it’s more complicated – for example, the data of the application layer might be segmented at the transport layer (before sending to network layer), and fragmented at the network layer (before sending to data link layer.)
Finally, let’s see the classification of network devices with reference to the layers of OSI model:
Layer 1 devices: repeater, hub
Layer 2 devices: bridge, switch
Layer 3 device: router
ARP spoofing is related to the ARP protocol (described in the next section of the article), which is used to bind IP and MAC addresses (from the network and data link layers of the OSI model; Ethernet is commonly used at data link layer.)
3. ARP protocol
Let’s assume that two computers, COMP1 and COMP2, are in the same LAN and want to communicate. COMP1 knows the IP address of COMP2 (denoted by COMP2_IP in the article), but what is actually needed in order to send the message to COMP2 is the MAC address of COMP2 (denoted by COMP2_MAC). Here, the ARP protocol is used. COMP1 sends an ARP request to everyone in the LAN (broadcasting). That request can be understood as something like this: “Hi, COMP2_IP, would you tell me your MAC address (COMP2_MAC)?” What happens next is the ARP reply message with COMP2_MAC. Now, COMP1 can send the message to COMP2, because he knows COMP2_MAC’s address. That’s how it should work. However, there may be an attacker spoofing ARP’s reply messages. Let’s see how that works in this case.
4. ARP protocol – spoofing ARP reply
Let’s continue the story from the previous section and denote the computer of the attacker with COMP_ATTACKER, the IP address of COMP_ATTACKER with COMP_ATTACKER_IP, and the MAC of COMP_ATTACKER by COMP_ATTACKER_MAC.
Now let’s recall the description of the ARP protocol from the previous section of the article, and change one thing – when the ARP request is sent, the COMP_ATTACKER replies (ARP reply) with their own MAC address (COMP_ATTACKER_MAC). Then, COMP1 thinks that they’re sending the message to COMP2, but they’re actually sending it to COMP_ATTACKER, because they’ve received COMP_ATTACKER_MAC in the ARP reply. In addition, COMP_ATTACKER sends the ARP request to everyone in the LAN to learn the COMP2_MAC. At that point, COMP_ATTACKER knows the COMP2_MAC and can forward the message (received form COMP1) to COMP2. Before forwarding the message, COMP_ATTACKER can read and/or modify the message if it’s not protected. That’s how ARP spoofing works. As we can see, that attack can lead to serious problems.
5. Passive and active sniffing
Switches know the MAC addresses attached to their ports and send the messages only to the port, where the destination host is attached. That way, other hosts cannot see the messages or sniff the traffic. As we’ve already seen, ARP spoofing can be used to overcome that problem from the attacker’s point of view. ARP spoofing is an active attack, because sending the spoofed ARP reply is involved.
Now let’s go back to the old days and discuss hubs. Those devices work at Layer 1 of the OSI model. The message received by the hub is sent to all its ports. Normally, the host reads the messages that are sent to them. However, the host can be put in promiscuous mode to see all the messages. That’s how passive sniffing works: the attacker is just sniffing the traffic.
 6. Mitigation of ARP spoofing
Normally, the entries in the ARP table of the host are dynamic. These entries could be predefined (static) to solve the problem with ARP spoofing. However, this approach has an administrative overhead.
Another idea is delegating the problem to the upper layers of the OSI model and using cryptography to provide authentication (digital certificates), data confidentiality (symmetric encryption), and data integrity (HMAC). SSL/TLS can be used here (it works between application and transport layers of the OSI model.) That way, we can detect if something wrong has happened with the message, and prevent reading it.
7. Summary
The OSI model of internet communication was introduced. Then the ARP protocol and ARP spoofing were discussed. The difference between active and passive sniffing was presented. Finally, the mitigation of ARP spoofing was briefly analyzed.


Lesson 1 TCP/IP

Lesson 1 - Introduction to TCP/IP Layers
In this lesson we take a sneak peek at the fundamentals regarding TCP/IP. This is one of the most important aspects to understand in order to follow the upcoming lessons. It is not my ambition to explain everything here as this would turn into a hefty book to read. You can find a lot of them on the market anyway. Instead, I will try to focus on some basic aspects of TCP/IP. They will constitute the minimum knowledge to help us understand how computers communicate.

Introduction
Computer communication follows some well defined rules and guidelines which we call protocols. In order for the computers to exchange data they have to agree on using the same rules, otherwise they become incompatible. That was the case in the past. This was one of the reasons to create a common model for communication. It was called OSI Model (Open Systems Interconnection). This was an attempt to make different vendor's computers exchange data easily. This way IBM machines could talk to DEC machines and so on. Today however, it is the TCP/IP model that is all-pervasive. This protocol suite is derived from OSI model and somewhat loosely follows its rules and terminology. This is going to be the focus of our discussion and the main topic of this lesson.



TCP/IP Layers
The designers decided to break down the whole complexity of data exchange and created five layers of functions to accomplish the goal. This approach helps develop and modify certain layers of code without touching other layers. For instance, if you are an application programmer, you will be writing a code in the 'application layer' which allows you to use already written code dealing with the transport of data between computers. You do not even have to learn how this transport is done. It has already been written for you. This way, you focus on the application you're creating, what it does and how it works rather than learning about network adapter drivers, signaling and other gory, hardware details.

TCP/IP model divides the functions related to data transmission by using five distinct layers of responsibility. Below area these layers.

Layer 5-7 - Application
This is where the data's journey begins. Everyday, you use many applications that  rely on network services. Those applications are classified to be layer 5 code. Your web browser and web server, mail client and mail server, ssh client and ssh server etc. You may have noticed the term client and server often used in the above description. Pretty much all applications use this architecture. Client, is an application that requests some services from the server application. Server application is providing a client with what they want. A common example of that architecture is your Firefox or Internet Explorer web browser (client application) requesting a page from Apache or IIS web server (server application). Applications, in general, provide a User Interface (UI) which offloads us from a burden of knowing how a computer does things internally.

So, once your application formed the request, that one is sent down to the layer 4 (transport layer) asking for the delivery to the host somewhere in the network.

Layer 4 - Transport
This layer accepts all requests coming from the upper layer (application layer) and tries to organize the transport of that request across the network. In TCP/IP model this layer of software is responsible for:
  • Breaking down big files that are sent across into smaller chunks called segments. There are technology limitations that do not allow our computers to send large files in one piece. It would not be a good idea anyway as any small change of the data during transmission would make the sender re-transmit the whole file again instead of the smaller chunk only. That of course, would take more time and resources to successfully transmit the data.
  • As your computer uses many applications that will transmit something across the network at the same time, the system must know how to mark those request such that they are delivered to the right receiver applications. And once the replies are coming back, they should be delivered back to the same process that initiated them. The concept of the port number has been introduced to deal with that. Source and destination ports ensure that all requests and replies are delivered to the appropriate processes on the computers exchanging data. More on that later in the upcoming lessons.
  • This layer also allows the application to use connection-oriented or connectionless services. The former, establishes communication with the receiving computer (or more generally: destination host) before data can be exchanged, the latter will send data without ensuring that the destination application is running and willing to receive anything. This form of transmission is used primarily for voice and video applications.
  • This layer will also give applications some options in terms of the reliability. Depending on which layer 4 protocol the application is designed to use, the reception of data can be verified or not. That creates reliable versus unreliable transport respectively. In the reliable transport any data that has not been delivered will be retransmitted, unlikely the unreliable transport.
  • One other function of layer 4 could be to moderate the transmissions so that the receiving host is neither swamped by the excess of packets coming in nor is it waiting and doing nothing because the sender's speed of transmission is too slow. Majority of the functions above are performed by TCP protocol, not UDP as applications choose one of them to use.
Once all aspects and functions in this layer have been taken care of, layer 4 sends the data it received from layer 5 down to the layer 3 requesting its service.

Layer 3 - Internet
Upon receiving a request from layer 4, this code is going to process the incoming information. Since, typically we have more than one path between the sender (source) and the receiver (destination), the function of this layer is to find the best path between them. In order to accomplish that there are two concepts I need to introduce here.

Firstly, we need to know how computers find themselves in the network. This is accomplished by using specially designed, layer 3 addresses uniquely identifying computers in any network. The addresses used by this layer consist of four bytes delimited by the dots (e.g. 10.1.1.1) which are followed by a, so called, 'netmask' also consisting of four bytes with the dot used as the delimiter (e.g. 255.255.255.0). The whole IP address can look like this:
10.1.1.1 255.255.255.0. More on those later.

Secondly, because the destination of our data can be outside of our own network, a device called router has been introduced to find the optimal paths between the different networks in which the computers reside. The data processed by layer 3 is called a packet or datagram. This layer also uses a mapping to the upper layer 4 that has requested its services. This is due to the fact that there are more than one protocols available in layer 4 (TCP or UDP). This information (which layer 4 protocol is sending the data) is going to be useful when the data arrives at the destination and the destination's layer 3 process needs to send the content to the appropriate layer 4 protocol for processing. It has to be the SAME protocol that the sender used in layer 4.


Layer 2 - Network Interface (Data Link in OSI model)
There is a great variety of technologies that handle data transmission on media such as copper and fiber optics cables or air (wireless). In order to offload the layer 3 protocols from learning all possible signaling methods, layer 2 was created. Thus, layer 3 can focus on finding the best path between the source and the destination, and the layer 2 functions will handle the details of preparing the data to be placed on the actual media (copper wire, fiber, air etc.). The piece of information processed at this layer is called a frame. This layer will also use specially designed addressing scheme to recognize the next device which a computer is sending the data to. For instance, in the commonly used layer 2 technology called Ethernet, this address uniquely identifying hosts in the same network is called MAC Address. The reason why we use different addressing schemes: layer 3 and layer 2, will become clearer when we get into some details of the actual data exchange. Please, bear with me till we reach the right lesson that explains it in more detail. The device that is capable of understanding the structure of a frame and delivers the data between the hosts in the SAME network is called bridge, or switch. As of the time of writing this, switches are very popular devices and bridges can be found mostly in museums.

Once the layer 2 has prepared the data which layer 3 requested to send, (the process is called 'framing'), layer 2 will send the request to layer 1 asking for the data to be placed onto the wire/fiber/air using the appropriate signaling method.

Layer 1 - Network Interface (Physical Layer in OSI)
This layer receives requests from layer 2 (data traveled from layer 5 to layer 1 now). The physical layer is going to encode data received from layer 2 software and place them in the form of bits (1s and 0s) on the medium. This way, ones and zeros travel across the media to deliver them to their receipient. The bits can traverse multiple devices as they go across such as hubs, switches, routers. What type of devices will forward those bits depends on the design of the network. The devices referred to as layer 1 devices are hubs, cables, network adapters, connectors, transceivers etc. Also the data processed by this layer is called bits. This layer defines low level aspects of the transmission such as cables used, maximum distance the cable can reliably sent bits across, types of the connectors, speed of the transmissions etc.


Friday, 14 March 2014

Usefull tips for deleting virus in the Pendrive.



 
If the  Pen Drive is infected with any of the following viruses:

* Autorun.inf
* new folder.exe
* Iexplorer.vbs
* Bha.vbs
* nfo.exe
* New_Folder.exe
* ravmon.exe
* RVHost.exe or any other files with extension.


These viruses are hidden and can't be seen even after
you enable show hidden folders. Instead of formatting pen drive.
Following simple dos command will change the attributes of
these files ,there after you can remove it by pressing delete
key.
 

Follow these steps:
Step1.:Type cmd in Run
 

Step2.: Switch to the drive on which pen drive is connected
(like C:\> h: enter)
 

Step3.: type exactly as attrib -s -h *.* /s /d and hit enter
(don't forget spaces).
 

Now you can see hidden virus files and you can delete them.

Wednesday, 12 March 2014

Top 10 interesting WWW facts



25 years of the World Wide Web: Top 10 interesting WWW facts  :





Today, you are able to access numerous webpages through a Web browser over the Internet. You use Facebook, Twitter, Wikipedia, Google and many other websites often to get information, stay in touch with people and remain updated.

Most of us heavily rely on the Internet, and the power of Internet is not hidden from anyone. But do you know the source of this power? Do you know when did you get this power to freely view any webpage? 25 years ago, on March 12, 1989 British engineer and computer scientist Sir Tim Berners-Lee, now Director of the World Wide Web Consortium (W3C), wrote a proposal for what would finally become the World Wide Web.

On the 25th anniversary of the World Wide Web - a true milestone in the Internet history - here we bring you certain interesting facts about the Web:

25 years of the World Wide Web: Top 10 interesting WWW facts

On March 12, 1989 the seed of the idea that would go on to become the World Wide Web was sown.

1. On August 6, 1991, the first website http://info.cern.ch went online.

2. A NeXT Computer was used by Sir Tim Berners-Lee as the world's first web server and also to write the first web browser - WorldWideWeb - in 1990.

3. Berners-Lee uploaded the first photo on the Web in 1992. That was an image of the CERN house band Les Horribles Cernettes.


The first photo on the Web in 1992



4. On April 30, 1993, CERN, a European research organisation near Geneva, announced that the World Wide Web would be free to anyone, with no fees due.


5. It is believed that a turning point in the history of the the World Wide Web began with the launch of the Mosaic web browser in 1993. It was a graphical browser developed by a team at the National Center for Supercomputing Applications at the University of Illinois. Mosaic is the web browser credited with popularising the World Wide Web.


6.  The World Wide Web Consortium (W3C), the main international standards organisation for the World Wide Web, was founded by Tim Berners-Lee after he left CERN in October 1994.


7.  Archie is considered to be the first Internet search engine. It was the first tool for indexing FTP archives, allowing people to find specific files.


8.  If you dislike Internet users being addressed to as 'surfers', blame Jean Armour Polly. It was she who coined the term "Surfing the Internet".


9.  Pornography constitutes a big chunk of the Web for most of its existence, but the first website on the .xxx domain (meant for adult websites) went online in August 2011.


10.  Most people tend to treat the Internet and the Web as synonymous. They, in fact while being related, are not. Internet refers to the vast networking infrastructure that connects millions of computers across the world and the World Wide Web is the worldwide collection of text pages, digital photographs, music files, videos, and animations, which users can access over the Internet. The Web uses the HTTP protocol to transmit data and is only a part of the Internet. The Internet includes a lot that is not necessarily the Web.



World of FIREWALL'S
Firewalls are used to control the inbound and outbound traffic on a protected network. They have an ability to block and allow the internal as well as external services within the network. Before allowing access to the service, a firewall may also force the client / user to pass through authentication. It is also used to monitor the ingoing and outgoing traffic of the protected network. Sometimes a firewall can be also used in IPSEC tunnels as a platform. It monitors security-related events.
The packet filtering mechanism mainly contains inspection on TCP/IP and UDP packets. It also includes all ports in its inspection. In this process, certain rules are written for allowing and rejecting the packets passing through the network. The rules written in the firewall may contain TCP and UDP port numbers, source and destination addresses. One can implement firewall rules which may work in both inbound and outbound directions.
Types of Firewalls
There are basically four types of firewalls:
  1. Packet Filter Firewall
  2. Stateful Packet Filter Firewall
  3. Circuit Level Gateway
  4. Application Level Gateway
  1. Packet Filter Firewall
This firewall comes into play when an administrator wants only certain packets to enter into the protected network. In this case, each packet will be monitored and inspected before passing through the network, and after monitoring and inspecting, the firewall will decide whether to let it pass or not.

Figure : Packet Filtering Firewall Location
There are two types of packet filter mechanisms:
  1. Stateful Packet Filtering
This type of firewalls is known as a smart / clever firewall. If the firewall remembers the packets it allowed and blocked in the network, then it is known as stateful packet filtering. Sometimes it is also called a dynamic packet process.
  1. Stateless Packet Filtering
In this case, information about all those previous packets passed through the networks is not being remembered by a firewall. This type of firewall can be bypassed and easily fooled by attackers, and is especially dangerous for UDP data packets. This firewall will never come to know whether the given packet is a part of existing connection or any rough useless packet, because it isolates each and every packet.
  1. Stateful Filter Firewall
I have already mentioned the stateful packet filtering process in the above section. Additionally, this type of firewall keeps a track record of TCP streams to inspect each and every packet passing through and in and out of the network. Generally this type of firewall is only constructed to inspect packets which are coming in only one direction, from client to server. There is an automatic process which handles counter requests (replies) going from server to client. It has an ability to support a wider range of protocols such as IRC, FTP, etc…

Example: FTP Inspection

Figure : FTP Inspection Architecture
Example:
First, the client sends an FTP request to the server, and the firewall will store the connection state. Then the server will give the server port to the client in order to tell the client that we will be using this 2050 port (as mentioned in above pic). Then it checks for the state of connection such as SYN, ACK, etc. If everything is right and legitimate, then the server establishes the connection and starts transferring the data by keeping in mind that packets should not be lost.
  1. Circuit Level Gateway
This is a type of firewall within the session layer. It checks the legitimacy of the TCP handshake between packets in order to check if the established session is legitimate or not. The problem with circuit level gateway is, they do not provide cleaning of entity packets. Due to this, private networks have an advantage and it is that they can hide their network information. Here individual packets will not be filtered. SOCKS are commonly used in this type of firewall.

 Circuit Level Gateway Architecture
Here TCP/IP packets will be accepted by the firewall. There is identical specification of the rules such as packet filter firewalls. Adding to that, several rules can state which connections will be allowed and which connections should be not allowed. An allowed connection (via rules) will create a new fresh connection with the server.
  1. Application-Level Gateway
Application-level gateway contains security components such as a firewall. As per its name, sometimes it is also denoted as ALG (application-level gateway). Here the client connects to the server using any TCP/IP application such as FTP or Telnet. Then the name of the remote host is needed from the user to access it. The gateway also relays the TCP segment which has some information of application data between client and server.

Application layer proxy firewall flow scenario
Here the client and server both will connect to the proxies instead of connecting directly. So if the proxy finds any suspicious file or activity on their network, it will drop that connection. This firewall is made to handle complex protocols such as SIP, H.323, etc. Here packets are received and also processed by the server. The client generates new packets every time.
Types of Policies
There are two types of policies in these types of firewalls.
  1. Permissive: Generally it allows all packets to pass through the network using relative ports and block just some packets, such as IRC, TELNET, SNMP, etc. If you forget to block something, then it can cause a vulnerable network.
  1. Restrictive: By default it blocks
    all packets and allows some packets to pass through the network using certain ports and allows HTTP/s, SSH, SMTP, POP3 etc. This policy is secure, compared to the permissive one. Here if you forget to allow any port, someone will complain to you and you can allow that port; on the other hand, in permissive policy, if ports are open without you knowing, then no one will complain or tell you.
Netfilter/IPtables
Netfilter.org is home of the packet filtering framework for Linux kernels starting from 2.4.x and later. Software within that framework is known as Iptables. All incoming packets passing through the routing engine determines whether it should be allowed to deliver to the client or not. Using iptables, you can create your own firewall based on your rules. Netfilter has three tables:
1.     Filter – Contains INPUT, FORWARD & OUTPUT chains.
2.     NAT – Contains the rule for source/destination address along with port translation.
3.     Mangle - Contains rules for specialized packet routing flag.
Syntax
#iptables < option >< chain >< matching_criteria>< target >
Features
·         It provides stateless and stateful packet filtering for both IPv4 and IPv6.
·         It provides each and every kind of port translation in both IPv4 and IPv6.
Creating Basic Firewall Using Iptables And Testing It Simultaneously
Challenge: Create a firewall for a university/college in which they want their SSH port to be open so students can work in the university’s virtual Linux environment. They specifically want to block the icmp echo packets coming to their server. Also allow all these ports on networks such as 80, 443, 21 & 20. The university also wants packets containing SYN & FIN flag to be allowed, and the rest of them must be blocked over the Internet. The network also must allow traffic on the loopback device which will be localhost/127.0.0.1
Solution:
Before you start configuring your firewall, you need to make sure that iptables/netfilters is installed on your Linux environment. Generally it is there, if it is not there then you can download it from any website.

If there is an iptables running on your machine, it means it will have some rules by default. If you want to see that which rules are currently set to which option, you can run the below command.
#iptables -L

So it shows that currently the input and forward chain’s values are set as DROP and output chain’s value has been set to ACCEPT.
One should keep a practice that if he/she is going to create a new firewall, he/she needs to flush all previous/old rules which are previously set. To clear all those rules, you need to give this command.
#iptables –F or #iptables –flush
It will flush/remove/clear/delete all the previous rules set in the firewall. Now we are in a position to create a new firewall with our custom rules to address the given challenge.
In reading our challenge, one thing is sure: that our primary policy set will be in such a way that our input chain policy will be set to DROP in order to drop all the packets initially at the firewall level so that they don’t come inside the network, and then we will set different packets to let them come inside our network. So our input chain’s policy value will be set to DROP along with forward policy value. And we will set the output policy value as ACCEPT, as there won’t be any harm if some packets are going from the network to the outside world on the Internet. So here are those terminal commands.
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
The first two commands are easily understandable: we are dropping each and every incoming packet and we are allowing every outgoing packet. We use the forward policy to route the packets within the LAN. So if you want to allow that scenario, set the value of that policy as ACCEPT. In our case they have not mentioned anything, so we will block it for this moment.
Now the second thing to do is to allow the traffic which establishes new sessions. In order to do that, we can give the below command.
#iptables -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT
Here I am going to describe some options used in this command which will remain constant throughout this example. Option –A stands for append. Its work is to append the given rule to the relevant rule chain. Here our rule chain is INPUT. The –m option is used for matching. Here it is matching our different state with the live traffic and if it finds anything, it applies the policy on that. Lastly, the -j option tells exactly what to do with the packet which is matched and found by the firewall.
For the jump, there are some targets such as ACCEPT & DROP. As you know, ACCEPT will allow that packet to come inside the network through the firewall and DROP will simply drop the packet. When you give this command to the terminal, and if you set the policy using the iptables –L command, you will see the below result.

I have given the below command to print “Starting New firewall”:



#echo “Starting New firewall”
Moving forward, now we need to open the SSH port whose port number is 22. So basically students of the university/college can play in their virtual Linux box via port number 22 (SSH) by sitting anywhere. Here is a command for that:
#iptables -A INPUT -i eth1 -p tcp –dport 22 -d 192.168.150.136 -j ACCEPT
Here we have two new options which are –i and –p. The “-i” stands for interface declaration. It may be possible that the university has more than one interface, so set which interface you want to apply these rules. According to the challenge, they want to apply these rules on their eth1 interface.
The “-p” option is used to define the protocol such as tcp and udp. Here we are defining our SSH port, so we will use the tcp option and last but not least we will use the “-dport” option, which stands for destination port. In our case, we are working on SSH and the port number of SSH is 22. So we will define dport value as 22.
The “-d” option is used for the destination IP address. We mentioned it in our command. When you set this policy through your terminal and then you list the result, it will look something like this:
 

Testing SSH with ACCEPT and DROP Value Set
Value set ACCEPT:

Value set DROP:


Now it is time to configure all ports mentioned in our challenge. We will form a group of two. First we will accept all those packets which are coming through http and https. The command for that is shown below.
#iptables -A INPUT -p tcp –dport 80 -j ACCEPT
#iptables -A INPUT -p tcp –dport 443 -j ACCEPT
All the options are mentioned. The only difference is that in dport value we give 80 which stands for http and 443 which stands for https. We will not be testing each and every policy we set in our firewall. We will test only some of those major ones. After running the above commands in your terminal, when you list the rules it will look something like this:

Now we are going to allow loopback traffic on the localhost which will be l0/127.0.0.1. Here we will allow both inbound and outbound traffic in loopback. The command for that is shown below.
#iptables -A INPUT -i l0 -p all -j ACCEPT
#iptables -A OUTPUT -o l0 -p all -j ACCEPT
As you can assume, we have set our interface value of the loopback device which is l0/127.0.0.1. On top of that we are allowing all ports to communicate with this device. After you set this result, when you list the rules it will look something like this:

You can see that entries are increasing at the down side as we are setting our each and every rule. Now we will allow port 21 & 20 to communicate with our network. The command for that is:
#iptables -A INPUT -p tcp –dport 21 -j ACCEPT
#iptables -A INPUT -p tcp –dport 20 -j ACCEPT
After applying these rules to the terminal, list the firewall policy. It will look like this:

Moving forward, the university specifically wants to block all icmp echo packets coming to their server. To do that, here is the command:
#iptables -A INPUT -p icmp -d 192.168.150.136 –icmp-type 8 -j DROP
Here we have defined the destination IP address where we want to block/DROP all icmp echo packets. Icmp has some number types starting from 0 to 41-255 and each number has some meaning, for example number 8 stands for echo. If you want to know more about that, refer to RFC792. List the rules after setting this latest icmp policy.

Value set ACCEPT: Reply from the server is coming.

Value set DROP: Server won’t respond anymore.

Finally, at last, the university wants to accept the packets coming on the network which contains SYN and FIN flags only. It means the rest of the all packets containing flags such as RST, PSH, ACK & URG must be blocked. I could only give two commands in order to show that only packets containing SYN and FIN flags should come through, though I have typed all commands in order to have a close look if you want to use them in the future, and then you can uncomment and change them according to your needs.
So here are the commands for that:
iptables -A INPUT -m tcp -p tcp –tcp-flags ALL SYN -j ACCEPT
iptables -A INPUT -m tcp -p tcp –tcp-flags ALL FIN -j ACCEPT
#iptables -A INPUT -m tcp -p tcp –tcp-flags ALL RST -j ACCEPT
#iptables -A INPUT -m tcp -p tcp –tcp-flags ALL PSH -j ACCEPT
#iptables -A INPUT -m tcp -p tcp –tcp-flags ALL ACK -j ACCEPT
#iptables -A INPUT -m tcp -p tcp –tcp-flags ALL URG -j ACCEPT
I have executed first two commands and the rest of those are in the comment. So in the first two commands it shows that they have loaded first all flags by defining ALL and then they just want SYN and FIN to enter via packet into the network. So when you list the rules it will look like this:

So in the last two commands it tells us that it is showing that total tcp flags are FIN, SYN, RST, PSH, ACK & URG, but from all of them you need to accept the packets which only contains SYN and FIN flags. So here is the testing for that. For testing purpose I am using hping3 utility in KALI Linux.

To write all these commands in the terminal is a very tedious thing. It is better to write a script for this. To create a script, you will need to go to the /sbin/ folder and then you create any file with a touch command. In my case it’s a ccc.sh file, so the command would be like this:
#touch ccc.sh
Then you open that file with your favorite editor and write all the following code in that. Before you start writing all the rules, you must define the below:
#!/bin/bash
This command will identify that it’s a bash script. Otherwise it won’t recognize anything written in that file. So your final code would be like this:
#!/bin/bash
echo “Starting New firewall”
#Flushing old firewall rules
iptables -F
#standard Firewall rules for INPUT OUTPUT and FORWARD
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
#Allowing 3-way handshake
iptables -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT
#Allowing SSH server
iptables -A INPUT -i eth0 -p tcp –dport 22 -d 192.168.150.136 -j ACCEPT
#Allow HTTP HTTPS – 80,443
iptables -A INPUT -p tcp –dport 80 -j ACCEPT
iptables -A INPUT -p tcp –dport 443 -j ACCEPT
#Allowing Loopback traffic(Input and Output both) – l0
iptables -A INPUT -i l0 -p all -j ACCEPT
iptables -A OUTPUT -o l0 -p all -j ACCEPT
#Allow FTP – 21 20
iptables -A INPUT -p tcp –dport 21 -j ACCEPT
iptables -A INPUT -p tcp –dport 20 -j ACCEPT
#Blocking IMCP packets
iptables -A INPUT -p icmp -d 192.168.150.136 –icmp-type 8 -j DROP
#Blocking Packets with all tcp flags
iptables -A INPUT -m tcp -p tcp –tcp-flags ALL SYN -j ACCEPT
iptables -A INPUT -m tcp -p tcp –tcp-flags ALL FIN -j ACCEPT
#iptables -A INPUT -m tcp -p tcp –tcp-flags ALL RST -j ACCEPT
#iptables -A INPUT -m tcp -p tcp –tcp-flags ALL PSH -j ACCEPT
#iptables -A INPUT -m tcp -p tcp –tcp-flags ALL ACK -j ACCEPT
#iptables -A INPUT -m tcp -p tcp –tcp-flags ALL URG -j ACCEPT
#Listing the Set Rules
iptables -L
Now you run that file by simply typing ./ccc.sh in the terminal. In case it does not work, then you need to set the permission to that file by simply typing chmod 777 ccc. sh and then it will surely run.