欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySQL • 源码分析 • mysql认证阶段漫游  

程序员文章站 2024-03-18 17:41:34
...
摘要: client发起一个连接请求, 到拿到server返回的ok包之间, 走三次握手, 交换了[不可告人]的验证信息, 这期间mysql如何完成校验工作? 过程(三次握手) 信息是如何加密的 client: hash_stage1 = sha1(password) hash_stage2 = sh...

client发起一个连接请求, 到拿到server返回的ok包之间, 走三次握手, 交换了[不可告人]的验证信息, 这期间mysql如何完成校验工作?

过程(三次握手)

没加滤镜的三次握手

信息是如何加密的

client:

hash_stage1 = sha1(password) hash_stage2 = sha1(hash_stage1) reply = sha1(scramble, hash_stage2) ^ hash_stage1
server: (逻辑位于sql/password.c:check_scramble_sha1中, 下文亦有提及)


// mysql.user表中, 对应user的passwd实际上是hash_stage2 res1 = sha1(scramble, hash_stage2) hash_stage1 = reply ^
res1 hash_stage2_reassured = sha1(hash_stage1) 再根据hash_stage2_reassured == hash_stage2(from mysql.user)是否一致来判定是否合法
涉事函数们

如图, client发起连接请求, server创建新的线程, 并进入acl_authenticate(5.7位于sql/auth/sql_authentication.cc, 5.6位于sql/sql_acl.cc)函数完成信息验证, 并把包里读出的信息更新到本线程.

流程堆栈:

#0 parse_client_handshake_packet 
#1 server_mpvio_read_packet 
#2 native_password_authenticate
#3 do_auth_once 
#4 acl_authenticate 
#5 check_connection 
#6 login_connection 
#7 thd_prepare_connection
#8 do_handle_one_connection
接下来考察这些函数中做了哪些事. check_connection(sql/sql_connect.cc) 当接收到client的建连接请求时, 进入check_connection, 先对连接本身上下文分析(socket, tcp/ip的v4/6 哇之类的) 当然你用very long的host连进来, 也会在这里被cut掉防止overflow. 不合法的ip/host也会在这里直接返回, 如果环境ok, 就进入到acl_authenticate的逻辑中 acl_authenticate: 初始化MPVIO_EXT, 用于保存验证过程的上下文; 字符集, 挑战码, …的坑位, 上锁, 根据command进行分派, (新建链接为COM_CONNECT

COM_CONNECT下会进入函数do_auth_once(), 返回值直接决定握手成功与否. 先对authentication plugin做判定, 咱们这里基于”mysql_native_password”的情况

if (plugin)
{
st_mysql_auth *auth= (st_mysql_auth *) plugin_decl(plugin)->info;
res= auth->authenticate_user(mpvio, &mpvio->auth_info);
...
在mysql_native_password时会进入native_password_authenticate 逻辑:

/* generate the scramble, or reuse the old one */ if (mpvio->scramble[SCRAMBLE_LENGTH])
create_random_string(mpvio->scramble, SCRAMBLE_LENGTH, mpvio->rand);

/* send it to the client */ if (mpvio->write_packet(mpvio, (uchar*) mpvio->scramble, SCRAMBLE_LENGTH + 1))
DBUG_RETURN(CR_AUTH_HANDSHAKE);

/* read the reply with the encrypted password */ if ((pkt_len= mpvio->read_packet(mpvio, &pkt)) < 0)
DBUG_RETURN(CR_AUTH_HANDSHAKE);
DBUG_PRINT("info", ("reply read : pkt_len=%d", pkt_len));
可见这里才生成了挑战码并发送到client, 再调用mpvio->read_packet等待client回包, 进入server_mpvio_read_packet, 这里的实现则调用常见的my_net_read读包, 当拿到auth包时, 逻辑分派到parse_client_handshake_packet, 对包内容进行parse, 这里会根据不同client protocol, 去掉头和尾, 还对client是否设置了ssl做判定. 接着:

if (mpvio->client_capabilities & CLIENT_SECURE_CONNECTION)
{
/*
Get the password field.
*/
passwd= get_length_encoded_string(&end, &bytes_remaining_in_packet,
&passwd_len);
}
else
{
/*
Old passwords are zero terminatedtrings.
*/
passwd= get_string(&end, &bytes_remaining_in_packet, &passwd_len);
}
...
在拿到了client发来的加密串(虽然叫passwd), 暂时存放在内存中, 返回native_password_authenticate, 当判定为需要做password check时(万一有人不设置密码呢), 进入check_scramble, 这个函数中才实现了对密码的验证:

// server decode回包中的加密信息 // 把上面提到的三个公式包在函数中 my_bool
check_scramble_sha1(const uchar *scramble_arg, const char *message,
const uint8 *hash_stage2) {
uint8 buf[SHA1_HASH_SIZE];
uint8 hash_stage2_reassured[SHA1_HASH_SIZE];

/* create key to encrypt scramble */
compute_sha1_hash_multi(buf, message, SCRAMBLE_LENGTH,
(const char *) hash_stage2, SHA1_HASH_SIZE);
/* encrypt scramble */
my_crypt((char *) buf, buf, scramble_arg, SCRAMBLE_LENGTH);

/* now buf supposedly contains hash_stage1: so we can get hash_stage2 */
compute_sha1_hash(hash_stage2_reassured, (const char *) buf, SHA1_HASH_SIZE);

return MY_TEST(memcmp(hash_stage2, hash_stage2_reassured, SHA1_HASH_SIZE));
}
native_password_authenticate拿到check_scamble的返回值, 返回OK, 再返回到acl_authenticate, 讲mpvio中环境信息更新到线程信息THD中, successful login~

(所以可以魔改这块代码搞事, 密码什么的, 权限什么的…. (我就说说, 别当真

版权声明:本文内容由互联网用户自发贡献,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。