WEBVTT

00:00.000 --> 00:29.680
Welcome everybody to SQL injection isn't dead smuggling queries at the protocol level or how i like to call it sql injection lower decks because usually with sql injection you're at the high level like at the bridge where the captain and the high-ranking officers are and they're speaking their query language and their business language but today we're going to do go down to the lower decks where the mechanics are and where they shift around bits and bytes.

00:30.720 --> 00:33.620
So, to hook you at the beginning, I have a small teaser.

00:33.620 --> 00:43.920
This code snippet is a small HTTP request handler written in Go and it just takes a user ID from the body and uses it in a prepared statement to select the user.

00:44.120 --> 00:50.480
And if you think everything's fine here, there's no way for a scale injection, then you should stay because I'm going to prove you wrong.

00:51.480 --> 01:05.400
And at a high level in this talk, we're going to look at how applications talk to their databases over the network and how attackers can inject in that connection and what can go wrong and also how prevalent the problem is.

01:06.480 --> 01:08.620
A bit about me, I'm Paul.

01:08.620 --> 01:11.740
I'm a vulnerability researcher at Sonar's R&D team.

01:11.820 --> 01:17.600
And if you don't know Sonar, we're the home of clean code, so we help developers write clean code, which also means secure code.

01:17.600 --> 01:22.200
And that's why we look at a lot of code and try to find stuff there.

01:22.960 --> 01:32.640
So, for the rough outline, first we're going to look at the idea of all of this research and then we'll go into the main part of the talk, which is attacking the database wire protocols.

01:32.640 --> 01:35.780
And we're going to focus on Postgres and MongoDB here.

01:35.900 --> 01:46.620
Then we'll put things into perspective by looking at the real world applicability of the research and then give you some ideas for future research and draw some takeaways.

01:47.840 --> 01:53.640
So, in short, the idea is to do request smuggling, but for binary protocols.

01:54.700 --> 02:04.460
And of course, for this I have to mention James Kettle's HTTP desync attacks, where you basically cause a disagreement between two systems over the end of an HTTP request.

02:04.460 --> 02:08.140
For example, between a reverse proxy and an upstream back end.

02:08.140 --> 02:15.780
And if they don't agree on where a message ends, or an HTTP request in this case, then the attacker can do some bad things.

02:15.800 --> 02:27.280
And the root causes of this were manifold, but some examples are text parsing, where one system would only accept the exact word chunked, and the other one would also allow a tapped character in front.

02:27.580 --> 02:34.700
Or they were logical, where one system used the content length header and the other used the transfer encoding, and then things were off again.

02:35.100 --> 02:37.120
But what about other protocols?

02:37.220 --> 02:40.040
And specifically, what about binary protocols?

02:40.840 --> 02:42.740
Where do they have message boundaries?

02:42.740 --> 02:44.020
How do they do it?

02:44.260 --> 02:52.160
Some of them have delimiters, where, for example, in null terminated strings, the null byte is the delimiter that tells the end of the string.

02:52.380 --> 03:04.820
Or you might have length fields, and there's an entire family of protocols that are called TLV, or type length value protocols, because they have a type field, and then a length field, and then the rest of the message is the value.

03:05.940 --> 03:09.180
So how could we desync in these protocols?

03:09.440 --> 03:11.460
For delimiters, it sounds quite easy.

03:11.460 --> 03:16.080
You just have to find a way to put a delimiter where it's not supposed to be, in a value.

03:16.220 --> 03:21.800
Of course, it's not that simple, but at least you directly have an idea of what you could do.

03:22.460 --> 03:31.520
But for length fields, I was stuck for a moment and thought about, okay, you have a length field, it has a fixed size, it has a fixed location in the message, so what could go wrong there?

03:31.520 --> 03:38.940
And then I thought, okay, maybe some endianness issues, or maybe integer overflows, and I wanted to go deeper into that.

03:39.460 --> 03:46.620
So for that, I first looked at the modern web application landscape, where our binary protocols used there.

03:46.820 --> 03:57.620
So at the center, you always have the application, and it talks to databases, or caches, storages, message queues, structured logging, and so on.

03:58.180 --> 04:02.580
And I wanted to focus on the connection between the application and the database.

04:02.920 --> 04:11.900
And that was because I thought that if I would find something, it would have a great applicability, because almost every web app has a database.

04:12.320 --> 04:24.780
Then there's a big potential severity, because databases are high-value targets, because they either contain interesting data, like personal information, or relevant data to the application, for example, used for authentication.

04:25.660 --> 04:34.880
And then also the exploitability should be quite good, because what web app has a database, but doesn't let the user query something from that database, right?

04:35.020 --> 04:38.040
So we have kind of guaranteed user input.

04:39.460 --> 04:44.120
So let's start in and look at some database wire protocols.

04:44.180 --> 04:52.360
And for that, I have a small overview of four popular databases and how their messages and their protocols look like.

04:52.720 --> 04:55.740
So for Postgres, this is how a message looks like.

04:55.740 --> 05:03.820
You have a type byte, then a four-byte length, and then the value, which in this case is just the string that's an SQL statement.

05:04.180 --> 05:07.160
For MySQL, it looks kind of similar.

05:07.160 --> 05:18.580
You first have the length, it's only three bytes, but then you have a one-byte sequence, which would allow you to send multiple packets that would be reassembled to one big message at the end.

05:18.880 --> 05:21.280
And there, of course, you have the value.

05:21.720 --> 05:32.400
For Redis, which is not exactly a binary protocol, but their docs kind of read like this, because they talk about, oh, here they have this byte, so I wanted to include it anyways.

05:32.580 --> 05:37.120
There, you first have a type byte, then you have a length, but it's not a fixed size field.

05:37.120 --> 05:39.760
It's a decimal integer that has to be parsed.

05:39.780 --> 05:43.660
Then you have a delimiter, and then the value, and then another delimiter.

05:44.460 --> 05:48.200
And finally, for MongoDB, it's a little bit longer.

05:48.200 --> 05:51.040
You first have the message length, again, a four-byte field.

05:51.100 --> 05:53.000
Then you have a little bit more metadata.

05:53.180 --> 06:00.360
For example, the opcode is like the message type, and then the value is a little bit more of a complicated binary structure called BSON.

06:01.800 --> 06:04.340
So let's start with Postgres.

06:04.700 --> 06:07.480
Again, this is how a packet looks there.

06:07.480 --> 06:12.820
One-byte type identifier, four-byte integer length fields, and then the value.

06:13.140 --> 06:16.300
So I thought about, okay, four-byte integer fields.

06:16.300 --> 06:24.000
You can fit quite big numbers in there, like two to the power of 32 minus one, which is roughly the size of four gigabytes.

06:24.300 --> 06:30.360
But maybe what if we could somehow make a query that's bigger than that?

06:30.360 --> 06:36.320
How do actually libraries handle this kind of too-large thing?

06:36.600 --> 06:42.920
And for that, I started to look into code of Postgres client libraries, and I found the first bug.

06:43.240 --> 06:46.500
This is the code of a library called PGX.

06:47.980 --> 06:54.200
It's supposed to encode the content of a message into a larger message buffer.

06:54.380 --> 06:56.660
So first, it writes the message type.

06:56.660 --> 06:59.220
In this case, it's a B for a bind message.

06:59.220 --> 07:03.340
Then it saves the offset, where it should later write the size to.

07:03.360 --> 07:06.640
Then it adds all of the actual data of the message.

07:06.760 --> 07:08.860
And finally, it writes the size.

07:08.900 --> 07:17.640
And to do that, it first slices the message buffer to the exact size of what is included in the length, for example, not the type byte.

07:17.840 --> 07:20.680
Then it takes the length of the sliced buffer.

07:20.720 --> 07:26.200
And here, the length is of type int, which in most modern systems is a 64-bit integer.

07:26.400 --> 07:31.300
But then this is truncated down explicitly to an int32.

07:31.540 --> 07:40.540
So we have a problem here, because if the length was bigger than what can be expressed in an int32, we now have a truncation and the number becomes very small.

07:40.540 --> 07:44.940
But then the library still sends all of the data after that.

07:45.060 --> 07:56.200
So then if the database has to parse this message, it will just see a very small length and only parse a very small message, and then try to parse whatever is coming after that as more messages.

07:56.200 --> 07:59.120
But in fact, it's data from the first message.

08:00.080 --> 08:03.000
So let's look at this again with example messages .

08:04.060 --> 08:06.140
So here we have a very small one.

08:06.140 --> 08:08.000
We have a 4-byte value.

08:08.000 --> 08:13.140
And then the length becomes 8, because the size of the length field itself is also included in the length.

08:13.140 --> 08:14.780
So it's a length of 8.

08:15.800 --> 08:19.120
This is how the largest possible message looks like.

08:19.120 --> 08:26.420
You can see the length is filled to the brim with hex FFFFF, because the value is just almost 4 gigabytes of ace.

08:26.420 --> 08:34.720
But still, everything's good, because the application can express this length in the 4 bytes, and so the database will parse the same size.

08:35.420 --> 08:44.440
But if we make it just a little bit larger, we can see that the truncation happens, because the most significant bit is not represented in the length anymore.

08:44.440 --> 08:48.360
So the length is just 4, which means this is a tiny message.

08:48.360 --> 08:53.640
And then after that, in the connection, there comes some garbage ace that the database can't really understand.

08:53.840 --> 09:02.280
But if the attacker chooses not ace, but some bytes that the database can understand, they can craft an injected message here.

09:03.660 --> 09:06.580
So let's zoom out again and look at this again.

09:06.580 --> 09:09.140
This is the small message, all is good.

09:09.140 --> 09:13.060
This is the large message that does not cause a truncation.

09:13.240 --> 09:17.740
So both the application and the database see it and parse it the same way.

09:18.500 --> 09:28.600
But if we make the size a little bit too big, the application thinks it's writing this big number, but actually, because of the truncation bug, it writes a small number.

09:28.600 --> 09:32.840
So when the database parses it, it sees one small message and then garbage.

09:32.840 --> 09:43.920
Or if the payload is cleverly crafted, it sees more messages that can contain attacker-controlled SQL statements like this one that adds a new admin user.

09:44.740 --> 09:47.640
So the impact is quite high here.

09:47.640 --> 09:50.420
You can inject entire SQL statements.

09:50.420 --> 09:56.180
You're not limited to union-based or subquery-based SQL injection like with the very classic ones.

09:56.180 --> 10:08.400
It's more like if you have stack queries enabled, so you can just finish the first statement that you're injecting into and then have a whole other one that can be an insert, even if the other one was a select, and so on.

10:08.880 --> 10:15.660
So this means the attacker can essentially read, write, and delete all the data in the database with the permissions of the application.

10:15.860 --> 10:25.640
And if the database is configured insecurely, this can already lead to code execution or you just have all of the data from the database, which is bad enough.

10:26.560 --> 10:44.300
The only thing that's a little bit less convenient than with classic SQL injections is that data exfiltration is a little bit less straightforward because if you're injecting more messages into the connection, then the application will still only process the answer for the first message.

10:44.300 --> 10:51.760
So you don't directly get the results of your injected message where you maybe select all the password recept tokens of users.

10:51.920 --> 11:03.500
So you have to go a different route of maybe doing a subquery and then using an insert to put this data you want to exfiltrate in another table and then use business logic of the application to access the data.

11:03.500 --> 11:06.200
So it still works, just a little less convenient.

11:07.340 --> 11:09.620
But how does this look in the real world?

11:09.640 --> 11:19.480
Some of you might have already noticed that if we control all of these A's that we have in a message, why can't we just directly put any SQL statement in the first place?

11:19.480 --> 11:21.120
Why do we need to have the overflow?

11:21.400 --> 11:24.680
And you would be correct, because this was just the simplification.

11:25.080 --> 11:27.540
So let's look at how this actually looks.

11:28.220 --> 11:30.480
This is a two-line code snippet.

11:30.480 --> 11:32.980
The ID here is user controlled.

11:32.980 --> 11:35.280
In this case, it's a very small one.

11:35.440 --> 11:37.200
And then we have our query.

11:37.200 --> 11:42.720
You might recognize it from the teaser code snippet that uses the ID to select a user from the database.

11:42.720 --> 11:46.780
And the resulting packet or message that's sent over the wire looks like this.

11:46.780 --> 11:52.620
We have our type byte, then a small length, and then we have the value with a trailing null byte.

11:52.680 --> 11:54.200
All is fine here.

11:54.200 --> 12:05.680
But now if the attacker makes the user controlled ID very large, 4 gigabytes, then suddenly the length in the packet is smaller than before, even though we send more data.

12:05.680 --> 12:08.240
So we see that the truncation happened.

12:08.440 --> 12:14.620
And now when the database tries to parse this packet, it will see, okay, it's a length of 38 bytes.

12:14.620 --> 12:17.300
So I'm going to parse until that.

12:17.320 --> 12:20.140
And after that, it will try to parse the next message.

12:20.520 --> 12:28.160
So the attacker has to put their cleverly crafted payload at that exact offset in this big buffer of A's.

12:28.740 --> 12:31.280
But how does the attacker know this offset?

12:32.660 --> 12:41.380
The offset depends on the query that they are injecting into, and especially on the injection point in the query and the length of the whole query.

12:41.860 --> 12:45.580
So if you know these things, you can just calculate the offset and you're done.

12:45.600 --> 12:51.500
But maybe you're lazy and don't want to do the math, or you don't know the query because it's kind of a black box test.

12:51.800 --> 12:53.480
So what can you do then?

12:54.300 --> 12:57.800
And of course, the naive solution is to brute force the offsets.

12:57.800 --> 13:10.080
But unfortunately, now for each try, you have to send 4 gigabytes, which is slow, even on local setups and on production setups, it creates a lot of noise and risks denial of service, because it's a lot of data.

13:10.120 --> 13:12.160
So can we make this more reliable?

13:13.140 --> 13:22.320
And the first technique I tried out here was to borrow something from binary exploitation from 20 plus years ago, which is called a knob sled.

13:22.520 --> 13:35.880
And mapped to this problem, we just use a lot of very small message at the beginning of the buffer, so that when we hit the start of one of these messages, everything's fine, because the message is parsed, and then the next one, and then the next one,

13:35.880 --> 13:44.760
and so on, until at the very end, the big message with the actual attacker SQL payload is parsed and executed.

13:44.760 --> 13:55.700
But if we hit any other byte, the database will cause a fatal error because it cannot parse there, and it will close the connection between the application and the database.

13:55.700 --> 14:02.580
But usually, that's not too bad, because applications have connection pools, and they just reopen connections if they get closed.

14:03.100 --> 14:04.720
So let's visualize this.

14:04.720 --> 14:08.660
This is the smallest possible message in the PostgreSQL protocol.

14:08.940 --> 14:14.040
So we repeat this a lot of times, and in the end, we have our actual payload.

14:14.340 --> 14:21.060
So if the offset happens to align with the first or the fifth and so on byte, then we're good.

14:21.060 --> 14:27.260
But if it hits one of the other bytes, then there's this fatal parsing error and the connection is closed.

14:27.640 --> 14:30.880
So if that happens, we can try again.

14:31.000 --> 14:41.180
And now, we just prepend one padding byte, and then if the offset happened to align with the second byte and so on, then now, this attempt would be lucky.

14:41.180 --> 14:48.080
Otherwise, we just try again, and again, and again, until we're lucky at the last try.

14:48.760 --> 14:58.020
So this means that after a maximum of five attempts, the attack is successful, because each try has a 20% chance of success, and we can repeat the attack.

14:58.460 --> 15:04.520
But still, in the worst case, we have to send five times four gigabytes, which can still be a lot.

15:04.520 --> 15:06.720
So maybe we can make it even better.

15:07.540 --> 15:12.100
And for this, I looked at what was holding us back with the last strategy.

15:12.100 --> 15:18.140
And here, it was the length bytes, because those were not valid type bytes, so not a valid start of a message.

15:18.300 --> 15:21.120
So maybe we can make them valid type bytes.

15:21.540 --> 15:24.200
And I call this technique trampolines.

15:24.260 --> 15:37.400
And what we do here is, first, we just use the queue, which is a valid type byte for each byte, because this is also some big length, it's hex 5151, and so on.

15:37.660 --> 15:42.280
And we can see down below the whole attacker payload.

15:42.360 --> 15:48.820
And we'll see how things are parsed when the offset hits certain bytes here.

15:48.820 --> 15:53.440
So if it hits the very beginning, the database parses it like this.

15:53.440 --> 15:58.560
First queue is the type byte, and then qqqq is the length of the first message.

15:58.560 --> 16:06.520
So the database will parse the following, whatever qqqq means as a number, bytes, and try to run this as a query.

16:06.600 --> 16:11.960
And of course, this will cause like a semantic syntax error, but this does not close the connection.

16:11.960 --> 16:17.420
So the database will just complain with an error response, but continue parsing the next message.

16:17.420 --> 16:20.740
So this is like jumping over this many bytes.

16:21.080 --> 16:23.460
So that's the trampoline right there.

16:23.480 --> 16:39.260
And where we land after that, we have a pre-constructed message that has the exact size that then does another jump that ends up right at the end of the attacker payload, where the actual SQL statement message is.

16:39.660 --> 16:45.420
So if we would have hit any of the other bytes, things look slightly different, but not too different.

16:45.580 --> 16:49.200
Here, the length is parsed as qsss.

16:49.200 --> 16:52.360
The s is also a very valid type byte.

16:52.440 --> 17:03.760
Now it jumps over some more bytes, and it lands at a different pre-constructed message that, again, has the exact size that it does the right jump to land at the end.

17:05.100 --> 17:08.720
But does this actually work like we planned to do?

17:08.780 --> 17:21.200
And unfortunately, after I tried to implement this, I noticed, well, Postgres has a logical maximum message size next to the actual size that you can possibly fit into four bytes.

17:21.200 --> 17:23.640
And this is just under one gigabyte.

17:23.640 --> 17:28.000
So the first byte of the length can never be larger than hex 3f.

17:28.000 --> 17:31.340
So that's bad, because with a q, that doesn't work.

17:32.340 --> 17:37.120
So what if we just use 3f as the type and all of the length bytes then?

17:37.720 --> 17:47.940
Also doesn't work, because hex 3f is not a valid type byte, and also none below that, at least not for messages from the application to the database.

17:48.440 --> 17:52.380
So I had to use a compromise here, which is an alternating pattern.

17:52.660 --> 17:58.720
And with this, now every second byte is a valid type byte, so a valid start of a message.

17:58.720 --> 18:04.200
So if we hit any of those bytes, we're good, because everything is parsed okay.

18:04.200 --> 18:07.060
And then we can do our trampolines, like I just showed.

18:07.060 --> 18:12.540
And if we hit any of the other bytes, like a null byte, then we get the error and the connection is closed.

18:12.540 --> 18:14.400
But still, we can try again.

18:14.460 --> 18:21.860
So after a maximum of two attempts, we are successful , which is much better than the initial brute force.

18:23.500 --> 18:28.480
All right, let's see how many vulnerable libraries there were that we found.

18:28.520 --> 18:34.920
And this is just a selection, we looked at more, but these are only the ones that were potentially vulnerable.

18:35.320 --> 18:41.320
So for Go, we tested four libraries, and we found all of them to be vulnerable and also exploitable.

18:41.320 --> 18:43.940
And I'm going to go into exploitability in a minute .

18:44.440 --> 18:49.820
Unfortunately, only one of them fixed the issues we reported, and the others did not.

18:49.820 --> 18:52.360
So they are still vulnerable in the latest version.

18:52.800 --> 19:02.720
For C Sharp, there was one library we looked at, it was vulnerable and exploitable, but unfortunately, they fixed and also backported it to older branches of the library.

19:03.040 --> 19:04.720
So that's great to see.

19:05.500 --> 19:16.820
And then for Java and JavaScript, we tested a bunch of libraries, and a lot of them were in theory vulnerable, they had overflows without the right checks, but they were not exploitable.

19:16.820 --> 19:30.040
And this was most of the times due to language limitations, where for example, you cannot have large enough strings or buffers that would be more than four gigabytes, so the length would never overflow the four byte length fields.

19:30.080 --> 19:33.540
And we're going to look into these language differences later.

19:34.420 --> 19:44.820
So short disclosure timeline for transparency, we send out the advisories in February this year, then PGX fixed in March, then PGSQL fixed in May.

19:45.160 --> 19:53.580
And for PG and PGDriver, the maintainer first responded to our advisory, but then stopped and didn't implement fixes until now.

19:53.620 --> 20:02.460
And for PQ, we couldn't even reach the maintainers, we tried to do it via their GitHub, made an issue or made a pull request with the fix, but they got no attention, unfortunately.

20:03.800 --> 20:07.700
So now let's go from vulnerable libraries to exploitable applications.

20:07.960 --> 20:14.480
So first, we have a set of applications that use these libraries, or have used libraries in a vulnerable version.

20:15.200 --> 20:22.620
And these, for example, include Grafana or Gitty or SyncThing, but we weren't able to confirm if there's an exploitable path here.

20:22.620 --> 20:25.480
So there might be, but we haven't proven it yet.

20:26.240 --> 20:34.520
And then there's a set of applications that we found to be vulnerable in a non default configuration, for example, Metamost or Gorgs.

20:34.760 --> 20:41.140
And then there was also the sweet spot, which is applications that are vulnerable in the default config.

20:41.180 --> 20:43.300
And our example here was Harbor.

20:43.400 --> 20:50.540
And Harbor is a container registry, it's a CNCF graduate project, which means it's quite mature and people are using it.

20:50.540 --> 20:56.200
It's also apparently part of VMware Tanzu Kubernetes, but we didn't look into more of that.

20:56.200 --> 20:59.320
So we don't know if it's exploitable in that scenario as well.

20:59.560 --> 21:08.200
But in its default version, it's vulnerable in the default configuration, and also pre-authentication, which is quite bad.

21:08.240 --> 21:14.320
So they fixed this in version 2.11, which is the latest one, by updating their dependency.

21:14.520 --> 21:18.920
So if you have a Harbor instance, you definitely should update to the latest version.

21:19.240 --> 21:22.700
And I'm going to try to demo the Harbor exploit now.

21:22.720 --> 21:24.860
I'm going to try to do it live.

21:24.860 --> 21:26.960
I also have a backup video.

21:27.020 --> 21:28.380
But let's see.

21:28.600 --> 21:37.700
So first, I will try to log in as the attacker to show that there should be no attacker user.

21:37.820 --> 21:40.340
As you can see, the login doesn't work.

21:40.420 --> 21:50.280
So now if we start the exploit, and I hope this works over the unstable internet, we have to wait a bit.

21:50.280 --> 21:53.380
So there we see the query that the exploit is sending.

21:53.380 --> 21:57.520
This is an insert query that will insert the attacker user into the database.

21:57.520 --> 21:59.080
So then we could log in.

21:59.080 --> 22:02.480
And as we can see, it's sending a lot of bytes, four gigabytes.

22:02.520 --> 22:04.920
So it takes some time.

22:05.320 --> 22:17.780
And if it works successfully, then we should be able to log in and have administrative privileges in the Harbor application, so that we then could send all mess with the containers in there.

22:17.780 --> 22:20.280
So we saw an error, which is expected here.

22:20.280 --> 22:26.900
So if I now try to log in again, we should log in successfully, I hope.

22:29.240 --> 22:30.420
Okay, it doesn't seem to work.

22:30.420 --> 22:32.700
I have to switch to the backup video.

22:37.440 --> 22:40.060
And there on the left, now we try to log in again.

22:41.160 --> 22:44.700
And there we can see we can now log in and have admin privileges.

22:52.420 --> 22:53.960
All right, that was Postgres.

22:53.960 --> 22:59.480
Now also, let's look at MongoDB to show that it's not a Postgres and Go-only problem.

23:00.120 --> 23:04.260
So to look back, this is what a message in MongoDB looks like.

23:04.260 --> 23:05.880
We have a four-byte length field.

23:05.880 --> 23:07.480
This time, it's little-endian.

23:07.480 --> 23:10.420
Then we have two metadata fields that are not relevant here.

23:10.420 --> 23:13.160
And then we have the opcode, which is like the message type.

23:13.160 --> 23:19.020
And then the value is a complex binary structure that's called bson, which I think stands for binary JSON.

23:19.040 --> 23:23.920
And it's some nested data that's serialized to TLV sections.

23:23.920 --> 23:25.580
So they also have lengths again.

23:26.500 --> 23:30.800
And to directly look at some code, it's a little bit more, but I'm going to walk you through it.

23:30.860 --> 23:34.820
We first have a line here that takes the content bytes.

23:34.820 --> 23:39.260
So it takes the query and puts it into this bson representation.

23:39.960 --> 23:45.960
Then it computes the total length of the message by adding the header length and so on.

23:45.960 --> 23:48.940
And this is saved as a type usize variable.

23:48.940 --> 23:53.000
Again, on most modern systems, this is a 64 -bit integer.

23:53.560 --> 23:59.900
And then again, this is truncated explicitly to an int32, because that's what fits into the field.

23:59.900 --> 24:02.300
So the developer had to cut it down here.

24:02.300 --> 24:05.340
But of course, this is the same issue as before.

24:05.340 --> 24:14.940
If the length was larger than what can be expressed here, we have our truncation and things go wrong the exact same way as it did for Postgres.

24:16.240 --> 24:23.340
So for crafting a payload for MongoDB, it's a little bit more involved, because we have to avoid bad bytes.

24:23.340 --> 24:29.800
The payload must be valid UTF-8, because the strings are serialized as UTF-8 to the wire.

24:29.800 --> 24:33.400
So we cannot create any bytes that are invalid UTF-8.

24:33.560 --> 24:40.560
But the problem is, the message type that we need to write in order to craft the right injected message is 0xDD07.

24:40.620 --> 24:43.100
And that's always invalid UTF-8.

24:43.100 --> 24:45.260
So how can we do that?

24:45.460 --> 24:52.420
And also, size fields inside the message could become some byte values that would also be invalid UTF-8.

24:52.420 --> 24:54.460
So we have to be careful there as well.

24:54.720 --> 25:02.020
And the solution was to just use other metadata of the message we're injecting into to create these byte values.

25:02.140 --> 25:03.980
And I have an example for that.

25:03.980 --> 25:06.680
So this is a normal MongoDB query.

25:06.680 --> 25:09.580
It just queries for a movie based on a few attributes.

25:09.980 --> 25:13.340
And this is serialized to the following BSON document.

25:13.680 --> 25:17.140
In the beginning, we have in blue, the length of the whole document.

25:17.140 --> 25:23.620
Then in red, we have a type identifier that says the next key value pair has a string value.

25:23.900 --> 25:29.580
Then in yellow, we have the key, which in this case is title with a trailing null byte.

25:29.580 --> 25:33.020
Then in blue again, we have the length of the following string.

25:33.200 --> 25:36.720
And then in green, we have the actual string with a terminating null byte.

25:36.800 --> 25:39.540
Then we have the next key value pairs and so on.

25:39.540 --> 25:43.320
And at the end, we have another null byte that shows the end of the document.

25:43.700 --> 25:53.200
So if we want to have the hex sequence 0xDD07 anywhere in here, we cannot use the yellow and green parts because they have to be a valid UTF-8.

25:53.400 --> 26:05.660
But what we can do is, for example, we can take the length of a string and just make the string the length that will, when serialized to bytes, will be the exact byte sequence we need.

26:05.660 --> 26:12.220
So here we need to subtract one because there's a appended null byte, but then we have the DD07 there.

26:12.220 --> 26:16.580
And with this trick, now we can craft the entire injected message.

26:16.580 --> 26:23.020
I cannot go into all of this here because we don't have time, but this is the trick you need to be able to do it.

26:23.540 --> 26:32.740
So we also looked at libraries here and we found the official Rust MongoDB client library to be vulnerable and exploitable.

26:32.740 --> 26:37.340
We sent out our advisory also in February and they fixed in March.

26:37.340 --> 26:44.640
So if you use this, especially if you use it in production, which I doubt because it's Rust, but then you should update to 2.8.2.

26:46.160 --> 26:49.000
Okay, let's now take a step back.

26:49.000 --> 26:55.060
We've seen all the cool bugs and the exploitation, but is this actually applicable to the real world?

26:55.100 --> 26:58.660
We've seen the demo, but maybe this was just a lucky punch.

26:58.800 --> 27:04.920
So let's talk about constraints and especially about the elephant in the room, which here weighs four gigabytes.

27:05.220 --> 27:12.340
Because a lot of you probably are thinking, well, can we actually send four gigabytes into all the applications?

27:12.340 --> 27:14.740
Aren't apps limiting input sizes?

27:14.780 --> 27:16.360
And you would be right.

27:16.380 --> 27:29.260
There's a lot of common protections like default body size limits of your favorite web framework, or maybe JSON and form decode maximum sizes, or you have your reverse proxy that's limiting request size and so on.

27:29.920 --> 27:34.380
And I also encountered this and thought about some potential bypasses here.

27:34.380 --> 27:38.660
So the first idea is that sometimes endpoints are just unprotected.

27:38.660 --> 27:41.760
So maybe your framework doesn't have a default size limit.

27:41.760 --> 27:46.340
And also sometimes the developers explicitly disable the limits.

27:46.340 --> 27:58.780
This was the case with Harbor, for example, they have an NGINX reverse proxy, which usually has a one megabyte request limit, but they explicitly disabled it, I guess, because they need to receive big docker images.

27:58.780 --> 28:01.240
So they need more request size.

28:01.240 --> 28:05.400
But this also meant that the whole API is now not limited anymore.

28:05.400 --> 28:12.420
And the goal framework that's used under the hood of the server did not limit this, so we could send our big payload.

28:13.520 --> 28:16.400
The next trick would be to use compression.

28:16.420 --> 28:24.980
This, on the one hand, allows you to send your attacker payload much quicker because you can compress it down and don't have to send four gigabytes over the internet.

28:25.380 --> 28:34.340
And also there can be logic bugs where maybe your reverse proxy or your framework first checks the request size, but does a doc decompression after that.

28:34.340 --> 28:46.940
So the check is essentially useless because your compressed data will inflate a lot and you can craft a big payload that after decompression still causes the overflow.

28:46.940 --> 28:51.080
So for NGINX, for example, this works because NGINX does not look at the body.

28:51.080 --> 28:52.680
It doesn't try to decompress it.

28:52.680 --> 28:55.280
It just looks at the raw size of the request.

28:55.280 --> 28:59.960
So if your backend does decompression, then you could bypass this.

28:59.960 --> 29:08.200
And also, for example, the JavaScript Fastify framework does both checks the body size and also does the decompression, but it does it in the wrong order.

29:08.200 --> 29:10.480
So you can bypass the limits.

29:11.280 --> 29:16.120
Then you might also have WebSockets support on the server of your target.

29:16.160 --> 29:20.820
This is cool because WebSockets also, in theory, supports compression.

29:20.820 --> 29:23.280
It's not always enabled, but it can help you.

29:23.280 --> 29:27.940
But even without that, a single WebSocket request can be super large.

29:27.940 --> 29:31.740
I think it's a 64-bit integer, the size field.

29:31.740 --> 29:35.520
So you can definitely send four gigabyte messages with that.

29:35.520 --> 29:43.140
And maybe the middlewares and filters that are present in your framework don't apply to each individual WebSocket message.

29:43.140 --> 29:44.980
So maybe there's no limit there.

29:45.760 --> 29:52.120
Then you could also use alternate body types, for example, a multipart form instead of a JSON body.

29:52.120 --> 29:56.900
And maybe the limits are different there, or the developer forgot to configure a limit.

29:56.900 --> 29:58.940
So this could be another bypass.

29:59.660 --> 30:13.640
And then this one, which I like the most, is a little bit more creative and open, which I call server-side creation, which is you just find other means of bringing a big string to the server without sending it in your requests.

30:13.640 --> 30:22.960
And one example is SSRF, where you point some functionality of the server to an attacker server, which then returns a very large response.

30:22.960 --> 30:28.580
And if this one gets saved into the database or used in a query, it will also cause the overflow.

30:28.580 --> 30:32.840
And this, for example, worked in GOGS, where you have a web hooks feature.

30:33.280 --> 30:39.760
But beyond the SSRF, you might also have templating or translation that you could use to inflate a string.

30:40.040 --> 30:43.800
And this all depends on the business logic and the features of the server.

30:43.800 --> 30:46.920
So that's where you can get creative to find bypasses.

30:48.140 --> 30:55.160
All right, now that we've seen how we might bypass limitations of frameworks and servers, we also need to look at languages.

30:55.160 --> 30:58.500
We've already seen some differences at the vulnerable libraries.

30:58.640 --> 31:04.860
So we need to see how well languages handle large payloads, like how big can strings and buffers be.

31:04.860 --> 31:07.200
And also, what about integer overflows?

31:07.200 --> 31:11.800
Does every language have silent integer overflows, or maybe some languages are safe?

31:11.800 --> 31:26.020
So for the large payloads, we can see that Go, Python, and Rust, all of the languages we looked at, have large enough string and buffer sizes that you can send four gigabytes and they can handle it.

31:26.120 --> 31:28.000
For Java, you're out of luck.

31:28.000 --> 31:33.880
And we also seen this with unexploitable libraries in Java.

31:34.200 --> 31:40.560
And that's because strings and buffers here are backed by arrays, which have integer indexes.

31:40.560 --> 31:46.180
So you cannot index more than, I think, 2 to the power of 31 here.

31:46.460 --> 31:51.740
For C Sharp, the string size is not big enough, but the buffer size is.

31:51.760 --> 31:59.080
So if you have multiple strings that you control, then you can still end up with a buffer that becomes bigger than four gigabytes.

31:59.460 --> 32:02.700
And for JavaScript, it totally depends on the implementation.

32:02.860 --> 32:08.780
For Node, strings are much smaller compared to the other languages, but buffers can be large enough.

32:10.180 --> 32:24.880
For integer overflows, we can see that for addition overflows, which just means you add two integers, we can see that in Go, Java, and C Sharp, a silent integer overflow happens, so it will just wrap around.

32:25.300 --> 32:28.240
For JavaScript, there will be no overflow.

32:28.240 --> 32:33.480
Things will just get more inaccurate because it's JavaScript and they use floats under the hood.

32:34.040 --> 32:39.700
And in Python, you have arbitrarily sized integers, so you cannot overflow that.

32:40.000 --> 32:42.840
And in Rust, it depends on how you build your project.

32:42.840 --> 32:50.540
If you build it in debug mode, the compiler will emit a check that checks for overflows, but in a release build, it will not do that.

32:51.440 --> 33:07.080
And for serialization overflows, which basically is the case when you have a standard function in your language that allows you to write an integer to a fixed size field, does this function check for overflows or not?

33:07.080 --> 33:19.080
And in all of the type-safe languages, this does not apply because the types that the function can receive cannot overflow the size field or the field that it gets serialized to.

33:19.420 --> 33:29.120
But this just means that the developers now have to do the check before casting to this type, and we've seen that this leads to bugs in our Go and Rust examples.

33:29.120 --> 33:33.500
In JavaScript, it's again implementation-dependent.

33:33.600 --> 33:37.920
I think in Node.js, it does the check, but in Bun or Deno, it might not do it.

33:38.720 --> 33:43.260
And in Python, the struct.pack function actually does a check.

33:43.260 --> 33:48.660
If you pass a too-large integer, it will throw an error if you try to serialize it to a too-small field.

33:49.700 --> 34:03.920
So, to summarize the real-world applicability, we can say that in a lot of times, we can send large payloads past the limits for silent integer overflows or truncations.

34:03.920 --> 34:10.180
We've also seen that in many languages, it works, or the developers do the mistake for the language.

34:10.340 --> 34:15.420
And we've also seen that you can definitely exploit some real-world applications with this.

34:16.760 --> 34:18.320
All right.

34:18.320 --> 34:24.960
Now, let's look at what is there more to do in this kind of research for binary protocol smuggling.

34:25.260 --> 34:28.460
But first, I have to give a small disclaimer here.

34:28.460 --> 34:41.580
Please don't go out and send 4 gigabytes to every API endpoint in your bug bounty scope, because you will crash systems, you will get blocked, and you will ruin a DevOps engineer's day because they have to look at the monitoring alert.

34:41.700 --> 34:43.540
So, don't do it, please.

34:44.660 --> 34:50.540
But, speaking of this, it would be cool to actually have a non-invasive detection mechanism.

34:50.540 --> 34:52.380
In white-box tests, it's okay.

34:52.380 --> 34:56.640
You can make your local testing environment and just hammer it with gigabytes of data.

34:56.640 --> 34:59.660
But, of course, in black-box tests, that doesn't work.

34:59.660 --> 35:15.880
So, if we could find a methodology to find, for example, a fingerprint of a database client library being used, and we would know that this library is vulnerable to these kind of texts, then we could either directly report it or only do the large payload then.

35:16.300 --> 35:21.820
And if this would be a tool, it would be great to aid you in pen tests and so on.

35:22.920 --> 35:38.200
And, of course, it would be awesome to research a lot of more stuff, more protocols, like there's more databases than the stuff we looked at today, and there's all of the other systems, caches, message queues, and so on, that the application has to talk to.

35:38.750 --> 35:41.780
We can also try to find more desync techniques.

35:41.780 --> 35:45.820
For example, look into protocols that use delimiters and not size fields.

35:46.260 --> 35:57.320
And we can also find more large payload message, which is ways to bypass the filters of a framework, for example, to bypass the request size limits.

35:57.320 --> 36:03.180
Or maybe we can find generic ways of making a server craft a very large string.

36:03.600 --> 36:05.910
Maybe there's some way to do this.

36:07.350 --> 36:12.750
And, of course, all of the stuff we looked at today had 4-byte length fields.

36:12.790 --> 36:14.670
But what about 2-byte length fields?

36:14.670 --> 36:17.070
This would be much easier to exploit.

36:17.290 --> 36:21.670
Sending 65 kilobytes instead of 4 gigabytes is much easier.

36:21.670 --> 36:25.550
You don't have the hassle with all the limitations.

36:25.950 --> 36:34.790
And I already started to look into this a little bit, so stay tuned for more to come in the future, but feel free to also go out and check this yourself.

36:36.710 --> 36:39.450
So, let's wrap things up.

36:39.890 --> 36:41.750
I have a few takeaways.

36:41.770 --> 36:47.610
And the first one is that I think integer overflows are still relevant in memory-safe languages.

36:47.610 --> 36:55.790
Of course, in C, if you have an integer overflow, it's easy to get some memory corruption with it, so that's done.

36:55.790 --> 37:02.210
But in memory-safe languages, the worst case that can happen is an array out of bounds or whatever.

37:02.370 --> 37:10.030
But with these data attacks in the binary protocols that we've seen today, now we have a new way of exploiting these.

37:10.030 --> 37:17.710
And I think developers kind of forgot about integer overflows and maybe researchers as well, at least in the memory-safe languages.

37:18.270 --> 37:29.070
Then we've also seen that it's entirely feasible to send large amounts of data, even with protections, because there's many ways how you can bypass these protections.

37:29.570 --> 37:34.150
And finally, to get back to the title, SQL injection isn't dead.

37:34.210 --> 37:38.170
Of course, it was never dead, at least not in the real world.

37:38.170 --> 37:42.710
We've seen a lot of real-world applications with SQL injections.

37:42.710 --> 37:50.250
But on paper, you could say developers have the right tools to write secure SQL-based applications.

37:50.250 --> 37:55.270
They have parameterized queries, they have query builders, they have ORMs.

37:55.270 --> 38:02.450
So if they use all of this, and the application is safe, so if you can't hack it, you just have to go a level deeper.

38:02.930 --> 38:03.670
Thank you.

