1 00:00:00,000 --> 00: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. 2 00:00:30,720 --> 00:00:33,620 So, to hook you at the beginning, I have a small teaser. 3 00:00:33,620 --> 00: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. 4 00:00:44,120 --> 00: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. 5 00:00:51,480 --> 00: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. 6 00:01:06,480 --> 00:01:08,620 A bit about me, I'm Paul. 7 00:01:08,620 --> 00:01:11,740 I'm a vulnerability researcher at Sonar's R&D team. 8 00:01:11,820 --> 00: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. 9 00:01:17,600 --> 00:01:22,200 And that's why we look at a lot of code and try to find stuff there. 10 00:01:22,960 --> 00: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. 11 00:01:32,640 --> 00:01:35,780 And we're going to focus on Postgres and MongoDB here. 12 00:01:35,900 --> 00: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. 13 00:01:47,840 --> 00:01:53,640 So, in short, the idea is to do request smuggling, but for binary protocols. 14 00:01:54,700 --> 00: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. 15 00:02:04,460 --> 00:02:08,140 For example, between a reverse proxy and an upstream back end. 16 00:02:08,140 --> 00: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. 17 00:02:15,800 --> 00: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. 18 00:02:27,580 --> 00: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. 19 00:02:35,100 --> 00:02:37,120 But what about other protocols? 20 00:02:37,220 --> 00:02:40,040 And specifically, what about binary protocols? 21 00:02:40,840 --> 00:02:42,740 Where do they have message boundaries? 22 00:02:42,740 --> 00:02:44,020 How do they do it? 23 00:02:44,260 --> 00: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. 24 00:02:52,380 --> 00: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. 25 00:03:05,940 --> 00:03:09,180 So how could we desync in these protocols? 26 00:03:09,440 --> 00:03:11,460 For delimiters, it sounds quite easy. 27 00:03:11,460 --> 00:03:16,080 You just have to find a way to put a delimiter where it's not supposed to be, in a value. 28 00:03:16,220 --> 00:03:21,800 Of course, it's not that simple, but at least you directly have an idea of what you could do. 29 00:03:22,460 --> 00: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? 30 00:03:31,520 --> 00:03:38,940 And then I thought, okay, maybe some endianness issues, or maybe integer overflows, and I wanted to go deeper into that. 31 00:03:39,460 --> 00:03:46,620 So for that, I first looked at the modern web application landscape, where our binary protocols used there. 32 00:03:46,820 --> 00: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. 33 00:03:58,180 --> 00:04:02,580 And I wanted to focus on the connection between the application and the database. 34 00:04:02,920 --> 00: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. 35 00:04:12,320 --> 00: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. 36 00:04:25,660 --> 00: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? 37 00:04:35,020 --> 00:04:38,040 So we have kind of guaranteed user input. 38 00:04:39,460 --> 00:04:44,120 So let's start in and look at some database wire protocols. 39 00:04:44,180 --> 00:04:52,360 And for that, I have a small overview of four popular databases and how their messages and their protocols look like. 40 00:04:52,720 --> 00:04:55,740 So for Postgres, this is how a message looks like. 41 00:04:55,740 --> 00: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. 42 00:05:04,180 --> 00:05:07,160 For MySQL, it looks kind of similar. 43 00:05:07,160 --> 00: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. 44 00:05:18,880 --> 00:05:21,280 And there, of course, you have the value. 45 00:05:21,720 --> 00: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. 46 00:05:32,580 --> 00:05:37,120 There, you first have a type byte, then you have a length, but it's not a fixed size field. 47 00:05:37,120 --> 00:05:39,760 It's a decimal integer that has to be parsed. 48 00:05:39,780 --> 00:05:43,660 Then you have a delimiter, and then the value, and then another delimiter. 49 00:05:44,460 --> 00:05:48,200 And finally, for MongoDB, it's a little bit longer. 50 00:05:48,200 --> 00:05:51,040 You first have the message length, again, a four-byte field. 51 00:05:51,100 --> 00:05:53,000 Then you have a little bit more metadata. 52 00:05:53,180 --> 00: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. 53 00:06:01,800 --> 00:06:04,340 So let's start with Postgres. 54 00:06:04,700 --> 00:06:07,480 Again, this is how a packet looks there. 55 00:06:07,480 --> 00:06:12,820 One-byte type identifier, four-byte integer length fields, and then the value. 56 00:06:13,140 --> 00:06:16,300 So I thought about, okay, four-byte integer fields. 57 00:06:16,300 --> 00: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. 58 00:06:24,300 --> 00:06:30,360 But maybe what if we could somehow make a query that's bigger than that? 59 00:06:30,360 --> 00:06:36,320 How do actually libraries handle this kind of too-large thing? 60 00:06:36,600 --> 00:06:42,920 And for that, I started to look into code of Postgres client libraries, and I found the first bug. 61 00:06:43,240 --> 00:06:46,500 This is the code of a library called PGX. 62 00:06:47,980 --> 00:06:54,200 It's supposed to encode the content of a message into a larger message buffer. 63 00:06:54,380 --> 00:06:56,660 So first, it writes the message type. 64 00:06:56,660 --> 00:06:59,220 In this case, it's a B for a bind message. 65 00:06:59,220 --> 00:07:03,340 Then it saves the offset, where it should later write the size to. 66 00:07:03,360 --> 00:07:06,640 Then it adds all of the actual data of the message. 67 00:07:06,760 --> 00:07:08,860 And finally, it writes the size. 68 00:07:08,900 --> 00: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. 69 00:07:17,840 --> 00:07:20,680 Then it takes the length of the sliced buffer. 70 00:07:20,720 --> 00:07:26,200 And here, the length is of type int, which in most modern systems is a 64-bit integer. 71 00:07:26,400 --> 00:07:31,300 But then this is truncated down explicitly to an int32. 72 00:07:31,540 --> 00: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. 73 00:07:40,540 --> 00:07:44,940 But then the library still sends all of the data after that. 74 00:07:45,060 --> 00: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. 75 00:07:56,200 --> 00:07:59,120 But in fact, it's data from the first message. 76 00:08:00,080 --> 00:08:03,000 So let's look at this again with example messages . 77 00:08:04,060 --> 00:08:06,140 So here we have a very small one. 78 00:08:06,140 --> 00:08:08,000 We have a 4-byte value. 79 00:08:08,000 --> 00:08:13,140 And then the length becomes 8, because the size of the length field itself is also included in the length. 80 00:08:13,140 --> 00:08:14,780 So it's a length of 8. 81 00:08:15,800 --> 00:08:19,120 This is how the largest possible message looks like. 82 00:08:19,120 --> 00: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. 83 00:08:26,420 --> 00: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. 84 00:08:35,420 --> 00: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. 85 00:08:44,440 --> 00:08:48,360 So the length is just 4, which means this is a tiny message. 86 00:08:48,360 --> 00:08:53,640 And then after that, in the connection, there comes some garbage ace that the database can't really understand. 87 00:08:53,840 --> 00: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. 88 00:09:03,660 --> 00:09:06,580 So let's zoom out again and look at this again. 89 00:09:06,580 --> 00:09:09,140 This is the small message, all is good. 90 00:09:09,140 --> 00:09:13,060 This is the large message that does not cause a truncation. 91 00:09:13,240 --> 00:09:17,740 So both the application and the database see it and parse it the same way. 92 00:09:18,500 --> 00: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. 93 00:09:28,600 --> 00:09:32,840 So when the database parses it, it sees one small message and then garbage. 94 00:09:32,840 --> 00: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. 95 00:09:44,740 --> 00:09:47,640 So the impact is quite high here. 96 00:09:47,640 --> 00:09:50,420 You can inject entire SQL statements. 97 00:09:50,420 --> 00:09:56,180 You're not limited to union-based or subquery-based SQL injection like with the very classic ones. 98 00:09:56,180 --> 00: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. 99 00:10:08,880 --> 00: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. 100 00:10:15,860 --> 00: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. 101 00:10:26,560 --> 00: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. 102 00:10:44,300 --> 00: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. 103 00:10:51,920 --> 00: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. 104 00:11:03,500 --> 00:11:06,200 So it still works, just a little less convenient. 105 00:11:07,340 --> 00:11:09,620 But how does this look in the real world? 106 00:11:09,640 --> 00: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? 107 00:11:19,480 --> 00:11:21,120 Why do we need to have the overflow? 108 00:11:21,400 --> 00:11:24,680 And you would be correct, because this was just the simplification. 109 00:11:25,080 --> 00:11:27,540 So let's look at how this actually looks. 110 00:11:28,220 --> 00:11:30,480 This is a two-line code snippet. 111 00:11:30,480 --> 00:11:32,980 The ID here is user controlled. 112 00:11:32,980 --> 00:11:35,280 In this case, it's a very small one. 113 00:11:35,440 --> 00:11:37,200 And then we have our query. 114 00:11:37,200 --> 00:11:42,720 You might recognize it from the teaser code snippet that uses the ID to select a user from the database. 115 00:11:42,720 --> 00:11:46,780 And the resulting packet or message that's sent over the wire looks like this. 116 00:11:46,780 --> 00:11:52,620 We have our type byte, then a small length, and then we have the value with a trailing null byte. 117 00:11:52,680 --> 00:11:54,200 All is fine here. 118 00:11:54,200 --> 00: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. 119 00:12:05,680 --> 00:12:08,240 So we see that the truncation happened. 120 00:12:08,440 --> 00:12:14,620 And now when the database tries to parse this packet, it will see, okay, it's a length of 38 bytes. 121 00:12:14,620 --> 00:12:17,300 So I'm going to parse until that. 122 00:12:17,320 --> 00:12:20,140 And after that, it will try to parse the next message. 123 00:12:20,520 --> 00:12:28,160 So the attacker has to put their cleverly crafted payload at that exact offset in this big buffer of A's. 124 00:12:28,740 --> 00:12:31,280 But how does the attacker know this offset? 125 00:12:32,660 --> 00: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. 126 00:12:41,860 --> 00:12:45,580 So if you know these things, you can just calculate the offset and you're done. 127 00:12:45,600 --> 00: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. 128 00:12:51,800 --> 00:12:53,480 So what can you do then? 129 00:12:54,300 --> 00:12:57,800 And of course, the naive solution is to brute force the offsets. 130 00:12:57,800 --> 00: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. 131 00:13:10,120 --> 00:13:12,160 So can we make this more reliable? 132 00:13:13,140 --> 00: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. 133 00:13:22,520 --> 00: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, 134 00:13:35,880 --> 00:13:44,760 and so on, until at the very end, the big message with the actual attacker SQL payload is parsed and executed. 135 00:13:44,760 --> 00: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. 136 00:13:55,700 --> 00:14:02,580 But usually, that's not too bad, because applications have connection pools, and they just reopen connections if they get closed. 137 00:14:03,100 --> 00:14:04,720 So let's visualize this. 138 00:14:04,720 --> 00:14:08,660 This is the smallest possible message in the PostgreSQL protocol. 139 00:14:08,940 --> 00:14:14,040 So we repeat this a lot of times, and in the end, we have our actual payload. 140 00:14:14,340 --> 00:14:21,060 So if the offset happens to align with the first or the fifth and so on byte, then we're good. 141 00:14:21,060 --> 00:14:27,260 But if it hits one of the other bytes, then there's this fatal parsing error and the connection is closed. 142 00:14:27,640 --> 00:14:30,880 So if that happens, we can try again. 143 00:14:31,000 --> 00: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. 144 00:14:41,180 --> 00:14:48,080 Otherwise, we just try again, and again, and again, until we're lucky at the last try. 145 00:14:48,760 --> 00: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. 146 00:14:58,460 --> 00:15:04,520 But still, in the worst case, we have to send five times four gigabytes, which can still be a lot. 147 00:15:04,520 --> 00:15:06,720 So maybe we can make it even better. 148 00:15:07,540 --> 00:15:12,100 And for this, I looked at what was holding us back with the last strategy. 149 00:15:12,100 --> 00: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. 150 00:15:18,300 --> 00:15:21,120 So maybe we can make them valid type bytes. 151 00:15:21,540 --> 00:15:24,200 And I call this technique trampolines. 152 00:15:24,260 --> 00: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. 153 00:15:37,660 --> 00:15:42,280 And we can see down below the whole attacker payload. 154 00:15:42,360 --> 00:15:48,820 And we'll see how things are parsed when the offset hits certain bytes here. 155 00:15:48,820 --> 00:15:53,440 So if it hits the very beginning, the database parses it like this. 156 00:15:53,440 --> 00:15:58,560 First queue is the type byte, and then qqqq is the length of the first message. 157 00:15:58,560 --> 00: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. 158 00:16:06,600 --> 00:16:11,960 And of course, this will cause like a semantic syntax error, but this does not close the connection. 159 00:16:11,960 --> 00:16:17,420 So the database will just complain with an error response, but continue parsing the next message. 160 00:16:17,420 --> 00:16:20,740 So this is like jumping over this many bytes. 161 00:16:21,080 --> 00:16:23,460 So that's the trampoline right there. 162 00:16:23,480 --> 00: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. 163 00:16:39,660 --> 00:16:45,420 So if we would have hit any of the other bytes, things look slightly different, but not too different. 164 00:16:45,580 --> 00:16:49,200 Here, the length is parsed as qsss. 165 00:16:49,200 --> 00:16:52,360 The s is also a very valid type byte. 166 00:16:52,440 --> 00: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. 167 00:17:05,100 --> 00:17:08,720 But does this actually work like we planned to do? 168 00:17:08,780 --> 00: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. 169 00:17:21,200 --> 00:17:23,640 And this is just under one gigabyte. 170 00:17:23,640 --> 00:17:28,000 So the first byte of the length can never be larger than hex 3f. 171 00:17:28,000 --> 00:17:31,340 So that's bad, because with a q, that doesn't work. 172 00:17:32,340 --> 00:17:37,120 So what if we just use 3f as the type and all of the length bytes then? 173 00:17:37,720 --> 00: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. 174 00:17:48,440 --> 00:17:52,380 So I had to use a compromise here, which is an alternating pattern. 175 00:17:52,660 --> 00:17:58,720 And with this, now every second byte is a valid type byte, so a valid start of a message. 176 00:17:58,720 --> 00:18:04,200 So if we hit any of those bytes, we're good, because everything is parsed okay. 177 00:18:04,200 --> 00:18:07,060 And then we can do our trampolines, like I just showed. 178 00:18:07,060 --> 00: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. 179 00:18:12,540 --> 00:18:14,400 But still, we can try again. 180 00:18:14,460 --> 00:18:21,860 So after a maximum of two attempts, we are successful , which is much better than the initial brute force. 181 00:18:23,500 --> 00:18:28,480 All right, let's see how many vulnerable libraries there were that we found. 182 00:18:28,520 --> 00:18:34,920 And this is just a selection, we looked at more, but these are only the ones that were potentially vulnerable. 183 00:18:35,320 --> 00:18:41,320 So for Go, we tested four libraries, and we found all of them to be vulnerable and also exploitable. 184 00:18:41,320 --> 00:18:43,940 And I'm going to go into exploitability in a minute . 185 00:18:44,440 --> 00:18:49,820 Unfortunately, only one of them fixed the issues we reported, and the others did not. 186 00:18:49,820 --> 00:18:52,360 So they are still vulnerable in the latest version. 187 00:18:52,800 --> 00: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. 188 00:19:03,040 --> 00:19:04,720 So that's great to see. 189 00:19:05,500 --> 00: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. 190 00:19:16,820 --> 00: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. 191 00:19:30,080 --> 00:19:33,540 And we're going to look into these language differences later. 192 00:19:34,420 --> 00: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. 193 00:19:45,160 --> 00: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. 194 00:19:53,620 --> 00: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. 195 00:20:03,800 --> 00:20:07,700 So now let's go from vulnerable libraries to exploitable applications. 196 00:20:07,960 --> 00:20:14,480 So first, we have a set of applications that use these libraries, or have used libraries in a vulnerable version. 197 00:20:15,200 --> 00: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. 198 00:20:22,620 --> 00:20:25,480 So there might be, but we haven't proven it yet. 199 00:20:26,240 --> 00: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. 200 00:20:34,760 --> 00:20:41,140 And then there was also the sweet spot, which is applications that are vulnerable in the default config. 201 00:20:41,180 --> 00:20:43,300 And our example here was Harbor. 202 00:20:43,400 --> 00: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. 203 00:20:50,540 --> 00:20:56,200 It's also apparently part of VMware Tanzu Kubernetes, but we didn't look into more of that. 204 00:20:56,200 --> 00:20:59,320 So we don't know if it's exploitable in that scenario as well. 205 00:20:59,560 --> 00:21:08,200 But in its default version, it's vulnerable in the default configuration, and also pre-authentication, which is quite bad. 206 00:21:08,240 --> 00:21:14,320 So they fixed this in version 2.11, which is the latest one, by updating their dependency. 207 00:21:14,520 --> 00:21:18,920 So if you have a Harbor instance, you definitely should update to the latest version. 208 00:21:19,240 --> 00:21:22,700 And I'm going to try to demo the Harbor exploit now. 209 00:21:22,720 --> 00:21:24,860 I'm going to try to do it live. 210 00:21:24,860 --> 00:21:26,960 I also have a backup video. 211 00:21:27,020 --> 00:21:28,380 But let's see. 212 00:21:28,600 --> 00:21:37,700 So first, I will try to log in as the attacker to show that there should be no attacker user. 213 00:21:37,820 --> 00:21:40,340 As you can see, the login doesn't work. 214 00:21:40,420 --> 00: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. 215 00:21:50,280 --> 00:21:53,380 So there we see the query that the exploit is sending. 216 00:21:53,380 --> 00:21:57,520 This is an insert query that will insert the attacker user into the database. 217 00:21:57,520 --> 00:21:59,080 So then we could log in. 218 00:21:59,080 --> 00:22:02,480 And as we can see, it's sending a lot of bytes, four gigabytes. 219 00:22:02,520 --> 00:22:04,920 So it takes some time. 220 00:22:05,320 --> 00: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. 221 00:22:17,780 --> 00:22:20,280 So we saw an error, which is expected here. 222 00:22:20,280 --> 00:22:26,900 So if I now try to log in again, we should log in successfully, I hope. 223 00:22:29,240 --> 00:22:30,420 Okay, it doesn't seem to work. 224 00:22:30,420 --> 00:22:32,700 I have to switch to the backup video. 225 00:22:37,440 --> 00:22:40,060 And there on the left, now we try to log in again. 226 00:22:41,160 --> 00:22:44,700 And there we can see we can now log in and have admin privileges. 227 00:22:52,420 --> 00:22:53,960 All right, that was Postgres. 228 00:22:53,960 --> 00:22:59,480 Now also, let's look at MongoDB to show that it's not a Postgres and Go-only problem. 229 00:23:00,120 --> 00:23:04,260 So to look back, this is what a message in MongoDB looks like. 230 00:23:04,260 --> 00:23:05,880 We have a four-byte length field. 231 00:23:05,880 --> 00:23:07,480 This time, it's little-endian. 232 00:23:07,480 --> 00:23:10,420 Then we have two metadata fields that are not relevant here. 233 00:23:10,420 --> 00:23:13,160 And then we have the opcode, which is like the message type. 234 00:23:13,160 --> 00:23:19,020 And then the value is a complex binary structure that's called bson, which I think stands for binary JSON. 235 00:23:19,040 --> 00:23:23,920 And it's some nested data that's serialized to TLV sections. 236 00:23:23,920 --> 00:23:25,580 So they also have lengths again. 237 00:23:26,500 --> 00: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. 238 00:23:30,860 --> 00:23:34,820 We first have a line here that takes the content bytes. 239 00:23:34,820 --> 00:23:39,260 So it takes the query and puts it into this bson representation. 240 00:23:39,960 --> 00:23:45,960 Then it computes the total length of the message by adding the header length and so on. 241 00:23:45,960 --> 00:23:48,940 And this is saved as a type usize variable. 242 00:23:48,940 --> 00:23:53,000 Again, on most modern systems, this is a 64 -bit integer. 243 00:23:53,560 --> 00:23:59,900 And then again, this is truncated explicitly to an int32, because that's what fits into the field. 244 00:23:59,900 --> 00:24:02,300 So the developer had to cut it down here. 245 00:24:02,300 --> 00:24:05,340 But of course, this is the same issue as before. 246 00:24:05,340 --> 00: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. 247 00:24:16,240 --> 00:24:23,340 So for crafting a payload for MongoDB, it's a little bit more involved, because we have to avoid bad bytes. 248 00:24:23,340 --> 00:24:29,800 The payload must be valid UTF-8, because the strings are serialized as UTF-8 to the wire. 249 00:24:29,800 --> 00:24:33,400 So we cannot create any bytes that are invalid UTF-8. 250 00:24:33,560 --> 00: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. 251 00:24:40,620 --> 00:24:43,100 And that's always invalid UTF-8. 252 00:24:43,100 --> 00:24:45,260 So how can we do that? 253 00:24:45,460 --> 00:24:52,420 And also, size fields inside the message could become some byte values that would also be invalid UTF-8. 254 00:24:52,420 --> 00:24:54,460 So we have to be careful there as well. 255 00:24:54,720 --> 00:25:02,020 And the solution was to just use other metadata of the message we're injecting into to create these byte values. 256 00:25:02,140 --> 00:25:03,980 And I have an example for that. 257 00:25:03,980 --> 00:25:06,680 So this is a normal MongoDB query. 258 00:25:06,680 --> 00:25:09,580 It just queries for a movie based on a few attributes. 259 00:25:09,980 --> 00:25:13,340 And this is serialized to the following BSON document. 260 00:25:13,680 --> 00:25:17,140 In the beginning, we have in blue, the length of the whole document. 261 00:25:17,140 --> 00:25:23,620 Then in red, we have a type identifier that says the next key value pair has a string value. 262 00:25:23,900 --> 00:25:29,580 Then in yellow, we have the key, which in this case is title with a trailing null byte. 263 00:25:29,580 --> 00:25:33,020 Then in blue again, we have the length of the following string. 264 00:25:33,200 --> 00:25:36,720 And then in green, we have the actual string with a terminating null byte. 265 00:25:36,800 --> 00:25:39,540 Then we have the next key value pairs and so on. 266 00:25:39,540 --> 00:25:43,320 And at the end, we have another null byte that shows the end of the document. 267 00:25:43,700 --> 00: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. 268 00:25:53,400 --> 00: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. 269 00:26:05,660 --> 00:26:12,220 So here we need to subtract one because there's a appended null byte, but then we have the DD07 there. 270 00:26:12,220 --> 00:26:16,580 And with this trick, now we can craft the entire injected message. 271 00:26:16,580 --> 00: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. 272 00:26:23,540 --> 00:26:32,740 So we also looked at libraries here and we found the official Rust MongoDB client library to be vulnerable and exploitable. 273 00:26:32,740 --> 00:26:37,340 We sent out our advisory also in February and they fixed in March. 274 00:26:37,340 --> 00: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. 275 00:26:46,160 --> 00:26:49,000 Okay, let's now take a step back. 276 00:26:49,000 --> 00:26:55,060 We've seen all the cool bugs and the exploitation, but is this actually applicable to the real world? 277 00:26:55,100 --> 00:26:58,660 We've seen the demo, but maybe this was just a lucky punch. 278 00:26:58,800 --> 00:27:04,920 So let's talk about constraints and especially about the elephant in the room, which here weighs four gigabytes. 279 00:27:05,220 --> 00:27:12,340 Because a lot of you probably are thinking, well, can we actually send four gigabytes into all the applications? 280 00:27:12,340 --> 00:27:14,740 Aren't apps limiting input sizes? 281 00:27:14,780 --> 00:27:16,360 And you would be right. 282 00:27:16,380 --> 00: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. 283 00:27:29,920 --> 00:27:34,380 And I also encountered this and thought about some potential bypasses here. 284 00:27:34,380 --> 00:27:38,660 So the first idea is that sometimes endpoints are just unprotected. 285 00:27:38,660 --> 00:27:41,760 So maybe your framework doesn't have a default size limit. 286 00:27:41,760 --> 00:27:46,340 And also sometimes the developers explicitly disable the limits. 287 00:27:46,340 --> 00: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. 288 00:27:58,780 --> 00:28:01,240 So they need more request size. 289 00:28:01,240 --> 00:28:05,400 But this also meant that the whole API is now not limited anymore. 290 00:28:05,400 --> 00: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. 291 00:28:13,520 --> 00:28:16,400 The next trick would be to use compression. 292 00:28:16,420 --> 00: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. 293 00:28:25,380 --> 00: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. 294 00:28:34,340 --> 00: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. 295 00:28:46,940 --> 00:28:51,080 So for NGINX, for example, this works because NGINX does not look at the body. 296 00:28:51,080 --> 00:28:52,680 It doesn't try to decompress it. 297 00:28:52,680 --> 00:28:55,280 It just looks at the raw size of the request. 298 00:28:55,280 --> 00:28:59,960 So if your backend does decompression, then you could bypass this. 299 00:28:59,960 --> 00: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. 300 00:29:08,200 --> 00:29:10,480 So you can bypass the limits. 301 00:29:11,280 --> 00:29:16,120 Then you might also have WebSockets support on the server of your target. 302 00:29:16,160 --> 00:29:20,820 This is cool because WebSockets also, in theory, supports compression. 303 00:29:20,820 --> 00:29:23,280 It's not always enabled, but it can help you. 304 00:29:23,280 --> 00:29:27,940 But even without that, a single WebSocket request can be super large. 305 00:29:27,940 --> 00:29:31,740 I think it's a 64-bit integer, the size field. 306 00:29:31,740 --> 00:29:35,520 So you can definitely send four gigabyte messages with that. 307 00:29:35,520 --> 00:29:43,140 And maybe the middlewares and filters that are present in your framework don't apply to each individual WebSocket message. 308 00:29:43,140 --> 00:29:44,980 So maybe there's no limit there. 309 00:29:45,760 --> 00:29:52,120 Then you could also use alternate body types, for example, a multipart form instead of a JSON body. 310 00:29:52,120 --> 00:29:56,900 And maybe the limits are different there, or the developer forgot to configure a limit. 311 00:29:56,900 --> 00:29:58,940 So this could be another bypass. 312 00:29:59,660 --> 00: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. 313 00:30:13,640 --> 00: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. 314 00:30:22,960 --> 00:30:28,580 And if this one gets saved into the database or used in a query, it will also cause the overflow. 315 00:30:28,580 --> 00:30:32,840 And this, for example, worked in GOGS, where you have a web hooks feature. 316 00:30:33,280 --> 00:30:39,760 But beyond the SSRF, you might also have templating or translation that you could use to inflate a string. 317 00:30:40,040 --> 00:30:43,800 And this all depends on the business logic and the features of the server. 318 00:30:43,800 --> 00:30:46,920 So that's where you can get creative to find bypasses. 319 00:30:48,140 --> 00: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. 320 00:30:55,160 --> 00:30:58,500 We've already seen some differences at the vulnerable libraries. 321 00:30:58,640 --> 00:31:04,860 So we need to see how well languages handle large payloads, like how big can strings and buffers be. 322 00:31:04,860 --> 00:31:07,200 And also, what about integer overflows? 323 00:31:07,200 --> 00:31:11,800 Does every language have silent integer overflows, or maybe some languages are safe? 324 00:31:11,800 --> 00: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. 325 00:31:26,120 --> 00:31:28,000 For Java, you're out of luck. 326 00:31:28,000 --> 00:31:33,880 And we also seen this with unexploitable libraries in Java. 327 00:31:34,200 --> 00:31:40,560 And that's because strings and buffers here are backed by arrays, which have integer indexes. 328 00:31:40,560 --> 00:31:46,180 So you cannot index more than, I think, 2 to the power of 31 here. 329 00:31:46,460 --> 00:31:51,740 For C Sharp, the string size is not big enough, but the buffer size is. 330 00:31:51,760 --> 00: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. 331 00:31:59,460 --> 00:32:02,700 And for JavaScript, it totally depends on the implementation. 332 00:32:02,860 --> 00:32:08,780 For Node, strings are much smaller compared to the other languages, but buffers can be large enough. 333 00:32:10,180 --> 00: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. 334 00:32:25,300 --> 00:32:28,240 For JavaScript, there will be no overflow. 335 00:32:28,240 --> 00:32:33,480 Things will just get more inaccurate because it's JavaScript and they use floats under the hood. 336 00:32:34,040 --> 00:32:39,700 And in Python, you have arbitrarily sized integers, so you cannot overflow that. 337 00:32:40,000 --> 00:32:42,840 And in Rust, it depends on how you build your project. 338 00:32:42,840 --> 00: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. 339 00:32:51,440 --> 00: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? 340 00:33:07,080 --> 00: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. 341 00:33:19,420 --> 00: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. 342 00:33:29,120 --> 00:33:33,500 In JavaScript, it's again implementation-dependent. 343 00:33:33,600 --> 00:33:37,920 I think in Node.js, it does the check, but in Bun or Deno, it might not do it. 344 00:33:38,720 --> 00:33:43,260 And in Python, the struct.pack function actually does a check. 345 00:33:43,260 --> 00: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. 346 00:33:49,700 --> 00: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. 347 00:34:03,920 --> 00:34:10,180 We've also seen that in many languages, it works, or the developers do the mistake for the language. 348 00:34:10,340 --> 00:34:15,420 And we've also seen that you can definitely exploit some real-world applications with this. 349 00:34:16,760 --> 00:34:18,320 All right. 350 00:34:18,320 --> 00:34:24,960 Now, let's look at what is there more to do in this kind of research for binary protocol smuggling. 351 00:34:25,260 --> 00:34:28,460 But first, I have to give a small disclaimer here. 352 00:34:28,460 --> 00: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. 353 00:34:41,700 --> 00:34:43,540 So, don't do it, please. 354 00:34:44,660 --> 00:34:50,540 But, speaking of this, it would be cool to actually have a non-invasive detection mechanism. 355 00:34:50,540 --> 00:34:52,380 In white-box tests, it's okay. 356 00:34:52,380 --> 00:34:56,640 You can make your local testing environment and just hammer it with gigabytes of data. 357 00:34:56,640 --> 00:34:59,660 But, of course, in black-box tests, that doesn't work. 358 00:34:59,660 --> 00: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. 359 00:35:16,300 --> 00:35:21,820 And if this would be a tool, it would be great to aid you in pen tests and so on. 360 00:35:22,920 --> 00: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. 361 00:35:38,750 --> 00:35:41,780 We can also try to find more desync techniques. 362 00:35:41,780 --> 00:35:45,820 For example, look into protocols that use delimiters and not size fields. 363 00:35:46,260 --> 00: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. 364 00:35:57,320 --> 00:36:03,180 Or maybe we can find generic ways of making a server craft a very large string. 365 00:36:03,600 --> 00:36:05,910 Maybe there's some way to do this. 366 00:36:07,350 --> 00:36:12,750 And, of course, all of the stuff we looked at today had 4-byte length fields. 367 00:36:12,790 --> 00:36:14,670 But what about 2-byte length fields? 368 00:36:14,670 --> 00:36:17,070 This would be much easier to exploit. 369 00:36:17,290 --> 00:36:21,670 Sending 65 kilobytes instead of 4 gigabytes is much easier. 370 00:36:21,670 --> 00:36:25,550 You don't have the hassle with all the limitations. 371 00:36:25,950 --> 00: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. 372 00:36:36,710 --> 00:36:39,450 So, let's wrap things up. 373 00:36:39,890 --> 00:36:41,750 I have a few takeaways. 374 00:36:41,770 --> 00:36:47,610 And the first one is that I think integer overflows are still relevant in memory-safe languages. 375 00:36:47,610 --> 00: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. 376 00:36:55,790 --> 00:37:02,210 But in memory-safe languages, the worst case that can happen is an array out of bounds or whatever. 377 00:37:02,370 --> 00: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. 378 00:37:10,030 --> 00: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. 379 00:37:18,270 --> 00: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. 380 00:37:29,570 --> 00:37:34,150 And finally, to get back to the title, SQL injection isn't dead. 381 00:37:34,210 --> 00:37:38,170 Of course, it was never dead, at least not in the real world. 382 00:37:38,170 --> 00:37:42,710 We've seen a lot of real-world applications with SQL injections. 383 00:37:42,710 --> 00:37:50,250 But on paper, you could say developers have the right tools to write secure SQL-based applications. 384 00:37:50,250 --> 00:37:55,270 They have parameterized queries, they have query builders, they have ORMs. 385 00:37:55,270 --> 00: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. 386 00:38:02,930 --> 00:38:03,670 Thank you.