|
| 1 | +import hmac |
| 2 | +import datetime |
| 3 | +import json |
| 4 | +import time |
| 5 | +import markupsafe |
| 6 | +import os |
| 7 | +import re |
| 8 | +import subprocess |
| 9 | +import tempfile |
| 10 | +import zipfile |
| 11 | +import base64 |
| 12 | +import importlib |
| 13 | +import ipaddress |
| 14 | +import hashlib |
| 15 | +import shufflepy |
| 16 | +from io import StringIO |
| 17 | +from contextlib import redirect_stdout |
| 18 | +import random |
| 19 | +import string |
| 20 | + |
| 21 | +import xmltodict |
| 22 | +from json2xml import json2xml |
| 23 | +from json2xml.utils import readfromstring |
| 24 | + |
| 25 | +from ioc_finder import find_iocs |
| 26 | +from dateutil.parser import parse as dateutil_parser |
| 27 | +from google.auth import crypt |
| 28 | +from google.auth import jwt |
| 29 | + |
| 30 | +import py7zr |
| 31 | +import pyminizip |
| 32 | +import rarfile |
| 33 | +import requests |
| 34 | +import tarfile |
| 35 | +import binascii |
| 36 | +import struct |
| 37 | + |
| 38 | +import paramiko |
| 39 | +import concurrent.futures |
| 40 | +import multiprocessing |
| 41 | + |
| 42 | +from pip._internal import main as pip_main |
| 43 | +from pip._internal.commands.show import search_packages_info |
| 44 | + |
| 45 | +from walkoff_app_sdk.app_base import AppBase |
| 46 | + |
| 47 | +class Tools(AppBase): |
| 48 | + __version__ = "1.2.0" |
| 49 | + app_name = ( |
| 50 | + "Shuffle Tools Fork" # this needs to match "name" in api.yaml for WALKOFF to work |
| 51 | + ) |
| 52 | + |
| 53 | + def __init__(self, redis, logger, console_logger=None): |
| 54 | + """ |
| 55 | + Each app should have this __init__ to set up Redis and logging. |
| 56 | + :param redis: |
| 57 | + :param logger: |
| 58 | + :param console_logger: |
| 59 | + """ |
| 60 | + super().__init__(redis, logger, console_logger) |
| 61 | + |
| 62 | + def dynamic_import(package_name: str): |
| 63 | + """Import a package and return the module""" |
| 64 | + return importlib.import_module(package_name.split('==')[0].split('>=')[0].split('<=')[0].split('>')[0].split('<')[0]) |
| 65 | + |
| 66 | + |
| 67 | + def get_missing_packages(required_packages: list) -> list: |
| 68 | + """ |
| 69 | + Returns a list of packages that aren't currently installed. |
| 70 | + |
| 71 | + Args: |
| 72 | + required_packages: List of package names (can include version specs) |
| 73 | + |
| 74 | + Returns: |
| 75 | + List of package names that aren't installed |
| 76 | + """ |
| 77 | + missing = [] |
| 78 | + for package in required_packages: |
| 79 | + # Remove version specifiers if present (e.g., 'pandas>=1.0.0' -> 'pandas') |
| 80 | + package_name = package.split('==')[0].split('>=')[0].split('<=')[0].split('>')[0].split('<')[0].strip() |
| 81 | + |
| 82 | + # Check if package exists in environment |
| 83 | + if not list(search_packages_info([package_name])): |
| 84 | + missing.append(package) |
| 85 | + |
| 86 | + return missing |
| 87 | + |
| 88 | + def install_packages(self, packages=[]) -> None: |
| 89 | + """ |
| 90 | + Install Python packages using pip's Python interface. |
| 91 | + |
| 92 | + Args: |
| 93 | + packages: List of package names to install |
| 94 | + """ |
| 95 | + |
| 96 | + packages_not_found = self.get_missing_packages(packages) |
| 97 | + |
| 98 | + for package in packages_not_found: |
| 99 | + try: |
| 100 | + pip_main(['install', package]) |
| 101 | + print(f"Successfully installed {package}") |
| 102 | + except Exception as e: |
| 103 | + print(f"Failed to install {package}: {str(e)}") |
| 104 | + |
| 105 | + def execute_python(self, code, packages) -> dict: |
| 106 | + if os.getenv("SHUFFLE_ALLOW_PACKAGE_INSTALL") == "true": |
| 107 | + allow_package_install = True |
| 108 | + |
| 109 | + packages = packages.split("\n") if packages else [] |
| 110 | + |
| 111 | + if packages: |
| 112 | + if allow_package_install: |
| 113 | + self.install_packages(packages) |
| 114 | + self.dynamic_import(packages) |
| 115 | + |
| 116 | + if len(code) == 36 and "-" in code: |
| 117 | + filedata = self.get_file(code) |
| 118 | + if filedata["success"] == False: |
| 119 | + return { |
| 120 | + "success": False, |
| 121 | + "message": f"Failed to get file for ID {code}", |
| 122 | + } |
| 123 | + |
| 124 | + if ".py" not in filedata["filename"]: |
| 125 | + return { |
| 126 | + "success": False, |
| 127 | + "message": f"Filename needs to contain .py", |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + # Write the code to a file |
| 132 | + # 1. Take the data into a file |
| 133 | + # 2. Subprocess execute file? |
| 134 | + try: |
| 135 | + f = StringIO() |
| 136 | + def custom_print(*args, **kwargs): |
| 137 | + return print(*args, file=f, **kwargs) |
| 138 | + |
| 139 | + #with redirect_stdout(f): # just in case |
| 140 | + # Add globals in it too |
| 141 | + globals_copy = globals().copy() |
| 142 | + globals_copy["print"] = custom_print |
| 143 | + |
| 144 | + # Add self to globals_copy |
| 145 | + for key, value in locals().copy().items(): |
| 146 | + if key not in globals_copy: |
| 147 | + globals_copy[key] = value |
| 148 | + |
| 149 | + globals_copy["self"] = self |
| 150 | + |
| 151 | + exec(code, globals_copy) |
| 152 | + |
| 153 | + s = f.getvalue() |
| 154 | + f.close() # why: https://www.youtube.com/watch?v=6SA6S9Ca5-U |
| 155 | + |
| 156 | + #try: |
| 157 | + # s = s.encode("utf-8") |
| 158 | + #except Exception as e: |
| 159 | + |
| 160 | + try: |
| 161 | + return { |
| 162 | + "success": True, |
| 163 | + "message": json.loads(s.strip()), |
| 164 | + } |
| 165 | + except Exception as e: |
| 166 | + try: |
| 167 | + return { |
| 168 | + "success": True, |
| 169 | + "message": s.strip(), |
| 170 | + } |
| 171 | + except Exception as e: |
| 172 | + return { |
| 173 | + "success": True, |
| 174 | + "message": s, |
| 175 | + } |
| 176 | + |
| 177 | + except Exception as e: |
| 178 | + return { |
| 179 | + "success": False, |
| 180 | + "message": f"exception: {e}", |
| 181 | + } |
| 182 | + |
| 183 | +if __name__ == "__main__": |
| 184 | + Tools.run() |
0 commit comments