Open Source v. Security lazarus / 21-General ---------------------------------------------------------------------- Spoonhorse · 2021-11-02 17:20 There must be a standard way of dealing with this, it must come up all the time. But I don't know what it is. I want to make my project open-source once I've refactored the more shameful parts of the codebase. But one of the units connects to a PHP/SQL backend, and that bit knows the password to get in. So in order for people to be able to debug the code properly I'd need a convenient way for them to be able to use that bit but not read the source code. I have a further concern that however I split that bit off, I'm advertising to potential hackers that this is where the crucial password is stored. (And there's a reasonable chance that I'll get lots of users, I at least believe it, I've bought a logo and a domain name and am going to try and go big. So I should take that seriously.) Thanks for your help. ---------------------------------------------------------------------- SymbolicFrank · 2021-11-02 17:36 Security through obscurity and Open Source don't go well together. Do they connect to the same database on the same server? Either put a back-end in front or make an SQL file that allows them to create their own database. ---------------------------------------------------------------------- Spoonhorse · 2021-11-02 18:00 It's not security-through-obscurity though, is it? I'm happy for everyone to know everything about the system except the password. But how do I keep that safe? Yes, it all has to be the same database. It's how the users pass data to one another, that's all it's for. I'm sorry, but I don't understand your advice about "put a back-end in front". Even though this is 2021 and I am all grown up, this is the first application I've ever written that has a backend. I'm kind of a n00b here. ---------------------------------------------------------------------- MarkMLl · 2021-11-02 18:13 The user has to provide something. Any attempt to hide login credentials in your program, whether in source or branded into the binary, is security by obscurity and will fail given a moderately- competent opponent. There was a tedious thread about this a couple of weeks ago, started by somebody who didn't understand the difference between a (non- reversible) hash function and (reversible) encryption. I'd rather not attempt to summarise that, but basically good practice dictates something like * A user chooses a login ID and passphrase. * The passphrase is hashed, the hash is stored locally, and it is this that's used as a login password. Now if you're writing your own frontend, there's a lot you can do. For example, when the user chooses his ID you can issue him with a temporary passphrase which is hashed etc. and used to create a new user/role on the backend database, and that has implicit rights to the shared storage (I've done that for PostgreSQL and Firebird, and whilst hard work it was doable). BUT you'll still have the problem of finding a secure way for your frontend program to tell the backend that it should be granted user- creation rights. Some sort of private/public key arrangement is one possibility, something like CHAP is another, but in all cases you end up with /something/ being shipped out with the code that an adversary could use. Hope that's coherent, long day exacerbated by last weekend's DST change. MarkMLl ---------------------------------------------------------------------- Warfley · 2021-11-02 18:38 Simple answer, you can't. Either every user should use the password, in which case the password can't be secret, or the password is secret in which case the users can't use it. A password can never be secret and also publicly available for everyone to use. Simple as that. ---------------------------------------------------------------------- Handoko · 2021-11-02 19:52 Maybe, move the part you want to hide into a compiled library (dll or so). ---------------------------------------------------------------------- Mr.Madguy · 2021-11-02 20:06 I don't see problem here. Users should configure database by themselves, i.e. install DB provider and have their own passwords to access it. I.e. you can simply remove password from your sources. What you should provide - is SQL template to init your database. ---------------------------------------------------------------------- MarkMLl · 2021-11-02 20:27 Quote from: Handoko on November 02, 2021, 08:52:40 pmMaybe, move the part you want to hide into a compiled library (dll or so). At which point the user asks for the source, or puts it through binwalk and a disassembler. However that's an interesting question: can an open source program rely on a DLL (.so in unix terms)? I very much suspect that the FSF would argue strongly against it, but I don't think they could stop it from relying on shelling out to run a program which supplied the credentials... which any damnfool would then get from tracing execution making the exercise pretty pointless. Bottom line: you can't ship the credentials with the program. You need to supply them separately once you have some sort of contractual relationship with the user, and each user needs separate credentials so that he doesn't simply give them to somebody else. MarkMLl ---------------------------------------------------------------------- SymbolicFrank · 2021-11-02 20:32 RSA would work if you don't want supervision, but simply activating a username/password combo for all users is much easier. ---------------------------------------------------------------------- Warfley · 2021-11-02 21:39 Quote from: SymbolicFrank on November 02, 2021, 09:32:08 pmRSA would work if you don't want supervision, but simply activating a username/password combo for all users is much easier. RSA won't help here, because this is not an issue of confidentiality (i.e. encryption) but authentication. While you can use asymetrical encryption for authentication (via a cryptographic challenge like in the FIDO protocol), this does not solve the problem, because then you need to solve the problem of where to store the secret key. ---------------------------------------------------------------------- SymbolicFrank · 2021-11-02 21:58 The public key would distinguish the user. So it would be equivalent to a username. ---------------------------------------------------------------------- Warfley · 2021-11-02 22:11 If it's like a username, why not simply use a username? I mean sure you can use pk cryptography for authentication, it has some advantages and disadvantages and can be quite useful in some scenarios, but it doesn't solve the problem at hand. The problem is you can't have an authentication mean be publicly available and secret at the same time, so the question is more, what Spoonhorse actually wants to accomplish with the password and if there might be a different approach that solves this problem on a conceptual level ---------------------------------------------------------------------- dbannon · 2021-11-02 22:49 Spoonhorse, you perhaps need to be clearer in what you are trying to achieve. Maybe its a question of what you intend to sell ? Obviously not the client itself, but access to the server ?  Or will you make money from showing people ads ? Is it your intention that someone can download your open source client and use it to connect to your server ? For free ?  In that case, no password needed.  (obviously, separate infrastructure for management, maybe user accounts).  Can they change your client ?  For example, I hate dark backgrounds, can I download your client, alter the colours, compile and run ? Or can I replace all the code between "program" and "end."  ?  Compile and still connect ? On the other hand, if you don't want people connecting with modified versions of your client, then you really need to rethink your whole model. Having a hidden password will not protect from that. Davo ---------------------------------------------------------------------- Thaddy · 2021-11-04 09:17 Quote from: SymbolicFrank on November 02, 2021, 10:58:35 pmThe public key would distinguish the user. So it would be equivalent to a username. That is absolutely correct. When I was still working for a bank, we used certificates for the developers to code and debug to circumvent the password issue,  The private key not accessible to end-users, although those would use 2 factor authentication. ---------------------------------------------------------------------- Spoonhorse · 2021-11-04 17:16 Just wanted to say thanks for your comments and I see how I got muddled. I am unmuddled. I was trying to achieve things the wrong way but it's a simple fix. ---------------------------------------------------------------------- MarkMLl · 2021-11-04 17:43 Glad to hear that. What have you decided on? MarkMLl ---------------------------------------------------------------------- Spoonhorse · 2021-11-04 18:39 It had just been so long since I hacked out the basic internet stuff that I'd forgotten how half of it works and how the security's mean to work. Let's see if I've got this straight. So long as the user has to send their own password with every transaction and have it verified against the hashed version stored in the database, then a successful hacker needs to either: (a) Find out their username and password. (b) Know my credentials on the server and sign in as me and edit my backend. Have I got that right? ---------------------------------------------------------------------- MarkMLl · 2021-11-04 21:09 Quote from: Spoonhorse on November 04, 2021, 07:39:15 pmIt had just been so long since I hacked out the basic internet stuff that I'd forgotten how half of it works and how the security's mean to work. Let's see if I've got this straight. So long as the user has to send their own password with every transaction and have it verified against the hashed version stored in the database, then a successful hacker needs to either: (a) Find out their username and password. (b) Know my credentials on the server and sign in as me and edit my backend. Have I got that right? OK, there's three things I'd suggest there. First, if the user is giving their password to a custom program which is then logging in on their behalf, you'd do better if that client (cryptographically) hashed the password before sending it to the server, irrespective of what the server does. That reduces the risk that you could be contributing to the problems of a naive user who uses the same password on everything he attaches to. Second, if any of your software is handling a user's password (including hashing it) then every string MUST be overwritten with rubbish before it has its length changed or goes out of scope, since the FPC string handling doesn't do this itself and could potentially leave sensitive data on the heap. I can't comment on whether the LCL takes similar precautions, but I've never seen it discussed. Third, do not under any circumstances use your personal credentials for the database administrator. Set up a separate account for that, called pgadmin or whatever and with its own password. That reduces the risk that if that particular program and/or connection is cracked the information can be used to attack you or parallel projects, and that if you are hacked it compromises everything that you've set up. Finally, there are advantages to giving each user some form of login token (variously called a certificate, public key and so on) rather than relying on their entering a username and password: as an example see what can be done with ssh. You can combine various ideas from the above by first having them contact the backend using a username and freshly-generated password, at which point the frontend is given a token which it is to subsequently use for login... but this must under no circumstances be something that can be easily decrypted to the information that they originally supplied, and you'll need a "lost token" recovery mechanism. Many of today's evils are caused by the way MS et al. embed credentials of various kinds into OSes etc. which are to be updated. Specifically, the use the /same/ credentials for all OSes shipped to users with whom they do not have a one-to-one contractual relationship. You NEED to identify and distinguish between your users. MarkMLl ---------------------------------------------------------------------- PierceNg · 2021-11-05 01:46 Quote from: Spoonhorse on November 04, 2021, 07:39:15 pmIt had just been so long since I hacked out the basic internet stuff that I'd forgotten how half of it works and how the security's mean to work. Let's see if I've got this straight. So long as the user has to send their own password with every transaction and have it verified against the hashed version stored in the database, then a successful hacker needs to either: (a) Find out their username and password. (b) Know my credentials on the server and sign in as me and edit my backend. Have I got that right? If you are talking about a 3-tier setup - web browser <-> web server running PHP <-> database - then usually the authentication is also in two parts. The user at the web browser authenticates with the PHP, and PHP authenticates to the database. For PHP<->database, this is configured by you and will involve some kind of storing credentials in plain text. Then you'd use platform facilities like operating system file permissions to protect said credentials file. The modern cloud-native approach injects said credentials via a "secret manager" so that no human ever gets to eyeball the secret bits. For user<->PHP, typically the relevant user data is stored within the database just as regular data. User enters username + password on a web form, PHP basically compares the user-entered data with that in the database. Other posters have suggested how to improve the security of the stored authentication data. If you are thinking about opening up a database to allow access from anywhere on the Internet (even with a network firewall in between doing IP address access control, and/or an application firewall inspecting the content of the traffic that gets through), then don't. That's just asking for trouble. ---------------------------------------------------------------------- Spoonhorse · 2021-11-05 13:42 PierceNg, thanks. That's basically what I have except I didn't know about a "secret manager", that sounds like a cool thing to have. PHP and MariaDB, salted and hashed passwords in the database, and the PHP knows the password to the database. And no-one can look at the PHP without my password to the server it lives on. ---------------------------------------------------------------------- Spoonhorse · 2021-11-05 13:51 MarkMLI, thank you, those are useful thoughts. Since you're an expert, I have been wondering about something else lately. This most sensitive items to be stored are just the passwords themselves (because people will re-use them) and the email addresses. Under those circumstances, would it be a good idea to hash the email addresses? It seems like a good idea but then so did a lot of dumb things and I'm a rank amateur, so ... ---------------------------------------------------------------------- MarkMLl · 2021-11-05 14:22 Quote from: Spoonhorse on November 05, 2021, 02:51:13 pmMarkMLI, thank you, those are useful thoughts. Since you're an expert, I have been wondering about something else lately. This most sensitive items to be stored are just the passwords themselves (because people will re-use them) and the email addresses. Under those circumstances, would it be a good idea to hash the email addresses? It seems like a good idea but then so did a lot of dumb things and I'm a rank amateur, so ... You're welcome, but I'm hardly an expert: just somebody who listens to what other people are saying is acceptable practice. However, DO NOT EVER store a passphrase "in clear", that's the sort of thing that- as an example- TalkTalk in the UK got fined £400,000 for doing. And explore every possible alternative before storing an email address without hashing or encryption, since it is "personally identifiable information" which you have an obligation to protect. If you can't organise a custom client that logs in securely (hashing on the client system and/or doing some form of cryptographic handshake and/or using non-passphase tokens such as a public/private key pair) then (a) make sure that traffic is carried securely over the Internet by- at an absolute minimum- using https rather than http and (b) run the user's passphrase through a decent cryptographic hash and store the result (making sure, as I've already said, than intermediate storage is cleared before being released). If you've got (a) and (b) right, then the next time the user submits his passphrase (over https etc.) hash it and compare the result against what you've got stored. Note that a "send password to recovery email address" normally sends a freshly-generated password, not the existing one. Consider whether you can get your customers to re-enter their recovery email address if they've lost their passphrase, in which case you can compare it against the hash of the original one rather than against the original stored "in clear". If you can't, then investigate whether the backend database you're using supports encrypted (columns in) tables, and/or use an encrypted filesystem... but make damn sure that nobody can reach that database via a handcrafted URI or by some sort of SQL injection. Obligatory xkcd: https://xkcd.com/327 MarkMLl ---------------------------------------------------------------------- Spoonhorse · 2021-11-05 15:04 Thanks. I knew what to do with passwords but I was worried that hashing emails too might be a bright idea of my own which would somehow introduce a vulnerability. (If you knew someone's email address, then you could make another address with the same hash value, and ask to change their password? It wouldn't get you their old password, but you could see their (totally non-personal data) and lock them out of their account ... it would be a lot of trouble for you to go to for that ...) I'm safe from SQL injection, I use prepared statements all through the PHP. ---------------------------------------------------------------------- MarkMLl · 2021-11-05 15:17 Two things there. First, if you use a decent cryptographic has function, then you will be hard pressed to make any sort of predictable email address that matches an existing hash. Second, the email address will only be used to send a new password to an existing user; if you store a hashed address then it must match the address entered by the user in the same way that a hashed password must match (this might, of course, be too much for must users but you can help a little by e.g. switching to all-lowercase before hashing). MarkMLl ---------------------------------------------------------------------- Warfley · 2021-11-05 15:20 Quote from: Spoonhorse on November 05, 2021, 04:04:36 pmThanks. I knew what to do with passwords but I was worried that hashing emails too might be a bright idea of my own which would somehow introduce a vulnerability. (If you knew someone's email address, then you could make another address with the same hash value, and ask to change their password? It wouldn't get you their old password, but you could see their (totally non-personal data) and lock them out of their account ... it would be a lot of trouble for you to go to for that ...) I'm safe from SQL injection, I use prepared statements all through the PHP. Hashing the email address is not a very good idea, because then you can't use it anymore, in which case the question is, why use the email in the first place instead of a username? Ususally online services want to have an email address so that they send emails like newslatters and other crap the user does not want. If you don't need the email address to send emails, don't ask the user for one, simple as that. With regards to the idea that someone could find a hash with the same value, well this is called the hash collision problem and modern cryptographic hash functions are designed to withstand that. The current recommendation (e.g. here in Germany by the BSI TR 02102-1) is to use SHA-256 and above (I usually use SHA-512). Generally speaking, go and look for the official guidelines by your government, to be on the secure side (especially with respect to liability). And if it is lower than what is recommended by NIST (the US governmental body for this sort of stuff), use theirs to be sure even on international markets. ---------------------------------------------------------------------- Warfley · 2021-11-05 18:00 So I was bored and because this is a topic I am highly interested in myself, here is my a way to detailed but still incomplete guide to IT- sec for app developers. Generally speaking to have a secure application must ensure three things: * Authenticity: The data is sent by the person claimed to send it (and the person is allowed to send this data) * Integrity: The data received is what was sent, no one tampered with the data * Confidentiality: No third party can get access to the data. You use HTTP for transmission, this is very easy to use over a TLS channel (as HTTPS) which already secures your integrity and confidentiality. So there is not much to do for you here except to enable HTTPS on the server side. That said, you should configure your server such that it always uses TLS 1.2 and no older versions to make sure to always use the state of the art. So your problem is basically just the authentication, more importantly the authentication of the client towards the server, because the server authentication is already part of TLS (via certificate checking). If you are really paranoid you could also implement custom certificate checking on the client side, such that it only accepts connections to a server where the certificate is signed by the correct CA (and not a self signed certificate, or a stolen certificate from the other side of the world). For example I only use Let's Encrypt certificates, so if I ever connect to my server and the certificate is signed by some other CA, let's say T-Online, I know that something is wrong. But back to topic, how should your client authenticate to the server. This can generally be done in two different ways, first through a shared secret that both the server and the client know. The goal hereby is for the client to prove to the server that they both have the same secret. The second possibility is for the client to have a secret and only needing to prove to the server that it has that secret without the server having to know this secret. Before we discuss these means further, first there is the question of what kind of secret are we talking about. There are three factors for authentication, something you know (usually a password), something you have (something physical like a device, or something virtual on a device like a file) or something you are (biometrics e.g. fingerprint). Maybe you have heard the phase 2 factor authentication, this refers to these factors, e.g. my online banking requires to factors, something I know (my password) and something I have (my tan generator). My tan generator is actually my phone, on which the app also needs a password to unlock, but because this is another knowledge factor (as my bank password is) this is still only considered 2 factors (or 2.5 sometimes) and not 3. Each of these factors have their pros and cons. Something you know is hard to get by against your will, but if leaked, can be used without your knowledge. Something you have can be stolen or lost (but you know it if that happens) and something you are is generally unchangeable (so if your fingerprint is leaked, you can't just get a new finger). Also it should be stated that most consumer grade biometric scanners (mobile phone fingerprint or face scanners) are pretty insecure and can easily be tricked. Generally it is considered the more factors the more secure it is, as they cancel out each others weaknesses, but also the more inconvenient. So you need to ask yourself the question, how important is the data and whats the best way of accessing it. For example, for my phone I mostly only use the fingerprint to unlock, while this is not very secure (most fingerprint sensors are easily tricked), because my phone isn't a high priority target for any hacker, is usually hard to get physical access to (because I carry it around all the time) and it is simply very convenient, I use this single and pretty insecure factor. So this is a question you need to answer for your app, how important is that data and how much security does it need (at the cost of inconvenience). Also you don't want your secret to be easily stolen, this is where hashing comes into place. Hashing functions are one way functions, meaning that you can use them to anonymize data such that the original data can not be rediscovered. Cryptographic hash functions are also considered collision free, so it is very hard to impossible to find other data resulting in the same hash function. So a hash of a value can be considered both unique (i.e. no other reasonable value generates that hash) as well as anonymous (i.e. no possibility ot get back to the data). That said, there is still a problem, that is the same data will always produce the same hash value. The most common password is "123456", the SHA-1 value of which is "7c4a8d09ca3762af61e59520943dc26494f8941b", so if you have a database with passwords that are simply hashed, all you need to do is to search for that hash to find all the accounts that use the password 123456. Do that for the top 1000 most common passwords and you have decoded probably more than half of your password database (Humans are really bad at passwords). That is why you usually should use a so called salt. It is basically a random value you hash together with your password to ensure that ensures that each password hash is different. The salt is usually stored together with the password, so it is not secret, just to spice up the password during computation (thats why it's called salt). I personally always use two hashes with two salts, one client hash, where the salt is the username, and one server hash where the salt is random. So basically the communication is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Client: h_client := Hash(Username, Password);SendLogin(Username, h_client);// Server:UserName, H_Client := ReceiveFromClient;UserEntry := GetUserDatabaseEntry(username);Result := Hash(UserEntry.Salt, H_Client) := UserEntry.PasswordHash;  But there is still the problem of bruteforcing. So while I can't just hash the most used passwords anymore and check them against the whole database at once, I can pick a user and compute Hash(salt, Hash(username, Password)) for the most common passwords and check if they line up. To prevent this, there are special key-derivation functions, which are basically salted hash functions designed to take a long amount of time (i.e. several dozen to hundereds of milliseconds). For the genuine user, this computation will not be noticable (if you wait 0.001 or 0.1 seconds doesn't matter for the user) but when trying out millions of passwords, this will take hours instead of seconds. NIST recommends among others PBKDF2 function as part of RSAs (the company not the cryptosystem) PKCS#5 standard. NIST also names Ballon. Other alternatives, but not directly named by NIST are bcrypt, scrypt or argon2, the last of which actually won the Password Hashing Competition by NIST and will potentially be added to the list of recommended functions by NIST in the future. So generally speaking, whenever you deal with a secret, chances are this secret is used multiple times (e.g. a user using the same password everywhere), so always anonymize it with a salted hash and use this anonymize version as your secret for all the technical stuff. Getting back to the means of authentication. The first scenario is that you have a shared secret and want to prove to the server that you have the same secret. The easiest way of doing this is to simply send over this secret. This is what basically most websites use. You set a password and when logging in, you send that password (salted and hashed) to be checked against what the server has stored. This is very easy and not a bad approach at all. But it could be vulnerable to an eavesdropper recording your communications. Basically if you record the password, you can login to the server by simply re- sending the same data again, a so called replay attack. There are a few different ways of targeting this problem, the most common solution is a so called cryptographic challenge. Basically rather then sending the secret, the server sends you a challenge to complete with your secret, which does not reveal the secret. By sending a different challenge each time, the response by the client required to log in must be also different each time. The challenge is usually a number (so called nonce: number used once) that the client has to do something with. For this, often a MAC (message authentication code) protocol is used. A MAC is basically a signature of the message generated by a shared secret. I.e. you need to know the secret to compute the MAC, and if you receive a MAC you can compute the same MAC using your secret to see if the result is the same. An easy way of doing such a MAC is to simply hash the secret together with the nonce, e.g. sending Hash(secret + Nonce). Note not all hash functions can be used for this, some (I think MD5 but I am not sure) have a vulnerability such that you can compute Hash(a + b) only from knowing Hash(a) and b, making this insecure. To make this more secure there are different approaches using so called padding. The most notable of which is probably HMAC which is used in TLS or IPSec. If you decide to use such a solution I would therefore recommend using HMAC. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Server:SendToClient(Nonce);// Client:MAC := HMAC(Secret, Nonce);SendToServer(Nonce, MAC);// Server:Response, MAC := ReceiveFromClient;MAC_Check := HMAC(Secret, Response);LoggedIn := (Response = Nonce) and (MAC_Check = MAC); Another way is to have a cryptographic challenge, by simply using the secret as password for encrypting the nonce. Basically: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Server:SendToClient(Nonce);// ClientNonce := ReceiveFromServer;Response := Encrypt(Secret, Nonce);SendToServer(Response);// ServerResponse := ReceiveFromClient;LoggedIn := Nonce = Decrypt(Secret, Response);Or to make it a MAC: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function EncryptionMac(Secret, Data);begin  DataHash := Hash(Data); // Hash so the MAC is always the same size no matter the size of the data  Result := Encrypt(Secret, DataHash);end;// Server:SendToClient(Nonce);// ClientNonce := ReceiveFromServer;MAC := EncryptionMac(Secret, Nonce);SendToServer(Nonce, Mac);// ServerResponse, MAC := ReceiveFromClient;MAC_Check := EncryptionMac(Secret, Response);LoggedIn := (Nonce = Response) and (MAC = MAC_Check); The second authentication mean is to have secret only known by the client and proving to the server that you know that secret without revealing it. The most common way of archiving this is with asymmetric cryptography. Asymmetric cryptography is cryptography where instead of one shared key, you have a private key, only you know, and a public key that is public to everyone. I will call the private key from now on SK (secret key) and the public key PK. There are generally three use cases for asymmetric cryptography: Key exchange, encryption/decryption and signatures. All of which can be used here. A key exchange is for example the Diffie-Hellman protocol. Basically is the idea that there is a so called key-agreement function KA(Pk, Sk, Generator) such that for any two key pairs (Pk1, Sk1) and (Pk2, Sk2) KA(Pk1, Sk2, Generator) = KA(Pk2, Sk1, Generator). Basically using a key exchange, you compute a shared secret from your private secret. To use this to authenticate, the challenge by the server is a randomly generated temporary private-public key pair. The server then sends its temporary public key to the client and computes KA(Pk_client, SK_temp, Generator) internally. The client can then compute the same secret KA(PK_temp, SK_client, Generator). Now you only need to prove that the client and server computed the same secret as discussed above this can be done using a MAC protocol. E.g. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Server:KeyPair := GenerateKeyPair;SendToClient(KeyPair.PublicKey, generator);// Client:Server_PK, Generator := ReceiveFromServer;SharedSecret := KA(Server_PK, KeyPair.PrivateKey, Generator);MAC := HMAC(Nonce, SharedSecret);SendToServer(Nonce, MAC);// ServerNonce, MAC := ReceiveFromClient;SharedSecret := KA(Client_PK, KeyPair.PrivateKey, Generator);MAC_Check := HMAC(Nonce, SharedSecret);LoggedIn := MAC = MAC_Check; Public key encryption is a method of encrypting data with the public key, such that it can be decrypted only by knowing the private key. An example for this would be RSA or the ElGamal cryptosystem. This way you can send data only the correct receiver (i.e. the one that has the correct secret) can decrypt the data. A possible protocol for establishing the trust here would be that the server sends a nonce to the client encrypted with the clients public key. The client decrypts the nonce and sends it back to the server. Now the server knows that the client has the private key corresponding to the public key. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Server:Challenge := Encrypt(Client_PK, Nonce);SendToClient(Challenge);// Client:Challenge := ReceiveFromServer;Nonce := Decrypt(KeyPair.PrivateKEy, Challenge);SendToServer(Nonce);// Server:Response := ReceiveFromServer;LoggedIn := Nonce = Response; Lastly there is the ability of creating digital signatures using asymmetric cryptography. An example for this is RSA or DSA. Here you can compute a signature over data using your private key, and anyone with the public key can check if that signature was actually created by your private key. With this the client can compute sig = Sign(Nonce, SK) and the server can can verify if the signature is valid: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Server:SendToClient(Nonce);// Client:Nonce := ReceiveFromServer;Response := Sign(KeyPair.PrivateKey, Nonce);SendToServer(Response);// ServerResponse := ReceiveFromClient;LoggedIn := Verify(Client_PK, Nonce, Response) = True; If you use any of those methods, the server needs to know the public key of your client, e.g. by sending it during the registration. In this case the public key (or more precisely the hash of the public key) can then be used as username/userID for the server to identify the client. When the client connects to the server, it simply sends it's public key so the server can generate the challenge. The question which system to use here depends on your use-case. The key exchange usually only makes sense if you need that shared secret afterwards, e.g. as password for symmetrical encryption of data. But if you build a session system, i.e. that you only need to authenticate once and then use a token to reauthenticate with each successive request, then you could use the shared secret generated by the key exchange as the token. The use of encryption can make sense if the encryption is easier than the decryption (e.g. with RSA the public key is usually chosen to be smaller than the private key making encryption faster than decryption) and you want to offload computational power from your server to the clients. But generally it is not much better than using signatures, and it does not validate that the client send you the nonce. If an attacker could guess the nonce, this would not protect against such an attacker. The signing on the other hand is pretty much exactly for this purpose, it guarantees that the response is from the person it claims to be (the real client). If the nonce is really only used once, even the ability to guess it doesn't help the attacker. If you don't have a secure connection you usually combine these things, like having a signed key exchange or using asymmetric encryption to send signed messages. So generally I would advice you to either use signatures or key exchange, depending on if you need a shared secret or not. Note that for asymmetric cryptography you can easiely generate the key pairs from a secret, so you could use a password from which a private key is generated which then is used for the cryptography. That said, that is usually not recommended because the secret needs to be as random as possible and for RSA for example usually it is recommended to use cryptographically secure pseudo random numbers (seeded from a true source of randomness) of at least 3000 bits. Your password 123456 isn't really random enough (you could use a large random salt, but in that case, basically the salt is your private key not the password). There are other protocols using asymmetrical encryption to prove identity, so called zero-knowledge proofs (e.g. Feige-Fiat-Shamir protocol) but I think that you don't really gain anything from using them (they have some very niche use cases with respect to blockchain applications) Lastly something that you might want to consider are so called one- time passwords using hash chains. Basically when your client registers at the server, it computes the hash multiple times Hash(Hash(...Hash(secret)))) (e.g. 1000 times) which I will now call Hash_OTP(n, secret) for n hashing iterations, and send this to the server which will store it (e.g. as H_stored). When you want to log in, you compute H_client := Hash_OTP(n-1, secret) and send it to the server. The server now checks if Hash(H_client) = Hash_stored, if so the log in was successful and the server updates Hash_stored to be H_client. If someone tries to log in with H_client again, the server will check Hash(H_Client) which is different to the required H_Client and fail the log in. On the next log in the client can log in again by computing Hash_OTP(n-2, secret) which hashed will be Hash(n-1, secret) which is H_Stored. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Client:OTP := Hash_OTP(n, secret);SendToServer(OTP);// Server:OTP := ReceiveFromServer;StoredHash := LoadHashFromDB(Username);if Hash(OTP) = StoredHash then  LoggedIn := True;  SaveHashToDB(Username, OTP);end;This has the advantage that you can first limit the clients logins to n, as after you reached n=0 you can't compute the previous hash anymore, and also, you imminently know if someone stole your secret and logged in, because then your next log in will fail and only succeed for a lower n then you previously stored. It should be noted that for such hash chains, you should use a fast hashing algorithm and not something with brute force protection like PBKDF2 because this will massively slow down your client. So I just threw a lot of the things out, all of these are viable solutions and are used by different applications around the world. What is best for you depends on a few things, what you are trying to archive, what you are comfortable with and what is easiest for you (as we don't really have so much variety with different crypto units, this is probably the deciding factor). ---------------------------------------------------------------------- Thaddy · 2021-11-05 19:55 Great write up. This should be in the wiki. It covers the subject in an understandable manner. You also define most pitfalls very well. Compliments. ---------------------------------------------------------------------- Warfley · 2021-11-05 20:48 Because I hit the char limit in my last post, here is a small addition to it: When deciding between symmetric and asymmetric cryptography, there are a few things to consider. Asymetric cryptography is usually slower, meaning computing a MAC is often much more performant than signing and verifying. This is why many protocols, such as TLS only use asymetric encryption to establish a symetric channel, e.g. by using a signed key exchange to compute a shared secret used further on for symetrical computation. Also asymetrical keys are usually pretty large. RSA keys or finite field DSA keys are at least 3000 bits large, on elliptic curves it is smaller with just 256 to 512 bits (equivalent to a 40-80 char passphrase and only if the passphrase would be fully random) but still rather large. This limits them usually only to possesion based factors (i.e. stored in a file or on a device), meaning if you lose your key file, you lose access. But having not a shared secret, but rathe a secret only the client has has a major advantage, that is that you can easiely use the same secret on multiple services. When you have two services where you use the shared same secret, you can log into one if you know the secret from the other. With asymetric cryptography you don't have that problem because the public key is meant to be public, you can't be impersonated by someone knowing it. This is somewhat mitigated by using a salt, but if the salt is not random (e.g. the username) having the same salt will still result in the same secret. Thats also why you should always use a large random salt. Thats why in my example above I use two salts, the username is available on the client side so it can already pre-hash to make it secure from evesdroppers, but to be stored on the server a large random salt is needed to secure against this. At last I want to mention that all cryptographic material needs to have an expiry date. For example RSA with key size of 2048 bit was considered secure until very recently, but due to advances in computing power, now it is recommendet to use at least 3000 bit. Always have an expiry date for your cryptographic materials, it does not need to be short, a few years should be possible, but this ensures that the user has to reregister their keys regularly, and if an algorithm is deprecated (like RSA-2048) this forces the user to update their keys. Many services and organizations enforce the changing of passwords in regular intervals. While the idea is not so bad and in line of what I just wrote about the expiry date, in practice this means there is this regular date where all the users increment the last digit of their password by 1, which does not add any security, so I am rather sceptical about this concept, but thats just a personal oppinion. I tried to keep this very high level, if you have more detailed questions, e.g. what algorithms/cryptosystems to use, feel free to ask them, but generally speaking, if you know the basic concepts, it's just looking up what your national cyber security agency (like NIST in the US) recommends and using pre made libraries for that. No one expects from you to know everything about the crypto behind this sort of stuff. ---------------------------------------------------------------------- jollytall · 2021-11-06 09:12 @Warfley. Excellent summary, thanks a lot. One small thing I rarely see against brute force attacks: Gradual slowdown of response. It is typical to get completely locked out after n failed attempts. You mention "slow" (0.1s) response on every attempt what makes brute force 100X slower than a 1ms response. What I seldom see is that the first n (e.g. 3) attempts are fast, the next is a bit longer, etc. If after 10 attempts it is already an hour wait time, brute force attackers will soon give up. Legitimate user will typically find the right, half forgotten combination of password in n attempts, where the wait time is still sort of acceptable. This helps both brute-force protection and avoidance of too many forgotten password requests at the same time. Using this, one needs to check the source (e.g. IP address), so a brute force attacker cannot lock out the real user. Edit: I have just noticed that this was my century post ---------------------------------------------------------------------- Josh · 2021-11-06 11:11 utilities run on your server can help brute force attacks, utilities like fail2ban can block ip address at your servers firewall, dependent on what rules you specify, ie block loginip for 24 hrs after 5 failed logins to database server ports, then if same ip attacks after they are allowed back in the period could be then raised to 7 days then 30 days then 1 year and finally forever. ---------------------------------------------------------------------- jollytall · 2021-11-06 11:53 Oh sure, there are ways to do it. My point was that compared to the fact how easy, user friendy and attacker unfriendly this solution is, it is seldom used. ---------------------------------------------------------------------- Warfley · 2021-11-06 12:02 Quote from: jollytall on November 06, 2021, 10:12:49 amOne small thing I rarely see against brute force attacks: Gradual slowdown of response. Yes, I didn't include such things because this post was already long enough only with the technical stuff. There is a lot of behavioral things your server should do. Another example would be to never say which of the login fields was wrong (user or password), but only say "Invalid credentials" or something like this, so the attacker can not use this to gather information about which users are using the site (which basically only halves the bruteforcing efforts if you only have to bruteforce one field instead of two) Quote from: josh on November 06, 2021, 12:11:56 pmutilities run on your server can help brute force attacks, utilities like fail2ban can block ip address at your servers firewall, dependent on what rules you specify, ie block loginip for 24 hrs after 5 failed logins to database server ports, then if same ip attacks after they are allowed back in the period could be then raised to 7 days then 30 days then 1 year and finally forever. This is actually not a good idea, because it operates on the assumption that the IP is a unique identifier, which is not true. Depending on your location, your ISP and your price plan, you can easiely sit behind a NAT with hundreds sometimes thousands of other users using the same IP. Banning thousands of users just because of one rouge on is completely unreasonable. I experienced this already, mobile phone networks operate in a similar way, by basically tunneling your requests from your phone to a server which does the actual IP communication (so you don't loose your IP address whenever you switch a cell tower), resulting in multiple hundered to thousands of people (depending on the load of each user and the mobile ISPs datacenters) sharing the same IP. So I actually ran into the problem that I was blocked from some rather large websites just because they used an IP ban for someone else who potentially used that IP address only for a few minutes potentially days ago. This could probably work if we finally started using IPv6 (but if you are using FreePascal, your applications are probably not IPv6 ready either ) where everyone gets their own IP, but then you run in another problem, that is that each device does not only get a single IPv6 address but a whole subnet with thousands to millions of addresses. The actual size depends on the ISP, your price plan and your router configuration, so there is no uniform setting, so you can easiely under or overblock, meaning either the person still has a few thousand IPs left if you ban a subrange to small, or you could potentially overbann thousands of other devices when your subrange is to large. Generally speaking, never use the IP as identifier for a device. This was maybe possible in the 2000s but today is not feasable anymore ---------------------------------------------------------------------- MarkMLl · 2021-11-06 12:04 Quote from: josh on November 06, 2021, 12:11:56 pmutilities run on your server can help brute force attacks, utilities like fail2ban can block ip address at your servers firewall, dependent on what rules you specify, ie block loginip for 24 hrs after 5 failed logins to database server ports, then if same ip attacks after they are allowed back in the period could be then raised to 7 days then 30 days then 1 year and finally forever. Subject to the whim of the owner if the computer's rented ("colocated" etc.). However since things like fail2ban perforce interact with several layers of the network stack, and since my experience is that having too many things like that piled up can lead to compatibility/interaction problems, it's always preferable IMO to choose an application server (database, web or whatever) that has implicit facilities for that sort of thing. MarkMLl ---------------------------------------------------------------------- Spoonhorse · 2021-11-06 16:43 This is epic.