Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- on December 30th 2024 the US Treasury Department announced that the state sponsored Chinese hackers had apparently breached the department systems the attackers got in through a tool called Beyond trust a remote support software and there was a bug in their database that had been sitting there for years just waiting to be found
- what you're about to see is just how two bytes was enough to expose a fundamental flaw in the postgress database one of the most or if not the most used database out there a flaw that is so simple that it challenged our core assumptions about preventing SQL injections in the first place so without further Ado let's get to it
- a portion of this video was sponsored by sneak more on that at the end of the video
- on December 5th 2024 Beyond trust a company that provided remote support software to the US Treasury detected something unusual in their systems 3 days later on December 8th
- Beyond trust had to make a difficult call to the US Treasury to let them know that their system has been breached when the news got around people started digging a little bit deeper when these security researchers at rapid 7 started analyzing these patches they found something unexpected even with proper escaping in place
- even with following all the security practices there was still a way in
- for decades developers have been following one simple rule to prevent all kinds of escal injection attacks always Escape your inputs every time you take the user's input that needs to go into a database query you escape all the special characters like quotes to make them safe
- consider this simple example of a plugin query here
- I'm trying to query a user in the database with that username and password but if I enter this password instead of I'm Batman our query would look something like this
- now that single quote breaks out of the password check and since one is always equal to one and this query cannot be false you're now logged in as pwn function without using the real password and this is why you can't just copy user input into database queries
- we always need to escape special characters like quotes just to make them safe and in the case of Beyond trust they were in fact removing special characters to make their query more safer but how on Earth did the attackers still manage to get in
- well the answer is that just two bites
- pretty simple isn't it how are these seemingly harmless looking bites responsible for all of this
- well to understand this more we have to dig a Little Deeper deeper into the source code of postgress database now here where things get really interesting
- Beyond trust used a function called PG Escape string so that it can safely Escape any special characters that the user input has and it's quite a standard practice you pass your input through this function and it handles all the escaping for you
- but looking beneath the surface reveals a concerning reality when you call PG Escape string it doesn't just do the escaping itself instead it makes a call to the postgress native function and things go a little bit deeper you can always follow the call stack that I've provided to know where we are in the source code
- okay so now let's look at a core function that determines how many bytes make up a character this is done with the function PG UTF MB Len and every piece of text that needs some sort of escaping pretty much goes through this function and this is how it looks
- like you know how some emojis or characters from other languages make up more space than a regular English letter well that's basically utf8 en coding at work
- well the letter a is just one bite a character like this an emoji might need multiple bytes to represent it and the postgress database needs to handle this correctly when it's actually escaping strings now here's the crucial part
- if the first bite is 0xc0 it's going to think ah this must be a two bite character so I'm just going to set the length to two and it does set the length to two
- now this length variable gets used in the code here
- now look at this line very carefully it's basically copying all the bytes from source to the Target trusting that the link that we got from the PG UTF MB Len function is correct
- it never really checks if these btes are actually valid utf8 characters or not now what if we set the second bite to a single quote 0 x27 now the first bite 0xc0 says hey I'm the start of the two bite character sequence and the postris database will look at it and say okay I'm just going to copy the next bite as it is it copies both 0xc0 and our sneaky little single quote without escaping
- so how did attackers really use this
- book apparently
- Beyond trust's remote support software had a component that accepted a parameter as the user's input and sent it to a PHP script
- now the script was properly trying to escape all the dangerous characters using the PG Escape function but we just learned how to trick that function into spitting out unsafe SQL queries with our two magical bytes
- this unsafe string gets passed into a program called psql which is basically a command line interface for postgress database but the interesting thing here is that it has a feature that could execute system commands so pretty much a game over by December 14th
- Beyond trust had patched their Cloud environments and 2 days later they announced the vulnerability with the patches for all the customers and moreover the true scope of this vulnerability became a lot more clearer in the January of 2025 when rapid 7's analysis revealed the underlying postgress flaw a bug that potentially affected millions of other systems worldwide
- the fix for the Beyond trust products was pretty straightforward
- Beyond trust added a simple check that only allowed letters and numbers as the users input so no special characters or complex bytes none of that and on the postrest side of things they also did release fixes for all the supported versions if you're running post quest ql uh more than 13 version I believe you should be good to go but do check out their website just to be safe
- speaking of fixes when was the last time you checked what kind of vulnerabilities were hiding in your code base
- (ad)
- this US Treasury breach revealed something surprising a fundamental database flaw that challenges our security assumptions quite a bit it's a humbling reminder that in the world of security our strongest assumptions often deserve a second look
Advertisement
Add Comment
Please, Sign In to add comment