From 363feaf62f93bff37ec856713ac165bb914e6e18 Mon Sep 17 00:00:00 2001 From: Serega007 Date: Sun, 1 Oct 2023 11:43:21 +0300 Subject: [PATCH 1/3] Attempt to fix https://github.com/Wikidepia/hektCaptcha-extension/issues/53 --- public/popup.html | 2 +- src/background.js | 30 +++++++++++++++ src/hacktimer.js | 98 +++++++++++++++++++++++++++++++++++++++++++++++ src/hcaptcha.js | 1 + src/recaptcha.js | 1 + 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/hacktimer.js diff --git a/public/popup.html b/public/popup.html index b50dcfc..d506342 100644 --- a/public/popup.html +++ b/public/popup.html @@ -204,7 +204,7 @@ data-settings="reload_delay_time" /> - +
diff --git a/src/background.js b/src/background.js index f030bbb..2bf95bf 100644 --- a/src/background.js +++ b/src/background.js @@ -49,6 +49,7 @@ chrome.runtime.onInstalled.addListener(async () => { }); const kvStorage = {}; +let fakeIdToId = {}; chrome.runtime.onMessage.addListener(function ( { type, label }, sender, @@ -66,7 +67,36 @@ chrome.runtime.onMessage.addListener(function ( label.key = `${sender.tab.id}_${label.key}`; } sendResponse({ status: 'success', value: kvStorage[label.key] }); + } else if (type === 'HackTimer') { + if (label.name === 'setInterval') { + fakeIdToId[label.fakeId] = setInterval(function () { + triggerTimer(label.name, sender.tab.id, label.fakeId); + }, label.time); + } else if (label.name === 'clearInterval') { + clearInterval(fakeIdToId[label.fakeId]); + delete fakeIdToId[label.fakeId]; + } else if (label.name === 'setTimeout') { + fakeIdToId[label.fakeId] = setTimeout(function () { + triggerTimer(label.name, sender.tab.id, label.fakeId); + delete fakeIdToId[label.fakeId]; + }, label.time); + } else if (label.name === 'clearTimeout') { + clearTimeout(fakeIdToId[label.fakeId]); + delete fakeIdToId[label.fakeId]; + } } })(); return true; }); + +async function triggerTimer(name, tabId, fakeId) { + try { + await chrome.tabs.sendMessage(tabId, { + type: 'HackTimer', + label: { fakeId }, + }); + } catch (error) { + if (name === 'setInterval') clearInterval(fakeIdToId[fakeId]); + delete fakeIdToId[fakeId]; + } +} diff --git a/src/hacktimer.js b/src/hacktimer.js new file mode 100644 index 0000000..f80aeb8 --- /dev/null +++ b/src/hacktimer.js @@ -0,0 +1,98 @@ +let fakeIdToCallback = {}, + lastFakeId = 0, + maxFakeId = 0x7fffffff; // 2 ^ 31 - 1, 31 bit, positive values of signed 32 bit integer +window.fakeIdToCallback = fakeIdToCallback; + +function getFakeId() { + do { + if (lastFakeId === maxFakeId) { + lastFakeId = 0; + } else { + lastFakeId++; + } + } while (fakeIdToCallback.hasOwnProperty(lastFakeId)); + return lastFakeId; +} + +window.setInterval = function (callback, time /* , parameters */) { + let fakeId = getFakeId(); + fakeIdToCallback[fakeId] = { + callback: callback, + parameters: Array.prototype.slice.call(arguments, 2), + }; + chrome.runtime.sendMessage({ + type: 'HackTimer', + label: { + name: 'setInterval', + fakeId, + time, + }, + }); + return fakeId; +}; +window.clearInterval = function (fakeId) { + if (fakeIdToCallback.hasOwnProperty(fakeId)) { + delete fakeIdToCallback[fakeId]; + chrome.runtime.sendMessage({ + type: 'HackTimer', + label: { + name: 'clearInterval', + fakeId, + }, + }); + } +}; +window.setTimeout = function (callback, time /* , parameters */) { + let fakeId = getFakeId(); + fakeIdToCallback[fakeId] = { + callback: callback, + parameters: Array.prototype.slice.call(arguments, 2), + isTimeout: true, + }; + chrome.runtime.sendMessage({ + type: 'HackTimer', + label: { + name: 'setTimeout', + fakeId, + time, + }, + }); + return fakeId; +}; +window.clearTimeout = function (fakeId) { + if (fakeIdToCallback.hasOwnProperty(fakeId)) { + delete fakeIdToCallback[fakeId]; + chrome.runtime.sendMessage({ + type: 'HackTimer', + label: { + name: 'clearTimeout', + fakeId, + }, + }); + } +}; + +chrome.runtime.onMessage.addListener(function ( + { type, label } /*, sender, sendResponse*/ +) { + if (type === 'HackTimer') { + let fakeId = label.fakeId, + request, + parameters, + callback; + if (fakeIdToCallback.hasOwnProperty(fakeId)) { + request = fakeIdToCallback[fakeId]; + callback = request.callback; + parameters = request.parameters; + if (request.hasOwnProperty('isTimeout') && request.isTimeout) { + delete fakeIdToCallback[fakeId]; + } + } + if (typeof callback === 'string') { + console.error('Callback is not supported as string'); + } + if (typeof callback === 'function') { + callback.apply(window, parameters); + } + } +}); diff --git a/src/hcaptcha.js b/src/hcaptcha.js index 94ab173..fadb6dc 100644 --- a/src/hcaptcha.js +++ b/src/hcaptcha.js @@ -13,6 +13,7 @@ import Jimp from 'jimp'; import { Time } from './utils'; +require('./hacktimer.js'); const ort = require('onnxruntime-web'); // Modify ort wasm path diff --git a/src/recaptcha.js b/src/recaptcha.js index efdd870..40f4b97 100644 --- a/src/recaptcha.js +++ b/src/recaptcha.js @@ -14,6 +14,7 @@ import Jimp from 'jimp'; import { KVStorage, Time } from './utils.js'; +require('./hacktimer.js'); const ort = require('onnxruntime-web'); // Modify ort wasm path From f2e5ae774ee10ecb7fda6274a9198f7e2cac6808 Mon Sep 17 00:00:00 2001 From: Serega007 Date: Mon, 2 Oct 2023 16:40:13 +0300 Subject: [PATCH 2/3] Update onnxruntime-web --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index a23742f..baaf6a6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2337,7 +2337,7 @@ }, "node_modules/onnxruntime-web": { "version": "1.16.0", - "resolved": "git+ssh://git@github.com/Wikidepia/hektCaptcha-onnxruntime-web.git#447fad36552bc2f801515a9efd2bbb9710364c42", + "resolved": "git+ssh://git@github.com/Wikidepia/hektCaptcha-onnxruntime-web.git#91ff497895161e3b7c00c54b75fa9c5cd0f29da6", "license": "MIT", "dependencies": { "flatbuffers": "^1.12.0", From 4bb24022a7c05529bb1ceabf2bc633391cdb18fe Mon Sep 17 00:00:00 2001 From: Serega007 Date: Wed, 4 Oct 2023 23:49:01 +0300 Subject: [PATCH 3/3] Some fixes with messaging --- src/background.js | 74 +++++++++++++++++++++++++---------------------- src/hacktimer.js | 25 +++++++--------- 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/src/background.js b/src/background.js index 2bf95bf..b8e6fbb 100644 --- a/src/background.js +++ b/src/background.js @@ -55,46 +55,50 @@ chrome.runtime.onMessage.addListener(function ( sender, sendResponse ) { - (async () => { - if (type === 'KV_SET') { - if (label.tab_specific) { - label.key = `${sender.tab.id}_${label.key}`; - } - kvStorage[label.key] = label.value; - sendResponse({ status: 'success' }); - } else if (type === 'KV_GET') { - if (label.tab_specific) { - label.key = `${sender.tab.id}_${label.key}`; - } - sendResponse({ status: 'success', value: kvStorage[label.key] }); - } else if (type === 'HackTimer') { - if (label.name === 'setInterval') { - fakeIdToId[label.fakeId] = setInterval(function () { - triggerTimer(label.name, sender.tab.id, label.fakeId); - }, label.time); - } else if (label.name === 'clearInterval') { - clearInterval(fakeIdToId[label.fakeId]); - delete fakeIdToId[label.fakeId]; - } else if (label.name === 'setTimeout') { - fakeIdToId[label.fakeId] = setTimeout(function () { - triggerTimer(label.name, sender.tab.id, label.fakeId); - delete fakeIdToId[label.fakeId]; - }, label.time); - } else if (label.name === 'clearTimeout') { - clearTimeout(fakeIdToId[label.fakeId]); + if (type === 'KV_SET') { + if (label.tab_specific) { + label.key = `${sender.tab.id}_${label.key}`; + } + kvStorage[label.key] = label.value; + sendResponse({ status: 'success' }); + } else if (type === 'KV_GET') { + if (label.tab_specific) { + label.key = `${sender.tab.id}_${label.key}`; + } + sendResponse({ status: 'success', value: kvStorage[label.key] }); + } else if (type === 'HackTimer') { + if (label.name === 'setInterval') { + fakeIdToId[label.fakeId] = setInterval(function () { + triggerTimer(label.name, sender, label.fakeId); + }, label.time); + } else if (label.name === 'clearInterval') { + clearInterval(fakeIdToId[label.fakeId]); + delete fakeIdToId[label.fakeId]; + } else if (label.name === 'setTimeout') { + fakeIdToId[label.fakeId] = setTimeout(function () { + triggerTimer(label.name, sender, label.fakeId); delete fakeIdToId[label.fakeId]; - } + }, label.time); + } else if (label.name === 'clearTimeout') { + clearTimeout(fakeIdToId[label.fakeId]); + delete fakeIdToId[label.fakeId]; } - })(); - return true; + } }); -async function triggerTimer(name, tabId, fakeId) { +async function triggerTimer(name, sender, fakeId) { try { - await chrome.tabs.sendMessage(tabId, { - type: 'HackTimer', - label: { fakeId }, - }); + await chrome.tabs.sendMessage( + sender.tab.id, + { + type: 'HackTimer', + label: { fakeId }, + }, + { + documentId: sender.documentId, + frameId: sender.frameId, + } + ); } catch (error) { if (name === 'setInterval') clearInterval(fakeIdToId[fakeId]); delete fakeIdToId[fakeId]; diff --git a/src/hacktimer.js b/src/hacktimer.js index f80aeb8..edf2f0d 100644 --- a/src/hacktimer.js +++ b/src/hacktimer.js @@ -1,21 +1,16 @@ -let fakeIdToCallback = {}, - lastFakeId = 0, - maxFakeId = 0x7fffffff; // 2 ^ 31 - 1, 31 bit, positive values of signed 32 bit integer -window.fakeIdToCallback = fakeIdToCallback; +let fakeIdToCallback = {}; -function getFakeId() { - do { - if (lastFakeId === maxFakeId) { - lastFakeId = 0; - } else { - lastFakeId++; - } - } while (fakeIdToCallback.hasOwnProperty(lastFakeId)); - return lastFakeId; +function uuidv4() { + return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (c) => + ( + c ^ + (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4))) + ).toString(16) + ); } window.setInterval = function (callback, time /* , parameters */) { - let fakeId = getFakeId(); + let fakeId = uuidv4(); fakeIdToCallback[fakeId] = { callback: callback, parameters: Array.prototype.slice.call(arguments, 2), @@ -43,7 +38,7 @@ window.clearInterval = function (fakeId) { } }; window.setTimeout = function (callback, time /* , parameters */) { - let fakeId = getFakeId(); + let fakeId = uuidv4(); fakeIdToCallback[fakeId] = { callback: callback, parameters: Array.prototype.slice.call(arguments, 2),