-
Notifications
You must be signed in to change notification settings - Fork 41
/
keepalive.c
405 lines (372 loc) · 12.9 KB
/
keepalive.c
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <winsock2.h>
typedef int socklen_t;
#else
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include "auth.h"
#include "configparse.h"
#include "debug.h"
#include "keepalive.h"
#include "libs/md4.h"
#include "libs/md5.h"
#include "libs/sha1.h"
int keepalive_1(int sockfd, struct sockaddr_in addr, unsigned char seed[], unsigned char auth_information[]) {
if (drcom_config.keepalive1_mod) {
unsigned char keepalive_1_packet1[8] = {0x07, 0x01, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00};
unsigned char recv_packet1[1024], keepalive_1_packet2[38], recv_packet2[1024];
memset(keepalive_1_packet2, 0, 38);
sendto(sockfd, keepalive_1_packet1, 8, 0, (struct sockaddr *)&addr, sizeof(addr));
if (verbose_flag) {
print_packet("[Keepalive1_packet1 sent] ", keepalive_1_packet1, 8);
}
if (logging_flag) {
logging("[Keepalive1_packet1 sent] ", keepalive_1_packet1, 8);
}
#ifdef TEST
printf("[TEST MODE]IN TEST MODE, PASS\n");
return 0;
#endif
socklen_t addrlen = sizeof(addr);
while (1) {
if (recvfrom(sockfd, recv_packet1, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
#ifdef WIN32
get_lasterror("Failed to recv data");
#else
perror("Failed to recv data");
#endif
return 1;
} else {
if (verbose_flag) {
print_packet("[Keepalive1 challenge_recv] ", recv_packet1, 100);
}
if (logging_flag) {
logging("[Keepalive1 challenge_recv] ", recv_packet1, 100);
}
if (recv_packet1[0] == 0x07) {
break;
} else if (recv_packet1[0] == 0x4d) {
DEBUG_PRINT(("Get notice packet.\n"));
continue;
} else {
printf("Bad keepalive1 challenge response received.\n");
return 1;
}
}
}
unsigned char keepalive1_seed[4] = {0};
int encrypt_type;
unsigned char crc[8] = {0};
memcpy(keepalive1_seed, &recv_packet1[8], 4);
encrypt_type = keepalive1_seed[0] & 3;
gen_crc(keepalive1_seed, encrypt_type, crc);
keepalive_1_packet2[0] = 0xff;
memcpy(keepalive_1_packet2 + 8, keepalive1_seed, 4);
memcpy(keepalive_1_packet2 + 12, crc, 8);
memcpy(keepalive_1_packet2 + 20, auth_information, 16);
keepalive_1_packet2[36] = rand() & 0xff;
keepalive_1_packet2[37] = rand() & 0xff;
sendto(sockfd, keepalive_1_packet2, 38, 0, (struct sockaddr *)&addr, sizeof(addr));
if (verbose_flag) {
print_packet("[Keepalive1_packet2 sent] ", keepalive_1_packet2, 38);
}
if (logging_flag) {
logging("[Keepalive1_packet2 sent] ", keepalive_1_packet2, 38);
}
if (recvfrom(sockfd, recv_packet2, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
#ifdef WIN32
get_lasterror("Failed to recv data");
#else
perror("Failed to recv data");
#endif
return 1;
} else {
if (verbose_flag) {
print_packet("[Keepalive1 recv] ", recv_packet2, 100);
}
if (logging_flag) {
logging("[Keepalive1 recv] ", recv_packet2, 100);
}
if (recv_packet2[0] != 0x07) {
printf("Bad keepalive1 response received.\n");
return 1;
}
}
} else {
unsigned char keepalive_1_packet[42], recv_packet[1024], MD5A[16];
memset(keepalive_1_packet, 0, 42);
keepalive_1_packet[0] = 0xff;
int MD5A_len = 6 + strlen(drcom_config.password);
unsigned char MD5A_str[MD5A_len];
MD5A_str[0] = 0x03;
MD5A_str[1] = 0x01;
memcpy(MD5A_str + 2, seed, 4);
memcpy(MD5A_str + 6, drcom_config.password, strlen(drcom_config.password));
MD5(MD5A_str, MD5A_len, MD5A);
memcpy(keepalive_1_packet + 1, MD5A, 16);
memcpy(keepalive_1_packet + 20, auth_information, 16);
keepalive_1_packet[36] = rand() & 0xff;
keepalive_1_packet[37] = rand() & 0xff;
sendto(sockfd, keepalive_1_packet, 42, 0, (struct sockaddr *)&addr, sizeof(addr));
if (verbose_flag) {
print_packet("[Keepalive1 sent] ", keepalive_1_packet, 42);
}
if (logging_flag) {
logging("[Keepalive1 sent] ", keepalive_1_packet, 42);
}
#ifdef TEST
printf("[TEST MODE]IN TEST MODE, PASS\n");
return 0;
#endif
socklen_t addrlen = sizeof(addr);
while (1) {
if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
#ifdef WIN32
get_lasterror("Failed to recv data");
#else
perror("Failed to recv data");
#endif
return 1;
} else {
if (verbose_flag) {
print_packet("[Keepalive1 recv] ", recv_packet, 100);
}
if (logging_flag) {
logging("[Keepalive1 recv] ", recv_packet, 100);
}
if (recv_packet[0] == 0x07) {
break;
} else if (recv_packet[0] == 0x4d) {
DEBUG_PRINT(("Get notice packet."));
continue;
} else {
printf("Bad keepalive1 response received.\n");
return 1;
}
}
}
}
return 0;
}
void gen_crc(unsigned char seed[], int encrypt_type, unsigned char crc[]) {
if (encrypt_type == 0) {
char DRCOM_DIAL_EXT_PROTO_CRC_INIT[4] = {0xc7, 0x2f, 0x31, 0x01};
char gencrc_tmp[4] = {0x7e};
memcpy(crc, DRCOM_DIAL_EXT_PROTO_CRC_INIT, 4);
memcpy(crc + 4, gencrc_tmp, 4);
} else if (encrypt_type == 1) {
unsigned char hash[32] = {0};
MD5(seed, 4, hash);
crc[0] = hash[2];
crc[1] = hash[3];
crc[2] = hash[8];
crc[3] = hash[9];
crc[4] = hash[5];
crc[5] = hash[6];
crc[6] = hash[13];
crc[7] = hash[14];
} else if (encrypt_type == 2) {
unsigned char hash[32] = {0};
MD4(seed, 4, hash);
crc[0] = hash[1];
crc[1] = hash[2];
crc[2] = hash[8];
crc[3] = hash[9];
crc[4] = hash[4];
crc[5] = hash[5];
crc[6] = hash[11];
crc[7] = hash[12];
} else if (encrypt_type == 3) {
unsigned char hash[32] = {0};
SHA1(seed, 4, hash);
crc[0] = hash[2];
crc[1] = hash[3];
crc[2] = hash[9];
crc[3] = hash[10];
crc[4] = hash[5];
crc[5] = hash[6];
crc[6] = hash[15];
crc[7] = hash[16];
}
}
void keepalive_2_packetbuilder(unsigned char keepalive_2_packet[], int keepalive_counter, int filepacket, int type, int encrypt_type) {
keepalive_2_packet[0] = 0x07;
keepalive_2_packet[1] = keepalive_counter;
keepalive_2_packet[2] = 0x28;
keepalive_2_packet[4] = 0x0b;
keepalive_2_packet[5] = type;
if (filepacket) {
keepalive_2_packet[6] = 0x0f;
keepalive_2_packet[7] = 0x27;
} else {
memcpy(keepalive_2_packet + 6, drcom_config.KEEP_ALIVE_VERSION, 2);
}
keepalive_2_packet[8] = 0x2f;
keepalive_2_packet[9] = 0x12;
if (type == 3) {
unsigned char host_ip[4] = {0};
if (strcmp(mode, "dhcp") == 0) {
sscanf(drcom_config.host_ip, "%hhd.%hhd.%hhd.%hhd",
&host_ip[0],
&host_ip[1],
&host_ip[2],
&host_ip[3]);
memcpy(keepalive_2_packet + 28, host_ip, 4);
} else if (strcmp(mode, "pppoe") == 0) {
unsigned char crc[8] = {0};
gen_crc(keepalive_2_packet, encrypt_type, crc);
memcpy(keepalive_2_packet + 32, crc, 8);
}
}
}
int keepalive_2(int sockfd, struct sockaddr_in addr, int *keepalive_counter, int *first, int *encrypt_type) {
unsigned char keepalive_2_packet[40], recv_packet[1024], tail[4];
socklen_t addrlen = sizeof(addr);
#ifdef TEST
printf("[TEST MODE]IN TEST MODE, PASS\n");
#else
if (*first) {
// send the file packet
memset(keepalive_2_packet, 0, 40);
if (strcmp(mode, "pppoe") == 0) {
keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 1, *encrypt_type);
} else {
keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 1, 0);
}
(*keepalive_counter)++;
sendto(sockfd, keepalive_2_packet, 40, 0, (struct sockaddr *)&addr, sizeof(addr));
if (verbose_flag) {
print_packet("[Keepalive2_file sent] ", keepalive_2_packet, 40);
}
if (logging_flag) {
logging("[Keepalive2_file sent] ", keepalive_2_packet, 40);
}
if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
#ifdef WIN32
get_lasterror("Failed to recv data");
#else
perror("Failed to recv data");
#endif
return 1;
}
if (verbose_flag) {
print_packet("[Keepalive2_file recv] ", recv_packet, 40);
}
if (logging_flag) {
logging("[Keepalive2_file recv] ", recv_packet, 40);
}
if (recv_packet[0] == 0x07) {
if (recv_packet[2] == 0x10) {
if (verbose_flag) {
printf("Filepacket received.\n");
}
} else if (recv_packet[2] != 0x28) {
if (verbose_flag) {
printf("Bad keepalive2 response received.\n");
}
return 1;
}
} else {
printf("Bad keepalive2 response received.\n");
return 1;
}
}
#endif
// send the first packet
*first = 0;
memset(keepalive_2_packet, 0, 40);
if (strcmp(mode, "pppoe") == 0) {
keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 1, *encrypt_type);
} else {
keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 1, 0);
}
(*keepalive_counter)++;
sendto(sockfd, keepalive_2_packet, 40, 0, (struct sockaddr *)&addr, sizeof(addr));
if (verbose_flag) {
print_packet("[Keepalive2_A sent] ", keepalive_2_packet, 40);
}
if (logging_flag) {
logging("[Keepalive2_A sent] ", keepalive_2_packet, 40);
}
#ifdef TEST
unsigned char test[4] = {0x13, 0x38, 0xe2, 0x11};
memcpy(tail, test, 4);
print_packet("[TEST MODE]<PREP TAIL> ", tail, 4);
#else
if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
#ifdef WIN32
get_lasterror("Failed to recv data");
#else
perror("Failed to recv data");
#endif
return 1;
}
if (verbose_flag) {
print_packet("[Keepalive2_B recv] ", recv_packet, 40);
}
if (logging_flag) {
logging("[Keepalive2_B recv] ", recv_packet, 40);
}
if (recv_packet[0] == 0x07) {
if (recv_packet[2] != 0x28) {
printf("Bad keepalive2 response received.\n");
return 1;
}
} else {
printf("Bad keepalive2 response received.\n");
return 1;
}
memcpy(tail, &recv_packet[16], 4);
#endif
#ifdef DEBUG
print_packet("<GET TAIL> ", tail, 4);
#endif
// send the third packet
memset(keepalive_2_packet, 0, 40);
if (strcmp(mode, "pppoe") == 0) {
keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 3, *encrypt_type);
} else {
keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 3, 0);
}
memcpy(keepalive_2_packet + 16, tail, 4);
(*keepalive_counter)++;
sendto(sockfd, keepalive_2_packet, 40, 0, (struct sockaddr *)&addr, sizeof(addr));
if (verbose_flag) {
print_packet("[Keepalive2_C sent] ", keepalive_2_packet, 40);
}
if (logging_flag) {
logging("[Keepalive2_C sent] ", keepalive_2_packet, 40);
}
#ifdef TEST
printf("[TEST MODE]IN TEST MODE, PASS\n");
exit(0);
#endif
if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
#ifdef WIN32
get_lasterror("Failed to recv data");
#else
perror("Failed to recv data");
#endif
return 1;
}
if (verbose_flag) {
print_packet("[Keepalive2_D recv] ", recv_packet, 40);
}
if (logging_flag) {
logging("[Keepalive2_D recv] ", recv_packet, 40);
}
if (recv_packet[0] == 0x07) {
if (recv_packet[2] != 0x28) {
printf("Bad keepalive2 response received.\n");
return 1;
}
} else {
printf("Bad keepalive2 response received.\n");
return 1;
}
return 0;
}