Hello folks,
Today i want to show you how you can implement a secure way of hashing passwords of your users.
For this, we use an external cryptography library. In this case it's Libsodium.
Why should you take into consideration to implement this?
It's a common habit that the average user signing up to a Service uses the same password over and over. In case of a database leak (which happens quite often in this scene), any malicious person will be able to crack these passwords (bruteforce, dictionary attacks, rainbow tables) with little effort, because of the use of unsalted and unsuitable Hashes by default. To be clear: Hash functions like MD5, SHA1 and even SHA2 are NOT suitable for password hashing. They are perfectly fine for other things, but not passwords. For this purpose, there are own algorithms like argon2, bcrypt, PBKDF2, which will also add a random generated salt for you. These Hashes have amongst other things, the property to be really compute-intensive, what makes them not really worth to crack, especially because they use a salt. You as Server-Admin are responsible to not put your users into jeopardy by running insecure Systems. A leak of a database containing poor hashed passwords is more catastrophic than you might think.
Why Libsodium?
It's a modern and easy-to-use Crypto Library. Most traditional Crypto Libraries are not that straightforward to implement and use, which can lead to additional vulnerabilities.
Keep in mind however, this not limited to just Libsodium. Use any library supporting password hashing algorithms, as you like.
HowTo
In advance, this guide is not intended to be implemented on a production server! For this guide, you need to rehash every password to get it up and running. A standard leaked game core source uses the PASSWORD() function from MySQL by default, which utilizes SHA1 twice in the background, This is what we are gonna replace now. I'm using here the Public Martysama Files 4.9. This should work similar on other Sources, but i cannot guarantee that. Please backup your files, before modifying them.
First we grab a fresh copy of the Libsodium source and then compile it.
Notice that you need to enter the commands below on a 32Bit FreeBSD, because the game core is also a 32Bit binary. Another approach, which i didn't tested would be to pass some additional 32Bit Parameters to the ./configure command, so the output will be a 32Bit binary on a 64Bit FreeBSD (otherwise you get an incompatible 64Bit binary).
Lets start:
Next open Server/game/src/Makefile (path may differ) and add the following below the OpenSSL lib block:
The above code gives the linker the instruction to link Libsodium statically.
Now open Server/game/src/utils.cpp and add the following header and function:
Open Server/game/src/utils.h and add the corresponding declaration:bool hash_secure_verify(const char *, const char *);
Open Server/game/src/db.cpp and do the following changes:
increase the size to 128 (max length of the hash):
to:
To match the variable names to the next changes, I renamed them. Replace the new names to all occurrences in this file.
In the same File replace:
with:
Further down remove:
Also replace:
with:
Open Server/game/src/input_auth.cpp and search for the third occerances of:
- // @fixme138 1. PASSWORD('%s') -> %s 2. szPasswd wrapped inside mysql_hash_password(%s).c_str()
- DBManager::instance().ReturnQuery(QID_AUTH_LOGIN, dwKey, p,
- "SELECT '%s',password,securitycode,social_id,id,status,availDt - NOW() > 0,"
- "UNIX_TIMESTAMP(silver_expire),"
- "UNIX_TIMESTAMP(gold_expire),"
- "UNIX_TIMESTAMP(safebox_expire),"
- "UNIX_TIMESTAMP(autoloot_expire),"
- "UNIX_TIMESTAMP(fish_mind_expire),"
- "UNIX_TIMESTAMP(marriage_fast_expire),"
- "UNIX_TIMESTAMP(money_drop_rate_expire),"
- "UNIX_TIMESTAMP(create_time)"
- " FROM account WHERE login='%s'",
- mysql_hash_password(szPasswd).c_str(), szLogin);
Change:
to:
We need the Password in Plaintext here, so we can verify it with hash_secure_verify().
That should be all for the C++ part. Recompile the core now.
From now the core will verify your passwords with the Argon2id password hash function.
The next step is the database. The default password field in the account/account table is a VARCHAR(42), which is to small for our use. It needs to be adjusted to 128. To do that, execute the following Query:
Now to work with this new implementation, we need to generate the new hashes.
In PHP there are two handy functions for this:
password_hash() - Bitte melden Sie sich an, um diesen Link zu sehen.
password_verify() - Bitte melden Sie sich an, um diesen Link zu sehen.
An example of the hashing logic for a SignUp Page:
If you want to build some Login-System, you can use password_verify().
An example:
PHP 7.3 was used in the above examples.
Last words
I hope this guide was clear enough. It's not a guide to 100% Security (in fact there is no), but it's a step in the right direction.
Last but not least, if you have any contributions/corrections don't hesitate to post them (I also understand German).
Sincerely,
Lain0