简体   繁体   English

PHP文件将密码存储为文本文件

[英]PHP File Storing Password as a Text File

For my very small website that only friends know of, I am developing a password system. 对于我的小网站,只有朋友知道,我正在开发一个密码系统。 I am saving the passwords in a php file like this. 我将密码保存在这样的php文件中。

<?php
if($_GET["p"]=="password"){
?>
user1 password1
user2 password2
<?php
}
?>

Then, I can read the password with file_get_contents(pass.php?p="password"); 然后,我可以用file_get_contents读取密码(pass.php?p =“password”); And I can still write the password by writing to the php file just like I would a text file. 我仍然可以像写文本文件一样写入php文件来编写密码。 Is this method insecure? 这种方法不安全吗? Are there similar methods that are that do not require a database? 是否有类似的方法不需要数据库? I don't think my site needs encryption, it wouldn't hurt too much if the passwords were compromised. 我不认为我的网站需要加密,如果密码被泄露,它不会受到太大伤害。

Whoa. 哇。 I would say it's quite insecure. 我会说这很不安全。 Is there a reason why for not using a database? 有没有使用数据库的原因?

Also alternatively, to make it more secure without a database if you wish, you could create a combination of username+password with md5 and/or sha1 encode and save it as an extensionless file in a directory, and compare if the combination is right with file_exists 另外,如果您希望在没有数据库的情况下使其更安全,您可以创建用户名+密码与md5和/或sha1编码的组合,并将其保存为目录中的无扩展名文件,并比较组合是否正确文件已存在

Check out the site http://www.hackthissite.org It allows you to do real life examples of how people hack websites. 查看网站http://www.hackthissite.org它允许您做人们如何破解网站的真实例子。 One of the basic missions is learning to get passwords from an included text file. 其中一项基本任务是学习从包含的文本文件中获取密码。 If anyone figures out the whereabouts of the text file. 如果有人弄清楚文本文件的下落。 Hidden files can be found easily, then they will have access to all of your passwords. 可以轻松找到隐藏文件,然后他们就可以访问您的所有密码。 It's up to you, you might as well not even have a login system if you don't care about security. 这取决于你,如果你不关心安全性,你甚至可能没有登录系统。 If you do care though, just get mysql, it's free and it's not hard to use. 如果您确实关心,只需获取mysql,它是免费的并且它并不难使用。

These are few suggestion for improving your current method. 这些是改善现有方法的一些建议。

  • you can make direct access to this file is restricted, using apache rewrite module as I remember. 您可以使用apache重写模块来限制直接访问此文件,我记得。

  • Use a complex password (phrase with special chars and nums) as the main password use for the page access. 使用复杂的密码(带有特殊字符和词组的短语)作为页面访问的主密码。

  • For the main password also, compare the md5 or sha1 hash value of the password and the md5 and sha1 hash value of the value of GET variable instead of putting plain text there. 对于主密码,还要比较密码的md5或sha1哈希值以及GET变量值的md5和sha1哈希值,而不是将纯文本放在那里。

  • Store md5 or sha1 hashes of the passwords instead of storing plain text password. 存储密码的md5或sha1哈希值,而不是存储纯文本密码。 then when comparing the passwords, you can convert the user entered value to the hash of that value and compare it with the stored one. 然后在比较密码时,您可以将用户输入的值转换为该值的哈希值,并将其与存储的值进行比较。

  • When hashing, you can use a seed for that as I remember, which will increase the security little bit more. 当进行散列时,你可以使用种子,因为我记得,这会增加安全性。

This will make it little bit more secure, because if someone logged to the server, he cannot directly get the password text of your users. 这会使它更安全一些,因为如果有人登录到服务器,他就无法直接获取用户的密码文本。 But please be aware that most of the hash values can be broken using a rain bow table. 但请注意,大多数哈希值都可以使用雨弓表来打破。 But this is at least more secure than storing a plain text password. 但这至少比存储纯文本密码更安全。

But I still prefer if you can store your data in a database instead of a file like this. 但我仍然更喜欢你是否可以将数据存储在数据库而不是像这样的文件中。

It doesn't matter whether you store the passwords in a file or in a database, the same measures should be taken. 无论是将密码存储在文件中还是存储在数据库中,都应采取相同的措施。 Do not store the plaintext password there, instead store only the hash-value of the password. 不要在那里存储明文密码,而只存储密码的哈希值 So even if somebody can read the file, he doesn't know the passwords. 因此,即使有人可以阅读该文件,他也不知道密码。 As a side effect, it will also avoid encoding problems with special characters. 作为副作用,它还可以避免使用特殊字符编码问题。

Use a slow key-derivation function like BCrypt to hash the passwords. 使用像BCrypt这样的慢键派生函数来散列密码。 Fast hash functions like MD5 or SHA-512 are not appropriate for hashing passwords, simply because an attacker can brute-force them ways too fast (about 8 Giga MD5 values per second with common hardware). 像MD5或SHA-512这样的快速哈希函数不适合散列密码,因为攻击者可以过快地强制它们(使用通用硬件每秒大约8 Giga MD5值)。

PHP soon offers two simple functions password_hash() and password_verify() to generate such BCrypt values. PHP很快提供了两个简单的函数password_hash()password_verify()来生成这样的BCrypt值。 There also exists functions for older PHP versions, have a look at this answer . 还有旧版PHP版本的功能,看看这个答案

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

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