-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
218 lines (179 loc) · 5.67 KB
/
main.cpp
File metadata and controls
218 lines (179 loc) · 5.67 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
218
#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>
using namespace std;
struct dot11
{
u_int8_t it_version; /* set to 0 */
u_int8_t it_pad;
u_int16_t it_len; /* entire length */
u_int32_t it_present; /* fields present */
} __attribute__((__packed__));
void print_dot11(struct dot11 *my_struct)
{
printf("it_version: %u\n", my_struct->it_version);
printf("it_pad: %u\n", my_struct->it_pad);
printf("it_len: %u\n", my_struct->it_len);
printf("it_present: %u\n", my_struct->it_present);
}
void usage()
{
printf("syntax: airo-mon <interface> \n");
printf("sample: airo-mon wlan0\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바이트 경계에 맞추기
}
}
vector<string> read_essids_from_file(const string &filename)
{
ifstream file(filename);
vector<string> essids;
string line;
while (getline(file, line))
{
if (!line.empty())
{
essids.push_back(line);
}
}
return essids;
}
void beacon_process(struct dot11 *header, struct pcap_pkthdr *pcap_header, const string &fake_essid)
{
// Check version
if (header->it_version != 0)
{
printf("packet's version must be 0 \n");
return;
}
int radiolength = header->it_len;
size_t offset = sizeof(dot11);
size_t length = pcap_header->caplen;
if (length < offset + 16)
{
printf("Packet too short for BSSID\n");
return;
}
const size_t ieee80211_header_length = 24;
// 비콘 프레임 페이로드 시작점
uint8_t *frame_payload_ptr = reinterpret_cast<uint8_t *>(header) + radiolength + ieee80211_header_length;
uint8_t *tagged_ptr = frame_payload_ptr;
uint8_t *essid_ptr = reinterpret_cast<uint8_t *>(tagged_ptr) + 12;
uint8_t essid_pkt = *essid_ptr;
uint8_t *essid_len_ptr = reinterpret_cast<uint8_t *>(essid_ptr) + 1;
const uint8_t real_essid_len = *essid_len_ptr;
const uint8_t fake_essid_len = fake_essid.length();
uint8_t *essid_content_ptr = reinterpret_cast<uint8_t *>(essid_ptr) + 2;
const uint8_t essid_content = *essid_content_ptr;
size_t remain_data_len = length - (essid_content_ptr - reinterpret_cast<uint8_t *>(header)) - real_essid_len; // 전체 패킷 - essid content 시작지점까지의 length - real essid length -> essid 가 끝나고 이후의 데이터 길이
if (real_essid_len != fake_essid_len)
{
// 데이터 이동
memmove(essid_content_ptr + fake_essid_len, essid_content_ptr + real_essid_len, remain_data_len);
int length_difference = fake_essid_len - real_essid_len;
length += length_difference;
pcap_header->caplen += length_difference;
pcap_header->len += length_difference;
}
// ESSID 업데이트
*essid_len_ptr = fake_essid_len;
memcpy(essid_content_ptr, fake_essid.c_str(), fake_essid_len);
string essid;
for (int i = 0; i < fake_essid_len; ++i)
{
essid += static_cast<char>(essid_content_ptr[i]);
}
printf("fake essid: %s\n", essid.c_str());
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
usage();
return -1;
}
char *dev = argv[1];
string essid_file = argv[2];
vector<string> fake_essids = read_essids_from_file(essid_file);
if (fake_essids.empty())
{
cerr << "ESSID list is empty or file not found: " << essid_file << endl;
return -1;
}
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;
}
size_t essid_index = 0;
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)
{
// Error or EOF, break the loop
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)))
{
string current_fake_essid = fake_essids[essid_index % fake_essids.size()];
beacon_process(radiotap_hdr, header, current_fake_essid);
essid_index++;
}
else
{
continue;
}
if (pcap_sendpacket(handle, packet, header->caplen) != 0)
{
fprintf(stderr, "Error sending the packet: %s\n", pcap_geterr(handle));
}
}
pcap_close(handle);
return 0;
}