forked from session-replay-tools/mysql-replay-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.c
286 lines (247 loc) · 7.11 KB
/
protocol.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
#include <xcopy.h>
#include "pairs.h"
#include "protocol.h"
#include "password.h"
static void
new_hash(uint64_t *result, const char *password)
{
int i = 0, length;
uint64_t nr = 1345345333L, add = 7, nr2 = 0x12345671L, tmp;
length = strlen(password);
for (; i < length; ++i) {
if (' ' == password[i] || '\t' == password[i]) {
/* skip spaces */
continue;
}
tmp = (0xff & password[i]);
nr ^= ((((nr & 63) + add) * tmp) + (nr << 8));
nr2 += ((nr2 << 8) ^ nr);
add += tmp;
}
result[0] = nr & 0x7fffffffL;
result[1] = nr2 & 0x7fffffffL;
}
/*
* right from Monty's code
*/
void
new_crypt(char *result, const char *password, char *message)
{
int length, i;
char b;
double d;
uint64_t pw[2], msg[2], max, seed1, seed2;
new_hash(pw, message);
new_hash(msg, password);
max = 0x3fffffffL;
seed1 = (pw[0] ^ msg[0]) % max;
seed2 = (pw[1] ^ msg[1]) % max;
length = strlen(message);
for (i =0; i < length; i++) {
seed1 = ((seed1 * 3) + seed2) % max;
seed2 = (seed1 + seed2 + 33) % max;
d = (double) seed1 / (double) max;
b = (char) floor ((d * 31) + 64);
result[i] = b;
}
seed1 = ((seed1 * 3) + seed2) % max;
d = (double) seed1 / (double) max;
b = (char) floor (d * 31);
for (i = 0; i < length; i++) {
result[i] ^= (char) b;
}
}
int
is_last_data_packet(unsigned char *payload)
{
size_t len;
unsigned char *p;
p = payload;
len = p[0] + (p[1] << 8) + (p[2] << 16);
if (len < 9) {
/* skip packet length */
p = p + 3;
/* skip packet number */
p = p + 1;
if (254 == p[0]) {
return 1;
}
}
return 0;
}
int
parse_handshake_init_cont(unsigned char *payload, size_t length,
char *scramble_buff)
{
/*
* The following is the protocol format of mysql handshake
*
* 1 protocol_version
* n (Null-Terminated String) server_version
* 4 thread_id
* 8 scramble_buff
* 1 (filler) always 0x00
* 2 server_capabilities
* 1 server_language
* 2 server_status
* 2 server capabilities (two upper bytes)
* 1 length of the scramble
* 10 (filler) always 0
* n rest of the plugin provided data
* (at least 12 bytes)
* 1 \0 byte, terminating
* the second part of a scramble
*/
char *str;
size_t len, count, str_len;
unsigned char *p;
p = payload;
/* skip packet length */
p = p + 3;
/* skip packet number */
p = p + 1;
/* skip protocol_version */
p++;
str = (char *) p;
len = strlen(str);
/* skip server_version */
p = p + len + 1;
/* skip thread_id */
p += 4;
count = p - payload + 8;
if (count > length) {
tc_log_info(LOG_ERR, 0, "payload len is too short for init:%u,%u",
length, count);
return 0;
}
strncpy(scramble_buff, (char *) p, 8);
/* skip scramble_buff */
p = p + 8 + 1;
/* skip server_capabilities */
p = p + 2;
/* skip server_language */
p = p + 1;
/* skip server_status */
p = p + 2;
/* skip server capabilities (two upper bytes) */
p = p + 2;
/* skip length of the scramble */
p = p + 1;
/* skip (filler) always 0 */
p = p + 10;
str = (char *) p;
str_len = strlen(str) + 8;
count = p - payload + strlen(str);
if (str_len > SCRAMBLE_LENGTH|| count > length) {
if (count >length) {
tc_log_info(LOG_ERR, 0, "payload len is too short for init2:%u,%u",
length, count);
} else {
tc_log_info(LOG_ERR, 0, "scramble is too long:%u", str_len);
}
return 0;
}
/* copy the rest of scramble_buff */
strncpy(scramble_buff + 8, str, strlen(str));
return 1;
}
int
change_clt_auth_content(unsigned char *payload, int length,
char *password, char *message)
{
/*
* 4 client_flags
* 4 max_packet_size
* 1 charset_number
* 23 (filler) always 0x00...
* n (Null-Terminated String) user
* n (Length Coded Binary) scramble_buff (1 + x bytes)
* n (Null-Terminated String) databasename (optional)
*/
char *str, user[256], *pwd;
size_t len, i;
unsigned char *p, *q, scramble_buff[SCRAMBLE_LENGTH + 1];
tc_memzero(scramble_buff, SCRAMBLE_LENGTH + 1);
p = payload;
/* skip mysql packet header */
/* skip packet length */
p = p + 3;
/* skip packet number */
p = p + 1;
/* skip client_flags */
p = p + 4;
/* skip max_packet_size */
p = p + 4;
/* skip charset_number */
p = p + 1;
/* skip (filler) always 0x00... */
q = p;
p = p + 23;
len = p - payload;
if ((int) len > length) {
tc_log_info(LOG_ERR, 0, "payload len is too short:%d,%u", length, len);
return 0;
}
for (i = 0; i < 23; i++) {
if (q[i] != 0 ) {
tc_log_info(LOG_WARN, 0, "it is not a login packet");
return 0;
}
}
str = (char *) p;
/* retrieve user */
tc_memzero(user, 256);
len = strlen(str);
if (len >= 256) {
tc_log_info(LOG_ERR, 0, "user len is too long:%s,%u", str, len);
return 0;
}
strcpy(user, str);
pwd = retrieve_user_pwd(user);
if (pwd == NULL) {
tc_log_info(LOG_WARN, 0, "user:%s,pwd is null", user);
return 0;
}
/* skip user */
p = p + len + 1;
/* skip scramble_buff length */
p = p + 1;
len = p - payload + SCRAMBLE_LENGTH;
if ((int) len > length) {
tc_log_info(LOG_ERR, 0, "payload len is too short too:%d,%u",
length, len);
return 0;
}
scramble((char *) scramble_buff, message, pwd);
/* change scramble_buff according the target server scramble */
for (i = 0; i < SCRAMBLE_LENGTH; i++) {
p[i] = scramble_buff[i];
}
/* save password */
strcpy(password, pwd);
return 1;
}
int
change_clt_second_auth_content(unsigned char *payload, size_t length,
char *new_content)
{
size_t i, len;
unsigned char *p;
p = payload;
/* skip mysql packet header */
/* skip packet length */
p = p + 3;
/* skip packet number */
p = p + 1;
len = p - payload + 8;
if (len > length) {
tc_log_info(LOG_ERR, 0, "payload len is too short for sec :%u,%u",
length, len);
return 0;
}
/* change scramble_buff according to the target server scramble */
for (i = 0; i < 8; i++) {
p[i] = new_content[i];
}
return 1;
}