@s4e/jsentinel
Version:
Script integrity and clipboard tamper detection for browsers.
2 lines (1 loc) • 3.61 kB
JavaScript
import{emitSafeTamper as e}from"./events-ffa6G8za.js";import"./index-hwEiSUPx.js";function t(e){try{return function(e){let t=0;for(let n=0;n<e.length;n++)t=31*t+e.charCodeAt(n)|0;return t>>>0}(Function.prototype.toString.call(e))}catch(e){return-1}}function n(e){try{return Function.prototype.toString.call(e)}catch(e){return"[unable to read]"}}function o(o){var i;if("undefined"==typeof window)return()=>{};const r=null!==(i=null==o?void 0:o.intervalMs)&&void 0!==i?i:4e3,d=t(window.addEventListener),w=t(window.removeEventListener),a=t(window.dispatchEvent),s=t(EventTarget.prototype.addEventListener),v=t(EventTarget.prototype.removeEventListener),p=t(EventTarget.prototype.dispatchEvent),c=t(window.CustomEvent),u=t(window.setTimeout),E=t(window.setInterval),h="function"==typeof window.fetch?t(window.fetch):-1,m=new Set,l=setInterval(()=>{try{const o={winAdd:t(window.addEventListener),winRemove:t(window.removeEventListener),winDispatch:t(window.dispatchEvent),protoAdd:t(EventTarget.prototype.addEventListener),protoRemove:t(EventTarget.prototype.removeEventListener),protoDispatch:t(EventTarget.prototype.dispatchEvent),CustomEvent:t(window.CustomEvent),setTimeout:t(window.setTimeout),setInterval:t(window.setInterval),fetch:"function"==typeof window.fetch?t(window.fetch):-1},i=[];o.winAdd===d||m.has("window.addEventListener")||(m.add("window.addEventListener"),i.push({api:"window.addEventListener",newSource:n(window.addEventListener)})),o.winRemove===w||m.has("window.removeEventListener")||(m.add("window.removeEventListener"),i.push({api:"window.removeEventListener",newSource:n(window.removeEventListener)})),o.winDispatch===a||m.has("window.dispatchEvent")||(m.add("window.dispatchEvent"),i.push({api:"window.dispatchEvent",newSource:n(window.dispatchEvent)})),o.protoAdd===s||m.has("EventTarget.prototype.addEventListener")||(m.add("EventTarget.prototype.addEventListener"),i.push({api:"EventTarget.prototype.addEventListener",newSource:n(EventTarget.prototype.addEventListener)})),o.protoRemove===v||m.has("EventTarget.prototype.removeEventListener")||(m.add("EventTarget.prototype.removeEventListener"),i.push({api:"EventTarget.prototype.removeEventListener",newSource:n(EventTarget.prototype.removeEventListener)})),o.protoDispatch===p||m.has("EventTarget.prototype.dispatchEvent")||(m.add("EventTarget.prototype.dispatchEvent"),i.push({api:"EventTarget.prototype.dispatchEvent",newSource:n(EventTarget.prototype.dispatchEvent)})),o.CustomEvent===c||m.has("window.CustomEvent")||(m.add("window.CustomEvent"),i.push({api:"window.CustomEvent",newSource:n(window.CustomEvent)})),o.setTimeout===u||m.has("window.setTimeout")||(m.add("window.setTimeout"),i.push({api:"window.setTimeout",newSource:n(window.setTimeout)})),o.setInterval===E||m.has("window.setInterval")||(m.add("window.setInterval"),i.push({api:"window.setInterval",newSource:n(window.setInterval)})),o.fetch===h||m.has("window.fetch")||(m.add("window.fetch"),i.push({api:"window.fetch",newSource:n(window.fetch)})),i.length>0&&e("native-api-modified",{modifiedAPIs:i,count:i.length})}catch(e){}},r);return()=>clearInterval(l)}function i(o,i){var r;if("undefined"==typeof window)return()=>{};const d=null!==(r=null==i?void 0:i.intervalMs)&&void 0!==r?r:5e3,w={};for(const[e,n]of Object.entries(o))w[e]=t(n);const a=new Set,s=setInterval(()=>{try{const i=[];for(const[e,r]of Object.entries(o))t(r)===w[e]||a.has(e)||(a.add(e),i.push({name:e,newSource:n(r)}));i.length>0&&e("native-api-modified",{modifiedFunctions:i,count:i.length})}catch(e){}},d);return()=>clearInterval(s)}export{i as startIntegrityGuard,o as startNativeGuard};