-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharp_packet.hpp
178 lines (159 loc) · 5.38 KB
/
arp_packet.hpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Description: ARP socket class. Provides functions for ARP sockets,
. including creating, sending, receiving, etc.
. This program is a part of arp_bomber.
Author: Hanson (SweetIceLolly)
Website: https://github.com/SweetIceLolly/arp_bomber
File: arp_packet.hpp
*/
#include <linux/wireless.h>
#include <linux/if_arp.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#ifndef __IP_ADDR
#define __IP_ADDR
struct ip_addr {
unsigned char ip_value[4];
unsigned char operator [] (int i) const {
return ip_value[i];
}
unsigned char & operator [] (int i) {
return ip_value[i];
}
};
#endif //__IP_ADDR
#ifndef __MAC_ADDR
#define __MAC_ADDR
struct mac_addr {
unsigned char mac_value[6];
unsigned char operator [] (int i) const {
return mac_value[i];
}
unsigned char & operator [] (int i) {
return mac_value[i];
}
};
#endif //__MAC_ADDR
struct __attribute__((packed)) arp_packet {
union {
unsigned char destination_mac[6];
mac_addr destination_mac_addr;
};
union {
unsigned char source_mac[6];
mac_addr source_mac_addr;
};
unsigned char type[2] = {0x8, 0x6}; //(0x8, 0x6) for ARP
unsigned char hardware_type[2] = {0x0, 0x1}; //(0x0, 0x1) for ethernet
unsigned char protocol_type[2] = {0x8, 0x0}; //(0x8, 0x0) for IPv4
unsigned char hardware_size = 0x6; //0x6 for MAC addresses
unsigned char protocol_size = 0x4; //0x4 for ethernet
unsigned char opcode[2] = {0x0, 0x1}; //(0x0, 0x1) = ARP request, (0x0, 0x2) = ARP reply
union {
unsigned char sender_mac[6];
mac_addr sender_mac_addr;
};
union {
unsigned char sender_ip[4];
ip_addr sender_ip_addr;
};
union {
unsigned char target_mac[6];
mac_addr target_mac_addr;
};
union {
unsigned char target_ip[4];
ip_addr target_ip_addr;
};
};
class arp_socket {
private:
int socket_descr = -1; //ARP socket descriptor
struct sockaddr_ll socket_address; //Socket address
public:
/*
Purpose: To create the ARP socket
Args: interface_name: Network interface name. (e.g. eth0)
Return: -1: Failed to create the socket;
. Other value: Socket created successfully
*/
int create(const char *interface_name) {
//Handle NULL string pointer
if (interface_name == NULL) {
return -1;
}
//Create the socket
socket_descr = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (socket_descr == -1) { //Failed to create socket
return -1;
}
//Get network interface index
struct ifreq ifr;
strncpy(ifr.ifr_ifrn.ifrn_name, interface_name, IFNAMSIZ);
if (ioctl(socket_descr, SIOCGIFINDEX, &ifr) == -1) {
::close(socket_descr);
return -1;
}
//Set address info
socket_address.sll_family = PF_PACKET;
socket_address.sll_protocol = htons(ETH_P_ARP);
socket_address.sll_ifindex = ifr.ifr_ifru.ifru_ivalue;
socket_address.sll_hatype = ARPHRD_ETHER;
socket_address.sll_pkttype = 0;
socket_address.sll_halen = 0;
socket_address.sll_addr[6] = 0x00;
socket_address.sll_addr[7] = 0x00;
return socket_descr;
}
/*
Purpose: To close the ARP socket
*/
void close() {
::close(socket_descr);
}
/*
Purpose: Receive data
Args: packet_content: The arp_packet structure to store data
Return: -1: Failed to receive data;
. Other value: The length of received data
*/
int recv(arp_packet *packet_content) {
memset(packet_content, 0, sizeof(arp_packet));
return recvfrom(socket_descr, packet_content, sizeof(arp_packet), 0, NULL, NULL);
}
/*
Purpose: Send data
Args: packet_content: The ARP packet to be sent
Return: -1: Failed to send data;
. Other value: The length of sent data
*/
int send(arp_packet packet_content, mac_addr source_mac_addr) {
memcpy(&socket_address.sll_addr, &source_mac_addr, 6);
return sendto(socket_descr, &packet_content, sizeof(packet_content), 0,
(struct sockaddr*)&socket_address, sizeof(socket_address));
}
/*
Purpose: Receive raw data
Args: buffer: The buffer to store data
. buffer_size: Size of the buffer to store data
Return: -1: Failed to receive data;
. Other value: The length of received data
*/
int recv(unsigned char *buffer, int buffer_size) {
memset(buffer, 0, buffer_size);
return recvfrom(socket_descr, buffer, buffer_size, 0, NULL, NULL);
}
/*
Purpose: Send raw data
Args: buffer: The buffer to be sent
. buffer_size: Size of the buffer to be sent
Return: -1: Failed to send data;
. Other value: The length of sent data
*/
int send(unsigned char *buffer, int buffer_size) {
return sendto(socket_descr, buffer, buffer_size, 0,
(struct sockaddr*)&socket_address, sizeof(socket_address));
}
};