Skip to content

Commit e791c73

Browse files
authored
Merge pull request #975 from orbisai0security/fix-command-injection-patcher-v001
fix: remove unsafe exec() in patcher.py
2 parents 1984f0d + fc6cf2b commit e791c73

1 file changed

Lines changed: 48 additions & 9 deletions

File tree

ExecuteStage/undetected_chromedriver_ES/patcher.py

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
import re
1111
import shutil
1212
import string
13-
import subprocess
13+
1414
import sys
1515
import time
16-
from urllib.request import urlopen
17-
from urllib.request import urlretrieve
16+
import requests
17+
import tempfile
1818
import zipfile
1919
from multiprocessing import Lock
2020

@@ -236,7 +236,7 @@ def fetch_release_number(self):
236236
path += f"_{self.version_main}"
237237
path = path.upper()
238238
logger.debug("getting release number from %s" % path)
239-
return LooseVersion(urlopen(self.url_repo + path).read().decode())
239+
return LooseVersion(requests.get(self.url_repo + path, timeout=30).text.strip())
240240

241241
def parse_exe_version(self):
242242
with io.open(self.executable_path, "rb") as f:
@@ -254,7 +254,11 @@ def fetch_package(self):
254254
u = "%s/%s/%s" % (self.url_repo, self.version_full.vstring, self.zip_name)
255255
logger.debug("downloading from %s" % u)
256256
# return urlretrieve(u, filename=self.data_path)[0]
257-
return urlretrieve(u)[0]
257+
response = requests.get(u, timeout=60)
258+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
259+
tmp.write(response.content)
260+
tmp.close()
261+
return tmp.name
258262

259263
def unzip_package(self, fp):
260264
"""
@@ -268,13 +272,13 @@ def unzip_package(self, fp):
268272
except (FileNotFoundError, OSError):
269273
pass
270274

271-
os.makedirs(self.zip_path, mode=0o755, exist_ok=True)
275+
os.makedirs(self.zip_path, mode=0o644, exist_ok=True)
272276
with zipfile.ZipFile(fp, mode="r") as zf:
273277
zf.extract(self.exe_name, self.zip_path)
274278
os.rename(os.path.join(self.zip_path, self.exe_name), self.executable_path)
275279
os.remove(fp)
276280
os.rmdir(self.zip_path)
277-
os.chmod(self.executable_path, 0o755)
281+
os.chmod(self.executable_path, 0o644)
278282
return self.executable_path
279283

280284
@staticmethod
@@ -285,11 +289,46 @@ def force_kill_instances(exe_name):
285289
286290
:return: True on success else False
287291
"""
292+
import signal
288293
exe_name = os.path.basename(exe_name)
294+
r = 1
289295
if IS_POSIX:
290-
r = os.system("kill -f -9 $(pidof %s)" % exe_name)
296+
if os.path.isdir("/proc"):
297+
for pid_dir in os.listdir("/proc"):
298+
if not pid_dir.isdigit():
299+
continue
300+
try:
301+
with open(os.path.join("/proc", pid_dir, "comm")) as fh:
302+
if fh.read().strip() == exe_name:
303+
os.kill(int(pid_dir), signal.SIGKILL)
304+
r = 0
305+
except (OSError, ProcessLookupError):
306+
pass
291307
else:
292-
r = os.system("taskkill /f /im %s" % exe_name)
308+
import ctypes
309+
class PROCESSENTRY32(ctypes.Structure):
310+
_fields_ = [
311+
("dwSize", ctypes.c_uint32), ("cntUsage", ctypes.c_uint32),
312+
("th32ProcessID", ctypes.c_uint32),
313+
("th32DefaultHeapID", ctypes.POINTER(ctypes.c_ulong)),
314+
("th32ModuleID", ctypes.c_uint32), ("cntThreads", ctypes.c_uint32),
315+
("th32ParentProcessID", ctypes.c_uint32),
316+
("pcPriClassBase", ctypes.c_long), ("dwFlags", ctypes.c_uint32),
317+
("szExeFile", ctypes.c_char * 260),
318+
]
319+
snap = ctypes.windll.kernel32.CreateToolhelp32Snapshot(0x2, 0)
320+
entry = PROCESSENTRY32(dwSize=ctypes.sizeof(PROCESSENTRY32))
321+
if ctypes.windll.kernel32.Process32First(snap, ctypes.byref(entry)):
322+
while True:
323+
if entry.szExeFile.decode("utf-8", errors="ignore") == exe_name:
324+
h = ctypes.windll.kernel32.OpenProcess(0x1, False, entry.th32ProcessID)
325+
if h:
326+
ctypes.windll.kernel32.TerminateProcess(h, 1)
327+
ctypes.windll.kernel32.CloseHandle(h)
328+
r = 0
329+
if not ctypes.windll.kernel32.Process32Next(snap, ctypes.byref(entry)):
330+
break
331+
ctypes.windll.kernel32.CloseHandle(snap)
293332
return not r
294333

295334
@staticmethod

0 commit comments

Comments
 (0)