Slashdot Mirror


How to Crack a Website - XSS, Cookies, Sessions

twistedmoney45 writes "Informit.com provides an insiders look at a real life XSS attack and how it was used to bypass the authentication scheme of an online web application, leading to "shell" access, an admin account, and more. XSS attacks are often discussed in theory — this walk through illustrates just how dangerous these types of attacks can be in reality."

33 of 167 comments (clear)

  1. So... by cp.tar · · Score: 4, Funny

    ... can I crack pr0n sites with it?

    (This would have even been a frosty piss if it weren't for a Slow Down Cowboy!)

    --
    Ignore this signature. By order.
    1. Re:So... by mgblst · · Score: 5, Interesting

      Sure, depending on the site. If they let you post information to the site, like having a guest book, then you may be able to exploit a xss.

      Also depending on what you want to do to their site, if they let you upload files, but don't handle it well. ie, they may let you upload pictures of your girlfriend/sister, but they don't check to see if it is a jpeg file, or a php/cfm/asp file. Also, they may let you execute that file from that directory or not.

      Any site designed badly can be used.

      I remember some free pron sites, trying different number at the end of pictures to get extra freebies, or trying different directories based on names. Even a google image search of that particular site would reveal a lot of extra images or movies.

  2. One of my old favourites... by baadger · · Score: 5, Interesting

    One of my old favourite's oopsies are upload scripts that don't prevent you from uploading PHP or other web script files.

    It's amazing how many webmasters leave little scripts in their public directories not stopping to think search engines may find them.

  3. Interesting read, but... by mdobossy · · Score: 5, Insightful

    Sure, it is an interesting read.. that being said, nothing here is exactly shocking.

    I may be reading this wrong, but, he gains access to the server by requiring a legitimate user to log on to the site, through a third party server of his (Might be done via phishing, etc..), then he nabs a valid php session id, via some injected javascript code. Why not just grab the users login and password when they submit the form through your server? If you already have them logging in via a proxy, this would be much easier, and more reliable- sessions expire, etc..

    As with most of these articles on security- simply make sure you sterilize any incoming data. Again, its not exactly rocket science.

    1. Re:Interesting read, but... by Anonymous Coward · · Score: 3, Informative

      if you already have them logging in via a proxy, this would be much easier, and more reliable- sessions expire, etc..

      RTFA. The whole A. 5 times or until you realise how dumb u are, whichever comes later

    2. Re:Interesting read, but... by mdobossy · · Score: 4, Informative

      Yes, I am a dumbass, I mis-read the first page.. (as I prefaced- I may be reading this wrong, in my original message)

      This still doesn't change the fact that what he is doing relies heavily on phishing/getting a user to go to his server first to gain initial access to the server, which, IMHO, makes this more than just a hack- it relies on social engineering/hacking/whatever you want to call it. It is just another form of playing off a user's lack of knowledge/ignorance, and letting unsterilized (sanitized, whatever you want to call it) data pass to the server. That being said, if you can get a user to fall for a "click here, come to my server to log in to your server", you can probably get a lot more out of them than just a session ID.

      As I said- it is an interesting article, but what it really boils down to is what should have been pounded into every web developers head from the start- make sure there is no way to inject melicious code into your POSTS.

    3. Re:Interesting read, but... by Skrynesaver · · Score: 2, Interesting

      With a site offering e-mail, you could craft a message that looked like the timeout page (document.write(target=_TOP)). please login again .... Then if read via the webmail interface, the user thinks they timed out, the sender gets the login etc.. Just another thing to worry about, I'm off to check how our webmail server behaves...

      --
      "Linux is for noobs"-The new MS fud strategy
  4. Ok? by Jah-Wren+Ryel · · Score: 4, Informative

    So, after a quick read, it looks like the attacker has to convince a user to get to the attacked website via his own website - how else would he be able to forcefeed his own code into the $error variable to begin with?

    What are the chances that:

    1) A user will go to the bad guy's website
    2) That the user will have an account on the attacked website
    3) That said user will want to log into the attacked website right after going to the badguy website?

    Sure, it is possible and a potential risk, but it sure seems to be highly-specific, probably only good if you are targetting known users to begin with.

    --
    When information is power, privacy is freedom.
    1. Re:Ok? by PowerKe · · Score: 5, Informative

      1) A user will go to the bad guy's website
      Well, that's the hard part, but you could even try using an HTML formatted mail.

      2) That the user will have an account on the attacked website
      The place to put the code injection was on the login screen, so it's open for anyone. You could hide the login page in an invisable iframe.

      3) That said user will want to log into the attacked website right after going to the badguy website?
      The important thing is that the target logs on during the timeframe where the cookie is valid. If you're lucky and the site uses a permanent cookie, you could even take over a login session from days ago. If it's a session cookie you could take over a previous session if the user didn't close his browser after previously using the admin application.

    2. Re:Ok? by Ford+Prefect · · Score: 4, Informative
      So, after a quick read, it looks like the attacker has to convince a user to get to the attacked website via his own website - how else would he be able to forcefeed his own code into the $error variable to begin with?

      Cross-site scripting attacks are possible through many different vectors - sometimes you can include it in a GET address (e.g. 'image.php?name=foo.jpg&caption=<script goes here>'), sometimes in text submitted to the website (one I saw wasn't escaping private, inter-user messages - so I promptly sent Javascript to the administrator), or whatever.

      Essentially, if you can get the website to output your own text in an unescaped form (i.e. '>' and friends aren't converted into '&gt;' etc.), you can print raw Javascript, which the user's browser promptly executes. Javascript is quite a helpful language, so has functions for getting the values of cookies, etc. - which you can then send off to a third-party server under your control.

      I had to do a rough security audit about a year ago on a website. From knowing absolutely nothing about the specifics of cross-site-scripting and SQL injection attacks, in a couple of hours I was hijacking administrator sessions or getting the website to dump passwords, private data, you name it. It was embarrassingly easy. So, some tips for web programmers on the receiving end of all this:

      •    
      • ALWAYS escape your output, whether it's user-provided or not. Be it a simple htmlspecialchars() in PHP, or a fancy {$your_variable|escape:'html':'UTF-8'} in Smarty, or whatever your templating or programming language provides, never forget.
           
      • Be suspicious of all user-provided content. If it's a URL, make sure it's not a javascript: one - and if it's an email address or subject, make sure it's not got mail()-mangling newlines in it. You can send spam by adding extra headers that way, you know.
           
      • Make sure all your database queries are clean. If it's supposed to be an integer, enforce that it's an integer. If it's a string, make sure it's properly escaped. Regardless of where it came from. Bound queries, or whatever they're called, are very useful for this - in my own stuff, I've been doing (integer )$id, ... '".mysql_real_escape_string( $text )."' ... when creating the queries.
           
      • Read up on anything potentially hazardous, or on anything you don't quite understand. When something goes in a header, make sure it's got appropriate content - both email and HTTP headers can easily be abused through the judicious addition of newlines...


      --
      Tedious Bloggy Stuff - hooray?
    3. Re:Ok? by Anonymous Coward · · Score: 2, Interesting

      >
      > What are the chances that [...] a user will go to the bad guy's website?
      >

      Example:

      The attacked website features a news commenting system. You write a good comment, or a joke, and you link to a website you control (but you should say it is not your website -indirectly, don't insist...), maybe asking people their view on the link... You might make the link looks like "http://www.example.org/2006/08/Foo_article_title" . A lot of people will just click the link. The page redirects back to the attacked website, with the POST informations, but most people will just write a new comment, like "huh, it doesn't work for me"... To avoid any suspicion, you might post new comments with different nicknames, like "it works for me...", and continue with a comment on the linked "article". People will just think it is their connection, or their browser... even if multiple people have the problem, they will just think it is some kind of incompatibility... (you are being redirected from an unknown website, to the website where you just were... most people will think it is a problem on their part, because the unknown website "can't know you came from the website where you just were"...).

      Now, if the user was not logged in, you got his session ID (note you have to redirect the user to the original news you were commenting, so he does not see any error message, and so he stays on the topic of the link you provided earlier -he might want to post a comment to tell you the link does not work, and voila, if the website does not allow anonymous comments, he will just login!). If he logs on during the time the session is valid, you bypassed the authentication system (now that I think about it, another countermeasure would be for the developer to simply change the session ID during the login procedure...).

      If the user was logged in, and the website does not show a big fat "You have been logged out" message, when the user is being logged out (some websites just redirect to the home page, or to the page you just were -but then, you might use frames to hide the message), or if the website first check the email address and send the error message, before checking if the user was already logged on, you might, when the user is first visiting your link (do this for every user, most visitors will probably be logged in users... and if not, and you can hide the error message with frames, there's no problem), send POST informations to log him out, instead of trying to log him in... As the user will not understand why he was redirected back to the original page, he will most probably try it a second time... then you just detect it is his second time, and this time, send the POST informations to try to log him in (with the erroneous email address), and here you go, you have his session ID... and if the user was logged in, if most probably will try to use his account soon, so he will have to login again... (like to post comments about the link not working, at the very least...).

      Now, we were just using a commenting system... Some people don't read comments, some others are more suspicious of links in comments (but if your comment is otherwise funny/interesting, who will be suspicious?).

      What about websites which permit to post news? even checked by editors, this is no problem for your attack... you might just create a fake website, so the editor finds the link interesting/funny, and then, when the news is published, you just switch your website back to an attack machine... (people might even think the linked website was just "slashdotted", if the website has many visitors...).

      Even if the website has few visitors, I'm pretty sure it would work quite well, even with "educated" users...

  5. Requires social engineering by cras · · Score: 4, Funny
    The most problematic part from the article:
    The end result was that I had to make a user click on a link that first took the victim to my server

    I think this is the reason why people aren't that concerned about XSS. This requires that the attacker knows someone who has access to the web site and a way to get him to click on the link. I would certainly never click on a suspicious looking link. But sure, not everyone does that and if there are other post-login holes to get yourself into an admin, that's a problem for you too.

    One thing that annoys me when discussing XSS problems and such is that people always just suggest to validate input. I've built perfectly secure PHP applications that don't validate input at all, they just don't print the output using "print" but another function that properly escapes the output. So much more easier that way than having to think about input validation for every single new field you add.

    1. Re:Requires social engineering by Corbets · · Score: 4, Informative

      I'm not sure that validating output (escaping it) will be any easier than validating the input. Really, you just need to write a function that does generic parsing of the input in the same way you have a special function that escapes it. get_safe_input($string) could be a function that reads in from the user, fixes it up, and returns the safe string. Bam, done, use that every time instead of your read_string or whatever the php function is.

  6. More Like a Phishing Attack by hagrin · · Score: 4, Insightful

    As if fate wanted to make it challenging, the maximum size of the HTML input field for the email address was 25 characters, and it only accepted POST data, which is somewhat limiting. As a result, I had to "outsource" my cross-site scripting attack to a third server. The end result was that I had to make a user click on a link that first took the victim to my server.

    Sounds more like a phishing victim than anything else to me. I understand that the rest of the article brings you through the process of session hijacking, etc., but to me the real problem here is the phishing "attack" and the misuse by the user. Is a system really insecure if the user is diligent in what links he clicks on in this instance? I mean, if I leave the keys to my car in the ignition it's not going to take a skilled theif or laser cut keys to steal my car and the security implementations taken by the manufacturer won't matter.

    1. Re:More Like a Phishing Attack by Fallus+Shempus · · Score: 5, Insightful

      So you're going to rely on user's intelligence?

      You're not a coder are you.

  7. With all due respect by rehashed · · Score: 5, Informative

    This is a perfect example of a shoddily developed website.
    Additionally, it is, in certain respects, a retarded piece of journalism.

    The XSS mentioned requires the use of phishing techniques - why not simply capture username and password and this point of the exercise, it will allow you to regain entry once the session expires, and will allow you to overcome and further validation that the session handler may require.
    The XSS technique itself, printing the value of the cookie data via javascript to perform a get request to the evil server should not occur in the first place. That is simply shoddy website development. Sanitize input, escape output. Its not more difficult than that. Any developer who fails to grasp this most basic concept should not be in that line of work.

    Secondly is the ability to transfer a session. In the example, the attacker utilizes a third party utility to modify the request data. Why he has done this is beyond me - much easier to simply edit the cookie itself, or even pass the session id back as a 'get' request, a tehnique accepted by default on many PHP installs. It is rather basic to overcome this kind of attack by utilizing a more sophisticated session handler, although this is rarely done as it is taken as a given that the attacker is not going to easily obtain a session ID.

    Thirdly, is simple abuse of a poorly designed web application. There is no validation in place to ensure that the user has permission to perform a task on a designated object. In this case, there is no validation to ensure that user 42 has permission to modify data related to user 36. This is simply poorly designed, and again would not happen where a developer has half a clue about what he is doing.

    Finally, is the mother of all attacks - the ability to upload and run abitrary code. This is a combination of two blatantly obvious (to those who are not clueless) issues that should not arise in a professional web application. Firstly, is the ability to upload files of a certain type. Apache, for example, doesnt require PHP files to be marked as executable, it will simply run anything with a .php extension (or others depending on configuration) through the PHP parser. If there is no reason for a user to be able to upload files of this type, basic sanitization should be in place to prevent the upload of these file types, or, more easily only allow files with permissable extensions to be uploaded. The second issue is related to basic site administration, unless there is need for direct access to the files, uploads should be located in a directory outside of the webroot, preventing direct access to (and possible execution of) these documents. If direct access is require, all external handlers should be disabled for that directory by the simple usage of a .htaccess file. This would mean that any uploaded scripts/executables would be treated in the same manner as a regular file, and be downloaded as opposed to 'run'.

    In short, this was a very poorly designed web application. It didnt take into consideration any secure web development practices, such as Sanitization, Validation, Authorization and Limitation.
    Unfortunately, in todays climate, every man and his dog is a web developer, and 99% of them are complete and utter idiots.

  8. Human engineering & login code by YeeHaW_Jelte · · Score: 4, Insightful

    While the crack is technically interesting the article doesn't answer two things: first how did he get the code for the login screen and how did he get a user to login via his evilsite.com mockup of the login screen.

    Maybe he could guess that the email variable was printed unfiltered, and thus vunerable to XSS-attack, I dunno how he would get a user to login via a unrelated URL.

    --

    ---
    "The chances of a demonic possession spreading are remote -- relax."
  9. Re:JavaScript Must Be Eradicated From The Web by Anonymous Coward · · Score: 2, Insightful

    The root cause of all of these exploits is one thing: JavaScript.

    Wrong, the root cause of all these exploits is developper's stupidity, JS is merely an attack vector, as is flash, and the only way to use them as attack vectors is if there are holes in the application (ability to upload executable files or scripts, reliance on unsafe authentification/securisation schemes, ...).

  10. Some simple fixes would be sufficient by vdboor · · Score: 5, Informative
    As short summary, what every (PHP) developer should do is:
    • limit the session to the IP-address of the visiting user.
    • use htmlentities() on all outputted HTML
    • secure file uploads to avoid uploading PHP code
    And most important (but not relevant for TFA):
    • use mysql_real_escape_string() on all database input, or better: the variable binding feature of PEAR::DB
    • disable register_globals, use $_GET, $_POST and $_COOKIE instead.
    • Use preg_replace( '/[^a-zA-Z0-9\-_]', '', $input ) on all input used in file names.
      Things like require_once("files/" + $input + ".html") actually read php files when it's called as ?input=file.php%00
    --
    The best way to accelerate a windows server is by 9.81 m/s2 ;-)
    1. Re:Some simple fixes would be sufficient by Anonymous Coward · · Score: 2, Insightful
      • secure file uploads to avoid uploading PHP code

      How about "forbid the upload of anything executable, be them script files, scriptable files (flash), CGI files (Python, Perl, ...) or executable pages (PHP/ASP)"? Of even better, "only ever allow uploads of explicitely specified file types, and validate every single file against these types explicitely at upload"? No amount of blacklisting will ever beat whitelisting.

      • use mysql_real_escape_string() on all database input, or better: the variable binding feature of PEAR::DB

      Or use PHP5's mysqli_ or pdo_.

      • Things like require_once("files/" + $input + ".html") actually read php files when it's called as ?input=file.php%00

      And if you absolutely have to include third-party files, use readfile as it passes the data right from the file to the output buffer without ever executing it.

      Also use readfile when you include parts of templates that don't and shouldn't have executable (php) code.

      Of course, the best advice one could give to a PHP developer would be "first and foremost, don't use PHP".

    2. Re:Some simple fixes would be sufficient by Sepodati · · Score: 2, Insightful
      disable register_globals, use $_GET, $_POST and $_COOKIE instead.

      Why? There's no security gained by making this change. Shitty programmers can write shitty, unsecure code with register_globals enabled or disabled. I guess if you make a habit of running just anyone's code on your server, then turning this off may disable a specific vector, but certainly not all of them. The whole "register_globals enabled is bad for security" myth is just that. Bad programmers are bad for security and always will be.


      ---John Holmes...

    3. Re:Some simple fixes would be sufficient by FooBarWidget · · Score: 5, Insightful
      limit the session to the IP-address of the visiting user.

      Is this really a good idea? I've heard stories from people on mailing lists who claim that many people are behind routers/proxies that cause IP changes very often, and that's restricting a session to an IP causes more problems than it's worth.
    4. Re:Some simple fixes would be sufficient by CastrTroy · · Score: 2, Insightful

      How about, when people upload files, don't store them in a folder accessible to the outside world, which means they can't be run. It makes more sense to store them somewhere else, so that even if they upload a .php file, they can't call it. Also, when sending the files back downstream, ensure that it isn't in a way that will execute anything on the client side.

      --

      Anthropic principle: We see the universe the way it is because if it were different we would not be here to see it.
    5. Re:Some simple fixes would be sufficient by Christianfreak · · Score: 2, Informative

      global $login;
      check_login( $user, $pass );

      if( $login == true )
      { // do something
      }
      else
      { // fail
      }

      Ooops! With register_globals on, if I do ?login=true in the URL, suddenly I have access!

      Granted, the function should return the variable and it wouldn't be a problem. But with register_globals off, while the code would still be bad, it would be harmless, and there are more complex examples as well.

      I don't get register_globals anyway, unless you're doing something really short (and even then questionable). With a large application one needs to be able to determine where variables are coming from. register_globals hides this, and makes it easier for the newbs to blow their whole leg off instead of just shooting themselves in the foot.

      PHP really needs something equivalent to Perl's taint mode (-T switch). The program dies unless you check the variable against something. You can still get around it but at least it makes you think twice before pulling the trigger.

  11. Re:I knew, but... by flumps · · Score: 3, Funny

    .. Nobody can pwn me, I use IIS!!

    --
    "So there he is, risen from the dead. Like that fella, E. T." - Father Ted Crilly
  12. Additionally, checks for MSIE by vdboor · · Score: 4, Informative
    Most forums are vulnerable to simple JavaScript insertion attacks. One reason is MSIE is able to execute code like this:

    <a href="java
    script:alert('test')">

    MSIE also allows developers to execute JavaScript in CSS code. A forum which translates

    [color=blue]
    to
    <span style="color: blue">;
    is vulnerable when you can enter
    [color=expression(alert('test'))]
    .
    --
    The best way to accelerate a windows server is by 9.81 m/s2 ;-)
  13. Re:Question,,, by Opportunist · · Score: 2, Informative

    It becomes obsolete as soon as the mainstream media pick it up. Until then, it's usually fairly useful.

    Few of the self proclaimed "admins" have a clue. Fewer know what they're doing and a selected, carefully handpicked number can actually understand what that article is about. Essentially, as long as there is no out-of-the-box fix for this issue that can be used by even the most braindead zombie out there, a hacking method retains its worth.

    If you're just looking for a bounce-off server, it's usually fairly reliable to assume that you can find a clueless victim easily. If you're targeting something specific which has presumably an admin with a Clue (capital C not a typo), anything that's been out for over a month is worthless. No matter whether it's been buzzworded or not.

    --
    We used to have a Bill of Rights. Now, with the rights gone, all we have left is the bill.
  14. The Cross Site Scripting FAQ by mrkitty · · Score: 2, Informative
    --
    Believe me, if I started murdering people, there would be none of you left.
  15. Meh. by goldenratiophi · · Score: 2, Funny

    I was just hoping for a text box and a button saying "Crack Website!"

  16. Re:I knew, but... by reduct101 · · Score: 2, Insightful

    It's easy to do a lot of things you shouldn't do, so it boils down to a question of ethics really, doesn't it?

  17. XSS is not the biggest problem by MobyDisk · · Score: 2, Insightful

    The biggest problem here was not the XSS attack. Even w/o the XSS, this attack could have been carried out. The real problem here was the ability to upload executables. Even basic unixy permissions should stop write access to the web directory. Unescaped strings happen. (Especially when programming in these type of languages, which is another entire discussion.) The fix isn't to modify the thousands of print commands. It is to fix the one permission setting.

    Suppose you want to keep mice from getting in to your flour. Do you seal every crack, windows, vent, hole, and drain in your house? Or do you put the flour in a sealed container?

  18. Use Chroot jail and block outgoing http/ftp reques by vivekg · · Score: 3, Interesting

    I found that if you block outgoing http/ftp requests from your webserver attacker can not install his/her code.
    Run Apache web server in chrooted jail where bash or any other shell/commands are not available to attacker
    Run almost all critical services in chrooted jail
    Use dedicated DB server

    Other extreme solution - is to put root file system on read only media such as CDROM (not useful for everyday)

    And yes I know that no computer system can ever be completely secure, you can make crackers job hard only with above techniques

    Just my 0.2

    --
    The important thing is not to stop questioning --Albert Einstein.
  19. Ugh by CTachyon · · Score: 2, Interesting

    The article uses an awful example of an XSS exploit. The vast majority of XSS exploits don't have to jump through the hoop of an HTTP POST, so they're mindlessly simple to pull off, and there's no phishing involved. Plus, the fact that he used a proxy to modify his own web browsing is a complete red herring and detracts from the article.

    In contrast to phishing (and in contrast to what's been said in most of the posts so far), an XSS exploit is a legitimate link to the target website. No amount of looking at the host part of the URL will tell you that it's a phish, because it truly, honestly isn't. If you take the time to manually browse the GET parameters individually before you click the link, you might catch that it's an XSS exploit (or an attempt at one) if you know some HTML and Javascript.

    What's worse, though, is that instead of a clickable link, the exploit could even be the URL of an <iframe> tag. Completely automatic, no clicking required, and AFAIK no modern browser allows the user to disable or manually confirm <iframe> tags. Even worse, XSS attacks on some sites are persistent and shared. For instance, if someone home-rolls their own comment system and forgets some checks, it might be possible to post an XSS exploit in the subject line of the comment, which not only affects everyone who reads the post, but also everyone who even reads a list of new messages.

    The important characteristic of an XSS attack is that, unlike most web-based attacks, XSS attacks are exploiting the website itself -- not you, and not your browser. You click the link, the website ships some HTML to your browser, and whaddya know, there's one of them newfangled <script> tags to run.... The injected Javascript code, which the webmaster didn't intend but nonetheless his website actually delivered to your browser, runs with the same permissions as any of his own code, which includes access to the document.cookie property. Since most websites these days stick session IDs or even *groan* username/passwords in cookies, all it takes is for the Javascript to e.g. generate an <img> or <iframe> tag that points to the attacker's website, with '?'+document.cookie tacked onto the end of the GET request. Now the attacker can log in as you, and can explore other, non-XSS exploits that require him to be logged in.

    Your only recourse as a user is to disable Javascript. Full stop. You can't even enable it on "trusted" sites, because if you mark them as "trusted", you're saying "I trust that this website will never, ever be hacked so long as I live". One or more of your "trusted" sites might have undiscovered XSS flaws in their backends, and you can't "untrust" them faster than the blackhats can exploit them.

    There is nothing, I repeat nothing that you can do as a user or as a browser designer that will simultaneously (a) prevent XSS and other server-side exploits, and (b) allow the features that modern web sites depend on by design. If you think you want a browser that's completely safe from server-side exploits, spend a week browsing the web using strictly Lynx, then see if you're still of the same opinion. (While XSS requires Javascript to pull off, there are other server-side data validation problems on many websites, and those can be exploited by something as innocuous as a CSS stylesheet reference.)

    --
    Range Voting: preference intensity matters