Web Application Security Basics
Transcript
[Music] Hello everybody! Welcome to Embrace the Red. This session is about web application security fundamentals. It's going to be the first session about web applications in general and security vulnerabilities in web applications. This shorter session is just going to introduce you to the basic concepts of web applications so you know and understand the vulnerabilities that we are going to discuss uh in the next couple of videos. Let's dive right into it.
So web applications consist of a set of kind of fundamental building blocks in particular it is the hypertext markup language called HTML then we have style sheets (CSS) that sort of define how the the elements are rendered that are part of the HTML document like what colors they have how big they are what kind of fonts they use and then we have the code which is most commonly JavaScript that sort of allows to add a lot of dynamic behavior to the web application.
So, when we talk about web applications we have to talk about the User Agent and what the User Agent is for us use agents like initially just think about the user agent being the browser like on all the RFC documents the request for comments like the the specifications of the World Wide Web. They always refer to the browser as a user user agent and basically a user agent is any application that sort of acts on the user's behalf and the most common ones we use are browsers right uh and you might be familiar with Firefox, Chrome right those are user agents, and they speak the HTTP protocol. HTTP stands for hypertext transfer protocol and that is sort of the language and how the communication happens in the worldwide web.
Think about this you have your browser open right you do a Google search and then you click a link on in the browser and that link now this click creates an http request that takes you to this other website that you clicked on right this is sort of this idea of hypertext transfer protocol and hyperlinking and when this happens right the browser actually issues a http request a get request and that looks like the following.
Right, it has this GET which is called the verb then it has the destination link or URL, the protocol that it use it is using and then a set of http request headers that sort of send additional information to the web server that it is communicating with you can see here for instance right it would send the user agent which we just described that is the browser in this case it's like Mozilla Firefox right it sends the version number and then it said sends other important or in interesting information as well for instance a referrer header which tells the web server that you're visiting where you actually came from like which site or which origin you came from in the in the web so that's very
interesting information that the web server can gather about you just clicking a link so what has sort of been happening on the other side right the web server now gets this request and the web server responds to it and the way a response looks like is the following so it is again text based it sends back the version number and we call this on the top here the response headers in the very beginning you also have the status code that you can see here 200 okay. That means that everything went well on the web server side and it's going to send you now the response it contains a link what kind of server version the web server is running it might then contain additional response headers about setting a cookie which we'll talk a lot in more detail because that's very security sensitive and it's also important to understand the content
type http response header so the content type talks about how the browser or the user agent should interpret the result in the body of this message that we'll look at soon in this case and it's very common that this is text html so it's hypertext markup language but this could also be just Javascript or this could be you know a binary blob or an image for instance so in our case we have an html document and this is sort of how response looks like we have
html elements in the document i'm gonna describe this a little bit better in in the next slide as well but sort of this is how an html document looks like we have html elements which is sort of the tags we have attributes which sort of further refine what an element looks like or what an element what kind of attributes an element has like you know it might have an id a name a style speaking of styles then you can specify the style and how this element the html element is actually rendered right what color it has
what transition behavior you might want to define and then very important also is the code so html documents can contain code and this is sort of was from the security point it becomes very very interesting so uh this is what we're gonna i'm gonna have another slide that talks much more about this code uh concept and also we're gonna have dedicated sessions for instance about very common vulnerabilities that happen when you have something like code injection
But, yeah let's look a little bit more now into this very basics. So you have the hypertext markup language um these are html text or html elements right then you have attributes which sort of further refine the text we set these elements and then you kind of might have string literals which is sort of in this case hello world so this will print out kind of a large header that just says hello world so that's sort of the overall idea of the markup language and then we have style sheets which are basically describing how these elements should be rendered in this case it would say hey the body element the body html element should be rendered right with a blue background and the fonts should all be white by default everywhere on the page so this is sort of the kind of behavior you can drive with style sheets
And the third concept again the javascript execution on the client site right it starts with the script html element tag and then it actually is just arbitrary code that is then being run by the browser and it can interact with the web page and the way this interaction happens is by uh the document object model so the DOM is basically this programming interface that javascript has to interact with the html document and then javascript can read from the
document it can update elements in the document and so on so this is sort of this idea of this document object model and what is so important to understand is that code can basically do everything that the user in front of the computer can do with this web application and this is sort of important to grasp so the code can modify read any data from this particular web application and you might have noticed that i said from this particular web application because to understand the security of the internet there's actually not a whole lot of security concepts which is very interesting uh when it comes to uh the web the one thing that sort of everything hangs upon is sort of the same rich policy so the same origin policy basically tells or is defined
in a way that if you load content or javascript in particular from one specific origin then that code or content can only interact with that same origin so in maybe what's sort of a little bit confusing right now is what is actually an origin so let's let's look at that first before we go into like very detailed examples so this is an origin it's consists of three parts which is the protocol in the very beginning or the scheme it's called scheme in this case https then the host name which in this case embrace the red and then the port so those those three pieces together make up the origin and now let's look at an example so these
are all different origins we have example.org and example.org from port 8080 and then one that is http example.org so all of those three are different origins i did like strike out the later part of the url because that is not relevant when it comes to looking at the origin so but what the same origin policy now says is that if javascript is loaded from example.org
it cannot access html from example.org on port 8080. So this is not possible so uh yeah this like the question is why is this important right and sort of i already kind of said that because in the worst case scenario uh consider this a tax scenario you have a website called attacker.embracethered.com and that website you visit that website as a user for some reason you might have clicked the link or browse facebook and somehow you end up on this website and that website loads
gmail into an iframe or which is an html tag that sort of allows you to embed another web page and this would now mean that this attacker page can run code to access your inbox technically right and this is exactly what the same original policy prevents let's look at an example you know very kind of visualizing this concept so this is the attacker home page you as a user for some reason ended up on this page so and since its attackers page is you know the attacker controls the code or the html document on it and what we the attacker did in this case they created an iframe which points to gmail mail.google.com and loads your inbox or the inbox
that you yeah your inbox into that iframe and closes this so now the attacker could try to access with javascript right they could not try to access this iframe and the content within that iframe and this is what the same origin policy prevents so if you try this example and then you will see that javascript will give you this error where the browser the use agent gives you this error it's an uncut dom exception and you get an access denied permission denied to access the body of this cross-origin object so this is the same origin policy in action
and to look on how this actually does look in code or in html end code is you can see here the html document this is the attacker's website it has an iframe with an identifier just called mail and then it loads the source of you know a web application like uh email program in this case i used outlook in the previous slide sorry i had uh gmail and so that loads this document and then we have the script tag defer just means don't run the script until the entire page is loaded so and this script particularly creates a reference right it does a document get element by id mail which is a reference to the iframe remember the id of the iframe is male and then we also get in reference to some place we can write the output to which is this diff
and the third line in the javascript is uh basically trying to access the mail content like the iframe and the emails in that iframe and what you can see like this line here is sort of this line that tries to do the malicious thing and when that actually happens this is exactly when that permission denied occurs in the browser so this is the same origin policy in action if you actually go ahead and try this with for instance with gmail it will actually not you will even you won't even get as far the reason for that is that there is an additional mitigation that was implemented by the web server which uses what's called x frame options which is sort of a mitigation to prevent framing or embedding another one application in another so if you have have an application and you do not want your own application to be framed
or embedded by somebody else by an attacker you can set this http response header x frame options and then the browser will not allow a ui application to be iframed in this case you see firefox and you see the error message the security message that says you know this page cannot be put in an iframe this actually has been uh implemented to mitigate an attack called click-jacking. I'm sorry i just saw it as a slight misspelling uh yeah so this was mitigated to implement click-jacking which is sort of a user tricking a user to click something that is not really visible at the moment by layering multiple iframes on top of each other so you click on one but in reality you click on something that is behind it let's say you know amazon one-click shopping you might overlay multiple iframes and then you
click some one button in reality you click the checkout button so this is click checking and x-frame options sort of denies or prevents this by not allowing an attacker to frame or embed another the other application if you look in more detail you can actually also inspect this and i'm going to show you later how you can do this but right clicking and looking at the network traffic you can see that the response here that comes back from google saying extreme options same origin and that's sort of how they implemented gmail to prevent being i-framed
the next thing i want to briefly talk about because this is becoming more important this didn't exist a couple of years ago this but now it becomes more important is this idea of same site so same site means that you have this top-level domain that is sort of the same like embracethered.com - and the so the top-level domain plus one so embracethered.com, www.embracethered.com is same site as blog.embracetheread.com and there's a few uh kind of variations of that that are defined in this public suffix list uh where you would have for instance things like or so on they they have one extension to that so
but it's not too important right now to get to understand the details but just this idea of same site that that concept exists that's one thing that i want you to to know and along the same lines uh there's more optional information as well there's more request headers a browser sends uh and there's actually a lot of changes happening right now to browsers as well like where they tell the web server more information of where they came from and none of them are fully they're not really implemented in all the browsers but i kind of want you to be aware that this change is going to happen and i can do a session about those three http headers in particular if there's interest so just mention it in the comments and I'll dive into the details of those three because they will they allow preventing
a lot of attacks actually in a very simple way or have basic mitigations for some of the web application attacks and it can be implemented simply on the server side but uh the most important thing that i want to talk about in this entire kind of session about the web application security fundamentals is cookies because a lot of security web application revolves around cookies so what is the cookie so imagine again our web application which you see on the left and our web server that is on the right so we said you know we click the link we visit the web page and the browser issues this get request the server now renders this page right it creates out this html document and in our case now also creates a cookie and sends both of these pieces
back to the user to the client in the user agent so the user agent now has the web application you know this html document and it also has this cookie so what is a cookie now so a cookie now and i put a save here because the browser kind of needs to kind of store or secure the cookie very securely and hold it very safely and you will understand in kind of just a few moments why that is so now every single time the browser visits the web server embrace the red again it will send that cookie
back to the web server so just the fact that the the domain matches this origin matches the cookie is being sent back actually the site matches that's why the cookie is being sent back for this request so any any cookie that a browser stores for a particular domain will be sent automatically so there's nothing the code javascript or anything needs to do anything any click and this cookie is being sent so this was the first click right visiting the info page there might be a post to an update endpoint again when that post happens the cookie is being sent with the request to the server so the reason this was implemented is because http is stateless and this means there's no it's very it's kind of difficult
to create a session this idea of the user being authenticated so that is this one way the http specification allows to create sort of a session concept which is the cookie which is a piece of data stored in the browser and whenever the user visits a particular domain again for which the browser stores a cookie the cookie will be sent by the browser to that web server so as you can see the cookie is very important because if somebody steals that cookie they can impersonate somebody else so they can impersonate that session in order to prevent that actually a couple of mitigations that uh can be set as part of an http response been setting they the cookie with the set cookie http response header
There is httponly which sort of defines that the cookie is not accessible from code, from the JavaScript. So it is only in transport so http only only in transport so it sort of becomes invisible to javascript the second mitigation is applying something that is called "secure", which is basically a setting that says the cookie will only be sent over an https connection which is an encrypted connection so it will not be sent if the connection is not encrypted so if there's somebody in the men in the middle or maybe in a coffee shop or something
somebody might sniff the traffic and gain access to the cookie if it's not sent over https this secure attribute or flag will prevent that so very important when set cookie happens from the web server initially to set the cookie for the client to specify the secure flag the third flag is same site which is actually pretty new but this specifies if the cookie is sent across site or not which is sort of also a little bit of a mitigation for advertisement
tracking right because cookies are used for by companies like Facebook and Google to track you across the internet because that's sort of what these cookies allow uh a little bit more about transport security uh i just wanted to bring up here is that you know traffic should always be encrypted so there's really no reason anymore to not have traffic encrypted this actually is even true for internal applications like if you have an internet all your traffic should be encrypted
there's also this http header response header called strict transport security which sort of instructs the browser to always upgrade to https so even if there's a you have a spelling mistake in one of your links or urls and you type http instead of https the browser will automatically replace that with https so this is a strict transport security and if you kind of worried on having to pay a lot of money to get certificates look into let's encrypt i'm using this for my own websites it's very easy to use it's free and there's a good way for you to get valid certificates
uh speaking more about actually you know inspecting traffic looking at html documents and proxying requests there's a couple of tools i've been using throughout my kind of security endeavors on windows i really like Fiddler which is free and it just plugs in nicely into the browser and it's very simple to use then there's burp there's zap and also the browser has browsers have built-in debuggers that are really useful so let me just quickly show you Fiddler real quick so this is Fiddler in action and it's turned on you open it on the left and you see all these requests that the browser issues and on the right you see the details of the request and the response nicely shown in the ui
similar if you just have the browser you can like right click and anywhere in the document and do inspect element that takes it into this web developer view where you can see here on the left you see the actual html document and you can also click on this network button up here and then then takes you to the view of seeing the http requests and responses so this is actually super useful and this is every browser nowadays actually has these web developer tools built into the browser super super convenient okay so this sort of wraps up this very first session it was a little bit basic and i hope i was able to convey sort of these foundations which will allow you to understand uh all the attacks that we're gonna talk in the next couple of sessions pretty easily i think again we learned about user agents right we learned about html
about transport protocol of html we learned about the same origin policy the same site concept we talked a lot about cookies and that http is a stateless protocol and cookies sort of can help you get state and kind of create sessions across multiple requests and in the end we looked a couple useful tools so yeah that's sort of wrapping it up now there will be more now more videos coming up i'm going to talk about cross-site scripting. tab-nabbing, cross-site request forgery, and many many more. If there's any sort of particular attack you want me to go into detail let me know make add a comment and i'll create a video for it uh thank you so much for watching i hope this is useful.
Make sure to subscribe so you get notifications when I

Ask anything
We've read this whole study set, so questions about it get the best answers.