#include "my_proto_headers.h"
#define ETH_FRAME_LENX 98
#define ETH_HDRLEN 14 // Ethernet header length
#define IP4_HDRLEN 20 // IPv4 header length
#define ICMP_HDRLEN 8 // ICMP header length for echo request, excludes data
extern char sourceip[30];
extern char destinip[30];
extern int type_replyc;//important field to check before modifing
extern int type_reqc;//important field to check before modifing
extern int seq_num; //important field to check before modifing
extern int smac[6];
extern int dmac[6];
extern int icmp_typec;
extern int icmp_chksumc;
extern int icmp_idc;
extern int proto_used;
extern char datagram[8096];
extern int datalen;
extern int iptotlen;
extern int iptos;
extern int ipfrag;
extern int sizeofdata;
extern int eth_prototype;
//FILE *fp;
//grandson variables
extern char sourceipxx[30];
extern char destinipxx[30];
extern int type_replycxx;//important field to check before modifing
extern int type_reqcxx;//important field to check before modifing
extern int seq_numxx; //important field to check before modifing
extern int smacxx[6];
extern int dmacxx[6];
extern int icmp_typecxx;
extern int icmp_chksumcxx;
extern int icmp_idcxx;
extern int proto_usedxx;
extern char datagramxx[8096];
extern int datalenxx;
extern int sizeofdataxx;
extern int eth_prototypexx;
extern int icmp_seqxx;
extern int imp_flag;
//.................
//for getting interface_index number
extern int interface_index ;
char * the_fucker();
char *dup_ip;
//USING RAW SOCKET: FOR SENDING THE NEW MODIFIED PACKET
//.....................................
void create_modi_reply_pack()
{
int rd2;
int minus;
char copy_data[1000];
printf("IN CREATE_MODIFIED _ REPLY_PACKET \n");
rd2=socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
int i,j;
int xsize;
// fp=fopen("/home/sourav/Desktop/table","r");
/*
printf("*****destinip: %s \n",destinip);
printf("*****sourceip: %s \n",sourceip);
printf("icmp_typec :%d \n",icmp_typec);
*/
void *send_buffer=(void*)malloc(ETH_FRAME_LENX);//for creation of modified packet
// unsigned char* etherhead = send_buffer + 14; /*userdata in ethernet frame*/
struct sockaddr_ll socket_address;
//constructing thernet header
struct ethhdr *eth2 = (struct ethhdr *)send_buffer;
eth2->h_source[0]=dmac[0];
eth2->h_source[1]=dmac[1];
eth2->h_source[2]=dmac[2];
eth2->h_source[3]=dmac[3];
eth2->h_source[4]=dmac[4];
eth2->h_source[5]=dmac[5];
// for(i=0,j=0;i<6,j<6;i++,j++)
//{
eth2->h_dest[0]=smacxx[0];
eth2->h_dest[1]=smacxx[1];
eth2->h_dest[2]=smacxx[2];
eth2->h_dest[3]=smacxx[3];
eth2->h_dest[4]=smacxx[4];
eth2->h_dest[5]=smacxx[5];
//}
eth2->h_proto=eth_prototype;
//socket_address********************************************************************************************************
socket_address.sll_family = PF_PACKET;
/*we don't use a protocoll above ethernet layer
->just use anything here*/
socket_address.sll_protocol = htons(ETH_P_IP);
socket_address.sll_ifindex = interface_index;
/*ARP hardware identifier is ethernet*/
socket_address.sll_hatype = 0;
/*target is another host*/
socket_address.sll_pkttype = 0;
/*address length*/
socket_address.sll_halen = ETH_ALEN;
/*MAC - begin*/
socket_address.sll_addr[0] = smacxx[0];
socket_address.sll_addr[1] = smacxx[1];
socket_address.sll_addr[2] = smacxx[2];
socket_address.sll_addr[3] = smacxx[3];
socket_address.sll_addr[4] = smacxx[4];
socket_address.sll_addr[5] = smacxx[5];
/*MAC - end*/
socket_address.sll_addr[6] = 0x00;/*not used*/
socket_address.sll_addr[7] = 0x00;/*not used*/
//******************************************************************************************
//printf("source address : %.x-%.x-%.x-%.x-%.x-%.x \n",eth2->h_source[0],eth2->h_source[1],eth2->h_source[2],eth2->h_source[3],eth2->h_source[4],eth2->h_source[5]);
//printf("destination address : %.x-%.x-%.x-%.x-%.x-%.x \n", eth2->h_dest[0] ,eth2->h_dest[1],eth2->h_dest[2],eth2->h_dest[3],eth2->h_dest[4],eth2->h_dest[5]);
struct iphdr *iph2 = (struct iphdr *)(send_buffer + sizeof(struct ethhdr));
//constructiing new ip header
iph2->version=4;
iph2->ihl=5;
iph2->tos=iptos;
iph2->ttl=64;
iph2->tot_len=sizeof(struct iphdr)+sizeof(struct icmpheader);
iph2->id=htons(54321);
iph2->frag_off=0x00;
//iph2->ident=htons(54321);
iph2->protocol=1;//for icmp
printf("ok 0 \n");
// dup_ip=the_fucker() ;
// printf("dup_ip: %s \n",dup_ip);
iph2->saddr=inet_addr(sourceipxx);//source add changed
iph2->daddr=inet_addr(destinipxx);
iph2->check=in_cksum((unsigned short *)iph2, sizeof(struct iphdr));
printf("ok 2 \n");
struct icmpheader *icmphdr2=(struct icmpheader *)(send_buffer+sizeof(struct iphdr)+sizeof(struct ethhdr));
//constructing the icmp header
icmphdr2->icmp_code=0;
icmphdr2->icmp_id=icmp_idc;
icmphdr2->icmp_seq=icmp_seqxx+2;
icmphdr2->icmp_type= 0; //icmp_typec;
icmphdr2->icmp_cksum= 0;
xsize=sizeof(struct icmpheader)+sizeofdata;
//copying the older datagram
minus=(sizeof(struct icmpheader)+sizeof(struct iphdr)+sizeof( struct ethhdr));
bzero(copy_data,1000);
printf("datagram in create_modi_pack reply: %s : \n",datagram);
strcpy(copy_data,datagram);
strcpy(send_buffer+datalen,copy_data);
icmphdr2->icmp_cksum=in_cksum((unsigned short *)icmphdr2, xsize);
//sending this packet through the second socket
if(sendto(rd2, send_buffer, ETH_FRAME_LENX, 0, (struct sockaddr *)&socket_address, sizeof(socket_address)) < 0)
{
printf("ok 3 \n");
perror("sendto");
}
else
{
printf("server %d :\n",socket_address.sll_ifindex);
printf("send successs");
}
close(rd2);
}
/*the fucker function
char * the_fucker()
{
int offset;
char * ip_string;
char *str_chr;
if(fp<= 0)
{
perror("fopen");
}
else
printf("file got: fp= %d \n",fp);
if(fscanf(fp,"%s",ip_string))
{
printf("yes \n");
printf("%s \n",ip_string);
return ip_string;
}
else
printf("fscanf failed \n");
fclose(fp);
return 0;
}
*/
#define ETH_FRAME_LENX 98
#define ETH_HDRLEN 14 // Ethernet header length
#define IP4_HDRLEN 20 // IPv4 header length
#define ICMP_HDRLEN 8 // ICMP header length for echo request, excludes data
extern char sourceip[30];
extern char destinip[30];
extern int type_replyc;//important field to check before modifing
extern int type_reqc;//important field to check before modifing
extern int seq_num; //important field to check before modifing
extern int smac[6];
extern int dmac[6];
extern int icmp_typec;
extern int icmp_chksumc;
extern int icmp_idc;
extern int proto_used;
extern char datagram[8096];
extern int datalen;
extern int iptotlen;
extern int iptos;
extern int ipfrag;
extern int sizeofdata;
extern int eth_prototype;
//FILE *fp;
//grandson variables
extern char sourceipxx[30];
extern char destinipxx[30];
extern int type_replycxx;//important field to check before modifing
extern int type_reqcxx;//important field to check before modifing
extern int seq_numxx; //important field to check before modifing
extern int smacxx[6];
extern int dmacxx[6];
extern int icmp_typecxx;
extern int icmp_chksumcxx;
extern int icmp_idcxx;
extern int proto_usedxx;
extern char datagramxx[8096];
extern int datalenxx;
extern int sizeofdataxx;
extern int eth_prototypexx;
extern int icmp_seqxx;
extern int imp_flag;
//.................
//for getting interface_index number
extern int interface_index ;
char * the_fucker();
char *dup_ip;
//USING RAW SOCKET: FOR SENDING THE NEW MODIFIED PACKET
//.....................................
void create_modi_reply_pack()
{
int rd2;
int minus;
char copy_data[1000];
printf("IN CREATE_MODIFIED _ REPLY_PACKET \n");
rd2=socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
int i,j;
int xsize;
// fp=fopen("/home/sourav/Desktop/table","r");
/*
printf("*****destinip: %s \n",destinip);
printf("*****sourceip: %s \n",sourceip);
printf("icmp_typec :%d \n",icmp_typec);
*/
void *send_buffer=(void*)malloc(ETH_FRAME_LENX);//for creation of modified packet
// unsigned char* etherhead = send_buffer + 14; /*userdata in ethernet frame*/
struct sockaddr_ll socket_address;
//constructing thernet header
struct ethhdr *eth2 = (struct ethhdr *)send_buffer;
eth2->h_source[0]=dmac[0];
eth2->h_source[1]=dmac[1];
eth2->h_source[2]=dmac[2];
eth2->h_source[3]=dmac[3];
eth2->h_source[4]=dmac[4];
eth2->h_source[5]=dmac[5];
// for(i=0,j=0;i<6,j<6;i++,j++)
//{
eth2->h_dest[0]=smacxx[0];
eth2->h_dest[1]=smacxx[1];
eth2->h_dest[2]=smacxx[2];
eth2->h_dest[3]=smacxx[3];
eth2->h_dest[4]=smacxx[4];
eth2->h_dest[5]=smacxx[5];
//}
eth2->h_proto=eth_prototype;
//socket_address********************************************************************************************************
socket_address.sll_family = PF_PACKET;
/*we don't use a protocoll above ethernet layer
->just use anything here*/
socket_address.sll_protocol = htons(ETH_P_IP);
socket_address.sll_ifindex = interface_index;
/*ARP hardware identifier is ethernet*/
socket_address.sll_hatype = 0;
/*target is another host*/
socket_address.sll_pkttype = 0;
/*address length*/
socket_address.sll_halen = ETH_ALEN;
/*MAC - begin*/
socket_address.sll_addr[0] = smacxx[0];
socket_address.sll_addr[1] = smacxx[1];
socket_address.sll_addr[2] = smacxx[2];
socket_address.sll_addr[3] = smacxx[3];
socket_address.sll_addr[4] = smacxx[4];
socket_address.sll_addr[5] = smacxx[5];
/*MAC - end*/
socket_address.sll_addr[6] = 0x00;/*not used*/
socket_address.sll_addr[7] = 0x00;/*not used*/
//******************************************************************************************
//printf("source address : %.x-%.x-%.x-%.x-%.x-%.x \n",eth2->h_source[0],eth2->h_source[1],eth2->h_source[2],eth2->h_source[3],eth2->h_source[4],eth2->h_source[5]);
//printf("destination address : %.x-%.x-%.x-%.x-%.x-%.x \n", eth2->h_dest[0] ,eth2->h_dest[1],eth2->h_dest[2],eth2->h_dest[3],eth2->h_dest[4],eth2->h_dest[5]);
struct iphdr *iph2 = (struct iphdr *)(send_buffer + sizeof(struct ethhdr));
//constructiing new ip header
iph2->version=4;
iph2->ihl=5;
iph2->tos=iptos;
iph2->ttl=64;
iph2->tot_len=sizeof(struct iphdr)+sizeof(struct icmpheader);
iph2->id=htons(54321);
iph2->frag_off=0x00;
//iph2->ident=htons(54321);
iph2->protocol=1;//for icmp
printf("ok 0 \n");
// dup_ip=the_fucker() ;
// printf("dup_ip: %s \n",dup_ip);
iph2->saddr=inet_addr(sourceipxx);//source add changed
iph2->daddr=inet_addr(destinipxx);
iph2->check=in_cksum((unsigned short *)iph2, sizeof(struct iphdr));
printf("ok 2 \n");
struct icmpheader *icmphdr2=(struct icmpheader *)(send_buffer+sizeof(struct iphdr)+sizeof(struct ethhdr));
//constructing the icmp header
icmphdr2->icmp_code=0;
icmphdr2->icmp_id=icmp_idc;
icmphdr2->icmp_seq=icmp_seqxx+2;
icmphdr2->icmp_type= 0; //icmp_typec;
icmphdr2->icmp_cksum= 0;
xsize=sizeof(struct icmpheader)+sizeofdata;
//copying the older datagram
minus=(sizeof(struct icmpheader)+sizeof(struct iphdr)+sizeof( struct ethhdr));
bzero(copy_data,1000);
printf("datagram in create_modi_pack reply: %s : \n",datagram);
strcpy(copy_data,datagram);
strcpy(send_buffer+datalen,copy_data);
icmphdr2->icmp_cksum=in_cksum((unsigned short *)icmphdr2, xsize);
//sending this packet through the second socket
if(sendto(rd2, send_buffer, ETH_FRAME_LENX, 0, (struct sockaddr *)&socket_address, sizeof(socket_address)) < 0)
{
printf("ok 3 \n");
perror("sendto");
}
else
{
printf("server %d :\n",socket_address.sll_ifindex);
printf("send successs");
}
close(rd2);
}
/*the fucker function
char * the_fucker()
{
int offset;
char * ip_string;
char *str_chr;
if(fp<= 0)
{
perror("fopen");
}
else
printf("file got: fp= %d \n",fp);
if(fscanf(fp,"%s",ip_string))
{
printf("yes \n");
printf("%s \n",ip_string);
return ip_string;
}
else
printf("fscanf failed \n");
fclose(fp);
return 0;
}
*/