I am working on a XSS prevention via Javascript. I am using the following JS-Code for that:
(function () {
/*
XSS prevention via JavaScript
*/
var XSSObject = new Object();
XSSObject.lockdown = function (obj, name) {
if (!String.prototype.startsWith) {
try {
if (Object.defineProperty) {
Object.defineProperty(obj, name, {
configurable: false
});
}
} catch (e) { };
}
}
XSSObject.proxy = function (obj, name, report_function_name, exec_original) {
var proxy = obj[name];
obj[name] = function () {
if (exec_original) {
return proxy.apply(this, arguments);
}
};
XSSObject.lockdown(obj, name);
};
XSSObject.proxy(window, 'alert', 'window.alert', false);
XSSObject.proxy(window, 'confirm', 'window.confirm', false);
XSSObject.proxy(window, 'prompt', 'window.prompt', false);
XSSObject.proxy(window, 'unescape', 'unescape', false);
XSSObject.proxy(document, 'write', 'document.write', false);
XSSObject.proxy(String, 'fromCharCode', 'String.fromCharCode', true);
})();
Using that script, it is not possible to execute the functions alert
, confirm
, prompt
, unescape
, write
and fromCharCode
.
Is there a way to bypass this prevention? If so, how and why?
var iframe = document.createElement('iframe'); iframe.srcdoc = ''; document.body.appendChild(iframe); iframe.contentWindow.alert.call(window, 'hello');
<>/\'
etc. Since JS is dynamic by nature it would be extremely difficult to cover all bases by blacklisting. Also, I haven't attempted this but you might be able to bypass by hex-encoding input, this blog is quite helpful for this specific thing: alihassanpenetrationtester.blogspot.com/2013/01/…