0

My web app still needs a lot of work on it in the security department, so I'm considering implementing homomorphic encryption for my SQL database- to help protect from the outcomes of SQLi.

I know the basic principles of how homomorphic encryption works with SQL queries, but being inexperienced in the field, I'm not sure if it really is a good idea or if there are any potential drawbacks.

What are the pros and cons of homomorphic query encryption and I want to know whether it’s highly recommended, or not?

And, some specific questions I have:

  • What are the performance implications of homomorphic encryption on SQL query data?
  • Are there any potential security risks or vulnerabilities associated with homomorphic encryption?
  • Are there any alternative solutions I should consider instead of homomorph… you know what I mean!

Thanks very much, anyway.

For anyone who isn’t entirely familiar with this lesser-known concept of database retrieval, this link covers it pretty well, I think.

Basically,

… is a way for a party to perform computations on encrypted data without ever decrypting it.

So,

Bob cannot learn anything about the encrypted data or the encrypted computation result. The encrypted data and the encrypted computation both look like gibberish to anyone other than Alice.

4
  • 1
    You want to encrypt queries to prevent SQL injections? How is this supposed to work? Do you have any references where you got this idea from?
    – Ja1024
    Commented Jun 5 at 10:44
  • 1
    It is hard, slow, and has limited queries., start from CryptDB. Eventually, we will be there..
    – kelalaka
    Commented Jun 5 at 10:52
  • 1
    The solution to SQL injections are prepared statements (for passing values) and whitelists (for passing query fragments). Encryption of critical data(!) may be used for defense-in-depth, so that a successful SQL injection attack still doesn't reveal the plaintext. But I don't see how encryption is supposed to prevent injections.
    – Ja1024
    Commented Jun 5 at 11:45
  • Since you edited the question: The article explains how homomorphic encryption protects the privacy of data, it doesn't say anything about encrypting SQL queries or preventing SQL injections, so this seems unrelated to the question you asked. As I already said, it's well-known that encryption can protect data in the case of a successful SQL injection attack, but this is very different from preventing SQL injections from happening in the first place. It's damage control, not a countermeasure.
    – Ja1024
    Commented Jun 5 at 21:14

3 Answers 3

3

There are all kinds of experimental approaches that try to prevent SQL injection attacks, some of which are quite clever. It's possible that you've found some paper or article where somebody claims that homomorphic encryption helps against SQL injection.

However, this is research – or just playing around. If you have an actual system that you have to protect, then doing experiments is the absolutely last thing you want. In practice, you should always use the most simple, robust and widely accepted solution.

In the case of SQL injections, the primary solution are prepared statements. The idea is that the database system pre-compiles a fixed query template which has parameters for dynamic values. This completely separates the structure of the query from the data used by the query, making it impossible for an attacker to change the query itself. As a concrete example:

SELECT price FROM books WHERE title = ? AND category_id = ?

Each question mark denotes a parameter. The first parameter is for a string which contains the book title, the second parameter is for a numeric category ID.

Once the prepared statement has been created, it's possible to bind concrete values to the parameter (like "The Trial" for title and 57 for category_id) and execute the statement with those values. There's no risk of untrusted values leading to an SQL injection, because the attacker cannot do more than specify the string and the integer which will be used by the query.

Sometimes escaping is recommended as an alternative to prepared statements, but this a very fragile approach. It's far too easy to forget the escaping, run into character encoding issues or use the wrong technique. This risk should only be taken if it's absolutely impossible to use prepared statements.

If you need to dynamically insert SQL identifiers or query fragments rather than values, this cannot be done with prepared statements. In this case, you have to validate and possibly escape the input for the specific target context. For example, if you want to let the user specify which column of a table should be searched, then a possible solution would be to have a whitelist of acceptable column names. This restricts the user to a set of predefined option, making it impossible to inject anything else.

Encryption can be used to limit the effects of an SQL injection attack, so this is a valid defense-in-depth measure. However, the primary protection is to prevent injection attacks from happening in the first place. And the encryption shouldn't be homomorphic, unless there's a specific reason for why you need its features.

3
  • Thanks very much for the answer. I think yes, I might go for a more common and researched approach. Also, to answer your questions in the comments- a quick google search showed many, many sites explaining homomorphic encryption in the context of sql databases- including this one- dualitytech.com/blog/… Commented Jun 5 at 20:41
  • @security_paranoid: Of course homomorphic encryption is used for protecting the confidentiality of data (which may be stored in an SQL database or anywhere else). That's the whole point. But you specifically claimed that you could prevent SQL injections by encrypting queries. That's obviously an entirely different topic, so I was asking for references on this specific idea. If you misspoke or misunderstood the purpose of homomorphic encryption, this is fine. But the approach you initially described was very odd, hence my question.
    – Ja1024
    Commented Jun 5 at 21:06
  • Ja, sorry. You are right and I am wrong. It was a small mix up grammatically on my part. I do understand the concept but I didn’t type it right. Thanks for letting me know. Commented Jun 5 at 21:25
2

I want to know whether it’s highly recommended, or not?

No, it's not recommended for all applications. It has its niche use cases, but if you are not sure you need it, you probably don't need it. I think I have never seen an application use homomorphic encryption.

Are you sure you meant to ask about homomorphic encryption? It seems like an advanced and rare solution for someone who is asking how to prevent SQL injection.

Are there any potential security risks or vulnerabilities associated with homomorphic query encryption?

I think the biggest risk is using homomorphic encryption in a way that it was not designed for, or that breaks its assumptions.

For example, some encryption algorithms are order-preserving, so that the lexical order of the plaintext data is preserved in the ciphertext data. However, if an attacker can create arbitrary data and check their ordering, they can eventually figure out the plaintext for all encryptions.

What are the performance implications of homomorphic encryption on SQL queries?

It's slower. How much slower depends on your use case. Encrypting and decrypting things typically takes in the order of milliseconds, so this won't be noticable in the context of a web request. If you want to do complex algorithms (e.g. computing the average without decrypting), this can be millions of times slower.

Are there any alternative solutions I should consider

To protect against SQL injection, it is common to use a ORM or some other layer between the application and the database. This provides a more secure API and creates the queries for you, reducing the risk of injection in a query.

1
  • Thanks for the answer- I will take a look at the link. Commented Jun 5 at 20:42
1

Don't roll-you-own-encryption.

Leaving aside the most common reason this advice is cited (because you'll do it wrong and your implementation will have vulnerabilties) and your concerns regarding performance, its a PITA to develop/maintain. SSL, now TLS has been in widespread usage for over 20 years. It has taken 20 years to get to the point where it is only a minor hurdle (but I still encounter cases where the wet-ware has issues).

Even if this were available as an off-the-shelf component, I am not aware of support for this in any of the major DBMS - so your protection would on cover part of the journey of queries to the DBMS. You'd also have a new dependency in your code base.

Are there any alternative solutions I should consider instead of homomorph

Stored procedures.

On their own, these only provide partial protection against SQL injection. However most (relational) DBMS will allow you to implement privilege separation in stored procedure calls. This provides full protection against SQL injection, enumeration and potentially other attacks.

If you are using the end-user client device (e.g. browser) for storing state which should not be modified then use symmetric encryption and/or a secure signature.

9
  • The solution to SQL injections are prepared statements (for passing values) and whitelists (for passing query fragments).
    – Ja1024
    Commented Jun 5 at 11:44
  • No, that is a solution.
    – symcbean
    Commented Jun 5 at 13:03
  • No, this is the canonical solution which completely solves the underlying problem (in the case of user-supplied values): That the attacker can change the structure of the query. What you're presenting is, at best, a defense-in-depth measure.
    – Ja1024
    Commented Jun 5 at 13:06
  • Client side parsing offers no benefit over escaping (an alternative solution). Agreed that server side parsing is preferable to escaping. Not all Databases / APIs support prepared statements. Prepared statements do not prevent enumeration / parameter injection attacks. When the only tool you have is a hammer.....
    – symcbean
    Commented Jun 5 at 13:15
  • 1
    If you have something constructive to say, please post your own answer. Repeating the same things again does not make them more correct.
    – symcbean
    Commented Jun 5 at 13:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .