妖娆的代理工具shadowProxy – 神出鬼没的切换IP地址

5,713 阅读6分钟
原文链接: liehu.tass.com.cn

前言

在渗透测试过程中,往往会遇到特别“小气”的目标,稍微碰一下就封IP。这种情况下,我们很自然的想到通过网上大量的免费代理进行IP隐匿。

那么问题来了,难道拿到哪些个代理,每用一次手动换下一个代理? 这太像火铳的工作方式了,想想就心累了。

so,小弟就来造一台机关枪,突突突突突… 想想就挺带感。

github.com/odboy/shado…

功能实现

代理功能

主要使用python内建的http.server和http.client库实现。

http.server相关的代码解读可参考我前一篇文章 Python源码分析之从SocketServer到SimpleHTTPServer

主要代理功能代码:

def do_GET(self):

    if self.path == 'http://shadow.proxy/':
        self.send_cacert()
        # print("%s download %s" % (self.client_address, self.cacert))
        return

    req = self
    content_length = int(req.headers.get('Content-Length', 0))
    req_body = self.rfile.read(content_length) if content_length else None

    if req.path[0] == '/':
        if isinstance(self.connection, ssl.SSLSocket): # ssl.SSLSocket or ssl.SSLContext
            req.path = "https://%s%s" % (req.headers['Host'], req.path)
        else:
            req.path = "http://%s%s" % (req.headers['Host'], req.path)

    u = urlparse(req.path)
    scheme, netloc= u.scheme, u.netloc
    assert scheme in ("http", "https")
    if netloc:
        req.headers['Host'] = netloc
    setattr(req, 'headers', self.filter_headers(req.headers))

    retryFlag = 0
    while retryFlag < 10 :
        try:
            target = (scheme, netloc)
            # 输入URL的协议和主机,返回可用的连接HTTP(S)Connection
            proxy = proxyCoor.dispatchProxy(target)
            if proxy is None:
                print("未能获取到可用Proxy...(可能是Proxy耗尽...)")
                self.send_error(502,"proxy resource RUN OUT!!!")
                return
            print("%s --> [ %d ] %s" % (proxy, retryFlag + 1, req.path))

            if proxy.split("://")[0] == "http":
                conn = http.client.HTTPConnection(proxy.split("://")[1], timeout=self.timeout)
            elif proxy.split("://")[0] == "https":
                conn = http.client.HTTPSConnection(proxy.split("://")[1], timeout=self.timeout)

            conn.request(self.command, req.path, req_body, dict(req.headers))
            res = conn.getresponse()
            # res.response_version = 'HTTP/1.1' if res.version == 11 else 'HTTP/1.0'
            res_body = res.read()       # Transfer-Encoding并不需要特殊处理(除了Content-Length外)

        except Exception as e:
            retryFlag += 1
            # self.send_error(502)
            # return
        else:
            try:
                if 'Content-Length' not in res.headers:
                    res.headers['Content-Length'] = str(len(res_body))
                setattr(res, 'headers', self.filter_headers(res.headers))
                self.send_response_only(res.status, res.reason)
                for keyword in res.headers:
                    self.send_header(keyword, res.headers.get(keyword, ""))
                self.end_headers()
                self.wfile.write(res_body)
                self.wfile.flush()
            except:
                pass
            finally:
                retryFlag = 9999  # 极大值,结束重试。
                conn.close()


# 其他方法重用GET的方法。
do_HEAD = do_GET
do_POST = do_GET
do_PUT = do_GET
do_DELETE = do_GET
do_OPTIONS = do_GET
do_TRACE = do_GET
代理协调者

主要实现:

导入代理列表

验证代理的可用性和匿名性

维护目标站点、代理列表二维表

根据维护的二维表,反馈可用的代理地址。

另外,我用的代理列表是从kuaidaili.com上爬取的,但代理的质量比较差,很头大。之前还用过xicidaili,情况也差不多。

验证公网IP的网站有如下几个:

ip.chinaz.com/getip.aspx

ifconfig.me/ip

api.ipify.org

ip.seeip.org

ifconfig.co/ip

myexternalip.com/raw

wtfismyip.com/text

icanhazip.com/

ipv4bot.whatismyipaddress.com/

ip4.seeip.org

测试验证

验证代码

透过shadowProxy访问http://ip.chinaz.com/getip.aspx,从而直观查看代理效果。

import requests
import time

i = 0
while True:
    try:
        i += 1
        r =requests.get("http://ip.chinaz.com/getip.aspx",proxies={"http":"http://127.0.0.1:8088"},timeout=10)
        if r.status_code == 200:
            msg = "第 %d 次请求 ✅\t%s"%(i,r.text)
        else:
            msg = "第 %d 次请求 ⭕\t%d"% (i, r.status_code)
            time.sleep(2)
    except KeyboardInterrupt:
        print('\r***********************\n\t用户中断\t\n***********************')
        break
    except Exception as e:
        msg = "第 %d 次请求 ❗\t%s" % (i, e )
        time.sleep(2)
    finally:
        print(msg)
效果展示

动图展示 : test_shadowProxy.gif

枪是好枪,但还是存在一些问题的。

缺弹少药 - 通过工具爬取到的代理很多重复,很多不可用,只有百八十个。

弹药质量差 - 获取到的代理,很多无法传输大数据包(中断),小包也不稳定。

机枪卡壳 - 由于上述问题,所以工具容错能力/重试功能有待提升。(后续考虑提升的点)

PS:后续代码完善后,可以考虑开源发布。

2018-01-10 Update

目前代理加入了自动重试功能,使其能更稳定的进行查询。

同时,找了个还算不错的proxylist。 github.com/fate0/proxy…

目前便可以比较顺畅的使用了:

开源发布

github.com/odboy/shado…

生成&安装证书

生成证书

shadowProxy git:(master) ✗ ll
    total 112
    -rw-r--r--  1 bingo  staff   573B Jan 10 16:42 PCtest.py
    -rw-r--r--  1 bingo  staff   5.9K Jan 10 16:42 ProxyCoordinator.py
    -rw-r--r--  1 bingo  staff    14B Jan 10 16:42 README.md
    drwxr-xr-x  3 bingo  staff    96B Jan 10 16:42 __pycache__
    -rw-r--r--  1 bingo  staff   100B Jan 10 16:42 proxylist-4.txt
    -rw-r--r--  1 bingo  staff    19K Jan 10 16:42 proxylist.txt
    -rwxr-xr-x  1 bingo  staff   302B Jan 10 16:42 setup_https_intercept.sh
    -rw-r--r--  1 bingo  staff    11K Jan 10 16:42 shadowProxy.py

    shadowProxy git:(master) ✗ ./setup_https_intercept.sh   # 直接运行脚本,生成根证书
    Generating RSA private key, 2048 bit long modulus
    ........................................................................................................................................+++
    ..................................................................+++
    e is 65537 (0x10001)
    Generating RSA private key, 2048 bit long modulus
    .....................................................................................................................................+++
    ...................+++
    e is 65537 (0x10001)

安装证书

在浏览器设置代理,指向 http://127.0.0.1:8088 , 然后访问 shadow.proxy/,即可弹出证书安装。(…

安装后,可以访问https网站。

使用测试
➜  shadowProxy git:(master) ✗ python shadowProxy.py -h
        .--.
       |o_o |    ------------------
       |:_/ |   < Author: Mr.Bingo >
      //   \ \   ------------------
     (|     | ) <    oddboy.cn     >
    /'\_   _/`\ ------------------
    \___)=(___/

usage: shadowProxy.py [-h] [--bind BIND] [--port PORT]
                      [--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
                      [--proxyListFile PROXYLISTFILE]

optional arguments:
  -h, --help            show this help message and exit
  --bind BIND           Default: 0.0.0.0
  --port PORT           Default: 8088
  --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        Default: WARNING
  --proxyListFile PROXYLISTFILE
                        代理列表文件
                       
➜  shadowProxy git:(master) ✗ python shadowProxy.py
        .--.
       |o_o |    ------------------
       |:_/ |   < Author: Mr.Bingo >
      //   \ \  ------------------
     (|     | ) <    oddboy.cn     >
    /'\_   _/`\  ------------------
    \___)=(___/

初始化代理池  本地IP :: 36.110.16.74
导入代理池:::  proxylist.txt
成功导入 110 个代理
Serving HTTP on 0.0.0.0 port 8088 (http://0.0.0.0:8088/) ...

直接访问站点进行测试。

由于该工具主要基于网上免费的代理进行IP隐匿的,所以稳定性仍然不够好,所以只建议用于特定的请求包测试。 在使用过程中遇到什么问题,欢迎给我邮件,我会进行修复完善,如果可以,给我的GitHub点颗星星,谢谢!

本站文章均属原创,转载请注明出处!