Skip to content

Instantly share code, notes, and snippets.

@step135
Created June 16, 2021 22:32
Show Gist options
  • Save step135/3316056060a8083ed64c59c066c4272f to your computer and use it in GitHub Desktop.
Save step135/3316056060a8083ed64c59c066c4272f to your computer and use it in GitHub Desktop.
var background = (function () {
var tmp = {};
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
for (var id in tmp) {
if (tmp[id] && (typeof tmp[id] === "function")) {
if (request.path === "background-to-page") {
if (request.method === id) tmp[id](request.data);
}
}
}
});
/* */
return {
"receive": function (id, callback) {tmp[id] = callback},
"send": function (id, data) {chrome.runtime.sendMessage({"path": "page-to-background", "method": id, "data": data})}
}
})();
var inject = function () {
const nat = {
toBlob: HTMLCanvasElement.prototype.toBlob,
toDataURL: HTMLCanvasElement.prototype.toDataURL,
getImageData: CanvasRenderingContext2D.prototype.getImageData
};
const strnat = {
toBlob: HTMLCanvasElement.prototype.toBlob.toString(),
toDataURL: HTMLCanvasElement.prototype.toDataURL.toString(),
getImageData: CanvasRenderingContext2D.prototype.getImageData.toString()
};
//
var nsfy = function (canvas, context) {
var rn = 1 < 6;
if (context) {
const shift = {
'r': rn ? 2 : Math.floor(Math.random() * 10) - 5,
'g': rn ? 4 : Math.floor(Math.random() * 10) - 5,
'b': rn ? 0 : Math.floor(Math.random() * 10) - 5,
'a': rn ? 3 : Math.floor(Math.random() * 10) - 5
};
//
const width = canvas.width;
const height = canvas.height;
if (width && height) {
const imageData = nat.getImageData.apply(context, [0, 0, width, height]);
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
const n = ((i * (width * 4)) + (j * 4));
imageData.data[n + 0] = imageData.data[n + 0] + shift.r;
imageData.data[n + 1] = imageData.data[n + 1] + shift.g;
imageData.data[n + 2] = imageData.data[n + 2] + shift.b;
imageData.data[n + 3] = imageData.data[n + 3] + shift.a;
}
}
//
window.top.postMessage("canvas-fingerprint-defender-alert", '*');
context.putImageData(imageData, 0, 0);
}
}
};
function toBlob() {
nsfy(this, this.getContext("2d"));
return nat.toBlob.apply(this, arguments);
}
HTMLCanvasElement.prototype.toBlob = toBlob;
function toDataURL() {
nsfy(this, this.getContext("2d"));
return nat.toDataURL.apply(this, arguments);
}
HTMLCanvasElement.prototype.toDataURL = toDataURL;
function getImageData() {
nsfy(this.canvas, this);
return nat.getImageData.apply(this, arguments);
}
CanvasRenderingContext2D.prototype.getImageData = getImageData;
const fnames = Object.keys(nat);
const origToString = Function.prototype.toString;
Function.prototype.toString = function(){
return fnames.indexOf(this.name)>-1 ? strnat[this.name]:
origToString.apply(this, arguments)
};
document.documentElement.dataset.cbscriptallow = true;
};
var script_1 = document.createElement("script");
script_1.textContent = "(" + inject + ")()";
document.documentElement.appendChild(script_1);
script_1.remove();
if (document.documentElement.dataset.cbscriptallow !== "true") {
var script_2 = document.createElement("script");
script_2.textContent = `{
const iframes = [...window.top.document.querySelectorAll("iframe[sandbox]")];
for (var i = 0; i < iframes.length; i++) {
if (iframes[i].contentWindow) {
if (iframes[i].contentWindow.CanvasRenderingContext2D) {
iframes[i].contentWindow.CanvasRenderingContext2D.prototype.getImageData = CanvasRenderingContext2D.prototype.getImageData;
}
if (iframes[i].contentWindow.HTMLCanvasElement) {
iframes[i].contentWindow.HTMLCanvasElement.prototype.toBlob = HTMLCanvasElement.prototype.toBlob;
iframes[i].contentWindow.HTMLCanvasElement.prototype.toDataURL = HTMLCanvasElement.prototype.toDataURL;
}
}
}
}`;
//
window.top.document.documentElement.appendChild(script_2);
script_2.remove();
}
window.addEventListener("message", function (e) {
if (e.data && e.data === "canvas-fingerprint-defender-alert") {
background.send("fingerprint", {"host": document.location.host});
}
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment