requests-ja3 icon indicating copy to clipboard operation
requests-ja3 copied to clipboard

Working example?

Open Scylla2020 opened this issue 2 years ago • 2 comments

Hi do you have any working example of this? I thought it was code like this but its not working.

import requests
from requests_ja3.patcher import Patcher
import urllib3

target_ja3_str = "771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-23-65281-10-11-35-16-5-13-18-51-45-43-27,,"
Patcher.patch (requests, target_ja3_str)
requests.get("https://example.com")

I get an FileNotFoundError or NotADirectoryError error

{'ssl_version': 771, 'accepted_ciphers': [47, 53, 156, 157, 4865, 4866, 4867, 49171, 49172, 49195, 49196, 49199, 49200, 52392, 52393], 'list_of_extensions': [0, 5, 10, 11, 13, 16, 18, 23, 27, 35, 43, 45, 51, 65281], 'elliptic_curve': None, 'elliptic_curve_point_format': None}
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
File c:\users\userx\appdata\local\programs\python\python39\lib\site-packages\requests_ja3\imitate\imitate.py:71, in _compile_libssl(use_in_tree_libssl)
     69         raise Exception (stderr_data.decode ())
---> 71 quiet_exec_in_src ("/usr/bin/chmod", "+x", "config")
     72 quiet_exec_in_src ("/usr/bin/chmod", "+x", "Configure")

File c:\users\userx\appdata\local\programs\python\python39\lib\site-packages\requests_ja3\imitate\imitate.py:65, in _compile_libssl.<locals>.quiet_exec_in_src(*args)
     64 def quiet_exec_in_src (*args):
---> 65     popen = subprocess.Popen (args, cwd = openssl_src_path, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
     66     return_code = popen.wait ()

File c:\users\userx\appdata\local\programs\python\python39\lib\subprocess.py:947, in Popen.__init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask)
    944             self.stderr = io.TextIOWrapper(self.stderr,
    945                     encoding=encoding, errors=errors)
--> 947     self._execute_child(args, executable, preexec_fn, close_fds,
    948                         pass_fds, cwd, env,
    949                         startupinfo, creationflags, shell,
    950                         p2cread, p2cwrite,
    951                         c2pread, c2pwrite,
    952                         errread, errwrite,
    953                         restore_signals,
    954                         gid, gids, uid, umask,
    955                         start_new_session)
    956 except:
    957     # Cleanup if the child failed starting.

File c:\users\userx\appdata\local\programs\python\python39\lib\subprocess.py:1416, in Popen._execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_gid, unused_gids, unused_uid, unused_umask, unused_start_new_session)
   1415 try:
-> 1416     hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
   1417                              # no special security
   1418                              None, None,
   1419                              int(not close_fds),
   1420                              creationflags,
   1421                              env,
   1422                              cwd,
   1423                              startupinfo)
   1424 finally:
   1425     # Child is launched. Close the parent's copy of those pipe
   1426     # handles that only the child should have open.  You need
   (...)
   1429     # pipe will not close when the child process exits and the
   1430     # ReadFile will hang.

FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

PermissionError                           Traceback (most recent call last)
File c:\users\userx\appdata\local\programs\python\python39\lib\shutil.py:616, in _rmtree_unsafe(path, onerror)
    615 try:
--> 616     os.unlink(fullname)
    617 except OSError:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\userx\\AppData\\Local\\Temp\\tmpu82bkhz8\\openssl-OpenSSL_1_1_1-stable.zip'

During handling of the above exception, another exception occurred:

PermissionError                           Traceback (most recent call last)
File c:\users\userx\appdata\local\programs\python\python39\lib\tempfile.py:801, in TemporaryDirectory._rmtree.<locals>.onerror(func, path, exc_info)
    800 try:
--> 801     _os.unlink(path)
    802 # PermissionError is raised on FreeBSD for directories

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\userx\\AppData\\Local\\Temp\\tmpu82bkhz8\\openssl-OpenSSL_1_1_1-stable.zip'

During handling of the above exception, another exception occurred:

NotADirectoryError                        Traceback (most recent call last)
Input In [3], in <cell line: 13>()
     10 import urllib3
     12 target_ja3_str = "771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-23-65281-10-11-35-16-5-13-18-51-45-43-27,,"
---> 13 Patcher.patch (requests, target_ja3_str)

File c:\users\userx\appdata\local\programs\python\python39\lib\site-packages\requests_ja3\patcher.py:26, in Patcher.patch(src_requests_module, target_ja3_str)
     20 @staticmethod
     21 def patch (src_requests_module: type (_clean_requests), target_ja3_str: str):
     22     # def ssl_wrap_socket_hook (*args, **kwargs):
     23     #     print (f"ssl_wrap_socket called with args {args} kwargs {kwargs}")
     24     #     return args, kwargs
     25     # Patcher._inner_patch (src_requests_module, ssl_wrap_socket_hook)
---> 26     fakessl = generate_imitation_libssl (target_ja3_str)
     27     Patcher._inner_patch (src_requests_module, fakessl)

File c:\users\userx\appdata\local\programs\python\python39\lib\site-packages\requests_ja3\imitate\imitate.py:23, in generate_imitation_libssl(ja3_str, use_in_tree_libssl, verify_against_real_ssl)
     20 ja3 = decoder.Decoder.decode (ja3_str)
     21 print (ja3)
---> 23 libssl_path, openssl_temp_dir = _compile_libssl (use_in_tree_libssl = use_in_tree_libssl)
     25 import requests_ja3.imitate.fakessl_py as fakessl
     26 fakessl.initialize (libssl_path, openssl_temp_dir)

File c:\users\userx\appdata\local\programs\python\python39\lib\site-packages\requests_ja3\imitate\imitate.py:86, in _compile_libssl(use_in_tree_libssl)
     84     return libcrypto_and_ssl_path, working_dir
     85 except:
---> 86     working_dir.cleanup ()
     87     raise

File c:\users\userx\appdata\local\programs\python\python39\lib\tempfile.py:830, in TemporaryDirectory.cleanup(self)
    828 def cleanup(self):
    829     if self._finalizer.detach():
--> 830         self._rmtree(self.name)

File c:\users\userx\appdata\local\programs\python\python39\lib\tempfile.py:812, in TemporaryDirectory._rmtree(cls, name)
    809     else:
    810         raise
--> 812 _shutil.rmtree(name, onerror=onerror)

File c:\users\userx\appdata\local\programs\python\python39\lib\shutil.py:740, in rmtree(path, ignore_errors, onerror)
    738     # can't continue even if onerror hook returns
    739     return
--> 740 return _rmtree_unsafe(path, onerror)

File c:\users\userx\appdata\local\programs\python\python39\lib\shutil.py:618, in _rmtree_unsafe(path, onerror)
    616             os.unlink(fullname)
    617         except OSError:
--> 618             onerror(os.unlink, fullname, sys.exc_info())
    619 try:
    620     os.rmdir(path)

File c:\users\userx\appdata\local\programs\python\python39\lib\tempfile.py:804, in TemporaryDirectory._rmtree.<locals>.onerror(func, path, exc_info)
    802     # PermissionError is raised on FreeBSD for directories
    803     except (IsADirectoryError, PermissionError):
--> 804         cls._rmtree(path)
    805 except FileNotFoundError:
    806     pass

File c:\users\userx\appdata\local\programs\python\python39\lib\tempfile.py:812, in TemporaryDirectory._rmtree(cls, name)
    809     else:
    810         raise
--> 812 _shutil.rmtree(name, onerror=onerror)

File c:\users\userx\appdata\local\programs\python\python39\lib\shutil.py:740, in rmtree(path, ignore_errors, onerror)
    738     # can't continue even if onerror hook returns
    739     return
--> 740 return _rmtree_unsafe(path, onerror)

File c:\users\userx\appdata\local\programs\python\python39\lib\shutil.py:599, in _rmtree_unsafe(path, onerror)
    597         entries = list(scandir_it)
    598 except OSError:
--> 599     onerror(os.scandir, path, sys.exc_info())
    600     entries = []
    601 for entry in entries:

File c:\users\userx\appdata\local\programs\python\python39\lib\shutil.py:596, in _rmtree_unsafe(path, onerror)
    594 def _rmtree_unsafe(path, onerror):
    595     try:
--> 596         with os.scandir(path) as scandir_it:
    597             entries = list(scandir_it)
    598     except OSError:

NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\Users\\userx\\AppData\\Local\\Temp\\tmpu82bkhz8\\openssl-OpenSSL_1_1_1-stable.zip'

Scylla2020 avatar Mar 21 '22 14:03 Scylla2020

Hi, yeah, apologies, but I currently only support Linux as that's my dev machine. The only things that should need to be changed for Windows support are some hardcoded paths (to default certificate locations) in the fakessl_py module and the majority of the compilation step done there in the function that fails. Feel free to make a PR :) otherwise I might add Windows support in the long-term future, but it's not super convenient for me right now

Also, this is a heavy work-in-progress, and I have most of the stuff in place to generate a custom libssl, but I have yet to add code to make changes to match the JA3. I want to work on this more this coming week, but right now it's not even close to functional, sorry for the confusion lol

an0ndev avatar Mar 21 '22 14:03 an0ndev

Okay thanks I'll wait till its functional then will dive in to fix the windows paths

Scylla2020 avatar Mar 21 '22 14:03 Scylla2020