So, hello, everybody. Thank you for coming this evening to this talk about connection string attacks. First of all, thank you. I know it's Saturday, it's late, everybody wants to be in another place having beers and so on. And for us, it's very good to have you here. So thank you very much. First of all, let me introduce ourselves. He is Jose Palazón, Palaco. He's working on Yahoo as a software architect. And I'm Chema Alonso. I'm a Microsoft MVP in enterprise security, but I'm not working on Microsoft. I'm working in a security company in Spain called I64. The talk for today is about connection string injection attacks. But before that, I would like to make a quick introduction about our country, about Spain, because maybe you don't know it. I know you don't know it, but you love Spain. Viva España. Viva España. Well, this year we won the World Cup, so we are very proud of this. I'm sorry for the Argentinian people and the rest of the world. I'm sorry for the people from Germany and Holland. Well, also we got very good sports power gas. Very well. Rafa Nadal. Rafa Nadal. And Alberto Contador. We are very proud of that guy. And we got sexy people like Antonio Banderas. Oh, my God. Penélope Cruz. Oh, no. And, of course, Chema Alonso and Jose Palaco. Oh, yeah. That's very nice, guys. That's all. Well, maybe everybody knows where is Spain, but that's this Europe, and Spain is not that country. It's not that country. Spain is that small country in the south of Europe. You know, we got a long, long beaches around the whole country. So it's a very nice place to be. Please come to Spain. We will love you. And, of course, in Spain we got parties for everybody. We got the most famous party. This is San Fermines. It's the greatest party ever. It's very nice. And it's very simple. You only have to run. It's quite simple. And, of course, we got food and flamenco. Everybody knows flamenco, of course. And that's very nice. And we got very good people like Dalí. Painters like Dalí or Picasso. Maybe you know it. Who knows? Picasso and Dalí. Despite all the good things that Spain has done for the world, we have to apologize on behalf of the government. And pretty much 80% of the population for the Macarena. Oh, Macarena. Sorry about that. I like it. I like the Macarena. How many of you have been dancing Macarena sometime in your life? Please, confess it. Macarena. Well, all right. Let's get into business. Okay. So what we're actually going to talk today is connection string attacks. So to define what this is. So basically a connection string is how do you define how your application, like a web application or any other kind of application is going to connect to a data source. Being a data source, either a database, LDAP, some kind of file system base, XML, kind of whichever you want to use. And in particular, what we're going to focus our work here is database connection strings. If you look at that slide, what you have here is the very minimal database connection string that you could put. That you can put together. It's in four lines for the sake of clarity. But you could consider that as like a single string. What you have is the data source. That is the server where your data source is going to be. Your database in this case. You have the schema. Of course, you might have many databases in your server. So you're going to choose which one you want to use. And then you have the credentials. Username and password. As I said, it is like a string. And this is an example of a piece of code where you can see how this string works. How the strings are constructed. Usually when an application connects to a database, there's two ways of doing that. It's either a static string or it's a dynamic string. If this is a static string, then you just put everything together, like data source, something, semicolon, the schema, something, semicolon, username and password. If this is dynamic, if for some reason, and we will see the cases afterwards, you want to use dynamic parameters here, you have to create this string somehow. Unfortunately, most of the times you create this string by simple concatenation. And I think we all know more or less where this is going, right? So that's an example of how you put together a string by concatenation. And you can see kind of pretty much good suspect there to allow us to play with that. There's a character there that we're using to separate the field. And we might be able to abuse that. So that's all. We're finished. That's all. You can go. Well, some of the cases is not needed even to inject to extract data from the connection stream. There are a lot of easy queries that you can throw through Google to discover a lot of information from connection stream. Just looking for loading pages with data source parameter and you can discover, of course, the data source, the username and the password. The password is this. It's quite simple to do this. There are a lot. In this case, this is an example with an IBM. There are a lot of cases. Just playing with Google, you can discover a lot of information from connection stream. And in some other cases, the database administrator creates a special file, which is only a plain text file. But with a special extension, which is UDL. And that file stores the information of the connection stream. So you can just use Google to discover that kind of files. Just searching for the file type UDL password. And there are a lot of cases. In this example, there is a very complex password. You will never figure out what it is. And you can do it. Let's do it. We got time. So just Google. So X UDL password. That's all. Oh. Oh. Oh. It's the other one. Thanks, Google. Yeah. They even suggest how to do it right. Well, as you can see, there are files with information about the database, about the connection stream. Let's download one of these for instance. Let's see. One of my country. Which one? Whatever. This one. The first one. Okay. Let's install the file. Okay. In Windows, it's quite simple because you only have to double-click the file. And then you can see it. And Microsoft has a special tool to connect against the databases. And you've got the user name, the password. And you just test the connection. It's in Spanish because in Spanish it's better. You only have to click this button. And that's all. So it's quite simple. Okay. So back to business. Let's review very briefly how to do it. How a web application connects to a database. Because when it comes to authenticating with the database, there's a big main separation that we want to do here. Usual kind of web application that you probably have built, you take control of your users. So you have to take care inside the database to create the columns and tables for the whole authentication and authentication scheme. So that's how you manage the users in your application. There's other ‑‑ I'll explain that in a bit later. The other main way of authenticating is when you don't want one single user authenticating to the database. And then you use ‑‑ you have your users inside the database. You actually want your users to be the database users, okay? So the ‑‑ in this slide, we have the two ‑‑ the two parts are how the strings are constructed here. And the main difference is if you're going to use the integrated security. Which is no when you want to handle your authentication, or yes, when you want to use the system authentication. Okay? Usually when you have the first one, most cases you have one single user that the application, the entire application uses to connect to the database, and then you do whatever. That is not very right. Because if something goes wrong, then whichever user, no matter how your permission scheme is in your application. If something is wrong, like you have a SQL injection problem, then you can exploit that with the ‑‑ with whichever permissions that user has in the database. And usually ‑‑ well, not usually, always, if you're using just one user, that user needs permission all over the database for writing, reading, and modifying. So if you have the chance, and you have to write one of these applications, ideally what you want to do is have as many roles as possible, not one role per user, but one role per kind of action or functionality. So you want one role to write, and if possible, one role to write here. One role to write in this other place, and then one other role to read. That way, if you have a SQL injection problem in a script that reads something from the database, then you're not going to be able to write or modify the database, right? So you can go back to the other? Okay. The way this works is, like, the way this application works, no matter if you use one single user. If you use several users, is that this is the user that the application is using to authenticate against the database, which is different than my user, when my user name as a user of the application, when I authenticate against the application. So in this case, first the application is going to use those kind of static credentials to authenticate against the database, and then the application is going to ask me, the user, for my credentials, and then using the database connection that has already been established. With that static credentials, then it's going to look in this scheme that I've defined to see if I have permissions to do whatever I've done as a programmer in the application. When the application is authenticated using database users, it goes the other way around. First, the application is going to ask me for my credentials, and it's going to use these credentials to establish the connection with the database, all right? And then all that I can do in the database is whichever my user has permissions to do on the database. That's good in those environments in which you have an internal application with single sign-on credentials, or you are working with internal user and you want to know what is doing every user in your database and so on. In the previous case, it's impossible to trace from the database who did anything, because the trace had been done. It has to be done in the web application. So it's different. In one case, one environment is good for one kind of application, and the other one is good for another kind of application. And of course, that environment is mandatory if you want to manage the database. If you want to manage the database to create tables, to create new databases, to expand table space or whatever, you need to manage the database. So you need a special connection against the database. This means that pretty much every possible control panel that you can find out there is going to use something similar to this to work, because the user that you're using when you're authenticating the application, it is actually a database user. So as we said before, this is put together in a string. And then you're going to do a string concatenation to put all these values together. So the way this works. Is you get one string with this value first, with the value equal, like parameter equal value, semi-colon, parameter equal value, semi-colon. So what we're going to do is we're going to use this semi-colon as a separator. And if this string is going to be put together as a standard CQL string, then we're going to be able to add new information here. So now we're going to see what can we do by adding new information. This was discovered, like the fact that this was not the right way of doing it, this was known something around 2005. And Microsoft was aware of this in 2005. So what they did is they provided the proper way of doing this. And I think it's ASP.net 2.0. So since 2.0, you have this CQL connection string builder, which is an object that the framework provides. And that's how you should do it. You shouldn't do string concatenations. To create your strings. And that's secure. The problem is that nobody was aware about how to exploit this vulnerability. What can we do if we got a connection string injection into a component? What can be done? When we were starting to do research, we were trying to discover all the information published about this. And in OWASP, last September, it was impossible to discover any single reference about connection string attacks or connection string injection and whatever. Nobody was aware about how can we exploit this. So the problem is, is this important? Can be done something important with the semicolon? Is this dangerous? So we did it. So you know that this is not how you should put together a string connection. But you didn't know that it's exploitable. So we've been working on a few ways of exploiting it. So there's two things. There's two main things that we're going to do. We're going to use the semicolon to add new parameters. Or we're going to use the semicolon to add existing parameters. And I'm going to refer now to something that I guess most of you know because it's been there for a couple of years now, which is HTTP parameter pollution. When you have HTTP parameter pollution, the way that works is you're going to have one parameter duplicated. So in your URL, you're going to have twice, two different values for the same parameter. Depending on what system you're using. That's going to behave differently. One platform, one set of technologies is going to take the first one. Another is going to take the second one. The most dangerous one is where sometimes you're going to use the first one and sometimes you're going to use the second one. So you can use that to go over filters and that. So we have something very, very similar here. Actually we have the same here. We have a parameter pollution behavior. And the way it works in this case is whichever happens last, that's what counts. So if in my string I have a value twice, sorry, I have two parameters twice, the second one is what I'm going to use. So if you look at the slide at the top, that would be my string, right? Then if I start reading left to right, I have value A for my first parameter. So that's fine. I have value B for my second parameter. That's fine. But then I repeat my first parameter with value C. Then what's going to happen is that value A is not there anymore. Value C is what counts. And the same for the second parameter. That's quite nice because you can rewrite the whole connection string, which is very good. And this is how it works. Like the previous slide was a generic example. This is how you modify the database connection object through the string. So you first usually have the data source, whichever value. Then you have your user name. Then you have your password. But then as your password, you can do this. You can do something, semicolon, data source equal, something different. And then you're changing the database that the application is going to connect to. So how can you use that? Well, you can use that, for example, to scan servers that you would not be able to scan because there's a firewall. Since the web application server is inside the network and the database server is inside the network, they can connect to each other. But you probably cannot connect to the database. You can't connect to the database from outside or to many other servers from outside because there's a firewall. However, we've just seen that I can override the data source parameter with whichever I want. So if in my string I override that to another internal server, I can just try to be using this application with development data server with maybe data that has not been released so far, important information inside the company like financial information. Or even things that have been there and are not maintained anymore. But there's another good thing that I can do, which is I can change the data source to a server that I control outside. And since most of the times the firewall is going to be allowing outbound connections, then if I change the data source to a server that I control, I'm actually getting the credentials, like the hash of the credentials. So I can use that. When you specify the value for the data source, you specify the server and the port. So same that we've just seen how to scan different servers, you can scan different ports. So the way it works is exactly the same. After whichever field in the data, in the connection stream where you can inject something, you do semicolon. And then overwrite the data source. In this case, try in different ports to do a port scan of a server that was not accessible to you before because of the firewall. That's how you change a parameter, right? We just change the data source to whichever. We've polluted the parameter. But you can also add a new parameter that was not there. The data stream, the connection stream that we've seen at the beginning is like the most simple string that you can have. With those four parameters. But there's more stuff that you can do here. And in this case, you can see that I can use my data source, my username, my password. But then I can inject other information that wasn't there before. Like integrated security equals two. This parameter is the one that defines the behavior between the two ways of authenticating that I've been explaining before. Integrated security equals no means that I handle the authentication by myself inside the database. Integrated security equals two is I'm going to use system and database accounts for this. So let's see this behavior in some demos in real with some commercial products. And first of all, let's analyze what kind of attacks can be done. So the first one, we're going to steal the system account of the web server. The idea is to steal the hash. And it's quite simple. First of all, we need a rogue server in which we are going to run a SQL server. That we are able to manage. Then we are activate one sniffer on it. And then we only have to rewrite the data source parameter into the connection stream. And of course, set the integrated security equals to true. That means that the credentials that are going to be used are the operating system credentials. And in this case, is the web application which is the one which is trying to connect to the database. And the web application is running on the web server. So the operating system account that is going to be used is the operating system account of the web server. Understood? So it's quite simple. Let's suppose that we got this connection stream. So attacker only has to inject something like semicolon data source equals to rogue server password empty semicolon integrated security equals to true. That's an example with ASP net enterprise manager. Which is. This is a product that is on the internet in a lot of places. And the idea is quite simple. In this example, as you can see, we just use semicolon data source to an IP address. And then just integrated security equals to true. It's a very bad product. As you can see, you can read the password even on the screen. But. And of course, you get access. If you have kind of running on your machine, you can read the hash of the system. Which is quite simple. Attack number two. Power scanning attack. In this example, we are going to duplicate the data source parameter. And we are going to use the data source, the target server. The server that we want to scan. So we only have to change the target port and check the error messages. In this example, if we obtain a no TCP connection, that means that the port is closed. If we retrieve no SQL server or no database and discover, then the port is open. And if we retrieve something like invalid password, then the SQL server is there. Which is very good. So in this example, with this connection string against SQL server 2005, the idea is just to inject something like semicolon data source equals to target server target port in the user ID. And in the password. Just something like semicolon integrated security equals to. This is a commercial product. Which is MySQL. My little admin. And in it, you've got a panel to connect to your database. And just trying to discover if Google is answering in the 80 port. You retrieve something like a connection work successfully established with the server and blah, blah, blah, blah. But if you try the 1420 port. You retrieve something like. There's. There's nothing listening there. The third attack is quite simple. The idea is just, okay, the web server has a system account that is in the company. The database is in the company. Why are you asking me a credential? Use your credential and get me in. Which is the idea. So duplicate the data source parameter. Then set integrated security equals to true. And that's all. Because the application is going to take. It's credential to connect again the database. And we are going to have a connection to the application. So the idea is quite simple. Just in this connection string. The only thing that we need to inject is data source equals to target server and integrated security equals to true. Quite simple. In this example, web data administrator. Which is, it was a Microsoft application. In 2004. It releases. A server. It's an open source tool. And right now it's not depending on Microsoft. But the idea is similar. Is the same. We got the user name and password to connect to the database. So data source equals to the database. Password set as integrated security equals to true. And then just we get into the control panel. And of course you get access to all the users and so on. So you can do the same with the rest of the products that we've been seeing before. And in this example, you can see better how it works. This is the connection string information that you can see when you get into a My Little Admin control panel. And in this example, you can see that we got one data source that is pointing to the original database and our data source, which is pointing to the local host server and then the integrated server. Integrated security is equals to no in the original connection string and in our connection string is set to true. So in this example, the last value wins. So it's quite simple. And we can do the same, of course, with the ASP net enterprise manager. So semicolon data source equals to whatever and semicolon integrated security equals to true. And you get into the database. So this we've been talking about how usually .NET applications connect to SQL server databases. But this if you want to extend this to other, MySQL doesn't really extend this. You can so doesn't really it doesn't support integrated security. But if you're using .NET application with a connection string to connect to MySQL database, you still can do all the other stuff that we're doing. Like the post scanning and stuff. With Oracle, it works. It works. All everything that we've said works, like scanning and the whole integrated security. It even works on Unix accounts. And it is there's this thing that you can use in Oracle to kind of use your users, kind of a super user to do every administration thing that you want to do in the database. So that's something that you can even do if your database here is Oracle. You will just. Append to your connection string this thing. And then your user has full control there. So let's see this in action. We got a testing environment in this example. It's a window 2003 and meet the password and let's open Internet Explorer and let's connect to different parameters, connection string. So let's start with the HP net. This is the connection string. This is the control panel for Microsoft SQL server. There are a lot of control panel like this on the Internet. Any of you are using this software to manage a database? Well, the idea is quite simple. We need to use only one tool. It's the most powerful tool ever created. Integrated, which is the notepad. And just integrated security equals true. That's all. Then right button, copy, it's in Spanish, but you know, in Spanish. So the user is test. The user is test and we are going to try to duplicate the parameter pollution to local host. In this example it's the same because you can set up in this field whatever servers, but to do this we are going to connect to the white house. It's not going to connect to the white house because the last parameter wins, you know. And then text integrated security equals to true and connect and that's all. And you get into the control panel. It's so easy. You can do the same on Internet. It's so easy. Just doing this. But it's illegal, don't do that. So web data administrator, the same idea. Username palako. Thank you. I love you. Palako. Palako and data source equals to local host. Index example is a SQL Server express edition. So we need to use ‑‑ we need to use ‑‑ to use the ‑‑ it's not here. Integrated security equals to true. Just space and get into ‑‑ okay. And you get into the database and manage whatever. The last one, the commercial product, my little admin, that's a SQL ‑‑ that's control using SQL Server express. We need to connect against a SQL Server express. It's simple. is more or less the same, but with local host slash SQL server express. That's it. And right button, paste, connect, and you get access to the database. In the example, we can analyze the connection stream. So connection. And as you can see here, we have a connection stream. We got in the first place the original data source parameter, which is pointing to blah, blah, blah. And then we got our data source, that in this case is on the same machine, but you can use another machine. And the original integrated security is set to no, but our integrated security is set to true. Then the last wins. And we get access to the control panel. And you can do the same with an Oracle application in this example. It's just a testing application because it's not that common to find on the Internet control panels to manage Oracle databases with integrated security enabled. So in this example, this is just a testing application that we developed doing the same. So user, Oracle, integrated, data source equals to local host, right button, integrated security equals to true, and you get access to the internal application. Is that all? So the good idea with this is that if something, someone is working on your company, like a vendor or a partner or whatever, or an internal user, he can try to do the same with the integrated security parameter. The idea is that he is able to publish an ASP or JSP application on the web server. He's going to be able to try to connect against any database on your company, which is very bad. In a hosting environment in which somebody is able to put a file into your server or Or maybe he's using FOCA and discovered a put option, put method enabled on your website. He can upload a scanner to discover the whole network trying the integrated security connection against the database. It's so simple. So we developed a scanner, which is CHPP scanner, which is available for download in that URL. And you can try it in your company. So the idea is quite simple. You put the ASP file on your website and just click the button, and then the application is going to scan the whole network with internal IP address trying to connection and string with the integrated security equals to true. And if the application discovered SQL Server, then try to give you a SQL query environment in which you can throw your SQL queries against the internal database. So it's quite simple. Let's see. see this in our demo, in our testing environment. So it looks like this. And the idea is that you only have to put this file into R, into the website, then the application is going to check how many network interface you have on the web server and just click it on the network interface. You can scan the whole network. Scan with 100 threads. Scan SQL servers. And now it is going to try to discover the SQL servers. In this case, there is only one SQL server, which is the one I'm using for the demos. And once it discovers it, I only have to click on it and get connection. So, finishing. All this product, all this commercial software was advised before we released the talk. And they released an advisory in September. People of my little admin and my little back up released this security advisory about this vulnerability. And it was a very successful service that was in use. So let's talk about that. How much the service was designed with? It says there is a new version, that fixes one security vulnerability. The good idea is, or the funny thing about this software is, there at the beginning, when we got into contact with the main developer, he said, that wasn't a vulnerability. That wasn't a feature. So, yeah. So it released, first, a small patch with minor security, minor security update, vulnerability because it's minor that anyone can get into that security. That's the your database. It's a minor security. It's not that important. Your application is running. So that's important. And that guy was very, very nervous at the beginning when we got in contact with him because he was worried about the business. So he counterattacked with a marketing campaign saying, okay, there's a minor security vulnerability or a minor security bug, but a lot of companies are using our products. Like the US government, go daddy. US Army. US Army again. Oh, my God. That's quite nice. Because I got the cam, but now I got the target. So it's quite nice. I love it. That's for you. This application is not maintained anymore. If you are so unlucky to be using this, you're going to have to patch it yourself. So it's basically you just have to go over the code. It's actually not very well written. So there's no one single place where you go and change it. There's connections and strings all over the place. So you have to go over the code and change these connection and strings for the proper way of doing it with the objects in the framework that we were mentioning before. And the last one is the HTTP 4.0 application. If you're running an originally it was a Microsoft application that in 2004 released as an open source project and since that moment Microsoft didn't maintain the software. The problem is that Microsoft was publishing the old version on the Internet and there is a small problem with the search engine optimization that Microsoft has more page rank than open source project. So everybody was trying to download the ASP net web data administrator, the first link that appears on the web search web engine was the Microsoft link. So it was a problem because there was no difference between the original which was unsecure and the open source project which is secure. So we got a problem. We got in contact with Microsoft and Microsoft in the end take it off from the Internet. So right now it's impossible to download the unsecure version which is good. How do you fix this? So first of all, we've seen that you can steal credentials by allowing outbound connections to your server. So set up your firewalls correctly and don't allow connections, outbound connections that you shouldn't. You've also seen that by using integrated security equals true, you can use system accounts to try different servers inside the network. Usually you don't need that many accounts in all your servers. So if you could take care of individual servers, what accounts do I need here and there, it's going to harden a lot of your organization. Because even with integrated security equals true, if there's no credentials in the other servers, then there's nothing you can do. So we're going to have to fix this. Of course, don't use string concatenations. Use the connection string builder object that is provided to you by the framework. And if you don't have any other option, filter the semicolons. And that's all. Questions at room 113. Thanks for all. Thank you.