PostgreSQL用户密码如何通过md5加密存储,是否加了salt
admin
2023-02-08 22:40:05
0

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

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

二、源码分析

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。

postgres=# create user yzs with password '123456';

2、堆栈信息

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 '\001', setHoldSnapshot=0 '\000', dest=0x99c3d74, completionTag=0xbf9119e6 "") at pquery.c:1178
#5  0x0840e8b7 in PortalRunMulti (portal=0x9a20634, isTopLevel=1 '\001', setHoldSnapshot=0 '\000', dest=0x99c3d74, altdest=0x99c3d74, completionTag=0xbf9119e6 "") at pquery.c:1324
#6  0x0840ded2 in PortalRun (portal=0x9a20634, count=2147483647, isTopLevel=1 '\001', run_once=1 '\001', dest=0x99c3d74, altdest=0x99c3d74, completionTag=0xbf9119e6 "") at pquery.c:799
#7  0x08408692 in exec_simple_query (query_string=0x99c31dc "create user yzs with password '123456';") at postgres.c:1099
#8  0x0840c5d4 in PostgresMain (argc=1, argv=0x997edc4, dbname=0x997ecf4 "postgres", username=0x99558cc "postgres") at postgres.c:4088
#9  0x083864e6 in BackendRun (port=0x9978038) at postmaster.c:4409
#10 0x08385c5d in BackendStartup (port=0x9978038) at postmaster.c:4081
#11 0x083822d9 in ServerLoop () at postmaster.c:1755
#12 0x083819d6 in PostmasterMain (argc=3, argv=0x9953810) at postmaster.c:1363
#13 0x082dfb60 in main (argc=3, argv=0x9953810) at main.c:228
(gdb) n
114     if (guessed_type != PASSWORD_TYPE_PLAINTEXT)
(gdb) 
123     switch (target_type)
(gdb) 
126             encrypted_password = palloc(MD5_PASSWD_LEN + 1);
(gdb) 
128             if (!pg_md5_encrypt(password, role, strlen(role),
(gdb) s
pg_md5_encrypt (passwd=0x99c3b4c "123456", salt=0x99c3b3c "yzs", salt_len=3, buf=0x9a0d984 "") at md5.c:326
326     size_t      passwd_len = strlen(passwd);
(gdb) n
329     char       *crypt_buf = malloc(passwd_len + salt_len + 1);
(gdb) 
332     if (!crypt_buf)
(gdb) 
339     memcpy(crypt_buf, passwd, passwd_len);
(gdb) 
340     memcpy(crypt_buf + passwd_len, salt, salt_len);
(gdb) p crypt_buf
$1 = 0x9979e68 "123456A"
(gdb) n
342     strcpy(buf, "md5");
(gdb) p crypt_buf
$2 = 0x9979e68 "123456yzsנ\tQ"
(gdb) n
343     ret = pg_md5_hash(crypt_buf, passwd_len + salt_len, buf + 3);
(gdb) p crypt_buf
$3 = 0x9979e68 "123456yzsנ\tQ"
(gdb) n
345     free(crypt_buf);
(gdb) p crypt_buf
$4 = 0x9979e68 "123456yzsנ\tQ"
(gdb) n
347     return ret;
(gdb) 
348 }
(gdb) p ret
$5 = 1 '\001'
(gdb) n
encrypt_password (target_type=PASSWORD_TYPE_MD5, role=0x99c3b3c "yzs", password=0x99c3b4c "123456") at crypt.c:131
131             return encrypted_password;
(gdb) 
146 }
(gdb) 
CreateRole (pstate=0x9a0d804, stmt=0x99c3bbc) at user.c:415
415                 CStringGetTextDatum(shadow_pass);
(gdb) p shadow_pass
$6 = 0x9a0d984 "md5aed8080c314507e15542d5e9519723a8"

3、从pg_authid表中观察该用户经过md5加过密的密码值,可以看出和堆栈信息中看到的一样

postgres=# select *from pg_authid where rolname='yzs';
 rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolbypa***ls | rolconnlimit |             rolpassword             | rolvaliduntil 
---------+----------+------------+---------------+-------------+-------------+----------------+--------------+--------------+-------------------------------------+---------------
 yzs     | f        | t          | f             | f           | t           | f              | f            |           -1 | md5aed8080c314507e15542d5e9519723a8 | 
(1 row)

相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...