0x00:前言
一,基於前面寫過給“掃描目錄+ N多代理”,這次給sqlmap加一個代理池。用處就是在跑sqlamp注入的時候,防止被ban掉IP。
二,這個想法是很久之前就有了,只不過這次是自己的研究一下原理結合網上公開的腳本,並用Python寫出來的。這次沒有創新的知識,純當做是練習python腳本的編寫。
0x01:思路
1.先爬取代理網站的代理IP,然后做一下驗證,驗證是否可用並輸出到文本里。
2.啟用本地代理127.0.0.1:5320(5320=我想愛你)
3.sqlmap加上代理“ --proxy = http://127.0.0.1:5320”
0x02:過程
一,獲取代理IP
import requests,re url="http://www.89ip.cn/tqdl.html?api=1&num=10"#采用89ip的接口采集 types="https" proxys={} #print (url) 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=re.findall("((?:[0-9]{1,3}\.){3}[0-9]{1,3})", r)#正則匹配出IP與端口 port=re.findall("(:\d{1,5})", r)#正則匹配出IP與端口 for i,j in zip(port[2:],ip): print (j+i)
二,驗證代理IP並輸出到文本
我們來回憶上次提到的Python中代理的編寫規則
proxy={'協議':'ip:端口'}
編寫格式:
tar=requests.get(url,headers=headers,proxies=proxy,timeout=5,verify=False)
獲取IP +驗證代理
#/usr/bin/python3 #author:Jaky import requests,re url="http://www.89ip.cn/tqdl.html?api=1&num=9000"#采用89ip的接口采集 types="https" proxys={} headers={'User-Agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)'} r=requests.get(url,headers=headers).text ip=re.findall("((?:[0-9]{1,3}\.){3}[0-9]{1,3})", r)#正則匹配出IP與端口 port=re.findall("(:\d{1,5})", r)#正則匹配出IP與端口 for i,j in zip(port[2:],ip): proxy=j+i print (proxy) proxys[types.lower()]='%s'%proxy try: tar=requests.get("https://ifconfig.me/ip",headers=headers,proxies=proxys,timeout=5,verify=False).text if tar in str(proxys): with open("ip.txt",'a') as file: file.write(proxy+'\n') # 保存文件 except : pass
我這里直接采集9000個+驗證
同時輸出結果到“ ip.txt”
三,完整代碼
#!/usr/bin/env python3 # coding:utf-8 import socket,time,random,threading,requests,re from socket import error localtime = time.asctime(time.localtime(time.time())) class ProxyServerTest(): def __init__(self, proxyip): # 本地socket服務 self.ser = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.proxyip = proxyip def run(self): try: # 本地服務IP和端口 self.ser.bind(('127.0.0.1', 5320)) # 最大連接數 self.ser.listen(10) except error as e: print("[-]The local service : " + str(e)) return "[-]The local service : " + str(e) while True: try: # 接收客戶端數據 client, addr = self.ser.accept() print('[*]accept %s connect' % (addr,)) data = client.recv(1024) if not data: break print('[*' + localtime + ']: Accept data...') except error as e: print("[-]Local receiving client : " + str(e)) return "[-]Local receiving client : " + str(e) while True: # 目標代理服務器,將客戶端接收數據轉發給代理服務器 mbsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print("[!]Now proxy ip:" + str(self.proxyip)) prip = self.proxyip[0] prpo = self.proxyip[1] try: mbsocket.settimeout(3) mbsocket.connect((prip, prpo)) except: print("[-]RE_Connect...") continue break try: mbsocket.send(data) except error as e: print("[-]Sent to the proxy server : " + str(e)) return "[-]Sent to the proxy server : " + str(e) while True: try: # 從代理服務器接收數據,然后轉發回客戶端 data_1 = mbsocket.recv(1024) if not data_1: break print('[*' + localtime + ']: Send data...') client.send(data_1) except socket.timeout as e: print(self. proxyip) print("[-]Back to the client : " + str(e)) continue # 關閉連接 client.close() mbsocket.close() def main(): print('Atuhor:Jaky') print('WeChat public number:luomiweixiong') file = open("ip.txt","r") for i in file: ip = i.split(':') ip_list = (ip[0],int(ip[1])) print(ip_list) try: try_ip = ProxyServerTest(ip_list) except Exception as e: print("[-]main : " + str(e)) return "[-]main : " + str(e) t = threading.Thread(target=try_ip.run, name='LoveJaky') print('[*]Waiting for connection...') # 關閉多線程 t.start() t.join() if __name__ == '__main__': main()
0x03:總結
1,使用之前得先爬取代理IP,驗證完然后會自動保存在“ ip.txt”里
2,執行以上代碼,然后
sqlmap.py -u "http://www.xxx.com/1.asp?id=1" --proxy=http://127.0.0.1:5320
注:本文轉自微信公眾號‘ ’,如有侵權立即刪除。