Question: outputTask 2 . 2 B: Spoof an ICMP Echo Request. Spoof an ICMP echo request packet on behalf of another machine ( i . e

outputTask 2.2B: Spoof an ICMP Echo Request. Spoof an ICMP echo request packet on behalf of another
machine (i.e., using another machine's IP address as its source IP address). This packet should be sent to a
remote machine on the Internet (the machine must be alive). You should turn on your Wireshark, so if your
spoofing is successful, you can see the echo reply coming back from the remote machine.
Questions. Please answer the following questions.
Question 4. Can you set the IP packet length field to an arbitrary value, regardless of how big the
actual packet is?
Question 5. Using the raw socket programming, do you have to calculate the checksum for the IP
header?
Question 6. Why do you need the root privilege to run the programs that use raw sockets? Where
does the program fail if executed without the root privilege? please write the code for icmp echo and answer question the spoofing code #include
#include
#include
#include
#include
#include
#include
// Note: This code is for educational or testing purposes only and should not be used maliciously.
// The IP header's structure
struct ipheader {
unsigned char iph_ihl:4, iph_ver:4; // Adjusted the bit fields
unsigned char iph_tos;
unsigned short int iph_len;
unsigned short int iph_ident;
unsigned short int iph_flags_offset; // Changed name and type for flags and offset
unsigned char iph_ttl;
unsigned char iph_protocol;
unsigned short int iph_chksum;
unsigned int iph_sourceip;
unsigned int iph_destip;
};
// Simple checksum function, might be needed for some headers
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 (unsigned short)(~sum);
}
int main(){
int sd;
char buffer[4096];
struct ipheader *ip =(struct ipheader *) buffer;
char *data = buffer + sizeof(struct ipheader); // Pointer to the data portion after the IP header
// Create a raw socket with IP protocol. The IPPROTO_RAW parameter tells the system
sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (sd 0){
perror("socket() error");
exit(-1);
}
// Set the fields in the IP header
ip->iph_ver =4; // Version (4 bits): IPv4
ip->iph_ihl =5; // IHL (4 bits): Number of 32-bit words in header
ip->iph_tos =0; // Type of service
// Include the length of IP header and message in total length
ip->iph_len = htons(sizeof(struct ipheader)+ strlen(data));
ip->iph_ident = htons(54321); // Identification
ip->iph_flags_offset = htons(0x4000); // Don't fragment flag, 0 offset
ip->iph_ttl =255; // Time to live
ip->iph_protocol = IPPROTO_TCP; // Protocol
ip->iph_chksum =0; // Checksum (temporarily set to 0)
// Source IP address, can be spoofed
ip->iph_sourceip = inet_addr("1.2.3.4");
// Destination IP address
ip->iph_destip = inet_addr("10.9.0.6");
// Copy the message into the buffer after the IP header
strcpy(data, "Hello");
// Calculate the IP checksum now that the header is filled out
ip->iph_chksum = csum((unsigned short *)buffer, sizeof(struct ipheader));
// Inform the kernel that headers are included in the packet
int one =1;
const int *val = &one;
if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one))0){
perror("setsockopt() error");
exit(-1);
}
// Destination address structure
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(80); // Port number if needed
sin.sin_addr.s_addr = ip->iph_destip;
// Send the packet
if (sendto(sd, buffer, ntohs(ip->iph_len),0,(struct sockaddr *)&sin, sizeof(sin))0){
perror("sendto() error");
exit(-1);
}
close(sd);
return 0;
}
outputTask 2 . 2 B: Spoof an ICMP Echo Request.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!