一、PG用户的密码如何通过md5加密,并且是否加了salt?本文将从源码角度跟踪分析。
PG用户通过md5加密时,加了salt,而这个salt是用户名字符串。
二、源码分析
代码语言:javascript复制CreateRole:
shadow_pass = encrypt_password(Password_encryption, stmt->role,password);
|-- pg_md5_encrypt(password, role, strlen(role),encrypted_password);
| |-- memcpy(crypt_buf, passwd, passwd_len);
| | memcpy(crypt_buf passwd_len, role, strlen(role));
| | strcpy(buf, "md5");
|-- |-- pg_md5_hash(crypt_buf, passwd_len salt_len, buf 3);
new_record[Anum_pg_authid_rolpassword - 1] =CStringGetTextDatum(shadow_pass);
三、gdb跟踪
1、在函数encrypt_password上打断点,然后客户端执行:create user yzs with password '123456';创建带密码的用户,观察是否默认使用md5。
代码语言:javascript复制postgres=# create user yzs with password '123456';
2、堆栈信息
代码语言:javascript复制Breakpoint 1, encrypt_password (target_type=PASSWORD_TYPE_MD5, role=0x99c3b3c "yzs", password=0x99c3b4c "123456") at crypt.c:111
111 PasswordType guessed_type = get_password_type(password);
(gdb) bt
#0 encrypt_password (target_type=PASSWORD_TYPE_MD5, role=0x99c3b3c "yzs", password=0x99c3b4c "123456") at crypt.c:111
#1 0x0827b7a2 in CreateRole (pstate=0x9a0d804, stmt=0x99c3bbc) at user.c:412
#2 0x0840fc18 in standard_ProcessUtility (pstmt=0x99c3c14, queryString=0x99c31dc "create user yzs with password '123456';", context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0,
dest=0x99c3d74, completionTag=0xbf9119e6 "") at utility.c:722
#3 0x0840f42a in ProcessUtility (pstmt=0x99c3c14, queryString=0x99c31dc "create user yzs with password '123456';", context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0,
dest=0x99c3d74, completionTag=0xbf9119e6 "") at utility.c:357
#4 0x0840e6ea in PortalRunUtility (portal=0x9a20634, pstmt=0x99c3c14, isTopLevel=1 '