第六章 扩展Burp代理

这一章的学习,个人感觉比前面几章稍微有难度一些,虽然过程挺艰苦的,但还算是勉强做出来了吧:)

这一章节的内容,因为jython对python3的兼容性不乐观,所以我们将使用python2编写代码。

Jython?

Jython是一种完整的语言,而不是一个Java翻译器或仅仅是一个Python编译器,它是一个Python语言在Java中的完全实现。Jython也有很多从CPython中继承的模块库。最有趣的事情是Jython不像CPython或其他任何高级语言,它提供了对其实现语言的一切存取。所以Jython不仅给你提供了Python的库,同时也提供了所有的Java类。这使其有一个巨大的资源库。

我个人理解就是python+java=jython

因为burp拓展需要jython环境,所以这里我们需要安装jython。

Burp配置:

这里安装的步骤直接略过,百度有很多教程可以自行搜索。

启动burpsuite后,我们将设置jython的路径位置

设置完成后基本环境已经搭建好了。

Burp模糊测试:

burpsuite软件中有许多API文档,我们可以通过查看文档来增加我们对burpsuite的接口以及框架的了解。(虽说是了解,但都是英文我啥也看不懂啊:(

这里通过文档我们可以知道我们脚本中需要用到的类

IBurpExtender:在编写Burp拓展时必须要使用的类,该类的作用是在Burp上正确注册,注册方法是使用registerExtenderCallbacks()方法,传递callbacks参数

package burp;/** @(#)IBurpExtender.java** Copyright PortSwigger Ltd. All rights reserved.** This code may be used to extend the functionality of Burp Suite Free Edition* and Burp Suite Professional, provided that this usage does not violate the* license terms for those products.*/
/*** All extensions must implement this interface.** Implementations must be called BurpExtender, in the package burp, must be* declared public, and must provide a default (public, no-argument)* constructor.*/
public interface IBurpExtender
{/*** This method is invoked when the extension is loaded. It registers an* instance of the* <code>IBurpExtenderCallbacks</code> interface, providing methods that may* be invoked by the extension to perform various actions.** @param callbacks An* <code>IBurpExtenderCallbacks</code> object.*/void registerExtenderCallbacks(IBurpExtenderCallbacks callbacks);
}

IIntruderPayloadGeneratorFactory:拓展Burp中intruder模块payload。使用时要在IBurpExtender类中正确注册后,将对象使用registerIntruderPayloadGeneratorFactory()方法在Intruder模块中正确注册。使用getGeneratorName()方法定义拓展工具名字,此方法需要成功返回一个字符串。使用createNewInstance()方法接收攻击相关的参数attack,并要返回一个IIntruderPayloadGenerator类型的对象。

package burp;/** @(#)IIntruderPayloadGeneratorFactory.java** Copyright PortSwigger Ltd. All rights reserved.** This code may be used to extend the functionality of Burp Suite Free Edition* and Burp Suite Professional, provided that this usage does not violate the* license terms for those products.*/
/*** Extensions can implement this interface and then call* <code>IBurpExtenderCallbacks.registerIntruderPayloadGeneratorFactory()</code>* to register a factory for custom Intruder payloads.*/
public interface IIntruderPayloadGeneratorFactory
{/*** This method is used by Burp to obtain the name of the payload generator.* This will be displayed as an option within the Intruder UI when the user* selects to use extension-generated payloads.** @return The name of the payload generator.*/String getGeneratorName();/*** This method is used by Burp when the user starts an Intruder attack that* uses this payload generator.** @param attack An* <code>IIntruderAttack</code> object that can be queried to obtain details* about the attack in which the payload generator will be used.* @return A new instance of* <code>IIntruderPayloadGenerator</code> that will be used to generate* payloads for the attack.*/IIntruderPayloadGenerator createNewInstance(IIntruderAttack attack);
}

IIntruderPayloadGenerator:此模块用来配置payload功能。hasMorePayloads()方法来判定是否将修改后的请求发送会Burp Intruder,返回True则继续,返回False则停止。getNextPayload()方法获得下一个payload,使用时要将一个数组传递进去,该方法需要返回一个payloadreset()方法重置有效载荷生成器的状态。

package burp;/** @(#)IIntruderPayloadGenerator.java** Copyright PortSwigger Ltd. All rights reserved.** This code may be used to extend the functionality of Burp Suite Free Edition* and Burp Suite Professional, provided that this usage does not violate the* license terms for those products.*/
/*** This interface is used for custom Intruder payload generators. Extensions* that have registered an* <code>IIntruderPayloadGeneratorFactory</code> must return a new instance of* this interface when required as part of a new Intruder attack.*/
public interface IIntruderPayloadGenerator
{/*** This method is used by Burp to determine whether the payload generator is* able to provide any further payloads.** @return Extensions should return* <code>false</code> when all the available payloads have been used up,* otherwise* <code>true</code>.*/boolean hasMorePayloads();/*** This method is used by Burp to obtain the value of the next payload.** @param baseValue The base value of the current payload position. This* value may be* <code>null</code> if the concept of a base value is not applicable (e.g.* in a battering ram attack).* @return The next payload to use in the attack.*/byte[] getNextPayload(byte[] baseValue);/*** This method is used by Burp to reset the state of the payload generator* so that the next call to* <code>getNextPayload()</code> returns the first payload again. This* method will be invoked when an attack uses the same payload generator for* more than one payload position, for example in a sniper attack.*/void reset();
}

在了解完上面这三个类后,我们可以开始编写我们的代码了(bhp_fuzzer.py),这里需要注意的是,千万不要在脚本里面写入中文注释,否则在导入拓展时会报出编码错误!!!

from burp import IBurpExtender
from burp import IIntruderPayloadGeneratorFactory
from burp import IIntruderPayloadGeneratorfrom java.util import List, ArrayListimport random# 定义BurpExtender类,并继承IBurpextender类与IIntruderPayloadGeneratorFactory类
class BurpExtender(IBurpExtender, IIntruderPayloadGeneratorFactory):# 传入callbacks参数在Burp中正确注册def registerExtenderCallbacks(self, callbacks):# 将传递过来的callbacks赋值到类中self._callbacks = callbacks# 将帮助信息传入到类中self._helpers = callbacks.getHelpers()# 将拓展在Burp Intruder模块中注册callbacks.registerIntruderPayloadGeneratorFactory(self)return# 使用return返回拓展名,并设置def getGeneratorName(self):return "BHP Payload Generator"# 接受attack参数,并返回IIntruderPayloadGenerator类。def createNewInstance(self, attack):return BHPFuzzer(self, attack)# 定义BHPfuzzer类,并继承IIntruderPayloadGenerator类
class BHPFuzzer(IIntruderPayloadGenerator):# 定义初始方法def __init__(self, extender, attack):self._extender = extenderself._helpers = extender._helpersself._attack = attackprint("BHP Fuzzer initialized")# 定义payload上限self.max_payloads = 1000# 记录payload数量self.num_iterations = 0return# 定义停止攻击条件def hasMorePayloads(self):print "hasMorePayload called."# 当记录的payload达到上限时停止if self.num_iterations == self.max_payloads:print("No more payloads.")return Falseelse:print("More payloads. Continuing.")return True# 返回payloaddef getNextPayload(self, current_payload):# convert into a string# 转换成字符串payload = "".join(chr(x) for x in current_payload)# call our simple mutator to fuzz the POST# 调用简单的变形器对POST请求进行模糊测试payload = self.mutate_payload(payload)# increase the number of fuzzing attempts# 记录payload次数self.num_iterations += 1return payload# 重置函数def reset(self):# 清零self.num_iterations = 0returndef mutate_payload(self, original_payload):# pick a simple mutator or even call an external script# like Radamsa does# 仅生成随机数,或者调用一个外部脚本picker = random.randint(1, 3)# select a random offset in the payload to mutate# 在载荷中选取一个随机的偏移变量去变形offset = random.randint(0, len(original_payload) - 1)payload = original_payload[:offset]# random offset insert a SQL injection attempt# 在随机便宜位置插入SQL注入尝试if picker == 1:payload += "'"# jam an XSS attempt in# 插入XSS尝试if picker == 2:payload += "<script>alert('BHP!');</script>"# repeat a chunk of the original payload a random number# 随机重复原始载荷if picker == 3:chunk_length = random.randint(len(payload[offset:]), len(payload) - 1)repeater = random.randint(1, 10)for i in range(repeater):payload += original_payload[offset:offset+chunk_length]# add the remaining bits of the payload# 添加载荷中剩余的字节payload += original_payload[offset:]return payload

程序编写完成后,我们将脚本导入Burp中。

导入成功

导入成功后,我们可以尝试运用:

首先抓取http://testphp.vulnweb.com请求头,在输入框里输入任意字符,然后点击go使用brup抓取请求头

使用ctrl+i将请求头发送到intruder

点击payloads设置我们刚刚加入的拓展

开始攻击

在Burp中利用Bing服务:

略,由于本人对bing的API key实在时注册不上去,所以这一小节选择跳过,见谅。

利用网站内容生成密码字典:

敲写代码之前,我们可以先看下脚本需要用到的类:

IContextMenuFactory:此模块用来拓展右键菜单功能首先使用registerContextMenuFactory()方法注册菜单拓展,然后建立createMenuItem()方法创建菜单,使用add()函数,配合JmenuItem()方法设置菜单信息,在此方法内定义actionPerformed参数设置行动函数。

package burp;/** @(#)IContextMenuFactory.java** Copyright PortSwigger Ltd. All rights reserved.** This code may be used to extend the functionality of Burp Suite Free Edition* and Burp Suite Professional, provided that this usage does not violate the* license terms for those products.*/
import java.util.List;
import javax.swing.JMenuItem;/*** Extensions can implement this interface and then call* <code>IBurpExtenderCallbacks.registerContextMenuFactory()</code> to register* a factory for custom context menu items.*/
public interface IContextMenuFactory
{/*** This method will be called by Burp when the user invokes a context menu* anywhere within Burp. The factory can then provide any custom context* menu items that should be displayed in the context menu, based on the* details of the menu invocation.** @param invocation An object that implements the* <code>IMessageEditorTabFactory</code> interface, which the extension can* query to obtain details of the context menu invocation.* @return A list of custom menu items (which may include sub-menus,* checkbox menu items, etc.) that should be displayed. Extensions may* return* <code>null</code> from this method, to indicate that no menu items are* required.*/List<JMenuItem> createMenuItems(IContextMenuInvocation invocation);
}

简单了解了Burp的各种API后,我们开始编写我们的代码:

# 导入相应的模块
from burp import IBurpExtender
from burp import IContextMenuFactoryfrom javax.swing import JMenuItem
from java.util import List, ArrayList
from java.net import URLimport re
from datetime import datetime
from HTMLParser import HTMLParserclass TagStripper(HTMLParser):def __init__(self):# 初始化函数HTMLParser.__init__(self)self.page_text = []# 获得标签之间的字符串def handle_data(self, data):self.page_text.append(data)# 获得页面中的注释def handle_comment(self, data):self.handle_data(data)def strip(self, html):# 接受一个字符串类型的html内容,进行解析self.feed(html)# 返回最后的字符串,每个字符串以空格相隔return " ".join(self.page_text)class BurpExtender(IBurpExtender, IContextMenuFactory):def registerExtenderCallbacks(self, callbacks):self._callbacks = callbacksself._helpers = callbacks.getHelpers()self.context = None# 使用set()函数防止重复self.hosts = set()# 初始化字典集合,默认增加‘password’self.wordlist = set(['password'])# 模块命名与注册callbacks.setExtensionName("BHP Wordlist")callbacks.registerContextMenuFactory(self)return# 创建菜单,返回菜单列表def createMenuItems(self, context_menu):self.context = context_menumenu_list = ArrayList()menu_list.add(JMenuItem("Create Wordlist", actionPerformed=self.wordlist_menu))return menu_list# 行动函数def wordlist_menu(self, event):# 获取用户点击的详情信息http_traffic = self.context.getSelectedMessages()for traffic in http_traffic:# 获得http服务对象http_service = traffic.getHttpService()# 得到http中host属性host = http_service.getHost()# 添加到集合中self.hosts.add(host)# 获取响应信息http_response = traffic.getResponse()# 在响应信息存在的情况下使用自定义的get_words()函数获取页面信息生成字典if http_response:self.get_words(http_response)# 将最后的结果利用此函数回显出来self.display_wordlist()# 生成密码def get_words(self, http_response):# 将响应使用tostring函数转换为字符串,并使用split()函数以两个换行为条件分割一次# 这里是将响应头信息与响应体进行分割headers, body = http_response.tostring().split('\r\n\r\n', 1)# 将响应头在不区分大小写的情况下找到指定字符串# find(str, beg, end=)包含字符串返回相应索引,否则返回-1# 忽略下一个相应if headers.lower().find("content-type: text") == -1:return# 实例化对象tag_stripper = TagStripper()# 将body带入类进行解析page_text = tag_stripper.strip(body)# 找到所有以字母开头后跟着两个及以上单词的字符串words = re.findall("[a-zA-Z]\w{2,}", page_text)# 遍历加入集合for word in words:# 过滤超长字符串if len(word) <= 12:self.wordlist.add(word.lower())# 将一些常见关键字与密码组合def mangle(self, word):year = datetime.now().yearsuffixes = ["", "1", "!", year]mangled = []for password in (word, word.capitalize()):for suffix in suffixes:mangled.append("%s%s" % (password, suffix))return mangled# 遍历打印所有生成密码def display_wordlist(self):print "#!comment: BHP Wordlist for site(s) %s" % ", ".join(self.hosts)# sorted()函数,将列表里的所有元素进行排序for word in sorted(self.wordlist):for password in self.mangle(word):print passwordreturn

代码敲写完毕,开始运用以下我们的代码

《Python黑帽子》python3代码实现(第六章)相关推荐

  1. 《Python黑帽子》python3代码实现(第二章)

    <Python黑帽子>代码实现 本篇笔记全部用python3实现,本人大一在校生第一次写博客,有错误之处希望大家积极指出. 参考大佬博客: https://www.cnblogs.com/ ...

  2. Python黑帽子--黑客与渗透测试编程之道 python3 实现代码

    最近在看 Python黑帽子–黑客与渗透测试编程之道 这本书 发现这本书的代码实现都是使用python2 的于是我就想使用python3来实现 缓慢更新中 python2版本 有一个博主写的特别好 这 ...

  3. Python黑帽子-黑客与渗透测试编程之道

    Python黑帽子-黑客与渗透测试编程之道 时间:2018年4月28日 前言 本文参考了两篇资料,优化补全了代码内容 giantbranch 的 Python黑帽子–黑客与渗透测试编程之道 意闲 的 ...

  4. 关于《Python黑帽子:黑客与渗透测试编程之道》的学习笔记

    本篇文章是学习<Python黑帽子:黑客与渗透测试编程之道>的笔记,会持续地将书上的代码自己敲一遍,从而让自己对Python的安全编程有更多的了解,同时希望各位可以给给建议,不足之处太多了 ...

  5. 《Python黑帽子:黑客与渗透测试编程之道》读书笔记(三):scapy——网络的掌控者

    目录 前言 1.窃取email认证 2.ARP缓存投毒 3.PCAP文件处理 结语 前言 <Python黑帽子:黑客与渗透测试编程之道>的读书笔记,会包括书中源码,并自己将其中一些改写成P ...

  6. 《Python黑帽子:黑客与渗透测试编程之道》读书笔记(九):自动化攻击取证

    目录 前言 1.Volatility配置 2.抓取口令的哈希值 3.直接代码注入 4.插入shellcode 结语 前言 <Python黑帽子:黑客与渗透测试编程之道>的读书笔记,会包括书 ...

  7. 《Python 黑帽子》学习笔记 - 命令行选项和参数处理 - Day 4

    在学习书中 netcat 代码的时候,发现其命令行选项和参数的处理存在一些小问题,由于调用 getopt 模块的 getopt() 函数时参数设置不当,会引起代码执行时获取不到参数值或引发异常.该问题 ...

  8. 《Python黑帽子:黑客与渗透测试编程之道》读书笔记(二):原始套接字和流量嗅探

    目录 前言 1.Windows和Linux上的包嗅探 2.解码IP层 3.解码ICMP层 4.发现主机 结语 前言 <Python黑帽子:黑客与渗透测试编程之道>的读书笔记,会包括书中源码 ...

  9. 《Python 黑帽子》学习笔记 - 准备 - Day 1

    信息安全是一个有意思的方向,也是自己的爱好,从零开始,想在工作之余把这个爱好培养为自己的技术能力,而 web 安全相对来说容易入门些,于是选择 web 渗透测试作为学习的起点,并选择同样是容易入门的 ...

最新文章

  1. 在C#中使用XML指南之读取XML
  2. python之禅中文-python之禅
  3. 阿里凑单算法首次公开!打包购商品挖掘系统解析
  4. 46 关于Linux的I/O重定向
  5. 向工作表中添加列表框或组合框
  6. Android JNI开发生成.h头文件问题(转)
  7. Oracle 数据类型及存储方式(袁光东 原创)
  8. jQuery Pagination Ajax分页插件中文详解
  9. 基于环信sdk的陌生人交友php服务器代码开源
  10. 我爱人71年3月出生,档案招工表填成74年了,如何办理退休?
  11. jQuery.fn.extend 与 jQuery.extend 用法
  12. 多线程之wait和notify使用注意事项
  13. 利用Oracle内置分析函数进行高效统计汇总
  14. mysql 行转列 列转行
  15. MATLAB 语言基础知识 矩阵和数组 从矩阵中删除行或列
  16. AppScan 的安装+激活以及+漏扫dvwa,生成安全报告
  17. 软件测试知识点 | APP蓝牙连接测试
  18. MyBatis学习第一步
  19. 轻量级java框架 light-4j
  20. SSM毕设项目游泳馆管理系统851a0(java+VUE+Mybatis+Maven+Mysql)

热门文章

  1. 2019 面试准备 - 图片
  2. 高颜值中科院师妹的读博日记!
  3. Java超市订单管理系统
  4. 将python代码打印成pdf
  5. 3ds max 2015 安装方法
  6. 计算机职称考试在线软件,职称计算机考试模拟软件windowXP模块免费版
  7. “5G 太耗电,关掉它!”
  8. surface pro3深度linux,Microsoft Surface Pro 3 (简体中文)
  9. 数据回归方法(二)—— 多元回归
  10. http请求过程(访问一个页面,发生了怎样的网络请求?)