Pages

Monday, October 18, 2010

Lab 10

/*****************************This code generates a bogus ethernet packet and sends it on the interface of your choice ********
******************************Compiling the code -> $g++ -o aha eth.cpp -lpcap  *****************************************************
******************************Running the code -> $sudo ./aha  ***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <pcap.h>
#include <iostream>
using namespace std;
 
 
 
struct ETHER_HDR
{
    unsigned char d_mac[6];
    unsigned char s_mac[6];
    unsigned char typecode[2];
} *P_EH;
 
 
 
struct ARP_HDR
{
  //complete this struct
};
 
 
 
 
/*    For future reference and for the bold who may wish to experiment
struct IP_HDR 
{
unsigned char  ip_ver_ihl;    // 4-bit IPv4 version   
        // 4-bit header length (in 32-bit words) 
        //    normally=5 Means 20 Bytes may be 24  
unsigned char  ip_tos;    // IP type of service    
unsigned short ip_total_length;        // Total length    
unsigned short ip_id;    // Unique identifier     
unsigned short ip_flag_frag_off;        // Flags & Fragment offset field          
unsigned char  ip_ttl;    // Time to live    
unsigned char  ip_protocol;// Protocol(TCP,UDP etc)    
unsigned short ip_checksum;// IP checksum    
unsigned int   ip_srcaddr;    // Source address    
unsigned int   ip_destaddr;// Source address
} *P_IPV4;
*/
//void prep_ip_hdr (u_char* pkt, IP_HDR* iph);
 
 
//Function Prototypes
 
void prep_ether_hdr(u_char* pkt, ETHER_HDR* eh);
void prep_ARP_hdr(u_char* pkt, ARP_HDR* eh);
 
 
 
int main ()
{
  
  ETHER_HDR* eh;
  //IP_HDR* iph;
  pcap_if_t *alldevs;
  pcap_if_t *d;
  int i=0;
  char errbuf[PCAP_ERRBUF_SIZE];
  pcap_t * fp;
 
  /* Retrieve the device list from the local machine */
  if (pcap_findalldevs(&alldevs, errbuf) == -1) 
  {
    cerr<<"Error in pcap_findalldevs "<<errbuf;
    exit(1);
  }
 
  /* Print the list */
  for(d= alldevs; d != NULL; d= d->next)
  {
    cout<<++i<<" "<<d->name;
    if (d->description)
      cout<<d->description<<endl;
    else
      cout<< "(No description available)\n";
    }
 
  if (i == 0)
  {
    cout<<"\nNo interfaces found! Make sure Pcap is installed.\n";
    return 1;
  }
 
 
  cout<<"Enter the interface number"<< (1-i);     
  int inum; 
  cin>>inum;
 
  if(inum < 1 || inum > i)
  {
    cout<<"\nInterface number out of range.\n";
    /* Free the device list */
    pcap_freealldevs(alldevs);
    return 1;
  }
 
  for (d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
 
  fp = pcap_open_live(d->name, 65536,1,1000,errbuf);  //open the device to enable sending the packet
  u_char packet[100];
 
  eh=new ETHER_HDR;
  eh->d_mac[0]=255;
  eh->d_mac[1]=255;
  eh->d_mac[2]=255;
  eh->d_mac[3]=255;
  eh->d_mac[4]=255;
  eh->d_mac[5]=255;
  eh->s_mac[0]=2;
  eh->s_mac[1]=2;
  eh->s_mac[2]=2;
  eh->s_mac[3]=2;
  eh->s_mac[4]=2;
  eh->s_mac[5]=2;
  //the following will cause the payload to be interpreted as an IP protocol packet,this is the Type Code for IP 0x0800 is the type code for IP
  eh->typecode[0] = 0x08;
  eh->typecode[1] = 0x00;
  prep_ether_hdr(packet, eh);
  for (int i=14;i<100;i++)
    packet[i]=i;
 
//iph = (struct IP_HDR*) (packet + sizeof(ETHER_HDR));
 
/*
iph=new IP_HDR;
 
iph->ip_ver_ihl = 0x45;
iph->ip_tos = 0;
iph->ip_total_length = 100 - sizeof(ETHER_HDR);
iph->ip_id = 2;
iph->ip_flag_frag_off = htons(0x4000); // 010 0000000000000 = 0 1000 0000 
// 0000 0000 = 0x4000
iph->ip_ttl = 3;
iph->ip_protocol = 0x01;
iph->ip_checksum = 0;
iph->ip_srcaddr = inet_addr("192.168.1.3"); // change IP as needed
iph->ip_destaddr = inet_addr("192.168.1.2");// change IP as needed
 
 
//prep_ip_hdr(packet, iph);
 
*/
 
/* Send down the packet */
 
 
 
  if (pcap_sendpacket(fp, packet, 100 /* size */) != 0)
  {
    cerr<<"\nError sending the packet: \n"<<pcap_geterr(fp);
    return 0;
  }
 
}
 
 
 
void prep_ether_hdr(u_char* pkt, ETHER_HDR* eh) 
{
  memcpy((pkt), eh->d_mac,6);
  memcpy((pkt+6), eh->s_mac,6);
  memcpy((pkt+12), &(eh->typecode),2);     
}
 
void prep_ARP_hdr(u_char* pkt, ARP_HDR* eh) 
{
  //complete this fuction     
}
 
 
 
 
//void prep_ip_hdr (u_char* pkt, IP_HDR* iph)
//{
// COMPLETE THIS FUNCTION
//}
 
 
 
 
 
 
 
 
 
 
 

No comments:

Post a Comment