|
4 | 4 | import os
|
5 | 5 | import random
|
6 | 6 | import re
|
| 7 | +import string |
7 | 8 | import sys
|
8 | 9 | import time
|
9 | 10 |
|
@@ -182,28 +183,44 @@ def gen_random_cdc():
|
182 | 183 | def is_binary_patched(self, executable_path=None):
|
183 | 184 | executable_path = executable_path or self.executable_path
|
184 | 185 | with io.open(executable_path, "rb") as fh:
|
185 |
| - for line in iter(lambda: fh.readline(), b""): |
186 |
| - if b"cdc_" in line: |
187 |
| - return False |
188 |
| - else: |
189 |
| - return True |
| 186 | + if b"window.cdc_adoQpoasnfa76pfcZLmcfl_" in fh.read(): |
| 187 | + return False |
| 188 | + return True |
190 | 189 |
|
191 | 190 | def patch_exe(self):
|
192 |
| - """ |
193 |
| - Patches the ChromeDriver binary |
194 |
| - :return: False on failure, binary name on success |
195 |
| - """ |
196 |
| - logger.info("patching driver executable %s" % self.executable_path) |
197 |
| - linect = 0 |
198 |
| - replacement = self.gen_random_cdc() |
| 191 | + """Patches the ChromeDriver binary""" |
| 192 | + def gen_js_whitespaces(match): |
| 193 | + return b"\n" * len(match.group()) |
| 194 | + |
| 195 | + def gen_call_function_js_cache_name(match): |
| 196 | + rep_len = len(match.group()) - 3 |
| 197 | + ran_len = random.randint(6, rep_len) |
| 198 | + bb = b"'" + bytes(str().join(random.choices( |
| 199 | + population=string.ascii_letters, k=ran_len |
| 200 | + )), 'ascii') + b"';" + (b"\n" * (rep_len - ran_len)) |
| 201 | + return bb |
| 202 | + |
199 | 203 | with io.open(self.executable_path, "r+b") as fh:
|
200 |
| - for line in iter(lambda: fh.readline(), b""): |
201 |
| - if b"cdc_" in line: |
202 |
| - fh.seek(-len(line), 1) |
203 |
| - newline = re.sub(b"cdc_.{22}", replacement, line) |
204 |
| - fh.write(newline) |
205 |
| - linect += 1 |
206 |
| - return linect |
| 204 | + file_bin = fh.read() |
| 205 | + file_bin = re.sub( |
| 206 | + b"window\\.cdc_[a-zA-Z0-9]{22}_(Array|Promise|Symbol)" |
| 207 | + b" = window\\.(Array|Promise|Symbol);", |
| 208 | + gen_js_whitespaces, |
| 209 | + file_bin, |
| 210 | + ) |
| 211 | + file_bin = re.sub( |
| 212 | + b"window\\.cdc_[a-zA-Z0-9]{22}_(Array|Promise|Symbol) \\|\\|", |
| 213 | + gen_js_whitespaces, |
| 214 | + file_bin, |
| 215 | + ) |
| 216 | + file_bin = re.sub( |
| 217 | + b"'\\$cdc_[a-zA-Z0-9]{22}_';", |
| 218 | + gen_call_function_js_cache_name, |
| 219 | + file_bin, |
| 220 | + ) |
| 221 | + fh.seek(0) |
| 222 | + fh.write(file_bin) |
| 223 | + return True |
207 | 224 |
|
208 | 225 | def __repr__(self):
|
209 | 226 | return "{0:s}({1:s})".format(
|
|
0 commit comments