Created
October 20, 2020 21:21
-
-
Save Nerixyz/fb8380f3c27b069058cdaae9cff34fa2 to your computer and use it in GitHub Desktop.
hackerCD - intercept canvas calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wrapObject(fn, that, args, name) { | |
const instance = fn.apply(that, args); | |
instance.__base = instance; | |
return new Proxy(instance, { | |
get: (target, key) => { | |
if(!['__base'].includes(key)) console.log(`[GET] ${name}.${key}`); | |
const value = target[key]; | |
return value; | |
}, | |
set: (target, key, value) => { | |
console.log(`${name}.${key} = ${value}`); | |
return target[key] = value; | |
}, | |
}); | |
} | |
function createHandler(fn, name) { | |
const handler = { | |
apply: (target, that, args) => { | |
if(that.__base) that = that.__base; | |
if (name === 'getContext') { | |
console.log(name, 'apply', args); | |
if (args[0] === 'webgl') return wrapObject(fn, that, args, 'WebGLRenderingContext'); | |
if (args[0] === '2d') return wrapObject(fn, that, args, 'CanvasRenderingContext2D'); | |
} | |
const res = fn.apply(that, args); | |
console.log(`${that.constructor.name}.${name}()`, args, res); | |
return res; | |
}, | |
}; | |
return new Proxy(fn, handler); | |
} | |
function wrapProto(obj) { | |
for(const key of Object.keys(obj.prototype).filter(x => {try { return typeof obj.prototype[x] === 'function'; } catch { return false; }})) { | |
obj.prototype[key] = createHandler(obj.prototype[key], key); | |
} | |
} | |
wrapProto(HTMLCanvasElement); | |
wrapProto(CanvasRenderingContext2D); | |
wrapProto(WebGLRenderingContext); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment