diff --git a/undetected_chromedriver/__init__.py b/undetected_chromedriver/__init__.py index ff4d0c6..520106d 100644 --- a/undetected_chromedriver/__init__.py +++ b/undetected_chromedriver/__init__.py @@ -17,7 +17,7 @@ by UltrafunkAmsterdam (https://github.com/ultrafunkamsterdam) from __future__ import annotations -__version__ = "3.4.2" +__version__ = "3.4.4" import json import logging @@ -470,18 +470,18 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): { "source": """ - Object.defineProperty(window, 'navigator', { - value: new Proxy(navigator, { - has: (target, key) => (key === 'webdriver' ? false : key in target), - get: (target, key) => - key === 'webdriver' ? - false : - typeof target[key] === 'function' ? - target[key].bind(target) : - target[key] - }) - }); - + Object.defineProperty(window, "navigator", { + Object.defineProperty(window, "navigator", { + value: new Proxy(navigator, { + has: (target, key) => (key === "webdriver" ? false : key in target), + get: (target, key) => + key === "webdriver" + ? false + : typeof target[key] === "function" + ? target[key].bind(target) + : target[key], + }), + }); """ }, ) diff --git a/undetected_chromedriver/patcher.py b/undetected_chromedriver/patcher.py index 22396d2..af87e5e 100644 --- a/undetected_chromedriver/patcher.py +++ b/undetected_chromedriver/patcher.py @@ -94,9 +94,7 @@ class Patcher(object): self.version_full = None def auto(self, executable_path=None, force=False, version_main=None): - if executable_path: - self.executable_path = executable_path self._custom_exe_path = True @@ -207,25 +205,24 @@ class Patcher(object): @staticmethod def gen_random_cdc(): - # make cdc_variables without underscores cdc = random.choices(string.ascii_letters, k=27) - - # cdc[-6:-4] = map(str.upper, cdc[-6:-4]) - # cdc[2] = cdc[0] - # cdc[3] = "_" return "".join(cdc).encode() def is_binary_patched(self, executable_path=None): executable_path = executable_path or self.executable_path - with io.open(executable_path, "rb") as fh: - return fh.read().find(b"undetected chromedriver") != -1 + try: + with io.open(executable_path, "rb") as fh: + return fh.read().find(b"undetected chromedriver") != -1 + except FileNotFoundError: + return False def patch_exe(self): start = time.perf_counter() logger.info("patching driver executable %s" % self.executable_path) with io.open(self.executable_path, "r+b") as fh: content = fh.read() - match_injected_codeblock = re.search(rb"{window.*;}", content) + # match_injected_codeblock = re.search(rb"{window.*;}", content) + match_injected_codeblock = re.search(rb"\{window\.cdc.*?;\}", content) if match_injected_codeblock: target_bytes = match_injected_codeblock[0] new_target_bytes = ( @@ -233,30 +230,18 @@ class Patcher(object): len(target_bytes), b" " ) ) - new_content = content.replace(target_bytes, new_target_bytes) - if new_content == content: - logger.warning( - "something went wrong patching the driver binary. could not find injection code block" - ) - else: - logger.debug( - "found block:\n%s\nreplacing with:\n%s" - % (target_bytes, new_target_bytes) - ) - fh.seek(0) - fh.write(new_content) - - # we just keep the cdc variables as they can't be injected anyways so no harm - # keeping for reference - # fh.seek(0) - # for line in iter( lambda: fh.readline() , b"" ): - # if b'cdc_' in line: - # fh.seek( -len( line ) , 1 ) - # new_line = re.sub( b"cdc_.{22}_" , self.gen_random_cdc() , line ) - # logger.debug( 'replaced %s\n\twith:%s' % (line , new_line) ) - # fh.write( new_line ) + new_content = content.replace(target_bytes, new_target_bytes) + if new_content == content: + logger.warning( + "something went wrong patching the driver binary. could not find injection code block" + ) else: - logger.info("%s seems already patched ?!?!" % self.executable_path) + logger.debug( + "found block:\n%s\nreplacing with:\n%s" + % (target_bytes, new_target_bytes) + ) + fh.seek(0) + fh.write(new_content) logger.debug( "patching took us {:.2f} seconds".format(time.perf_counter() - start) )