简体   繁体   English

api属性文件中的MD5哈希数据库密码

[英]MD5 hashed database password in api properties file

I have a rest api which talks to Postgres, right now in the properties file of the api we are hardcoding the DB password. 我有一个与Postgres对话的rest api,现在在api的属性文件中,我们正在对数据库密码进行硬编码。 so we thought when a user role is created in postgres we can use Md5 hash value(or any other encrypted value which should be decrypted by postgres) for the password...and we can use that value(hased value) in api property file instead of hardcoded one. 因此我们认为,在postgres中创建用户角色时,我们可以使用Md5哈希值(或应该由postgres解密的任何其他加密值)作为密码...并且我们可以在api属性文件中使用该值(已使用值)而不是硬编码。

My question is can we use that Md5 hash value in api dev property file and when the password is sent over network and tries to connect to postgres Will it (postgres) decrypt to actual password and allows the user to connect to DB without authentication failed????? 我的问题是,我们可以在api dev属性文件中使用该Md5哈希值吗,以及何时将密码通过网络发送并尝试连接到postgres它将(postgres)解密为实际密码并允许用户连接到数据库而不会导致身份验证失败吗? ????

TL;DR: you can't store the hashed password in a properties file and use it to authenticate unless the client application can recognise that it's pre-hashed and avoid the second hashing pass. TL; DR:您不能将散列密码存储在属性文件中,并且不能使用它来进行身份验证,除非客户端应用程序可以识别出它已被预散列并且避免了第二次散列通过。

If the client library does recognise pre-hashed passwords (libpq doesn't), the hashed password can be used as a proxy for the real password. 如果客户端库确实识别出了预加密的密码(libpq无法识别),则哈希密码可以用作真实密码的代理。 You don't need to know the real password if you know the hash. 如果您知道哈希,则不需要知道真实密码。 this means it's also no more secure to store the hashed password in the properties file than it is to store the original password. 这意味着将散列密码存储在属性文件中也没有比存储原始密码更加安全。

The password is salted and hashed again before being sent on the wire so you can't sniff what you see on the wire and use that to authenticate. 在通过网络发送密码之前,该密码会先加盐并再进行散列处理,这样您就无法嗅探在网络上看到的内容并使用该密码进行身份验证。


Looking at the source code, sendAuthRequest in src/backend/libpq/auth.c : 查看源代码,位于src/backend/libpq/auth.c sendAuthRequest

/* Add the salt for encrypted passwords. */
if (areq == AUTH_REQ_MD5)
    pq_sendbytes(&buf, port->md5Salt, 4);

port is struct Port in src/include/libpq/libpq-be.h , which has: portsrc/include/libpq/libpq-be.h struct Port ,具有:

char        md5Salt[4];     /* Password salt */

This is set by ConnCreate in src/backend/postmaster/postmaster.c : 这是由ConnCreatesrc/backend/postmaster/postmaster.c

    /*
     * Precompute password salt values to use for this connection. It's
     * slightly annoying to do this long in advance of knowing whether we'll
     * need 'em or not, but we must do the random() calls before we fork, not
     * after.  Else the postmaster's random sequence won't get advanced, and
     * all backends would end up using the same salt...
     */
    RandomSalt(port->md5Salt);

Now, passwords are verified in md5_crypt_verify in src/backend/libpq/crypt.c . 现在,在src/backend/libpq/crypt.c md5_crypt_verify中验证密码。 There we see that passwords already stored as md5 are hashed again with the session salt: 在那里,我们看到已经存储为md5的密码再次被会话盐散列:

        if (isMD5(shadow_pass))
        {
            /* stored password already encrypted, only do salt */
            if (!pg_md5_encrypt(shadow_pass + strlen("md5"),
                                port->md5Salt,
                                sizeof(port->md5Salt), crypt_pwd))
            {
                pfree(crypt_pwd);
                return STATUS_ERROR;
            }
        }

Thus the hashed password sent on the wire is protected against replay attacks by the session salt. 因此,通过会话盐可以保护在网络上发送的哈希密码免受重放攻击。


Whether the client app can recognise a pre-hashed password and the format it expects them to be in depends on the client library. 客户端应用程序是否可以识别预加密的密码以及密码的预期格式取决于客户端库。

According to pg_password_sendauth in src/interfaces/libpq/fe-auth.c the libpq front-end doesn't seem to check for pre-hashed password input. 根据src/interfaces/libpq/fe-auth.cpg_password_sendauth ,libpq前端似乎没有检查预哈希输入的密码。 Other clients may vary. 其他客户可能会有所不同。

Just to be clear - md5 hashing is not encryption. 只是要清楚md5哈希不是加密。

19.3.2. 19.3.2。 Password authentication 密码认证

The password-based authentication methods are md5 and password . 基于密码的身份验证方法是md5password These methods operate similarly except for the way that the password is sent across the connection, namely MD5-hashed and clear-text respectively . 除了通过连接发送密码的方式( 分别为MD5哈希和明文)以外,这些方法的操作方式相似。

If you are at all concerned about password "sniffing" attacks then md5 is preferred. 如果您完全担心密码“嗅探”攻击,则首选md5。 Plain password should always be avoided if possible. 如果可能,应始终避免使用纯密码。 However, md5 cannot be used with the db_user_namespace feature. 但是,md5不能与db_user_namespace功能一起使用。 If the connection is protected by SSL encryption then password can be used safely (though SSL certificate authentication might be a better choice if one is depending on using SSL). 如果连接受SSL加密保护,则可以安全地使用密码(尽管SSL证书身份验证取决于使用SSL可能是更好的选择)。

PostgreSQL database passwords are separate from operating system user passwords. PostgreSQL数据库密码与操作系统用户密码分开。 The password for each database user is stored in the pg_authid system catalog. 每个数据库用户的密码存储在pg_authid系统目录中。 Passwords can be managed with the SQL commands CREATE USER and ALTER USER, eg, CREATE USER foo WITH PASSWORD 'secret'. 可以使用SQL命令CREATE USER和ALTER USER管理密码,例如,使用PASSWORD'secret'创建CREATE USER foo。 If no password has been set up for a user, the stored password is null and password authentication will always fail for that user. 如果未为用户设置密码,则存储的密码为null,并且该用户的密码身份验证将始终失败。

If you configure your Postgres client authentication file ( pg_hba.conf ) for md5 password-based authentication , you don't need to explicitly use md5() function to keep database password in your property file. 如果为基于 md5 密码的身份验证配置了Postgres客户端身份验证文件pg_hba.conf ),则无需显式使用md5()函数将数据库密码保留在属性文件中。

For encrypting purposes - you can configure database connection to work over SSL. 出于加密目的-您可以配置数据库连接以通过SSL工作。 Please check Secure TCP/IP Connections with SSL . 请检查使用SSL的安全TCP / IP连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM