5 ms·
Yet another demonstration, if you needed it, on why blacklisting user input to avoid code injection is highly unlikely to succeed. There are many ways to skin
by patio11 15y ago
Yet another demonstration, if you needed it, on why blacklisting user input to avoid code injection is highly unlikely to succeed. There are many ways to skin a cat, and you only need to miss one.
You can also assume that the bad guys start with a list similar to this and tools to semi-randomly perturb their inputs until they find the right combo of hocus-locus to get past your filter and then "neutralized" by your regular expression magic into functioning exploit code.
(n.b. I'm talking about more interesting attacker goals than forcing a reload, obviously.)
- Cushman 15y agoTrue as that may be, this doesn't really demonstrate that— it's just 535 different ways to access `location`. A regex would trivially disable every one. Of the things on that list.
- teej 15y ago"Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." Show me your trivial regex solution to this problem and I'll show you how to break it.
- Cushman 15y agoI'm not saying there's a trivial regex solution to XSS, I'm saying there's a trivial regex solution to disable every one of these so-called "ways" of reloading the page: s/[\.\[=]//g will do the trick. I mean, s/location//g will do the trick too, but I was aiming a little more generic.
- jrockway 15y agoHow about "location++"?
- Cushman 15y agoSee, there's another clever trick that actually does something interesting. Really, there's a whole class of toString solutions missing from this list... but I guess you have to stop at some point.
- lanstein 15y agoSorry, what does this do? In Chrome it just redirects to /NaN when you put it in the console, am I missing something?
- jrockway 15y agoYeah, it causes the browser to go to a new page. += with a string probably works too, but I don't have Chrome here to test with.
- mike-cardwell 15y agoeval(unescape("%64%6F%63%75%6D%65%6E%74%2E%6C%6F%63%61%74%69%6F%6E%2E%68%72%65%66%3D%27%68%74%74%70%73%3A%2F%2F%67%72%65%70%75%6C%61%72%2E%63%6F%6D%2F%27"))
- patio11 15y agoIn case the import of this epic retort is lost on anybody: in the real world, you have to pick and code your defense and then the attacker, who gets essentially infinite time to observe the behavior of your defenses, gets to pick their attack string(s). Do not rely on regular expressions or blacklists to sanitize code for you. It will not end well.
- Cushman 15y ago...and if that were on this list of ways to reload a page in JavaScript that I'm talking about, it would be relevant here. Look, just read what I'm saying. I swear I know what I'm talking about, and I swear I'm not saying you can reliably sanitize JavaScript with a regex. Really. This must be like the FizzBuzz thing— programmers just can't resist a challenge, even if it is explicitly marked as not being a challenge. And thanks for the hscroll :P
- mike-cardwell 15y agohscroll? Here on Firefox 5, it wraps nicely...
- ay 15y agoMaybe, rather use something like this: s/</</g ?
- simonw 15y agoRegex this: window['loc' + 'ation']
- Cushman 15y agoNot to unduly humor the idea that I mean anything but the strings in that list, but my 'trivial' regex above turns that into a syntax error.
- deleted 15y ago[deleted]
- Cushman 15y agoI have no idea how this has become the most controversial thing I've ever posted... It's pretty straightforward. Ah well. TIL not to poke FizzBuzz.
- JoachimSchipper 15y agoOff-topic: Have you been hanging around tptacek a lot lately? You have suddenly started giving out security advice. A good thing, but quite noticeably different. ;-)
- ig1 15y agoI believe patio11 did some consulting for tptacek's firm, so I imagine the answer is yes...
- patio11 15y agoI've worked with Thomas (not primarily on the security side of his house), consider him a friend, and go out to dinner with him every time we're in the same time zone. That has probably increased my interest in web application security, since I nurse secret dreams of being a web pentester. (He would probably say much the same about running a primarily product business, depending on the level of seriousness we had in the conversation.) That said, every professional engineer should understand enough about security to carry out their duties to their customers. Accordingly, I've been peripherally interested in it for years. See, among many others: http://news.ycombinator.com/item?id=792900 http://news.ycombinator.com/item?id=792900
- deleted 15y ago[deleted]
- ig1 15y agoIt's been shown you can write pretty much any javascript code using operator symbols only (so things like +[]_) without any alphanumeric characters making it pretty much imposible to filter. User inputed javascript is pretty much impossible to make safe.
- stiff 15y agoWhat do you mean precisely? Normally you prevent code injection by not allowing user-supplied strings to be interpreted as JavaScript in the first place, not by trying to catch all the possible ways in which one can construct a script doing something harmful. There aren't so many ways to inject a <script> tag or something equivalent into the page.
- rmccue 15y agoSome sites let you insert some Javascript, but try and block certain techniques (and it all goes horribly wrong for them).
- AltIvan 15y agoIf you delete/change '<script' and '</script>' you pretty much have that covered.