Black Hat Python之#2:TCP代理
在本科做毕设的时候就接触到TCP代理这东西,当时需要使用代理来对发送和收到的数据做修改,同时使用代理也让我对HTTP协议有了更深的了解。
TCP Proxy用到的一个主要的东西就是socket。proxy通过socket分别对localhost和remotehost做连接,然后可以对通过proxy的流量和数据进行分析。
__author__ = 'seven'
import sys
import socket
import threading def hexdump(src, length=16):
result = []
digits = 4 if isinstance(src, unicode) else 2 for i in xrange(0, len(src), length):
s = src[i:i + length]
hexa = b' '.join(["%0*X" % (digits, ord(x)) for x in s])
text = b''.join([x if 0x20 <= ord(x) < 0x7F else b'.' for x in s])
result.append(b"%04X %-*s %s" % (i, length * (digits + 1), hexa, text)) print b'\n'.join(result) def receive_from(connection):
buffer = "" # We set a 2 second time out depending on your target this may need to be adjusted
connection.settimeout(2) try:
while True:
data = connection.recv(4096)
if not data:
break
buffer += data
except:
pass return buffer def request_handler(buffer):
# perform packet mofifications
return buffer def response_handler(buffer):
# perform pakect modifications
return buffer def proxy_handler(client_socket, remote_host, remote_port, receive_first):
remote_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote_socket.connect((remote_host, remote_port)) if receive_first:
remote_buffer = receive_from(remote_socket)
hexdump(remote_buffer)
remote_buffer = response_handler(remote_buffer)
if len(remote_buffer):
print "[<==] Sending %d bytes to localhost." % len(remote_buffer)
client_socket.send(remote_buffer) while True:
local_buffer = receive_from(client_socket)
if len(local_buffer):
print "[==>] Received %d bytes from localhost." % len(local_buffer)
hexdump(local_buffer)
local_buffer = request_handler(local_buffer)
remote_socket.send(local_buffer)
print "[==>] Sent to remote."
remote_buffer = receive_from(remote_socket)
if len(remote_buffer):
print "[<==] Received %d bytes from remote." % len(remote_buffer)
hexdump(remote_buffer)
remote_buffer = response_handler(remote_buffer)
client_socket.send(remote_buffer)
print "[<==] Sent to localhost."
if not len(local_buffer) or not len(remote_buffer):
client_socket.close()
remote_socket.close()
print "[*] No more data. Closing connections." break def server_loop(local_host, local_port, remote_host, remote_port, receive_first):
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
server.bind((local_host, local_port))
except:
print "[!!] Failed to listen on %s:%d" % (local_host, local_port)
print "[!!] Check for other listening sockets or correct permissions."
sys.exit(0)
print "[*] Listening on %s:%d" % (local_host, local_port) server.listen(5) while True:
client_socket, addr = server.accept()
print "[==>] Received incoming connection from %s:%d" % (addr[0], addr[1])
proxy_thread = threading.Thread(target=proxy_handler,
args=(client_socket, remote_host, remote_port, receive_first))
proxy_thread.start() def main():
if len(sys.argv[1:]) != 5:
print "Usage: ./tcp proxy.py [localhost] [localport] [remotehost] [remoteport] [receive_first]"
print "Example: ./tcp proxy.py 127.0.0.1 9000 10.12.132.1 9000 True"
sys.exit(0) local_host = sys.argv[1]
local_port = int(sys.argv[2]) remote_host = sys.argv[3]
remote_port = int(sys.argv[4]) receive_first = sys.argv[5] if "True" in receive_first:
receive_first = True
else:
receive_first = False server_loop(local_host, local_port, remote_host, remote_port, receive_first) main()
在request_handler和response_handler函数中就可以对收到的数据进行修改。
高大上的hexdump函数来自http://code.activestate.com/recipes/142812-hex-dumper/,表示不怎么看得懂..如果有看得懂的朋友欢迎和我交流
Black Hat Python之#2:TCP代理的更多相关文章
- 结合python实现的netcat与python实现的tcp代理,建立一个流量隧道
在proxy中 python2 proxy.py 127.0.0.1 3334 192.158.1.111 80 true 作为服务器在本地3334端口进行监听, 作为客户端连接远程web服务器192 ...
- 一个简单的tcp代理实现
There are a number of reasons to have a TCP proxy in your tool belt, bothfor forwarding traffic to b ...
- nginx TCP 代理& windows傻瓜式安装
一.下载nginx Windows http://nginx.org/en/download.html 二.解压到目录 三.进入目录并start nginx.exe即可启动 cd d:/java/ng ...
- python socket之tcp服务器与客户端demo
python socket之tcp服务器与客户端demo 作者:vpoet mails:vpoet_sir@163.com server: # -*- coding: cp936 -*- ''' 建立 ...
- nginx : TCP代理和负载均衡的stream模块
一直以来,Nginx 并不支持tcp协议,所以后台的一些基于TCP的业务就只能通过其他高可用负载软件来完成了,比如Haproxy. 这算是一个nginx比较明显的缺憾.不过,在1.90发布后这个认知将 ...
- python 单例模式获取IP代理
python 单例模式获取IP代理 tags:python python单例模式 python获取ip代理 引言:最近在学习python,先说一下我学Python得原因,一个是因为它足够好用,完成同样 ...
- iOS进阶之TCP代理鉴权过程
这段时间接触了网络代理,而自己的任务是完成TCP和UDP的网络代理,所以在这里写些自己的理解吧. 这篇文章先介绍一下TCP代理的鉴权过程(采用的是用户名和密码鉴权),下一篇文章再介绍UDP代理的鉴权过 ...
- 早期nginx tcp代理(基于patch实现)
nginx tcp代理功能由nginx_tcp_proxy_module模块提供,同时监测后端主机状态.该模块包括的模块有: ngx_tcp_module, ngx_tcp_core_module, ...
- python小练习---TCP服务器端
针对于上一篇分享python小练习---TCP客户端 http://www.cnblogs.com/zhaijiahui/p/6926197.html我继续按书中内容,向下进行这里需要强调一个事py3 ...
随机推荐
- zabbix监控nginx日志状态码
监控需求 监控Nginx常见的状态码并对其进行监控,对常见的错误状态码创建相对应的触发器以下按照分钟对数据进行抓取 Zabbix_Agentd创建监控脚本 1)创建脚本之前核对Nginx日志格式我这里 ...
- 2249: Altruistic Amphibians 01背包的应用 + lh的简单图论 图转树求lca
第一个 写了两个比较简单的数论题目,就是整除理论的两个题目,第一个题目比较蠢,第二个稍微要动一点脑筋 Codeforces Round #347 (Div. 2) – A. Complicated G ...
- Scrapy+selenium爬取简书全站
Scrapy+selenium爬取简书全站 环境 Ubuntu 18.04 Python 3.8 Scrapy 2.1 爬取内容 文字标题 作者 作者头像 发布日期 内容 文章连接 文章ID 思路 分 ...
- Python-SHA256加密算法接口测试
前言 小伙伴们在做接口测试的时候,是否遇到一些需要加密的接口,但是不知如何进行测试呢?今天我们来学习一下SHA256加密算法接口如何进行测试的. SHA256加密算法介绍: 比特币挖矿的御用算法 SH ...
- 01-Taro打造hello-world应用
01-Taro打造hello-world应用 一.简介 Taro是由京东凹凸实验室出品,书写一套代码通过 Taro 的编译工具,将源代码分别编译出可以在不同端(微信 / 京东 / 百度 / 支付宝 / ...
- 这一份MySQL书单,可以帮你搞定90%以上的面试题!
- [hdu5313]二分图性质,dp
题意:给定二分图,求添加的最多边数,使得添加之后还是二分图 思路:如果原图可以分成X,Y两个点集,那么边数最多为|X||Y|条.由于|X|+|Y|==n,所以需要使|X|与|Y|尽量接近.先对原图进行 ...
- 散列表PTA判断
1-1 在散列表中,所谓同义词就是具有相同散列地址的两个元素. (1分) T F 作者 DS课程组 单位 浙江大学 1-2 采用平方探测冲突解决策略(hi(k)=(H(k)+ ...
- css3 文字处理
text-overflow:ellipsis;超出的文字显示...前提是该盒子必须有overflow:hidden;属性 text-shadow 文字阴影 direction:控制文字方向,值有dow ...
- 使用js rem动态改变字体大小,自适应
<html> <head> <meta charset="utf-8"> <script> console.log(window.d ...