如何在命令中隐藏密码 "java -Djasypt.encryptor.password=somepassword -jar name.jar"

How to hide the password in the command "java -Djasypt.encryptor.password=somepassword -jar name.jar"

我正在使用 Jasypt 加密并在属性文件的 ENC() 中指定 属性 值。解密密码通过这样的命令行参数发送 java -Djasypt.encryptor.password=somepassword -jar name.jar。一切正常,但问题是当我搜索 运行 进程时,它也显示了密码。有没有办法通过从某处读取来隐藏加密密码?

我考虑过使用环境变量,但这也可能会暴露密码。所以,决定反对。

更新: 在另一个 SO post 中有一个解决方案

我遵循的解决方案是创建一个名为 JASYPT_ENCRYPTOR_PASSWORD 的环境变量,执行命令 java -jar name.jar 然后取消设置环境变量。这符合我的预期。

您绝对可以使用 Jasypt 读取存储在属性文件中的 EncryptableProperties,方法如下:

datasource.username=reportsUser 
datasource.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm) 

然后您可以从您的程序中检索属性(包括加密的和未加密的):

StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();     
encryptor.setPassword("jasypt"); // could be got from web, env variable...    

Properties props = new EncryptableProperties(encryptor);  
props.load(new FileInputStream("/path/to/my/configuration.properties"));

String datasourceUsername = props.getProperty("datasource.username");

String datasourcePassword = props.getProperty("datasource.password");

请注意,在上面的示例中,未加密和加密属性的检索方式相同,而 Jasypt 会自动处理 decryption/encryption。

这是相关 Jasypt 文档的 link: http://www.jasypt.org/encrypting-configuration.html

SO 中有一个类似的问题,有更多详细信息:

The solution what I followed was to create an environment variable with the name JASYPT_ENCRYPTOR_PASSWORD, execute the command java -jar name.jar and then unset the environment variable. This worked as I intended.

设置一个环境变量,时间短与否并不重要,这不是一个好主意。即使是最短的时间也足以让攻击者访问它。有类似的攻击,其中可以预测临时文件的名称并允许攻击者创建具有该名称的 link 例如访问 /etc/passwd-file,等等

这个问题已经存在了一段时间,所以已经有几个解决方案,其中大部分都使用包含密码的文件或用于对敏感数据进行加密和解密的密钥库。

如果你看在 JBoss 他们使用一种叫做保险库的东西。有个manual page explaining the single steps. You can recreate all of this for your application or just read the plain text password from a file where you specify the filename e.g. as system property. The file itself must be secured against unauthorized access on the file system level (set the read permission for the owner only) and - if necessary - within your application by setting a SecurityManager that denies access to this file by other classes than the one that is used to read it in. This is common practice with security relevant applications like PGP or OpenSSL.