tweak dynamic url rewriting logic

This commit is contained in:
Kevin Pham
2023-12-01 18:45:07 -06:00
parent 47def5c610
commit 59a57f2dbd
3 changed files with 316 additions and 298 deletions

View File

@@ -33,7 +33,7 @@ func NewProxySiteHandler(opts *ProxyOptions) fiber.Handler {
SetRequestModifications( SetRequestModifications(
// rx.SpoofJA3fingerprint(ja3, "Googlebot"), // rx.SpoofJA3fingerprint(ja3, "Googlebot"),
// rx.MasqueradeAsFacebookBot(), // rx.MasqueradeAsFacebookBot(),
//rx.MasqueradeAsGoogleBot(), rx.MasqueradeAsGoogleBot(),
rx.DeleteOutgoingCookies(), rx.DeleteOutgoingCookies(),
rx.ForwardRequestHeaders(), rx.ForwardRequestHeaders(),
//rx.SpoofReferrerFromGoogleSearch(), //rx.SpoofReferrerFromGoogleSearch(),
@@ -42,6 +42,7 @@ func NewProxySiteHandler(opts *ProxyOptions) fiber.Handler {
// rx.RequestArchiveIs(), // rx.RequestArchiveIs(),
). ).
AddResponseModifications( AddResponseModifications(
tx.InjectScriptBeforeDOMContentLoaded(`(() => {let d = document.createElement("div"); d.id = "adb-check"; document.body.append(d) })()`),
tx.ForwardResponseHeaders(), tx.ForwardResponseHeaders(),
tx.BypassCORS(), tx.BypassCORS(),
tx.BypassContentSecurityPolicy(), tx.BypassContentSecurityPolicy(),

View File

@@ -67,8 +67,11 @@
//console.log(`proxychain: origin: ${origin} // proxyOrigin: ${proxyOrigin} // original: ${oldUrl}`) //console.log(`proxychain: origin: ${origin} // proxyOrigin: ${proxyOrigin} // original: ${oldUrl}`)
//originDomain = origin.replace("https://", "");
let scheme = origin.split(":")[0];
if (url.startsWith("//")) { if (url.startsWith("//")) {
url = `/${origin}/${encodeURIComponent(url.substring(2))}`; url = `/${scheme}://${encodeURIComponent(url.substring(2))}`;
} else if (url.startsWith("/")) { } else if (url.startsWith("/")) {
url = `/${origin}/${encodeURIComponent(url.substring(1))}`; url = `/${origin}/${encodeURIComponent(url.substring(1))}`;
} else if ( } else if (
@@ -131,7 +134,7 @@
// monkey patch xmlhttprequest // monkey patch xmlhttprequest
const oldOpen = XMLHttpRequest.prototype.open; const oldOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function( XMLHttpRequest.prototype.open = function (
method, method,
url, url,
async = true, async = true,
@@ -147,7 +150,7 @@
); );
const oldSend = XMLHttpRequest.prototype.send; const oldSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(method, url) { XMLHttpRequest.prototype.send = function (method, url) {
return oldSend.call(this, method, rewriteURL(url)); return oldSend.call(this, method, rewriteURL(url));
}; };
hideMonkeyPatch( hideMonkeyPatch(
@@ -158,7 +161,7 @@
// monkey patch service worker registration // monkey patch service worker registration
const oldRegister = ServiceWorkerContainer.prototype.register; const oldRegister = ServiceWorkerContainer.prototype.register;
ServiceWorkerContainer.prototype.register = function(scriptURL, options) { ServiceWorkerContainer.prototype.register = function (scriptURL, options) {
return oldRegister.call(this, rewriteURL(scriptURL), options); return oldRegister.call(this, rewriteURL(scriptURL), options);
}; };
hideMonkeyPatch( hideMonkeyPatch(
@@ -169,7 +172,7 @@
// monkey patch URL.toString() method // monkey patch URL.toString() method
const oldToString = URL.prototype.toString; const oldToString = URL.prototype.toString;
URL.prototype.toString = function() { URL.prototype.toString = function () {
let originalURL = oldToString.call(this); let originalURL = oldToString.call(this);
return rewriteURL(originalURL); return rewriteURL(originalURL);
}; };
@@ -181,7 +184,7 @@
// monkey patch URL.toJSON() method // monkey patch URL.toJSON() method
const oldToJson = URL.prototype.toString; const oldToJson = URL.prototype.toString;
URL.prototype.toString = function() { URL.prototype.toString = function () {
let originalURL = oldToJson.call(this); let originalURL = oldToJson.call(this);
return rewriteURL(originalURL); return rewriteURL(originalURL);
}; };
@@ -197,11 +200,11 @@
"href", "href",
); );
Object.defineProperty(URL.prototype, "href", { Object.defineProperty(URL.prototype, "href", {
get: function() { get: function () {
let originalHref = originalHrefDescriptor.get.call(this); let originalHref = originalHrefDescriptor.get.call(this);
return rewriteURL(originalHref); return rewriteURL(originalHref);
}, },
set: function(newValue) { set: function (newValue) {
originalHrefDescriptor.set.call(this, rewriteURL(newValue)); originalHrefDescriptor.set.call(this, rewriteURL(newValue));
}, },
}); });
@@ -278,6 +281,19 @@
} }
}); });
// monkey-patching Element.setAttribute
const originalSetAttribute = Element.prototype.setAttribute;
Element.prototype.setAttribute = function (name, value) {
const isMatchingElement = elements.some((element) => {
return this.tagName.toLowerCase() === element.tag &&
name.toLowerCase() === element.attribute;
});
if (isMatchingElement) {
value = rewriteURL(value);
}
originalSetAttribute.call(this, name, value);
};
// sometimes, libraries will set the Element.innerHTML or Element.outerHTML directly with a string instead of setters. // sometimes, libraries will set the Element.innerHTML or Element.outerHTML directly with a string instead of setters.
// in this case, we intercept it, create a fake DOM, parse it and then rewrite all attributes that could // in this case, we intercept it, create a fake DOM, parse it and then rewrite all attributes that could
// contain a URL. Then we return the replacement innerHTML/outerHTML with redirected links. // contain a URL. Then we return the replacement innerHTML/outerHTML with redirected links.

View File

@@ -66,6 +66,7 @@ func PatchTrackerScripts() proxychain.ResponseModification {
// preflight checks // preflight checks
reqURL := chain.Request.URL.String() reqURL := chain.Request.URL.String()
isTracker := false isTracker := false
//
var surrogateScript io.ReadCloser var surrogateScript io.ReadCloser
for domain, domainRules := range rules { for domain, domainRules := range rules {