PHP Velho Oeste 2024

Verschlüsseltes Speichermodell

SSL/SSH schützt zwar Daten, die vom Client zum Server übertragen werden, aber SSL/SSH schützt keine dauernd in einer Datenbank gespeicherten Daten. SSL ist ein "auf-der-Leitung"-Protokoll.

Hat ein Angreifer direkten Zugriff auf Ihre Datenbank (unter Umgehung des Webservers), können gespeicherte sensible Daten aufgedeckt oder zweckentfremdet werden, außer wenn die Informationen durch die Datenbank selbst geschützt sind. Die Daten zu verschlüsseln ist eine gute Methode, diese Gefahr zu mildern, doch bieten nur wenige Datenbanken diese Art der Datenverschlüsselung an.

Der einfachste Weg, dieses Problem zu umgehen ist, erst einmal Ihr eigenes Verschlüsselungspaket zu erstellen, und dieses dann in Ihren PHP-Skripten zu nutzen. PHP kann Ihnen in diesem Fall mit seinen verschiedenen Erweiterungen helfen, wie z. B. OpenSSL und Sodium, welche eine große Auswahl an Verschlüsselungsalgorithmen abdecken. Das Skript verschlüsselt die Daten vor dem Speichern und entschlüsselt diese wieder beim Erhalt. Siehe die Verweise für weitere Beispiele, wie Verschlüsselung funktioniert.

Hashing

Im Fall von wirklich versteckten Daten, wenn deren unverschlüsselte Darstellung nicht nötig ist (d. h. die nicht angezeigt werden sollen), ist Hashing ebenfalls überlegenswert. Das bekannte Beispiel für Hashing ist das Speichern des kryptographischen Hashes eines Passwortes in einer Datenbank, anstatt des Passworts selbst.

Die Passwort-Funktionen bieten eine bequeme Möglichkeit, sensible Daten zu hashen und mit diesen Hashes zu arbeiten.

password_hash() wird verwendet, um eine gegebene Zeichenkette unter Verwendung des stärksten zurzeit verfügbaren Algorithmus zu hashen, und password_verify() prüft, ob ein gegebenes Passwort mit dem Hash, der in der Datenbank gespeichert ist, übereinstimmt.

Beispiel #1 Hashen eines Passwortfeldes

<?php

// Speichern des Passwort-Hashes
$query = sprintf("INSERT INTO users(name,pwd) VALUES('%s','%s');",
pg_escape_string($username),
password_hash($password, PASSWORD_DEFAULT));
$result = pg_query($connection, $query);

// Abfragen, ob der User das richtige Passwort übermittelt hat
$query = sprintf("SELECT pwd FROM users WHERE name='%s';",
pg_escape_string($username));
$row = pg_fetch_assoc(pg_query($connection, $query));

if (
$row && password_verify($password, $row['pwd'])) {
echo
'Willkommen, ' . htmlspecialchars($username) . '!';
} else {
echo
'Authentifizierung für ' . htmlspecialchars($username) . 'fehlgeschlagen.';
}

?>
add a note add a note

User Contributed Notes 5 notes

up
28
Reiner
13 years ago
Using functions to obfuscate the hash generation does not increase security. This is security by obscurity. The algorithm used to hash the data needs to be secure by itself.

I would not suggest to use other data as salt. For example if you use the username, you won't be able to change the values without rehashing the password.

I would use a dedicated salt value stored in the same database table.

Why? Because a lot of users use the same login credentials on different web services. And in case another service also uses the username as salt, the resulting hashed password might be the same!

Also an attacker may prepare a rainbow table with prehashed passwords using the username and other known data as salt. Using random data would easily prevent this with little programming effort.
up
33
seigoryu at hotmail dot de
11 years ago
I would strongly recommend using SHA-2 or better the new SHA-3 hash algorithm. MD5 is practically unusable, since there are very well working rainbow tables around the whole web. Almost the same for SHA-1. Of course you should never do a hash without salting!
up
7
somebody
17 years ago
A better way to hash would be to use a separate salt for each user. Changing the salt upon each password update will ensure the hashes do not become stale.
up
-5
about2mount at gmail dot com
8 years ago
It's difficult to post scripts here for all to view on the subject of best security practices. But i would wish to point out that using a salt with a randomized and odd numbered long length salt value is do_able with two Php functions while retrieving and separating the salt when it comes out using simple math functions. But with everything we add we also have to think of the constant standardized login systems we stay behind with.

For one,,, adding and validating two to four passwords is not a bad idea.  Also having no username or email going in. They can be stored after the user logs in after the validation process.  It is possible to store the email on the first signup and only at that time. And if the user loses his passwords then validate by email only upon request within a contact form by a validated phone number stored in the database,, and then via their email account.
up
-18
Fairydave at the location of dodo.com.au
18 years ago
I think the best way to have a salt is not to randomly generate one or store a fixed one. Often more than just a password is saved, so use the extra data. Use things like the username, signup date, user ID, anything which is saved in the same table. That way you save on space used by not storing the salt for each user.

Although your method can always be broken if the hacker gets access to your database AND your file, you can make it more difficult. Use different user data depending on random things, the code doesn't need to make sense, just produce the same result each time. For example:

if ((asc(username character 5) > asc(username character 2))
{
   if (month the account created > 6)
      salt = ddmmyyyy of account created date
   else
      salt = yyyyddmm of account created date
}
else
{
   if (day of account created > 15)
      salt = user id * asc(username character 3)
   else
      salt = user id + asc(username character 1) + asc(username character 4)
}

This wont prevent them from reading passwords when they have both database and file access, but it will confuse them and slow them up without much more processing power required to create a random salt
To Top