|
| 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 ipaddress |
| 13 | +import hashlib |
| 14 | +import shufflepy |
| 15 | +from io import StringIO |
| 16 | +from contextlib import redirect_stdout |
| 17 | +import random |
| 18 | +import string |
| 19 | + |
| 20 | +import xmltodict |
| 21 | +from json2xml import json2xml |
| 22 | +from json2xml.utils import readfromstring |
| 23 | + |
| 24 | +from ioc_finder import find_iocs |
| 25 | +from dateutil.parser import parse as dateutil_parser |
| 26 | +from google.auth import crypt |
| 27 | +from google.auth import jwt |
| 28 | + |
| 29 | +import py7zr |
| 30 | +import pyminizip |
| 31 | +import rarfile |
| 32 | +import requests |
| 33 | +import tarfile |
| 34 | +import binascii |
| 35 | +import struct |
| 36 | + |
| 37 | +import paramiko |
| 38 | +import concurrent.futures |
| 39 | +import multiprocessing |
| 40 | + |
| 41 | +from walkoff_app_sdk.app_base import AppBase |
| 42 | + |
| 43 | +class Tools(AppBase): |
| 44 | + __version__ = "1.2.0" |
| 45 | + app_name = ( |
| 46 | + "Shuffle Tools Fork" # this needs to match "name" in api.yaml for WALKOFF to work |
| 47 | + ) |
| 48 | + |
| 49 | + def __init__(self, redis, logger, console_logger=None): |
| 50 | + """ |
| 51 | + Each app should have this __init__ to set up Redis and logging. |
| 52 | + :param redis: |
| 53 | + :param logger: |
| 54 | + :param console_logger: |
| 55 | + """ |
| 56 | + super().__init__(redis, logger, console_logger) |
| 57 | + |
| 58 | + def execute_python(self, code): |
| 59 | + if len(code) == 36 and "-" in code: |
| 60 | + filedata = self.get_file(code) |
| 61 | + if filedata["success"] == False: |
| 62 | + return { |
| 63 | + "success": False, |
| 64 | + "message": f"Failed to get file for ID {code}", |
| 65 | + } |
| 66 | + |
| 67 | + if ".py" not in filedata["filename"]: |
| 68 | + return { |
| 69 | + "success": False, |
| 70 | + "message": f"Filename needs to contain .py", |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + # Write the code to a file |
| 75 | + # 1. Take the data into a file |
| 76 | + # 2. Subprocess execute file? |
| 77 | + try: |
| 78 | + f = StringIO() |
| 79 | + def custom_print(*args, **kwargs): |
| 80 | + return print(*args, file=f, **kwargs) |
| 81 | + |
| 82 | + #with redirect_stdout(f): # just in case |
| 83 | + # Add globals in it too |
| 84 | + globals_copy = globals().copy() |
| 85 | + globals_copy["print"] = custom_print |
| 86 | + |
| 87 | + # Add self to globals_copy |
| 88 | + for key, value in locals().copy().items(): |
| 89 | + if key not in globals_copy: |
| 90 | + globals_copy[key] = value |
| 91 | + |
| 92 | + globals_copy["self"] = self |
| 93 | + |
| 94 | + exec(code, globals_copy) |
| 95 | + |
| 96 | + s = f.getvalue() |
| 97 | + f.close() # why: https://www.youtube.com/watch?v=6SA6S9Ca5-U |
| 98 | + |
| 99 | + #try: |
| 100 | + # s = s.encode("utf-8") |
| 101 | + #except Exception as e: |
| 102 | + |
| 103 | + try: |
| 104 | + return { |
| 105 | + "success": True, |
| 106 | + "message": json.loads(s.strip()), |
| 107 | + } |
| 108 | + except Exception as e: |
| 109 | + try: |
| 110 | + return { |
| 111 | + "success": True, |
| 112 | + "message": s.strip(), |
| 113 | + } |
| 114 | + except Exception as e: |
| 115 | + return { |
| 116 | + "success": True, |
| 117 | + "message": s, |
| 118 | + } |
| 119 | + |
| 120 | + except Exception as e: |
| 121 | + return { |
| 122 | + "success": False, |
| 123 | + "message": f"exception: {e}", |
| 124 | + } |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + Tools.run() |
0 commit comments