PostgreSQL用户密码如何通过md5加密存储,是否加了盐

  

一、PG用户的密码如何通过md5加密,并且是否加了盐吗?本文将从源码角度跟踪分析。

  

PG用户通过md5加密时,加了盐,而这个盐是用户名字符串。

  

二、源码分析

  
 <代码> CreateRole:
  →shadow_pass=encrypt_password (Password_encryption支撑作用,密码);
  |——pg_md5_encrypt(密码、角色strlen(角色),encrypted_password);
  | |——memcpy (passwd, crypt_buf passwd_len);
  | | memcpy (crypt_buf + passwd_len、角色strlen(作用);
  | |拷贝字符串(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上打断点,然后客户端执行:创建用户与密码“123456”yz;创建带密码的用户,观察是否默认使用md5。

  
 <代码> postgres=#创建用户yz密码“123456”, 
  

2,堆栈信息

  
 <代码> 1号断点,encrypt_password (target_type=PASSWORD_TYPE_MD5角色=0 x99c3b3c yz,密码=0 x99c3b4c“123456”)在crypt.c: 111
  111年PasswordType guessed_type=get_password_type(密码);
  (gdb)英国电信
  # 0 encrypt_password (target_type=PASSWORD_TYPE_MD5角色=0 x99c3b3c yz,密码=0 x99c3b4c“123456”)在crypt.c: 111
  # 1 0 x0827b7a2 CreateRole (pstate=0 x9a0d804,支撑=0 x99c3bbc) user.c: 412
  # 2 0 x0840fc18 standard_ProcessUtility (pstmt=0 x99c3c14,变量=0 x99c31dc”创建用户yz密码“123456”;“、上下文=PROCESS_UTILITY_TOPLEVEL params=0 x0, queryEnv=0 x0,
  dest=0 x99c3d74 completionTag=0 xbf9119e6 utility.c " "): 722
  # 3 0 x0840f42a ProcessUtility (pstmt=0 x99c3c14,变量=0 x99c31dc”创建用户yz密码“123456”;“、上下文=PROCESS_UTILITY_TOPLEVEL params=0 x0, queryEnv=0 x0,
  dest=0 x99c3d74 completionTag=0 xbf9119e6 utility.c " "): 357
  # 4 0 x0840e6ea PortalRunUtility(门户=0 x9a20634 pstmt=0 x99c3c14 isTopLevel=1 \ 001, setHoldSnapshot=0 \ 000, dest=0 x99c3d74 completionTag=0 xbf9119e6 " ")在pquery.c: 1178
  # 5 0 x0840e8b7 PortalRunMulti(门户=0 x9a20634 isTopLevel=1 \ 001, setHoldSnapshot=0 \ 000, dest=0 x99c3d74 altdest=0 x99c3d74 completionTag=0 xbf9119e6 " ")在pquery.c: 1324
  # 6 0 x0840ded2 PortalRun(门户=0 x9a20634数=2147483647,isTopLevel=1 \ 001, run_once=1 \ 001, dest=0 x99c3d74 altdest=0 x99c3d74 completionTag=0 xbf9119e6 " ")在pquery.c: 799
  # 7 0 x08408692 exec_simple_query (query_string=0 x99c31dc”创建用户yz密码“123456”,“)在postgres.c: 1099
  # 8 0 x0840c5d4 PostgresMain(命令行参数个数=1,argv=0 x997edc4 dbname=0 x997ecf4“postgres”,用户名=0 x99558cc postgres) postgres.c: 4088
  # 9 0 x083864e6 BackendRun postmaster.c(端口=0 x9978038): 4409
  # 10 0 x08385c5d BackendStartup postmaster.c(端口=0 x9978038): 4081
  # 11 0 x083822d9 ServerLoop postmaster.c (): 1755
  # 12 0 x083819d6 PostmasterMain(命令行参数个数=3,argv=0 x9953810) postmaster.c: 1363
  # 13 0 x082dfb60主要(命令行参数个数=3,argv=0 x9953810)在c: 228
  (gdb) n
  114如果(guessed_type !=PASSWORD_TYPE_PLAINTEXT)
  (gdb)
  123开关(target_type)
  (gdb)
  126 encrypted_password=palloc (MD5_PASSWD_LEN + 1);
  (gdb)
  128如果!pg_md5_encrypt(密码、角色strlen(角色),
  (gdb)
  pg_md5_encrypt (passwd=0 x99c3b4c“123456”,盐=0 x99c3b3c yz, salt_len=3, buf=0 x9a0d984 " ")在md5.c: 326
  326年size_t passwd_len=strlen (passwd);
  (gdb) n
  329 char * crypt_buf=malloc (passwd_len + salt_len + 1);
  (gdb)
  332如果(crypt_buf !)
  (gdb)
  339 memcpy (crypt_buf passwd, passwd_len);
  (gdb)
  340年memcpy (crypt_buf + passwd_len、盐、salt_len);
  (gdb) p crypt_buf
  1美元=0 x9979e68“123456”
  (gdb) n
  342拷贝字符串(buf md5);
  (gdb) p crypt_buf
  2美元=0 x9979e68“123456 yz ? \ tQ”
  (gdb) n
  343 ret=pg_md5_hash (crypt_buf passwd_len + salt_len buf + 3);
  (gdb) p crypt_buf
  3美元=0 x9979e68“123456 yz ? \ tQ”
  (gdb) n
  345免费(crypt_buf);
  (gdb) p crypt_buf
  4美元=0 x9979e68“123456 yz ? \ tQ”
  (gdb) n
  347年返回受潮湿腐烂;
  (gdb)
  348}
  (gdb) p仓促
  5美元=1 \ 001的
  (gdb) n
  encrypt_password (target_type=PASSWORD_TYPE_MD5、角色=0 x99c3b3c yz,密码=0 x99c3b4c“123456”)在crypt.c: 131
  131年返回encrypted_password;
  (gdb)
  146}
  (gdb)
  CreateRole (pstate=0 x9a0d804支撑=0 x99c3bbc) user.c: 415
  415年CStringGetTextDatum (shadow_pass);
  (gdb) p shadow_pass
  6美元=0 x9a0d984“md5aed8080c314507e15542d5e9519723a8” 
  

PostgreSQL用户密码如何通过md5加密存储,是否加了盐