def perform_request(self, url): try: proxy_setting = settings.Settings().get_string( 'network.httpsProxy') if proxy_setting: opener = build_opener( ProxyHandler({'https': proxy_setting})) install_opener(opener) r = urlopen(pyNativeStr(url)) total_size = int(r.headers.get('content-length', 0)) bytes_sent = 0 while True: data = r.read(4096) if not data: break raw_bytes = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) bytes_wrote = core.BNWriteDataForDownloadInstance( self.handle, raw_bytes, len(raw_bytes)) if bytes_wrote != len(raw_bytes): core.BNSetErrorForDownloadInstance( self.handle, "Bytes written mismatch!") return -1 bytes_sent = bytes_sent + bytes_wrote continue_download = core.BNNotifyProgressForDownloadInstance( self.handle, bytes_sent, total_size) if continue_download is False: core.BNSetErrorForDownloadInstance( self.handle, "Download aborted!") return -1 if not bytes_sent: core.BNSetErrorForDownloadInstance( self.handle, "Received no data!") return -1 except URLError as e: core.BNSetErrorForDownloadInstance(self.handle, e.__class__.__name__) log.log_error(str(e)) return -1 except: core.BNSetErrorForDownloadInstance(self.handle, "Unknown Exception!") log.log_error(traceback.format_exc()) return -1 return 0
def perform_custom_request(self, method, url, headers, data): try: proxy_setting = settings.Settings().get_string( 'network.httpsProxy') if proxy_setting: proxies = {"https": proxy_setting} else: proxies = None r = requests.request(pyNativeStr(method), pyNativeStr(url), headers=headers, data=data, proxies=proxies) response = r.content if len(response) == 0: core.BNSetErrorForDownloadInstance( self.handle, "No data received from server!") return None raw_bytes = (ctypes.c_ubyte * len(response)).from_buffer_copy(response) bytes_wrote = core.BNWriteDataForDownloadInstance( self.handle, raw_bytes, len(raw_bytes)) if bytes_wrote != len(raw_bytes): core.BNSetErrorForDownloadInstance( self.handle, "Bytes written mismatch!") return None continue_download = core.BNNotifyProgressForDownloadInstance( self.handle, bytes_wrote, bytes_wrote) if continue_download is False: core.BNSetErrorForDownloadInstance(self.handle, "Download aborted!") return None return DownloadInstance.Response(r.status_code, r.headers, None) except requests.RequestException as e: core.BNSetErrorForDownloadInstance(self.handle, e.__class__.__name__) return None except: core.BNSetErrorForDownloadInstance(self.handle, "Unknown Exception!") log.log_error(traceback.format_exc()) return None
def perform_request(self, url): try: proxy_setting = settings.Settings().get_string( 'network.httpsProxy') if proxy_setting: proxies = {"https": proxy_setting} else: proxies = None r = requests.get(pyNativeStr(url), proxies=proxies) if not r.ok: core.BNSetErrorForDownloadInstance( self.handle, "Received error from server") return -1 data = r.content if len(data) == 0: core.BNSetErrorForDownloadInstance( self.handle, "No data received from server!") return -1 raw_bytes = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) bytes_wrote = core.BNWriteDataForDownloadInstance( self.handle, raw_bytes, len(raw_bytes)) if bytes_wrote != len(raw_bytes): core.BNSetErrorForDownloadInstance( self.handle, "Bytes written mismatch!") return -1 continue_download = core.BNNotifyProgressForDownloadInstance( self.handle, bytes_wrote, bytes_wrote) if continue_download is False: core.BNSetErrorForDownloadInstance(self.handle, "Download aborted!") return -1 except requests.RequestException as e: core.BNSetErrorForDownloadInstance(self.handle, e.__class__.__name__) return -1 except: core.BNSetErrorForDownloadInstance(self.handle, "Unknown Exception!") log.log_error(traceback.format_exc()) return -1 return 0
def perform_custom_request(self, method, url, headers, data): result = None try: proxy_setting = settings.Settings().get_string( 'network.httpsProxy') if proxy_setting: opener = build_opener( ProxyHandler({'https': proxy_setting})) install_opener(opener) if b"Content-Length" in headers: del headers[b"Content-Length"] req = PythonDownloadInstance.CustomRequest( pyNativeStr(url), data=data, headers=headers, method=pyNativeStr(method)) result = urlopen(req) except HTTPError as he: result = he except URLError as e: core.BNSetErrorForDownloadInstance(self.handle, e.__class__.__name__) log.log_error(str(e)) return None except: core.BNSetErrorForDownloadInstance(self.handle, "Unknown Exception!") log.log_error(traceback.format_exc()) return None total_size = int(result.headers.get('content-length', 0)) bytes_sent = 0 while True: data = result.read(4096) if not data: break raw_bytes = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) bytes_wrote = core.BNWriteDataForDownloadInstance( self.handle, raw_bytes, len(raw_bytes)) if bytes_wrote != len(raw_bytes): core.BNSetErrorForDownloadInstance( self.handle, "Bytes written mismatch!") return None bytes_sent = bytes_sent + bytes_wrote continue_download = core.BNNotifyProgressForDownloadInstance( self.handle, bytes_sent, total_size) if continue_download is False: core.BNSetErrorForDownloadInstance( self.handle, "Download aborted!") return None if not bytes_sent: core.BNSetErrorForDownloadInstance(self.handle, "Received no data!") return None return DownloadInstance.Response(result.getcode(), result.headers, None)
def _install_modules(self, ctx, modules): # This callback should not be called directly it is indirectly # executed binary ninja is executed with --pip option if len(modules.strip()) == 0: return True python_lib = settings.Settings().get_string("python.interpreter") python_bin_override = settings.Settings().get_string( "python.binaryOverride") python_bin, status = self._get_executable_for_libpython( python_lib, python_bin_override) if python_bin is not None and not self._pip_exists(str(python_bin)): log.log_error( f"Pip not installed for configured python: {python_bin}.\n" "Please install pip or switch python versions.") return False if sys.platform == "darwin" and not any( [python_bin, python_lib, python_bin_override]): log.log_error( f"Plugin requirement installation unsupported on MacOS with bundled Python: {status}\n" "Please specify a path to a python library in the 'Python Interpreter' setting" ) return False elif python_bin is None: log.log_error( f"Unable to discover python executable required for installing python modules: {status}\n" "Please specify a path to a python binary in the 'Python Path Override'" ) return False python_bin_version = subprocess.check_output([ python_bin, "-c", "import sys; sys.stdout.write(f'{sys.version_info.major}.{sys.version_info.minor}')" ]).decode("utf-8") python_lib_version = f"{sys.version_info.major}.{sys.version_info.minor}" if (python_bin_version != python_lib_version): log.log_error( f"Python Binary Setting {python_bin_version} incompatible with python library {python_lib_version}" ) return False args = [ str(python_bin), "-m", "pip", "--isolated", "--disable-pip-version-check" ] proxy_settings = settings.Settings().get_string("network.httpsProxy") if proxy_settings: args.extend(["--proxy", proxy_settings]) args.extend(["install", "--verbose"]) venv = settings.Settings().get_string("python.virtualenv") in_virtual_env = 'VIRTUAL_ENV' in os.environ if venv is not None and venv.endswith("site-packages") and Path( venv).is_dir() and not in_virtual_env: args.extend(["--target", venv]) else: site_package_dir = Path( binaryninja.user_directory() ) / f"python{sys.version_info.major}{sys.version_info.minor}" / "site-packages" site_package_dir.mkdir(parents=True, exist_ok=True) args.extend(["--target", str(site_package_dir)]) args.extend(filter(len, modules.decode("utf-8").split("\n"))) log.log_info(f"Running pip {args}") status, result = self._run_args(args) if not status: log.log_error( f"Error while attempting to install requirements {result}") return status