简体   繁体   English

在方法中将密码存储为本地常量的安全隐患?

[英]Security implication of storing a password as a local constant in a method?

I would like to educate myself on the subject of most common security anti-patterns. 我想就最常见的安全性反模式进行自我教育。

Hypothetically speaking, what are the security risks of storing a password in the following way: 假设地说,以以下方式存储密码有哪些安全隐患:

interface

type
  TFoo = class
   procedure DoSomething;
  end;

Implementation

procedure TFoo.DoSomething;
 const
   Password = 'Something';
begin 

end;

I know that the correct way would be to use a hashing algorithm to hash the password and save that value in an external file, however I'm really interested in how the password in the example is exposed to a malicious 3rd party. 我知道正确的方法是使用哈希算法对密码进行哈希处理并将该值保存在外部文件中,但是我对示例中的密码如何暴露给恶意的第三方非常感兴趣。

The password will be visible by examining the data in the executable. 通过检查可执行文件中的数据可以看到密码。 Eg use a hex editor or even notepad. 例如,使用十六进制编辑器甚至记事本。

I would like to educate myself... 我想教育自己...

In addition to egur's correct answer , to answer the unasked question whether any password-stored-in-source-solution would do: 除了egur的正确答案之外 ,还可以回答未提出的问题,即任何密码存储在源代码中的解决方案是否可以:

As already explained, a constant String appears literally in the executable. 如前所述,常量String实际上出现在可执行文件中。

Alternative 1: 选择1:

const
  PW0 = 'pas';
  PW1 = 'swo';
  PW2 = 'rd';
var
  PassWord: String;
...
initialization
  PassWord := PW0 + PW1 + PW2;

Result: 'Password' in executable. 结果:可执行文件中的“密码”。

Alternative 2: 选择2:

const
  PW2 = 'rd';
  PW1 = 'swo';
  PW0 = 'pas';
var
  PassWord: String;
...
initialization
  PassWord := PW0 + PW1 + PW2;

Result: 'Password' in executable. 结果:可执行文件中的“密码”。

Alternative 3: 选择3:

var
  PW2: String = 'rd';
  PW1: String = 'swo';
  PW0: String = 'pas';
  PassWord: String;

initialization
  PassWord := PW0 + PW1 + PW2;

Result: 'rd', 'swo' and 'pas' in executable. 结果:可执行文件中的“ rd”,“ swo”和“ pas”。

Try around yourself some more. 再多尝试一下。 Open the executable in Notepad and see for yourself. 在记事本中打开可执行文件,然后自己查看。

Besides, and needless to say, the above practices are no advice but have only complementary value to the question. 此外,不用说,上述做法不是建议,而只是对该问题的补充价值。

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

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