openresty实现隧道代理

目录

     openresty实现隧道代理      按照惯例开头先来两句废话。被各位师傅卷了一年了,今天我再水一下。众所周知,渗透不用代理就是在耍流氓。元旦没啥事刚好想起前几天看见的未闻Code师傅的关于隧道代理的文章,于是今天就搞了一下。作为一名究极缝合怪,专业老裁缝,是时候拿起针线缝缝补补了。好,我们直接开摆,呸,口误,开整。

Q:隧道代理与常规代理的不同之处是什么

A:隧道代理是一种代理IP存在方式,一般是代理IP。与传统的固定代理IP相比,它的特殊之处在于它会在代理服务器上自动更改IP,这样每个请求都会使用不同的IP

Q:什么是openresty

A:OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

创建下面三个文件

docker-compose.ymlversion: 3.5services:proxy_pool:build: .container_name: proxy_poolports:- "5010:5010"depends_on:- proxy_redisrestart: alwaysenvironment:DB_CONN: "redis://@proxy_redis:6379/0"networks:proxy_network:ipv4_address: 192.168.112.2proxy_redis:image: "redis"container_name: proxy_redisrestart: alwaysnetworks:proxy_network: ipv4_address: 192.168.112.3proxy_nginx: build:context: .dockerfile: Nginxrestart: alwaysdepends_on: - proxy_redisports:- "8888:80"container_name: proxy_nginxnetworks:proxy_network: ipv4_address: 192.168.112.112networks:proxy_network:name: "proxy_network"driver: "bridge"ipam:config:- subnet: 192.168.112.0/24

Nginx

FROM openresty/openresty:1.19.9.1-4-bionicRUN sed -i "s/archive.ubuntu.com/mirrors.aliyun.com/g" /etc/apt/sources.listCOPY nginx.conf /usr/local/openresty/nginx/conf/nginx.confCMD ["/usr/local/openresty/bin/openresty", "-g", "daemon off;"]

nginx.conf

worker_processes16;error_log /usr/local/openresty/nginx/logs/perror.log;events {worker_connections 1024;}stream {log_format tcp_proxy $remote_addr [$time_local] $protocol $status $bytes_sent $bytes_received $session_time "$upstream_addr" "$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time";access_log /usr/local/openresty/nginx/logs/paccess.log tcp_proxy;open_log_file_cache off;upstream backend{server 106.52.172.214:8088;balancer_by_lua_block {local balancer = require "ngx.balancer"local host = ""local port = 0host = ngx.ctx.proxy_hostport = ngx.ctx.proxy_port-- 设置 balancerlocal ok, err = balancer.set_current_peer(host, port)-- local ok=0if not ok thenngx.log(ngx.ERR, "failed to set the peer: ", err)end}}server {preread_by_lua_block{local redis = require "resty.redis"local red = redis:new()red:set_timeouts(1000, 1000, 1000)local ok, err = red:connect("192.168.112.3", 6379)if not ok thenngx.log(ngx.ERR,"failed to connect: ", err)return red:close()endlocal rkey = "use_proxy"local res, err = red:hkeys(rkey)if not res thenngx.log(ngx.ERR,"res num error : ", err)return red:close()endlocal radmnum = math.randomseed(tonumber(tostring(ngx.now()):reverse():sub(1, 6)))local proxy = res[math.random(#res)]-- ngx.log(ngx.ERR,"res num : ", proxy)local colon_index = string.find(proxy, ":")local proxy_ip = string.sub(proxy, 1, colon_index - 1)local proxy_port = string.sub(proxy, colon_index + 1)ngx.log(ngx.ERR,"redis data = ", proxy_ip, ":", proxy_port);ngx.ctx.proxy_host = proxy_ipngx.ctx.proxy_port = proxy_portlocal ok, err = red:close()if not ok then ngx.log(ngx.ERR,"failed to close: ",tostring(err)) returnend} listen 0.0.0.0:80; proxy_connect_timeout 3s; proxy_timeout 10s; proxy_pass backend; }}

   |  其中rkey值在redis-cli中使用keys *

   |  由于redis采用默认配置并未设置密码所以resty.redis的连接并未采用身份验证,同时切记,一点不要把redis端口映射出来,除非你的redis已做身份验证。

 

  |  如果你的redis有身份验证,只需要在上面的nginx.conf中local res, err = red:hkeys(rkey)的下方添加下面代码

-- pass参数为你的redis连接密码local pass = ""local res, err = red:auth(pass)if not res thenngx.log(ngx.ERR,"failed to authenticate: ", err)returnend

 

下载proxy_pool

git clone https://github.com/jhao104/proxy_pool.git

 

将上面创建的三个文件放在proxy_pool根目录下docker-compose up 启动容器

 

python 测试代码

import requestsimport timeproxies={"http":":8888"}for i in range(20):try:res = requests.get("",headers = {"Connection":"close"},proxies=proxies)if res.status_code == 200:print(res.status_code,res.text)except:passtime.sleep(5)

 

 恭喜现在你已经在疯狂乱跳了。顺便提一句,由于代理池爬取的代理有http的也有https的,得益于openresty的负载均衡,在http代理无法访问https页面时他会自动请求下一个代理,直到正常访问。

网络安全社团公众号

微信号 : qlnu_ctf

新浪微博:齐鲁师范学院网络安全社团