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 ...
随机推荐
- MAC使用Scrapy遇到的坑
MAC版本:EI Captain Python版本: 2.7.10 MAC默认没有安装pip,所以首先 sudo easy_install pip 然后安装Scrapy: sudo pip insta ...
- Java——多线程之Lock锁
Java多线系列文章是Java多线程的详解介绍,对多线程还不熟悉的同学可以先去看一下我的这篇博客Java基础系列3:多线程超详细总结,这篇博客从宏观层面介绍了多线程的整体概况,接下来的几篇文章是对多线 ...
- Hadoop入门学习笔记-第二天 (HDFS:NodeName高可用集群配置)
说明:hdfs:nn单点故障,压力过大,内存受限,扩展受阻.hdfs ha :主备切换方式解决单点故障hdfs Federation联邦:解决鸭梨过大.支持水平扩展,每个nn分管一部分目录,所有nn共 ...
- Oracle创建包
包: 在公司中,如果业务逻辑比较复杂,需要定义很多过程或者函数.有可能需要定义几十个过程或者函数,这些过程或者函数如果都放到一起,是不是不好管理?我们一般使用包来管理过程或者函数,一个包中可以定义多个 ...
- hdu2336 (匈牙利最大匹配+二分)
Describe 这是一个简单的游戏,在一个n*n的矩阵中,找n个数使得这n个数都在不同的行和列里并且要求这n个数中的最大值和最小值的差值最小. Input 输入一个整数T表示T组数据. 对于每组数据 ...
- Java设计模式之建造者模式(Builder Pattern)
前言 这篇文章主要向大家讲解什么是建造者模式,建造者模式的实例讲解及应用场景等知识点. 一.建造者介绍 用户可以不知道产品的构建细节直接可以创建复杂的对象,主要是分离了产品的构建和装配,这样就实现 ...
- HDU 2000 (水)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2000 题目大意:仨字符从小到大排序 解题思路: 很水很水,需要注意的地方是如果用苦力(三个if)要注意 ...
- webpack指南(一)HRM+Tree Shaking
参考:https://www.cnblogs.com/PasserByOne/p/12084323.html https://blog.csdn.net/qq593249106/article/det ...
- 终于明白了vue使用axios发送post请求时的坑及解决原理
前言:在做项目的时候正好同事碰到了这个问题,问为什么用axios在发送请求的时候没有成功,请求不到数据,反而是报错了,下图就是报错请求本尊 vue里代码如下: this.$http.post('/ge ...
- Holy Grail Bellman-Ford/spfa
Holy Grail Bellman-Ford #include <bits/stdc++.h> using namespace std; , maxm = ; const int inf ...