Thursday, 26 September 2013

Raw Socket Application 2


We can do spoofing using row socket . we can take the ip packet and change the ip address so receiver will think that packet came from some one else. This concept is widely used for hacking a network . Program one get message and change IP header

1. Program 1 : It get message from Program 3 and modify Ip header and send to program 2 . program 2 think packet is coming from program 1
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/select.h>
#include<sys/un.h>
#include<netinet/ip.h>
#include<netinet/udp.h>
unsigned short csum (unsigned short *buf, int nwords)
{
 unsigned long sum;
 for (sum = 0; nwords > 0; nwords--)
  sum += *buf++;
 sum = (sum >> 16) + (sum & 0xffff);
 sum += (sum >> 16);
 return ~sum;
}


int main()
{
 int rsfd=socket(AF_INET,SOCK_RAW,IPPROTO_UDP);
 char buf[4096]={0};
 char msg[1000]={0};
 struct ip* ipheader=(struct ip*)buf;
 ipheader->ip_hl=5;
 ipheader->ip_v=4;
 ipheader->ip_tos=0;
 ipheader->ip_len=20+8+sizeof(msg);
 ipheader->ip_id=0;
 ipheader->ip_off=0;
 ipheader->ip_ttl=64;
 ipheader->ip_p=17;
 inet_pton(AF_INET,"127.0.0.1",&(ipheader->ip_src));
 inet_pton(AF_INET,"127.0.0.1",&(ipheader->ip_dst));
 ipheader->ip_sum=csum((unsigned short*)buf,9);

     int k=1;
     const int *l = &k;
     if (setsockopt (rsfd, IPPROTO_IP, IP_HDRINCL ,l, sizeof (k)) < 0)
        printf ("ERROR IN HDRINCL!\n");
      
 struct udphdr *udpheader=(struct udphdr*)(buf+ipheader->ip_hl*4);

 udpheader->source=htons(3400);
 udpheader->dest=htons(2300);
 udpheader->len=htons(8+sizeof(msg));
 udpheader->check=0;
 memcpy(buf+20,udpheader,8);

 struct sockaddr_in addr;
 addr.sin_port=htons(2300);
 addr.sin_family=AF_INET;
 inet_pton(AF_INET,"127.0.0.1",&(addr.sin_addr));
 while(1)
 {
  fgets(msg,1000,stdin);
  strcpy(buf+28,msg);
  sendto(rsfd,buf,4096,0,(struct sockaddr*) &addr,sizeof addr);
 }

 return 0;
}



2. program 2
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/stat.h>
#include<signal.h>
#include<sys/sem.h>
#include<pthread.h>

int sfd;
struct sockaddr_in serv;
void* rcv()
{
 struct sockaddr_in clt;
 int length;
char buf[512];
while(1)
{
recvfrom(sfd,buf,512,0,(struct sockaddr*)&clt,&length);
printf("received msg is:%s\n",buf);
}
}

int main()
{
sfd=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in myaddr;
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(3400);
myaddr.sin_addr.s_addr=INADDR_ANY;
int b=bind(sfd,(struct sockaddr*)&myaddr,sizeof(struct sockaddr_in));
if(b==-1)
perror("bind error\n"); 



serv.sin_family=AF_INET;
serv.sin_port=htons(2300);
inet_pton(AF_INET,"127.0.0.1",&serv.sin_addr);
pthread_t t;
pthread_create(&t,NULL,&rcv,NULL);
while(1)
{
char buf[512]={0};
fgets(buf,512,stdin);
sendto(sfd,buf,512,0,(struct sockaddr*)&serv,sizeof serv);
}
return 0;
}




3. program 3
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/select.h>
#include<sys/un.h>
#include<netinet/ip.h>
#include<netinet/udp.h>

int main()
{
int usfd=socket(AF_INET,SOCK_DGRAM,0);
if(usfd==-1)
perror("sock error\n");
struct sockaddr_in servaddr,clientaddr;
socklen_t length=sizeof(struct sockaddr_in);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(2300);
servaddr.sin_addr.s_addr=INADDR_ANY;
int b=bind(usfd,(struct sockaddr*)&servaddr,sizeof(struct sockaddr_in));
if(b<0)
perror("Bind error\n");

while(1)
{
char msg[512]={0}; 
recvfrom(usfd,msg,512,0,(struct sockaddr*)&clientaddr,&length);
printf(" msg received from client of port id %d\n",htons(clientaddr.sin_port));
printf("msg received is:%s",msg);
printf("enter msg to send \n");
gets(msg);
sendto(usfd,msg,512,0,(struct sockaddr*)&clientaddr,length);
 perror("send");
}
return 0;
}


No comments:

Post a Comment