Okay, I'm Jeremiah Grossman. This is Lex Arquette. We're going to be doing a talk on web application security. Thank you. This talk is going to be different from the one we did last year. There's going to be a lot more exploit theory and a lot more exploits and how to beat different CGI filters. Things that every CGI application is going to be exposed to, if not now, later. So, let's just start. A lot to cover. What is web application security? Simple enough, it's the securing of web applications. It's pretty simple. Web applications, they're used everywhere, you know, they transfer money within bank accounts, you know, message boards, web mail, I mean, you name it, there's a web application for it. And the security that we're going to be showing applies to them all because they're all taking user input and sit between a web server with a web interface. Anything that does that, anything that processes via a web server is a CGI and this is what we're going to be covering. The difference between software programming, at least from a security standpoint, is different from the way that before when you wrote an application, you know, on a LAN or, you know, maybe like ten years ago, the application that you write, the user base is relatively small. So, you didn't see as much abuse of the applications that you do now. Now, when a web developer or any kind of software engineer writes a program that's shipped via the web, everybody has access to it and there's going to be some people, a thousand people, they're going to have an infinite amount of time to mess it up, you know, throw whatever they can at it to make it screw up or do whatever they feel. So, some of the attacks when we're talking about web application security is browser hijacking, which means is an attacker threw a web page on the client side. It's important to realize what client side is when we talk about it. Client side is like when you're viewing a web page, somebody injects JavaScript via like a message board or something and takes control over your browser. Client side through like document dot location or closes your window and a million different abuses like that. There's also cookie theft and we'll get into cookie theft in a minute. There's server and client side compromise, shipping weird characters and input validation manipulation to get the server to give up more than it should or more access than it should. There's also denial of service where creativity within a web application exploit, you can actually render a site useless by no one is allowed to get there. Every time they go there, they're pushed away. There's also different abuse mechanisms like cheating on games or cheating on auctions or buying a laptop for $20 where you should have paid $2,000. And there's also user privacy invasion. The client side scripting applications give you tremendous amount of power and information about the user of which they execute. You'll get IP addresses, host names, subnets, user names, email addresses, system configurations, you name it. There's all kinds of data you can access with a client side script. So, let's get right to it. How to steal cookies because it seems like many people aren't too sure how it's done. Cookies are, the cookie security is, you know, according to the RFCs and different documentation you might read, they're restricted to domains. So when a cookie is set within a client, there is a domain that is attached to the cookie which the client should only send the cookie back to cookies within that domain. Cookies bleeding over into another domain is considered not good and that's a cookie theft and can lead to all types of user compromise. Basically how a cookie is stolen is by somebody injecting JavaScript or some kind of cookie thieving tool onto the domain that steals a cookie. So basically if you're a web administrator, you do not want to have uncontrolled web data on your domain or at least on a protected domain. So, I just use JavaScript, you know, you can do the same thing with VB scripts and a lot of other, you know, web application scripting languages. But we have the expression document.cookie. Basically that's just a string of the cookie data of the domain of which the script is executed on. Now what you can do here is with these following functions like window.open, document.image.source, you set window.open equals to attacker.com.cgi.bin pointed at your CGI and append the cookie data to it. What happens is the cookie data actually gets passed over a get connection or an HTTP post or get or whatever and you can actually pick it up off domain. So you're basically just moving strings around and using the power of HTML and JavaScript to move the cookie off the domain. So there's lots of ways to do it. You know, I've written utilities on it, but I've been hesitant to do it recently because I don't feel like a bunch of script kiddies need to be running around stealing everybody else's cookies. So, what are secure web programming practices? You'll hear this a lot. Do not trust client-side data. You'll understand more why later, but as a whole, you cannot trust anything coming from a client. You have to validate it, you have to sanity check it. You do not trust it. There's also hidden form elements are not hidden. If you put a price inside of like an online order form, they're not hidden. These are just common things that we see around. Password elements, still transferring clear text, just because you can't see it doesn't mean it's encrypted or anything. Use solid crypto when you're doing anything online. Don't use DoubleRot13, no matter how secure your friend thinks it is. Of course, stick to DES, triple DES, Blowfish, there's lots of good ones out there, but stick to the ones you know that have been proven. Avoid authentication mechanisms using JavaScript. Basically everything happens client-side using authentication via JavaScript, all the passwords or at least all the authentication credentials are going to be at your fingertips. Re-authenticate the user with a password before issuing new passwords, you know, just goes right towards like the Unix mindset, you know, they have to type in their own password before you issue new ones. You'd be surprised or maybe not so surprised how many sites allow you to change password just by manipulating some of the input fields to change a password once you have a stolen cookie. Let's see. Do not host uncontrolled data on a protected domain. I realize for some people doing this is hard, but as a general rule, try not to do this. Sanity check and qualify all incoming data and another great resource is the World Wide Web Security FAQ, if you haven't read it already. By the way, all this material will be available on whitehatsec.com probably Monday when we get home. These are the basic client-side scripting languages. There are a lot of them. This is not a finite list. There are more. We have DHTML, JavaScript, Java, Applets, VBScript, Flash, ActiveX, XML, and XSL. Getting to XML, most people are using on the backside right now. However, browsers are supporting it on the client side and when you get to some of the security issues about XML and XSL and Microsoft.net at the end. And there's also CSS. We have the document object model. Basically, this is like the object-oriented model for HTML and JavaScript to access different functions and features and information within the browser. Basically, with all these client-side scripting languages, all their complexities, all the different things they can do can be combined to gain increased access to the DOM, which they really shouldn't. Basically, you can get background colors, you can get files, and you can even execute system calls. Okay, if you're running a web application, or at least with some kind of complexity, you're going to be taking input from the user. So again, never trust client-side data. Escape, validate, parse, filter, and sanity check all data. All of it. Not just like the ones where they're typing in like a search field. Get the hidden ones. Everything coming over has to be checked. With client-side data, you can never be too paranoid. Let your paranoia be your guide. Let's see, common input. Mistakes. Sanity check all input. When I say sanity checking, a lot of CGI's that we find, like, for instance, the yes or no, we find that a lot of times, you know, the pull-down menu says yes or no, but we can actually add like a nothing or some other string in there to cause panic within or an error within the web application. So for instance, if you're using a numeric value or a yes or no, check the input for what you're expecting and ditch the rest. Escape it. Don't be just taking in whatever you want or whatever they send to you. Same goes for file names, paths. Don't parse and especially don't use what you don't know. Escape all input special characters. So it's important to understand that special characters have a lot of meanings on a lot of different operating systems where it's piping, redirecting, semi-colons, stars, file-globbing. Special characters can do a lot to your system so they all have to be escaped or replaced or something has to happen to them. Preferably, if you need to use special characters, escape them. If you don't, remove them. And null characters is very important. Null characters don't belong in, should not come over the wire unless there are some really weird circumstances, but they all should be ripped out completely. If you have need to allow HTML into your site, however it's not supposed to execute, use these little guidelines so it doesn't. Basically, you just want to convert the less than and greater than signs and the quotes into HTML entities. It's real simple, very important though. Everything from file not founds to search fields to search errors. A few other things that you got to watch out for, you know, the directory transversals, the file-globbing, appending commands. We find a lot of times registering for, for instance, like a mailing list or something, you type in your email address and it auto-responds, it'll just do a quick send mail, send mail to this email address. Well, pipe a semi-colon on and execute another command, cat direct etc password and you got it. Data piping, quotes are, quotes can do a lot of damage, especially when you're using fixed strings like in a sequel and things like that. So if you're not wanting these, you know, strip them out or escape them. Output filtering a lot of times is ignored. Output filter queries from a database, for instance, somebody might be updating their profile information and put a lot of nice, a lot of little cool HTML and stuff and they go, hey, see my profile. So, does a query, ships it back to the person that saw and there's XML or HTML executing on the client side. So you have to output filter as well as input filter. And if you want to take a quick note, here's some other good CGI examples that give a lot of, a lot of really good data. HTML is dangerous. You know, it's, you know, it seems light, loose, everybody uses it, but it is dangerous, especially for web applications or client side security. If your system, if it's web mail, message boards or whatever, if it's using HTML input from users, it's very hard to get it secure. Even when the proper precautions are taken, it's very hard to lock it down. So the best way I could suggest to lock it down is use allow permit lists. Know what HTML you want your users to be able to send in and only allow those. This goes for attributes too. Also, let's see. Yeah, limit your lists, keep them short, know what they are, and know what attributes and tags are harmful so you don't put them on your allow list. We'll get to the list in a second. Well, there it is. So these are the common tags that are known to be dangerous for various reasons. Applet, you can input Java and gain a lot of access. Image, inline images can grab cookies. Layers, you can source in other data. You know, object, ActiveX controls. Style and script are pretty self-explanatory. But these are basically the tags that are known to be dangerous that every system allowing HTML has danger inherent when they are allowing these tags. Any tags with attributes that have style, source, href, or type are also at risk. So you have to watch out for these attributes in any tag that allows them. Now we're going to get a bit into user authentication. Web applications a lot of times have need to authenticate the user, whether it's username and password or persistent cookies and that nature. So we're going to get... go into a lot of things in that nature that we see out there. And some guidelines to help out people so they know what to do and what not to do. Passwords, as a general rule, are your user's weakest link. Users are always picking easy passwords. Users are always giving up their passwords. So, never store passwords in plain text. Age all passwords. It's very important that passwords, at least at a given amount of time, they die or they invalidate themselves. Age all passwords. Otherwise, make sure good system maintenance. So you don't have a password that's on the system for about, you know, six years and you forgot about and somebody can still log in or steal it. Age them and deny them. Password restrictions, we'll get to that next. General guidelines. Passwords should be, at least in my opinion, should be six letters in length. Six letters in length. Not matching the username. And not a common password and contains one capital letter. It's a very simple password, but however, it gets a decent amount of security. Now, a lot of usability experts and security people have different mindsets on it as far as usability versus security. This should, for the most part, secure your passwords to at least a minimum level. You know, it's better than password being password. If they have to have password as their password, make it a capital P with a 1 at the end. Passwords should be six letters in length. For an increased security, passwords should be six letters in length, not matching the username or part of it. Not a common password and must contain one capital and one special character. This is pretty good. This would be pretty hard to crack, at least on a brute force level on a web basis. Again, let your paranoia be your guide. What not to do. Now, we see this out there a lot. Placing a maximum password length. Okay, don't do that. I've seen places where they're allowing like, you know, no passwords over 4 or 5 characters. Now, how long does that take to crack? Passwords that are allowed to be changed into the old ones. So, let's say you're doing aging. Don't let your users change their password into the current password. Check for that. A lot of sites we've seen, well, maybe not a lot, but it does happen, echo the new password that they typed in over a non-SSL link. No need for sniffer bait. Password restrictions are too high. This is kind of like a sticky point. You can make your password restrictions pretty high for, you know, most competent users. But if you make them like too high, like, you know, really, really high. Both. These are like general guides. You can actually adapt these to a lot of secure programming. Making password restrictions too high. What happens if you make a password too high? You know, they're typing upper and lower case and numbers and symbols and stuff. Users are, at least on like a web sense when you're dealing with like a, you know, 50 million users or 30 million users or what have you. They're going to write down their password and stick it on a sticky note and put it on their monitor. So, you know, so you have to walk that line. You want to make it secure, yes, but not too secure where they're tempted to do stupid things. Okay, brute force attacks. We're going to go real quick over this because this is Uber Hacker. You guys should be all aware of this stuff. Brute force in a web account, web accounts, there's two main attacks, at least from like a web sense, for instance, like, you know, banking and stuff like that. One user name against many passwords or less common but very effective, one password against many usernames. So some systems, if they don't have password restrictions, pick the password password and level it against 1,000 user accounts and you're probably going to crack a couple. Each attack can be very effective and both must be defended against. We're going to state some defense mechanisms against it. So how do you defend your web apps against brute force? Set an acceptable threshold of the amount of failed attempts a single account can receive before that offender is blocked at an IP level, because you don't want to keep doing it, and the account itself is locked. Same thing goes for multiple accounts from the same IP. Okay, when an IP set, you know, goes over like 10 failed attempts, you have to block them. Okay, so for instance, like on the reverse brute force, a single account isn't going to get like 10 attempts. 10 attempts, however, you still want to block the IP. Now a lot of people are thinking like, you know, wait a minute, there's like DOS attacks regarding this, right? Like, for instance, if, you know, somebody's, two people are competing at an auction, you know, and I want to win, so I'm going to lock my opponent's account so he can't make a bid. So where to get to the point? Did you guys get that? I'll say that one more time. So let's say I'm, you know, I like this Dell laptop and I want to get another, so I log on to my favorite auction, and there's a bid war, you know, going up and up and up and up, and I'm sick of having to, you know, bid-bask my opponent. So what I do is, I sent, I knowingly send 10 failed attempts against his account, and now he can't bid, and I win. So, however, like, one second, yes please. He asked about proxied users, we'll get to that in a second. Okay. So, now, there's DOS attacks against these systems, as I just said. Basically if an attacker wanted to DOS an account, he could, because he doesn't really care about cracking the account, he just wants to block the user, many abuses in this regard. Now, also as a result from IP blocking, AOL and proxied users are blocked also, because they're amassed behind like a single subnet. Know your users. If you're in a restricted environment, you can IP block without restriction. However, if you're, like, servicing the entire web, you're going to have to make allowances for proxy users and AOL users, so, you know, basically, you can't block AOL. So, basically, you just have to set the rules, then, that you can't block AOL. Bummer! Possible solutions. When blocking an account, log the offending IP with the account block. Now, what happens is, if the legitimate user logs on to the account, and the system is going, okay, this account's blocked, but I want to take, I want to do a comparison against the new requesting IP and the one that was the original offending one. If they don't match, and if they get the password right, let's say in, like, three attempts, you can allow them. However, if they get it wrong over three attempts, then you can block them, too. So, basically, these are some of the solutions against the DOS attacks, against these types of anti-brute force mechanisms. Okay, we're going to get to cookie authentication. Cookies, everyone knows, this is UberEcker again, that cookies are used a lot of times for authentication, you know, maybe not htaccess or something like that, but cookies are used for authentication. Now, we're not going to get into all the different kind of ways people are doing the authentication, but we're going to show you some of the basic steps on how to increase the security of your cookie authentication that you're already using. Use SSL for, these are very simple, use SSL for username and passwords, again, no need for sniffer bait. Don't store plain text passwords or weakly encrypted passwords in a cookie. Again, don't use ROT13 for passwords in cookies, that's very bad. Cookies are going to get stolen. Now, there are just far too many ways this is going to happen, so you have to remember this, cookies are going to get stolen, okay? You know, there's plenty of IE bugs, there's plenty of cross-scripting exploits, cookies are going to get stolen. It's too bad, it's sad, but it's going to happen. Now, if a cookie is compromised, two things should not happen. The cookie should not be able to be reused or reused easily by another person on another computer. The password should not, information from the password should not be extracted that's confidential, okay? That's two very important things. If a cookie is to be compromised, those two things should not happen. Also, cookie timeout. Cookies should not be valid for an extended period of time. You know, time them out after every 24 hours, no sense putting a validated cookie out there that's infinitely usable. Now, how do you increase cookie security? Tie cookie authentication credentials to an IP address. However, again, this is going to, you know, this is going to be tricky for a lot of the AOL users and a lot of the SSL users. However, if you're on a small business internet, maybe, you know, a few hundred people, you can tie the cookie to an IP address or the authentication credential to an IP address without worry. You have to know your users. If you're servicing the entire web, use a portion of the IP address, it's better than nothing, okay? Now, this is an experimental security practice we've been working on, we've seen other people try to work on it. They've had mixed results, so your mileage may vary. You can actually add salt to your cookie authentication credentials by hashing in, like, an IE or a Netscape user agent. It gives you a lot of salt. It gives you, like, system type, it gives you browser version, things of that nature. So if you're tying it to an IP address, the user agent, a stolen cookie moved is going to have a lot harder time being used by another person. It's the basic trick. It's not limited to user agent and accept language. Any header that stays constant within a browser can be used. So, again, you have to know your users, you know, sniff the wire, look at your logs, and see what's going on. Further authentication credentials. Yes. Oh, sure. Yes, it is. He said, basically, user agent and accept language is spoofable. Yes, it is. However, if the user, let's say a legitimate user is logged in, okay, the user agent and the accept language came over from the user, it's hashed up, MD5, SHA1, whatever you want, with their password, wherever you want to do it. If somebody were to compromise that cookie, they have no idea what that other user had or submitted to begin with and they're not going to be able to reverse it if you use strong crypto mechanisms. The question was, if he logs in from home and you log in from work, there might be conflict? Sure. Oh, you're going to have to have logout mechanisms for that. If you stay logged in, you're going to have trouble. You should provide logout mechanisms. Like I said, the session should not last forever. There should be timeout. He asked about, you know, what if he upgrades his browser? Well, the session cookie should not last forever. Right, so, I'm sorry, I missed that one. Yes, please. Right, basically he said, if they can get the cookie, why can't they get the user agent? Basically because, you can in JavaScript, but it's not foolproof. You know, let me just state that. This is not foolproof. It's just increased. The level of skill of the attacker is going to be a lot greater than your average person. Yes, please. Could you stand up, please? I'm like a MAC address. All right, you could do that, but then, you know, you have key sharing. He basically said, yeah, you can solve it on the server side, but again, we're not going to get to that level yet. But again, this is not foolproof, but however, it is a lot better than a lot of things where you can move a cookie from one computer over to another and clone that user. I mean, imagine you can pop into any, you know, financial transaction, steal a cookie, and then you're them. So basically, if you're just hashing in a little bit more data, it's going to be that much harder for a person doing it. This is a new paper that came up from some people at MIT. It gives a lot of... it's one main reason why we didn't go over different cookie authentication mechanisms. They did an excellent job and pointed out some very good mechanisms that a lot of major sites are using. So if you have need, go check it out. Session tickets and passwording. A lot of times, when you're getting data from a web form or something from a user, you want to make sure it's not tampered with. You know, you can use SSL for that and has not been sent fraudulently on the behalf of a user. For instance, like a CGI action to delete a database or something. You want to make sure that the user sending that actually did that purposely. So how do you do that? Well, if the CGI is performing a particular critical action, have them reconfirm their password. It's a real simple step. It buys you a lot of security. Have a yes or no button. Very simple again. Yes, I did want to delete my entire domain off this web server. Instead of, like, just basically sending a get request, they have to say yes or no. Very simple stuff. Entirely underused out there. I can't stress the importance enough of this. Basically, if you know a user is logged into, like, you know, any kind of service, any kind of message board, you send it to a chat room or a message board administrator, you send them a personal message or some kind of message and they read it, guess what? You can perform any action as them, at least for the moment. So basically, if it's a critical action, say yes or no or a new password. Referrer checking. This is something I don't recommend. This is entirely spoofable. However, if you know your users, this can be done. Like, for instance, on a business internet, we're just going to kind of gloss over this. But as far as referrers are concerned, don't trust client-side data. Okay, that's very important. Get versus post. A lot of times the same results can be had from a CGI by using gets and posts. If a form is, if the legitimate form is supposed to be being sent as a post request, accept only post requests. This stops a lot of automated scripting because basically because if you send like a cross-scripting exploit to someone, it's, and it sends like a get request, it's going to, it sends a get request and not a post. So that way it buys you a little bit more security by checking the request method. Off-domain hosting of user data. If you can't control or if you don't want to control user data, host it on a different domain. Basically if you're using, if you're hosting on like acme.com, host user data on acme.net. It might be a little painful, however, it does buy you a lot of security. Now, the parts that I find most interesting and most fun to screw around with. Filter bypassing. JavaScript is a cockroach. It's hard to stamp out. There are all kinds of, you know, input filters in web applications that try to prevent HTML and JavaScript from entering into a system. So this next section is basically how do you audit, how do you beat HTML filters. A lot of data inside. Again, you don't have to scribble it all down. It'll be available later. So basically what these exploits or methods show is how to do, you know, cross-scripting exploits, browser hijacking. We've already kind of glossed over how you can do some of those attacks. These are how to execute them. Testing the filters, first of all, we're going to start real simple and work our way up pretty fast. If you're auditing your own application, send as much HTML to it as you can, every single tag in the arsenal, with every attribute you can find, and view the output and see what changed. Okay, it goes a long way towards auditing. The script tag, you know, still very simple. Script tag, you can enter any JavaScript expression, including document.cookie. This is important here. Anytime you can get like an alert to show up in an environment, basically that's cause for alarm. Sourcing JavaScript protocol. Basically, in Netscape, I don't think it works in IE, I have to double check, the JavaScript protocol, and after the colon, you can execute any expression that you want. The image that's source equals. Any tag that you can source has that problem. So you're going to have to substitute the JavaScript string inside a source attribute. You know, just change it a little bit. You can use the underscore, or you can erase a letter, or put a hyphen in it, whatever you want, but you have to do something with it. So, again, any tag that has a source attribute, you're going to have to put your pattern matching in and search for all the source attributes for this type of thing. Even worse, LiveScript and Mocha achieved the same results in Netscape, old Netscape codename throwback that still works to execute JavaScript. What you're going to find in this, HTML is so loose that you can actually just totally manipulate and warp everything, and things still execute, which is one of the good things about XML that it'll help out with. Another one, the line breaks. Same thing, JavaScript and alert. Line break it, and it'll probably beat the pattern match. Basically, if you visualize the regular expression, it's going to be trying to match JavaScript as a string inside a source. However, if there's a carriage return in it, it's not going to match, and it's just going to blow up, and that will still execute. Oh, tabs, new lines, carriage returns in all whitespace count for this type of thing. HTML entities, kind of a derivative of the previous one, using HTML entities to put whitespace into the strings. This works, as well as the decimal, the hex, and multiple zeros in front will all execute JavaScript expressions inside any source attribute. I call this and curly. I don't know where this came from. I kind of saw it online. I was kind of screwing around with it, but the syntax is very...