-
Notifications
You must be signed in to change notification settings - Fork 2
/
send_raw_ethernet.c
154 lines (134 loc) · 4.62 KB
/
send_raw_ethernet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Original version from Austin Martin:
* https://gist.github.com/austinmarton/1922600
*
* Adapted by Paul Craven
*/
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <unistd.h>
/* ALERT: Replace the bytes below with the MAC address
you are sending to.
So if 'ifconfig' on the RECEIVER (not the sender) says:
wlan0 Link encap:Ethernet HWaddr b8:27:eb:44:08:62
You'd replace the bytes below with:
#define DEST_MAC0 0xB8
#define DEST_MAC1 0x27
#define DEST_MAC2 0xEB
#define DEST_MAC3 0x44
#define DEST_MAC4 0x08
#define DEST_MAC5 0x62
*/
#define DEST_MAC0 0xB8
#define DEST_MAC1 0x27
#define DEST_MAC2 0xEB
#define DEST_MAC3 0x44
#define DEST_MAC4 0x08
#define DEST_MAC5 0x62
/* ALERT: Update the field below to specify what network
adapter should we send data through. */
#define DEFAULT_IF "wlan0"
#define BUF_SIZ 1024
int main(int argc, char *argv[])
{
int sockfd;
int i;
struct ifreq if_idx;
struct ifreq if_mac;
int tx_len;
char sendbuf[BUF_SIZ];
struct sockaddr_ll socket_address;
char ifName[IFNAMSIZ];
/* Get interface name */
if (argc > 1)
strcpy(ifName, argv[1]);
else
strcpy(ifName, DEFAULT_IF);
/* Open RAW socket to send on */
if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("socket");
}
/* Get the index of the interface to send on */
memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, ifName, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0)
perror("SIOCGIFINDEX");
/* Get the MAC address of the interface to send on */
memset(&if_mac, 0, sizeof(struct ifreq));
strncpy(if_mac.ifr_name, ifName, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0)
perror("SIOCGIFHWADDR");
// Loop forever
while(1) {
/* Buffer of BUF_SIZ bytes we'll construct our frame in.
First, clear it all to zero. */
memset(sendbuf, 0, BUF_SIZ);
tx_len = 0;
/* Construct the Ethernet header */
/* Ethernet header */
/* Destination address */
sendbuf[tx_len++] = DEST_MAC0;
sendbuf[tx_len++] = DEST_MAC1;
sendbuf[tx_len++] = DEST_MAC2;
sendbuf[tx_len++] = DEST_MAC3;
sendbuf[tx_len++] = DEST_MAC4;
sendbuf[tx_len++] = DEST_MAC5;
/* Create the source */
sendbuf[tx_len++] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[0];
sendbuf[tx_len++] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[1];
sendbuf[tx_len++] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[2];
sendbuf[tx_len++] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[3];
sendbuf[tx_len++] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[4];
sendbuf[tx_len++] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[5];
/* Ethertype field */
sendbuf[tx_len++] = 0x08;
sendbuf[tx_len++] = 0x00;
/*
UPDATE: Packet data
This is the 'payload'. Replace this with your real data.
Because you'll probably have more interesting things to send
than the hex 0xDEAD 0xBEEF
*/
sendbuf[tx_len++] = 0xde;
sendbuf[tx_len++] = 0xad;
sendbuf[tx_len++] = 0xbe;
sendbuf[tx_len++] = 0xef;
/* Index of the network device */
socket_address.sll_ifindex = if_idx.ifr_ifindex;
/* Address length*/
socket_address.sll_halen = ETH_ALEN;
/* Destination MAC */
socket_address.sll_addr[0] = DEST_MAC0;
socket_address.sll_addr[1] = DEST_MAC1;
socket_address.sll_addr[2] = DEST_MAC2;
socket_address.sll_addr[3] = DEST_MAC3;
socket_address.sll_addr[4] = DEST_MAC4;
socket_address.sll_addr[5] = DEST_MAC5;
/* Send packet */
if (sendto(sockfd, sendbuf, tx_len, 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0)
printf("Send failed\n");
else {
printf("Sent :");
for (i=0; i < tx_len; i++)
printf("%02x:", sendbuf[i]);
printf("\n");
}
/* Wait specified number of microseconds
1,000,000 microseconds = 1 second
*/
usleep(1000000);
}
return 0;
}