python | 实现判断代理是否可用以及扫描目录

0x00

    在渗透测试过程中扫目录经常会碰到waf ban ip的情况,面对这种情况我们只能使用自己手上的代理或者是自己的服务器,但是自己手上的服务器和代理终归有限,这时候我们就需要,写一个python的脚本,通过进行多层代理目录爬取,但是实际情况是免费的代理往往意味着不好用,和不稳定。我们通过代理访问正常的网站如果code = 200所以说明代理可以使用。

 try:
          r = requests.get(url,proxies = proxy, headers=headers,timeout = 8)
          if r.status_code == 200:
            proxy_list = []
            proxy_list.append(proxy)
            write_txt(proxy)

      except:
          print("[-]代理不可用")

并且将可用的代理存放在test.txt然后我们使用正常的目录字典去扫描,源代码如下:

这里的dir.txt为字典目录需要与脚本在同一级目录下,test.txt为自动生成的可用代理目录。

# -*- coding: utf-8 -*-
import requests, sys, threading
from queue import Queue
import requests
import re

# 多线程实现扫描目录
class DirScan(threading.Thread):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; '
                      'Intel Mac OS X 12_10) AppleWebKit/600.1.25 '
                      '(KHTML, like Gecko) Version/12.0 Safari/1200.1.25'}

    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue

    def run(self):
        # 获取队列中的URL
        while not self.queue.empty():
            url = self.queue.get()
            with open('test.txt', 'r') as f:
                for www in f:
                    proxy = www.strip()
                    try:
                        tar = requests.get(url, headers=headers, proxies=proxy, timeout=10, verify=False)
                        if tar.status_code == 200:  # 这里只显示200的状态码,需要可加其他状态码做筛选
                            urls = {url}
                            print(list(urls))
                    except Exception as e:
                        pass


def start(url, count):
    queue = Queue()

    f = open('dir.txt', 'r')
    for i in f:
        queue.put(url + i.rstrip('\n'))
    # 多线程
    threads = []
    thread_count = int(count)

    for i in range(thread_count):
        threads.append(DirScan(queue))

    for t in threads:
        t.start()
    for t in threads:
        t.join()

def proxy(api_url):
    url=api_url  #url_api
    headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 12_10) '
                           'AppleWebKit/600.1.25 (KHTML, like Gecko) Version/12.0 Safari/1200.1.25'}
    r=requests.get(url,headers=headers).text
    ip_proxy=re.findall("((?:[0-9]{1,3}\.){3}[0-9]{1,3}:*[0-9]*)", r)#re IP
    for i in ip_proxy:#用for循环取出IP并赋予i
      types ="https"
      proxy = {}
      proxy[types.lower()] = '%s' % i  # ex:{'https': '1.2.3.4:3308'}
      url = 'http://www.baidu.com/'
      try:
          r = requests.get(url,proxies = proxy, headers=headers,timeout = 8)
          if r.status_code == 200:
            proxy_list = []
            proxy_list.append(proxy)
            write_txt(proxy)

      except:
          print("[-]代理不可用")

def write_txt(content):
    f1 = open('test.txt', 'a+')
    f1.write(str(content)+"\n")



if __name__ == "__main__":
    api_url = " "  # proxy_api
    proxy(api_url)
    url=sys.argv[1]
    count = 10
    start(url, count)

这里的api接口可以选择去自行购买,或者使用免费的代理api接口

 

本文参考:jaky表哥

posted @ 2020-07-10 15:09  sasdsaxvcx  阅读(235)  评论(0编辑  收藏  举报