Project Report On Implementation of Firewall On Linux Platform
Project Report On Implementation of Firewall On Linux Platform
1
Project Report on Implementation of Firewall on Linux Platform
2
Project Report on Implementation of Firewall on Linux Platform
In the past ten years , the internet's growth has exploded.In 1985
the internet included only 1961 host computers . The growth of
internet has been phenomenal and by 1999 there has been
nearly 300,000 computers.The internet has continued to grow at
exponential rates.
More important than the increase in the number of hosts
connected to the internet is the increase in the number of
internet users. The internet has essentially doubled in size. Today
the number of internet users increases at a rate of nearly
200,000 new logins each month.
Security in Internet
3
Project Report on Implementation of Firewall on Linux Platform
Security Experts :
Most security experts are capable of hacking,but decline
from doing so for moral or economic reasons.Computer
security experts have found that there's more money in
preventing hacking than in perpetrating it.A number of large
internet service companies employ ethical hackers to test
their security systems.
Student Hackers :
These hackers belong to junior high, high school or college.
Their social position is Student. They usually perform joy-
riding through cyberspace looking for targets of opportunity
4
Project Report on Implementation of Firewall on Linux Platform
Criminal Hackers :
They hack for revenge or to perpetrate theft.These are the
persons who compromise internet servers to steal credit
card numbers or hack the internet banking mechanism to
steal money. They regarded as real criminals
Spies:
These are hackers employed by Foreign Governments
against high technology businesses. Many high technology
businesses are naïve about security, making them easy
targets for the experienced intelligence agencies of foreign
governments.
The main purpose is to extract technology that give their
own corporations an edge.
Disgruntled Employees:
They pose the most dangerous threat.They are the most
difficult to detect. An employee who is constantly picked by
an employer may become an hacker. As he is aware of the
security details in the company.
Hacking stages can be classified into the following areas:
• Eavesdropping and snooping
• Denial-of-service
• Impersonation
• Man-in-the-middle
• Hijacking
5
Project Report on Implementation of Firewall on Linux Platform
6
Project Report on Implementation of Firewall on Linux Platform
Denial of Service:
The next thing a hacker can do is to disable some aspect of
the network or to bring the network down. Methods hacker
can use to disable computer services are:
Ping of Death: In this a specially constructed ICMP packet
that violates the construction rules is sent by the hacker to
crash the computer if the computer's networking software
does not check for invalid ICMP packets.
SYN Attacks and ICMP flooding : This is another method
used by the Hackers.The initial IP packet of a TCP
Connection attempt is simple and easy to generate and
responding to this takes more time and memory space as
the receiving computer must record information about the
new connection. The Hacker can send one SYN packet after
another to the target computer and then the target
computer will be unable to process legitimate connection
attempts as its memory and time is wasted processing SYN
requests.
In ICMP flooding ,the Hacker sends a constant stream of
ICMP echo requests to the target computer.The target
computer then spends most of its time responding to the
echo requests instead of processing legitimate network
traffic.
Impersonation:
This is the next step the Hacker takes. By impersonating
another computer that the computers on a given network
trust, the hacker's computer may be able to trick the
computers in revealing enough information for the hacker
to get into the security. The tactic that a hacker uses is :
Source routed Attacks: Source routing is the route the
packet takes as it crosses the TCP/IP based network.This
makes it possible for the hacker to send data from one
computer and make it look like it comes from a trusted
source.The Hacker can use source routing to impersonate
an already connected user and inject additional information
into an otherwise benign communication between server
and authorised client computer.
Man -in - the -Middle:
This is a special case of impersonation, where the hacker
operates between two computers on a network. When the
client computer opens a connection to the server
7
Project Report on Implementation of Firewall on Linux Platform
8
Project Report on Implementation of Firewall on Linux Platform
IPCHAINS USING C
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <linux/if.h>
#include <linux/ip_fwchains.h>
int setsockopt (int socket, IPPROTO_IP, int command, void
*data, int length)
DESCRIPTION
The IP firewall facilities in the Linux kernel provide mechanisms
for accounting IP packets, for building firewalls based on packet-
level filtering, for building firewalls using transparent proxy
servers (by redirecting packets to local sockets), and for
masquerading forwarded packets. The administration of these
functions is maintained in the kernel as a series of separated
table (hereafter referred to as chains) each containing zero or
more rules. There are three builtin chains which are called input,
forward and output which always exist. All other chains are user
defined. A chain is a sequence of rules; each rule contains
specific information about source and destination addresses,
protocols, port numbers, and some other characteristics.
Information about what to do if a packet matches the rule is also
contained. A packet will match with a rule when the
characteristics of the rule match those of the IP packet.
A packet always traverses a chain starting at rule number 1. Each
rule specifies what to do when a packet matches. If a packet does
not match a rule, the next rule in that chain is tried. If the end of
a builtin chain is reached the a default policy for that chain is
returned. If the end of a user defined chain is reached then the
rule after the rule which branched to that chain is tried. The
purpose of the three builtin chains are
Input firewall
9
Project Report on Implementation of Firewall on Linux Platform
10
Project Report on Implementation of Firewall on Linux Platform
replaced by the original address and port number that was saved
when the first packet was masqueraded. This function only
applies to TCP or UDP packets.
There is also a special target RETURN which is equivalent to
falling off the end of the chain.
This paragraph describes the way a packet goes through the
firewall. Packets received via one of the local network interface
will pass the following chains:
input firewall (incoming device) Here, the device (network
interface) that is used when trying to match a rule with an
IP packet is listed between brackets. After this step, a
packet will optionally be redirected to a local socket. When
a packet has to be forwarded to a remote host, it will also
pass the next set of rules: forwarding firewall (outgoing
device) After this step, a packet will optionally be
masqueraded. Responses to masqueraded packets will
never pass the forwarding firewall (but they will pass both
the input and output firewalls). All packets sent via one of
the local network interfaces, either locally generated or
being forwarded, will pass the following sets of rules: output
firewall (outgoing device)
When a packet enters one of the three above chains rules are
traversed from the first rule in order. When analysing a rule one
of three things may occur.
Rule unmatched:
If a rules is unmatched then the next rule in that chain is
analysed. If there are no more rules for that chain the
default policy for that chain is returned (or traversal
continues back at the calling chain, in the case of a user-
defined chain).
Rule matched (with branch to chain):
When a rule is matched by a packet and the rule contains a
branch field then a jump/branch to that chain is made.
Jumps can only be made to user defined chains. As
described above, when the end of a builtin chain is reached
then a default policy is returned. If the end of a used
defined chain is reached then we return to the rule from
whence we came.
11
Project Report on Implementation of Firewall on Linux Platform
12
Project Report on Implementation of Firewall on Linux Platform
13
Project Report on Implementation of Firewall on Linux Platform
14
Project Report on Implementation of Firewall on Linux Platform
STRUCTURES
The ip_fw structure contains the following relevant fields to be
filled in for adding or replacing a rule:
struct in_addr fw_src, fw_dst
Source and destination IP addresses.
struct in_addr fw_smsk, fw_dmsk
Masks for the source and destination IP addresses. Note
that a mask of 0.0.0.0 will result in a match for all hosts.
char fw_vianame[IFNAMSIZ]
Name of the interface via which a packet is received by the
system or is going to be sent by the system. If the option
IP_FW_F_WILDIF is specified, then the fw_vianame need
only match the packet interface up to the first NUL
character in fw_vianame. This allows wildcard-like effects.
The empty string has a special meaning: it will match with
all device names.
__u16 fw_flg
Flags for this rule. The flags for the different options can be
bitwise or'ed with each other.
The options are: IP_FW_F_TCPSYN (only matches with TCP
packets when the SYN bit is set and both the ACK and RST
bits are cleared in the TCP header, invalid with other
protocols), The option IP_FW_F_MARKABS is described
under the fw_mark entry. The option IP_FW_F_PRN can be
15
Project Report on Implementation of Firewall on Linux Platform
16
Project Report on Implementation of Firewall on Linux Platform
17
Project Report on Implementation of Firewall on Linux Platform
RETURN VALUE
On success (or a straightforward packet accept for the CHECK
options), zero is returned. On error, -1 is returned and errno is set
appropriately. See setsockopt(2) for a list of possible error values.
ENOENT indicates that given chain name doesn't exist. When the
check packet command is used, zero is returned when the packet
would be accepted without redirection or masquerading.
Otherwise, -1 is returned and errno is set to ECONNABORTED
(packet would be accepted using redirection), ECONNRESET
(packet would be accepted using masquerading), ETIMEDOUT
(packet would be denied), ECONNREFUSED (packet would be
rejected), ELOOP (packet got into a loop), ENFILE (packet fell
off end of chain (only occurs for used defined chains)).
18
Project Report on Implementation of Firewall on Linux Platform
FIREWALL
1. What is a firewall?
A firewall protects networked computers from intentional
hostile intrusion that could compromise confidentiality or
result in data corruption or denial of service. It may be a
hardware device or a software program running on a secure
host computer. In either case, it must have at least two
network interfaces, one for the network it is intended to
protect, and one for the network it is exposed to. A firewall
sits at the junction point or gateway between the two
networks, usually a private network and a public network
such as the Internet. The earliest firewalls were simply
routers. The term firewall comes from the fact that by
segmenting a network into different physical subnetworks,
they limited the damage that could spread from one subnet
to another just like firedoors or firewalls.
19
Project Report on Implementation of Firewall on Linux Platform
20
Project Report on Implementation of Firewall on Linux Platform
21
Project Report on Implementation of Firewall on Linux Platform
22
Project Report on Implementation of Firewall on Linux Platform
23
Project Report on Implementation of Firewall on Linux Platform
24
Project Report on Implementation of Firewall on Linux Platform
Fwall.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <linux/if.h>
#include <linux/ip_fw.h>
#include <netdb.h>
#include <string.h>
#define MAXPORT 60000
struct ip_fwnew fwnew;
struct ip_fwchange fwappend;
struct ip_fwdelnum fwdelnum;
#define TOKENLEN 20
#define RULELEN 500
unsigned int lineno,src,dst,srcmsk,dstmsk,port,in_no,out_no,for_no,pos;
char token[TOKENLEN],rule[RULELEN];
struct protoent *proto;
struct servent *servent;
struct hostent *hostent;
struct ip_fwpolicy fwpolicy;
char protoname[20];
int getvalue(char *str,long *val)
{
char *endptr;
*val = strtol(str,&endptr,10);
if(*endptr != '\0')
return(0);
return(1);
}
int gettoken()
{
int i;
for(;rule[pos] == ' ' || rule[pos] == '\t';pos++);
if(rule[pos] == '\0' || rule[pos] == '\n')
return(0);
for(i = 0;rule[pos] != ' ' && rule[pos] != '\t' && rule[pos] != '\n'&& rule[pos] !
= '\0';pos++)
token[i++] = rule[pos];
token[i] = '\0';
return(1);
}
25
Project Report on Implementation of Firewall on Linux Platform
void initialise(void)
{
fwappend.fwc_rule.ipfw.fw_mark = 0;
fwappend.fwc_rule.ipfw.fw_flg = IP_FW_F_WILDIF;
fwappend.fwc_rule.ipfw.fw_invflg = 0;
fwappend.fwc_rule.ipfw.fw_vianame[0] = '\0';
fwappend.fwc_rule.ipfw.fw_tosand = 0xff;
fwappend.fwc_rule.ipfw.fw_tosxor = 0;
fwappend.fwc_rule.ipfw.fw_proto = 0;
fwappend.fwc_rule.ipfw.fw_outputsize = 0;
fwappend.fwc_rule.ipfw.fw_spts[0] = 0;
fwappend.fwc_rule.ipfw.fw_spts[1] = 0xffff;
fwappend.fwc_rule.ipfw.fw_dpts[0] = 0;
fwappend.fwc_rule.ipfw.fw_dpts[1] = 0xffff;
fwappend.fwc_rule.ipfw.fw_src.s_addr = 0;
fwappend.fwc_rule.ipfw.fw_dst.s_addr = 0;
fwappend.fwc_rule.ipfw.fw_smsk.s_addr = 0;
fwappend.fwc_rule.ipfw.fw_dmsk.s_addr = 0;
protoname[0] = '\0';
}
int getports(__u16 ports[2])
{
char *endptr;
int count;
gettoken();
if(token[0] >= 'a' && token[0] <= 'z'){
if(protoname[0] == '\0')
strcpy(protoname,"tcp");
servent = getservbyname(token,protoname);
if(servent == NULL){
printf("Invalid service name %s in line %d col
%d\n",token,lineno,pos);
return(0);
}
ports[0] = ports[1] = ntohs(servent->s_port);
return(1);
}
count = strtol(token,&endptr,10);
if(*endptr == '\0'){
ports[0] = ports[1] = count;
return(1);
}
else if(*endptr == '.' && endptr[1] == '.'){
endptr += 2;
ports[0] = count;
count = strtol(endptr,&endptr,10);
if(*endptr != '\0'){
printf("invalid range at line %d col %d",lineno,pos);
return(0);
}
ports[1] = count;
}
else{
printf(" Expected [!][port[..port]] in line %d col %d\n",lineno,pos);
return(0);
26
Project Report on Implementation of Firewall on Linux Platform
}
return(1);
}
int getsord(struct in_addr *sd,struct in_addr *sdmsk)
{
char src[20],msk[20];
int count,i;
__u32 flag;
char *endptr;
gettoken();
sdmsk->s_addr = 0xffffffff;
for(i = 0; token[i] != '/' && token[i] != '\0' ;i++)
src[i] = token[i];
src[i] = '\0';
hostent = gethostbyname(src);
if(hostent == NULL){
printf(" Host %s was not found in line %d \n",src,lineno);
return(0);
}
else{
sd->s_addr = hostent->h_addr_list[0];
}
if(token[i] == '/'){
i++;
strcpy(msk,&token[i]);
count = strtol(msk,&endptr,10);
if(*endptr == '\0'){
flag = 0x1;
sdmsk->s_addr = 0;
for( ; count >0;count--,flag <<= 1){
sdmsk->s_addr |= flag;
}
}
else{
if(inet_aton(msk,sdmsk) == 0){
printf(" Error in line no %d col %d invalid address
mask\n",lineno,pos);
perror("");
return(0);
}
}
}
return(1);
}
int appendrule(void)
{
gettoken();
strcpy(fwappend.fwc_label,token);
while(1){
if(gettoken() == 0) return(1);
if(strcmp(token,"-p") == 0){
gettoken();
strcpy(protoname,token);
proto = getprotobyname(token);
if(proto == NULL){
27
Project Report on Implementation of Firewall on Linux Platform
getvalue(token,&fwappend.fwc_rule.ipfw.fw_redirpt);
}
}
}
else if(strcmp(token,"-f") == 0)
fwappend.fwc_rule.ipfw.fw_flg |= IP_FW_F_FRAG;
else if(strcmp(token,"!-f") == 0)
fwappend.fwc_rule.ipfw.fw_invflg |= IP_FW_INV_FRAG;
else if(strcmp(token,"-y") == 0)
fwappend.fwc_rule.ipfw.fw_flg |= IP_FW_F_TCPSYN;
else if(strcmp(token,"!-y") == 0)
fwappend.fwc_rule.ipfw.fw_invflg |= IP_FW_INV_SYN;
else if(strcmp(token,"-m") == 0){
gettoken();
if(getvalue(token,(long *)&fwappend.fwc_rule.ipfw.fw_mark) ==
0){
28
Project Report on Implementation of Firewall on Linux Platform
29
Project Report on Implementation of Firewall on Linux Platform
strcpy(fname,"fw.conf");
lineno = 0;
in_no = 1;
out_no = 1;
for_no = 1;
sockfd = socket(AF_INET,SOCK_STREAM,0);
fp = fopen(fname,"r");
if(fp == NULL){
printf(" File %s not found \n",fname);
exit(0);
}
setsockopt(sockfd,IPPROTO_IP,IP_FW_FLUSH,"input",9);
setsockopt(sockfd,IPPROTO_IP,IP_FW_FLUSH,"output",9);
setsockopt(sockfd,IPPROTO_IP,IP_FW_FLUSH,"forward",9);
while(fgets(rule,RULELEN,fp) != NULL){
lineno++;
pos = 0;
initialise();
if(gettoken() == 0)
continue;
if(strcmp(token,"#") == 0)
continue;
if(strcmp(token,"-A") == 0){
if(appendrule() == 1){
if( setsockopt(sockfd,IPPROTO_IP,IP_FW_APPEND,&fwappend,sizeof(fwappend)) != 0)
{
printf("Error setting socket option in line %d :",lineno);
perror("");
}
}
}
else if(strcmp(token,"-N") == 0){
gettoken();
if(setsockopt(sockfd,IPPROTO_IP,IP_FW_CREATECHAIN,token,9)
< 0){
printf("Error setting socket option in line %d :",lineno);
perror("");
}
}
else if(strcmp(token,"-P") == 0){
gettoken();
strcpy(fwpolicy.fwp_label,token);
gettoken();
strcpy(fwpolicy.fwp_policy,token);
if(setsockopt(sockfd,IPPROTO_IP,IP_FW_POLICY,&fwpolicy,sizeof(fwpolicy)) <
0){
printf(" Error setting policy of line %d",lineno);
perror("");
}
}
else
printf(" Error in line %d col %d invalid command %s
",lineno,pos,token);
30
Project Report on Implementation of Firewall on Linux Platform
}
}
31
Project Report on Implementation of Firewall on Linux Platform
fw.conf
32
Project Report on Implementation of Firewall on Linux Platform
33